On Graph Rewriting, Reduction and Evaluation Ian Zerny Department - - PowerPoint PPT Presentation

on graph rewriting reduction and evaluation
SMART_READER_LITE
LIVE PREVIEW

On Graph Rewriting, Reduction and Evaluation Ian Zerny Department - - PowerPoint PPT Presentation

Introduction Formalization Derivation Conclusion On Graph Rewriting, Reduction and Evaluation Ian Zerny Department of Computer Science, Aarhus University, Denmark zerny@cs.au.dk Trends in Functional Programming, 2009 Ian Zerny (


slide-1
SLIDE 1

Introduction Formalization Derivation Conclusion

On Graph Rewriting, Reduction and Evaluation

Ian Zerny

Department of Computer Science, Aarhus University, Denmark zerny@cs.au.dk

Trends in Functional Programming, 2009

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 1 / 33

slide-2
SLIDE 2

Introduction Formalization Derivation Conclusion

Graph reduction

What? Represent terms as graphs instead of trees Why? Avoid redundant computation How? Two main approaches to graph reduction Reduction machines à la Turner Graph rewriting systems à la Barendregt et al.

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 2 / 33

slide-3
SLIDE 3

Introduction Formalization Derivation Conclusion

This talk

Goal: Connecting reduction machines à la Turner with graph rewriting systems à la Barendregt. Means: Mechanical program derivation based on Danvy’s AFP 2008 presentation.

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 3 / 33

slide-4
SLIDE 4

Introduction Formalization Derivation Conclusion

The syntactic correspondence

Danvy and students: calculi

  • syntactic

correspondence

  • abstract machines

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 4 / 33

slide-5
SLIDE 5

Introduction Formalization Derivation Conclusion

The syntactic correspondence

Danvy and students: calculi

  • syntactic

correspondence

  • abstract machines

a.o. λ-calculus

  • CK

a.o. λ ρ-calculus

  • CEK

n.o. λ ρ-calculus

  • KAM

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 4 / 33

slide-6
SLIDE 6

Introduction Formalization Derivation Conclusion

The syntactic correspondence

Danvy and students: calculi

  • syntactic

correspondence

  • abstract machines

a.o. λ-calculus

  • CK

a.o. λ ρ-calculus

  • CEK

n.o. λ ρ-calculus

  • KAM
  • SECD
  • CAM

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 4 / 33

slide-7
SLIDE 7

Introduction Formalization Derivation Conclusion

The syntactic correspondence

Danvy and students: calculi

  • syntactic

correspondence

  • abstract machines

a.o. λ-calculus

  • CK

a.o. λ ρ-calculus

  • CEK

n.o. λ ρ-calculus

  • KAM
  • SECD
  • CAM

λsec-calculus

  • σ-calculus
  • Ian Zerny (zerny@cs.au.dk)

On Graph Rewriting, Reduction and Evaluation TFP ’09 4 / 33

slide-8
SLIDE 8

Introduction Formalization Derivation Conclusion

What about graph reduction?

Graph rewriting systems

  • program transformation?
  • Reduction machines

Key issues Side effects Modification of executing code Non-functional formalizations

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 5 / 33

slide-9
SLIDE 9

Introduction Formalization Derivation Conclusion

Domain of discourse

The simplest setting: the S, K and I combinators Sxyz = xz(yz) Kxy = x Ix = x No loss of generality Formalization of graphs with Standard ML references

datatype atom = S | K | I datatype node = C of atom | A of graph × graph withtype graph = node ref

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 6 / 33

slide-10
SLIDE 10

Introduction Formalization Derivation Conclusion

Domain of discourse

The simplest setting: the S, K and I combinators Sxyz = xz(yz) Kxy = x Ix = x No loss of generality Formalization of graphs with Standard ML references

datatype atom = S | K | I datatype node = C of atom | A of graph × graph withtype graph = node ref

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 6 / 33

slide-11
SLIDE 11

Introduction Formalization Derivation Conclusion

Overview

Formalization of a reduction machine Formalization of a graph rewriting system Derivation Towards graph evaluation Conclusion

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 7 / 33

slide-12
SLIDE 12

Introduction Formalization Derivation Conclusion

Reduction machines à la Turner

Set of combinators and primitive operations Stack unwinding routine Application routine by graph transformation

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 8 / 33

slide-13
SLIDE 13

Introduction Formalization Derivation Conclusion

Our reduction machine for S, K and I

Restricted to only S, K and I Full normal form reduction Stack management by stack marking Transition functions unwind and apply Fits on a single slide

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 9 / 33

slide-14
SLIDE 14

Introduction Formalization Derivation Conclusion

Our reduction machine for S, K and I

(* unwind RM : graph × stack → graph *) fun unwind RM (g as ref (A (g0 , g1)), gs) = unwind RM (g0 , PUSH (g, gs)) | unwind RM (g as ref (C a), gs) = apply RM (a, g, gs) (* apply RM : atom × graph × stack → graph *) and apply RM (_, g, EMPTY) = g | apply RM (I, _, PUSH (r as ref (A (_, x)), gs)) = (r := !x; unwind RM (r, gs)) | apply RM (K, _, PUSH ( ref (A (_, x)), PUSH (r as ref (A (g0 , y as g1)), gs ))) = (g0 := C I; g1 := !x; r := !x; unwind RM (r, gs)) | apply RM (S, _, PUSH ( ref (A (_, x)), PUSH ( ref (A (_, y)), PUSH (r as ref (A (g0 , z as g1)), gs )))) = (g0 := A (ref (!x), ref (!z)); g1 := A (ref (!y), ref (!z)); unwind RM (r, gs)) | apply RM (a, _, PUSH (g as ref (A (_, g1)), gs)) = unwind RM (g1 , MARK (a, g, gs)) | apply RM (_, _, MARK (a, g, gs)) = apply RM (a, g, gs) Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 10 / 33

slide-15
SLIDE 15

Introduction Formalization Derivation Conclusion

Overview

Formalization of a reduction machine Formalization of a graph rewriting system Derivation Towards graph evaluation Conclusion

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 11 / 33

slide-16
SLIDE 16

Introduction Formalization Derivation Conclusion

Graph rewriting systems à la Barendregt

Graph: N, F, N → F, N ⇀ N × N Rewrite rules Reduction strategy

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 12 / 33

slide-17
SLIDE 17

Introduction Formalization Derivation Conclusion

Rewriting

Rewriting à la Barendregt

Extensional Rewiring is induced by the formalism

Rewriting à la Plasmeijer

Intensional Rewiring is a computational axiom

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 13 / 33

slide-18
SLIDE 18

Introduction Formalization Derivation Conclusion

Rewriting

Rewrite rules for I, K and S à la Barendregt

  • r : A(I, x),

r, x

  • r : A(A(K, x), y),

r, x

  • r : A(A(A(S, x), y), z) + r′ : A(A(x, z), A(y, z)),

r, r′

  • Rewrite rules I, K and S à la Plasmeijer

r : AIx → r := x

  • r :

Asy → r := x s : Acx c : K

  • r :

Asz → u : Axz s : Aty v : Ayz t : Acx w : Auv c : S r := w

u, v, w are fresh

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 14 / 33

slide-19
SLIDE 19

Introduction Formalization Derivation Conclusion

Rewriting

Computational axioms for rewiring

(* replace : graph × node × node → graph *) fun replace (g as ref (A (g0 , g1)), g0 ’, g1 ’) = (g0 := g0 ’; g1 := g1 ’; g) (* rewire : graph × node → graph *) fun rewire (g, x) = (g := x; g)

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 15 / 33

slide-20
SLIDE 20

Introduction Formalization Derivation Conclusion

Rewriting

Formalization of rewriting in Standard ML

datatype redex = RED_I of graph × graph | RED_K of graph × graph | RED_S of graph × graph × graph × graph (* contract : redex → graph *) fun contract (RED_I (r, ref x)) = rewire (r, x) | contract (RED_K (r, ref x)) = rewire (replace (r, C I, x), x) | contract (RED_S (r, ref x, ref y, ref z)) = replace (r, A (ref x, ref z), A (ref y, ref z))

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 16 / 33

slide-21
SLIDE 21

Introduction Formalization Derivation Conclusion

The rest of the story

Remaining operations: the reduction strategy Finding the next redex (decomposition) Rewriting the redex (contraction) Reconstructing the resulting graph (recomposition) Repeating if the result is not a normal form (iteration) Usually left implicit

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 17 / 33

slide-22
SLIDE 22

Introduction Formalization Derivation Conclusion

Decomposition and recomposition

datatype decomposition = NF

  • f graph

| DEC of redex × context (* decompose : graph → decomposition *) fun decompose g = ... (* recompose : graph × context → graph *) fun recompose (g, c) = ...

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 18 / 33

slide-23
SLIDE 23

Introduction Formalization Derivation Conclusion

One-step reduction

· · · ·

decompose

  • contract
  • recompose
  • (*

reduce : graph → graph *) fun reduce g = (case decompose g

  • f NF g’

⇒ g’ | DEC (red , c) ⇒ recompose (c, contract red))

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 19 / 33

slide-24
SLIDE 24

Introduction Formalization Derivation Conclusion

Reduction-based normalization

· · · ·

decompose

  • contract
  • recompose
  • ·

· · ·

decompose

  • contract
  • recompose
  • decompose
  • (*

iterate : decomposition → graph *) fun iterate (NF g) = g | iterate (DEC (g, c)) = iterate (decompose (recompose (c, contract g))) (* normalize : graph → graph *) fun normalize g = iterate (decompose g)

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 20 / 33

slide-25
SLIDE 25

Introduction Formalization Derivation Conclusion

Overview

Formalization of a reduction machine Formalization of a graph rewriting system Derivation Towards graph evaluation Conclusion

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 21 / 33

slide-26
SLIDE 26

Introduction Formalization Derivation Conclusion

This work

Key observation Side effects are restricted to axioms Navigation is without pointer swapping

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 22 / 33

slide-27
SLIDE 27

Introduction Formalization Derivation Conclusion

This work

Key observation Side effects are restricted to axioms Navigation is without pointer swapping Consequence Driving machinery is functional Amenable to the syntactic correspondence starting with refocusing

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 22 / 33

slide-28
SLIDE 28

Introduction Formalization Derivation Conclusion

Refocusing

· · · ·

decompose

  • contract
  • recompose
  • ·

· · ·

decompose

  • contract
  • recompose
  • decompose
  • refocus
  • refocus
  • Ian Zerny (zerny@cs.au.dk)

On Graph Rewriting, Reduction and Evaluation TFP ’09 23 / 33

slide-29
SLIDE 29

Introduction Formalization Derivation Conclusion

Derivation steps

Graph rewriting system · · · Abstract machine

Danvy and Nielsen’s refocusing

  • inlining
  • Ohori and Sasano’s lightweight fusion
  • transition compression
  • Ian Zerny (zerny@cs.au.dk)

On Graph Rewriting, Reduction and Evaluation TFP ’09 24 / 33

slide-30
SLIDE 30

Introduction Formalization Derivation Conclusion

Result: an abstract machine

This abstract machine coincides with Turner’s reduction machine.

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 25 / 33

slide-31
SLIDE 31

Introduction Formalization Derivation Conclusion

Rewriting system to reduction machine

Summary Side effects are restricted to axioms Driving machinery is functional Syntactic correspondence applies

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 26 / 33

slide-32
SLIDE 32

Introduction Formalization Derivation Conclusion

Overview

Formalization of a reduction machine Formalization of a graph rewriting system Derivation Towards graph evaluation Conclusion

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 27 / 33

slide-33
SLIDE 33

Introduction Formalization Derivation Conclusion

Towards graph evaluation

Background: Reynolds’s functional correspondence. Evaluator · · Abstract machine

closure conversion

  • CPS transformation
  • defunctionalization
  • Ian Zerny (zerny@cs.au.dk)

On Graph Rewriting, Reduction and Evaluation TFP ’09 28 / 33

slide-34
SLIDE 34

Introduction Formalization Derivation Conclusion

The functional correspondence

Danvy and students: abstract machines

functional correspondence

  • evaluators

CEK

  • KAM
  • SECD
  • monadic evaluator

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 29 / 33

slide-35
SLIDE 35

Introduction Formalization Derivation Conclusion

Towards graph evaluation

To defunctionalized form: stack marking to list of stack frames Refunctionalization Direct-style transformation

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 30 / 33

slide-36
SLIDE 36

Introduction Formalization Derivation Conclusion

Towards graph evaluation

Result: A graph evaluator Resembles the one of Okasaki, Lee and Tarditi

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 31 / 33

slide-37
SLIDE 37

Introduction Formalization Derivation Conclusion

Overview

Formalization of a reduction machine Formalization of a graph rewriting system Derivation Towards graph evaluation Conclusion

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 32 / 33

slide-38
SLIDE 38

Introduction Formalization Derivation Conclusion

Conclusion

Danvy et al.’s syntactic correspondence terms

graphs

Barendregt et al. and Turner’s graph reduction graph rewriting

reduction machines

Reynolds’s functional correspondence reduction machines

graph evaluators

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 33 / 33

slide-39
SLIDE 39

Introduction Formalization Derivation Conclusion

Conclusion

Danvy et al.’s syntactic correspondence terms

graphs

Barendregt et al. and Turner’s graph reduction graph rewriting

reduction machines

Reynolds’s functional correspondence reduction machines

graph evaluators

Thank you

Ian Zerny (zerny@cs.au.dk) On Graph Rewriting, Reduction and Evaluation TFP ’09 33 / 33