32
FPU Graph reduction CSSE , The University of Melbourne Bernie Pope, 2009 Simple graph reduction with visualisation

Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

  • Upload
    vulien

  • View
    219

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

CSSE , The University of MelbourneBernie Pope, 2009

Simple graph reduction with visualisation

Page 2: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Outline

• The miniFP language

• Graph representation of terms

• Tying the knot

• The compiler

• The reduction engine

• Literature

Page 3: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

The miniFP language

• Small functional language, intended for teaching. Ugly syntax borrowed from Schwartzbach’s notes (easy to parse, but annoying to use).

• Hindley/Milner style type inference, with let polymorphism.

• Data types: Bool, Int, List, ->

• No user defined types.

• Explicit fixed-point operator (fix).

• Minimal primitives (head, tail, cons, nil, zero, true, false, pred, succ, isZero, null, if-then-else).

Page 4: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

The miniFP language

let map = fix (\m -> \f -> \list -> if null list then nil else cons (f (head list)) (m f (tail list)) fi end end end)in map (\x -> succ x end) [0,1,2] end

Page 5: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Graph representation of terms

head (fix (\rec -> cons 1 rec end))

Page 6: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Graph representation of terms

head (fix (\rec -> cons 1 rec end))

Page 7: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Graph representation of terms

head (fix (\rec -> cons 1 rec end))

Page 8: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Graph representation of terms

head (fix (\rec -> cons 1 rec end))

Page 9: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Graph representation of terms

head (fix (\rec -> cons 1 rec end))

Page 10: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Graph representation of terms

head (fix (\rec -> cons 1 rec end))

Page 11: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Tying the knot

• Step through the reduction of: head (fix (\rec -> cons 1 rec end))

• Observe the reduction of the fixed point carefully.

• The “infinite” list of ones becomes a cyclic graph.

• Normal order reduction ensures that the program terminates.

• Diagrams generated by GraphViz. http://graphviz.org/

• The compiled program writes “dot graph” files at each important step in reduction.

Page 12: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Step 1. The initial graph

Page 13: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Step 1. The initial graph

This is the root of the graph. It represents the value of the program.

Page 14: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Step 2. unfold the fixed point

Page 15: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Step 2. unfold the fixed point

fix f = f (fix f)

Builds a new application node.

Page 16: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Step 3. copy the left child of the application

Page 17: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Step 3. copy the left child of the application

Copy

Page 18: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Step 4. substitute the parameter with the argument

Page 19: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Step 4. substitute the parameter with the argument

The lambda node is overwritten with a indirection.

Page 20: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Step 5. overwrite the application node with its result

Page 21: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Step 5. overwrite the application node with its result

The application node is overwritten with a indirection.

Page 22: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Step 6. overwrite the fix node with its result

Page 23: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Step 6. overwrite the fix node with its result

Page 24: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Step 6. overwrite the fix node with its result

Garbage, not reachable from the root node.

Page 25: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Step 6. overwrite the fix node with its result

Infinite list of ones. Notice the cycle. The knot has been tied

Page 26: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Step 7. overwrite head node with its result

Page 27: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Step 7. overwrite head node with its result

Page 28: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Step 7. overwrite head node with its result

This is the final answer of the program.

Page 29: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

The compiler

• Translates miniFP programs to C code.

• The output C code is a function which builds the initial graph representation of the program as a data structure in the heap.

• The output C code is linked with a library which contains a reduction engine.

• The reduction engine rewrites the graph until it reaches WHNF (or it loops).

• The final value is pretty printed on standard output.

Page 30: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Compiled output

#include "build.h"GraphPtr build (void){ GraphPtr v_0 = HEAD; GraphPtr v_1 = FIX; GraphPtr v_2 = LAM; GraphPtr v_3 = CONS; GraphPtr v_4 = SUCC; EDGE(v_4,0,zero); EDGE(v_3,0,v_4); EDGE(v_3,1,v_2); EDGE(v_2,0,v_3); EDGE(v_1,0,v_2); EDGE(v_0,0,v_1); return v_0;}

Page 31: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Reduction engine

GraphPtr reduce (GraphPtr g){ GraphPtr result = NULL;

if (is_whnf (g)) { return g; } else { switch (g->tag) { /* ... reduce graph and build result ... */ } indirect (g, result); return result; }}

Page 32: Simple graph reduction with visualisation - berniepope.id.auberniepope.id.au/assets/files/graphreduction.pdf · Bernie Pope, 2009 CSSE , The University of Melbourne The miniFP language

FPU Graph reduction

Bernie Pope, 2009 CSSE , The University of Melbourne

Selected Literature

• Theorie des ensembles, Bourbaki (1954) (according to Shivers and Wand).

• Semantics and Pragmatics of the Lambda Calculus, Wadsworth (1971).

• The implementation of functional languages, (Chapter 12) Peyton Jones (1987).

• An algorithm for optimal lambda-calculus reduction, Lamping (1990).

• Lambda Calculi plus Letrec, Ariola and Blom (1997).

• Bottom up beta substitution, Shivers and Wand (2004).

• Lambda animator, Mike Thyer (2007). http://thyer.name/lambda-animator/