Unification of Program Expressions with Recursive Bindings Manfred Schmidt-Schauß and David Sabel † Goethe-University Frankfurt am Main, Germany PPDP 2016, Edinburgh, UK † Research supported by the Deutsche Forschungsgemeinschaft (DFG) under grant SA 2908/3-1. 1
Motivation Unification as a core procedure for automated reasoning on programs and program transformations w.r.t. operational semantics for program calculi with higher-order constructs and recursive bindings, e.g. letrec x 1 = s 1 ; . . . ; x n = s n in t special focus: extended call-by-need lambda calculi with letrec that model core languages of lazy functional programming languages like Haskell 2/19
Application: Correctness of Program Transformations Program transformation T is correct iff ∀ ℓ → r ∈ T : ∀ C : C [ ℓ ] ↓ ⇐ ⇒ C [ r ] ↓ where ↓ = successful evaluation w.r.t. standard reduction Diagram-based proof method to show correctness of transformations: Compute overlaps between standard reductions and program transformations (automatable by unification ) Join the overlaps ⇒ forking and commuting diagrams Induction using the diagrams (automatable, see [RSSS12, IJCAR]) program program transformation transformation σ ( ℓ 1 )= σ ( ℓ 1 )= σ ( ℓ 2 ) σ ( r 2 ) σ ( r 2 ) σ ( ℓ 2 ) standard standard * * reduction reduction · · σ ( r 1 ) σ ( r 1 ) * * unifier σ for ℓ 1 . unifier σ for ℓ 1 . = ℓ 2 = r 2 3/19
Design Decisions for the Meta-Syntax Operational semantics of typical call-by-need calculi (excerpt) Reduction contexts: A ::= [ · ] | ( A e ) R ::= A | letrec Env in A | letrec { x i = A i [ x i +1 ] } n − 1 i =1 , x n = A n , Env , in A [ x 1 ] Standard-reduction rules and some program transformations (SR,lbeta) R [( λx.e 1 ) e 2 ] → R [ letrec x = e 2 in e 1 ] letrec Env 1 in letrec Env 2 in e → letrec Env 1 , Env 2 in e (SR,llet) (T,cpx) T [ letrec x = y, Env in C [ x ]] → T [ letrec x = y, Env in C [ y ]] (T,gc) T [ letrec Env in e ] → T [ e ] if LetVars ( Env ) ∩ FV ( e ) = ∅ 4/19
Design Decisions for the Meta-Syntax Operational semantics of typical call-by-need calculi (excerpt) Reduction contexts: A ::= [ · ] | ( A e ) R ::= A | letrec Env in A | letrec { x i = A i [ x i +1 ] } n − 1 i =1 , x n = A n , Env , in A [ x 1 ] Standard-reduction rules and some program transformations (SR,lbeta) R [( λx.e 1 ) e 2 ] → R [ letrec x = e 2 in e 1 ] letrec Env 1 in letrec Env 2 in e → letrec Env 1 , Env 2 in e (SR,llet) (T,cpx) T [ letrec x = y, Env in C [ x ]] → T [ letrec x = y, Env in C [ y ]] (T,gc) T [ letrec Env in e ] → T [ e ] if LetVars ( Env ) ∩ FV ( e ) = ∅ Meta-syntax must be capable to represent: contexts of different classes environments Env i , environment chains { x i = A i [ x i +1 ] } n − 1 i =1 4/19
Design Decisions for the Meta-Syntax Operational semantics of typical call-by-need calculi (excerpt) Reduction contexts: A ::= [ · ] | ( A e ) R ::= A | letrec Env in A | letrec { x i = A i [ x i +1 ] } n − 1 i =1 , x n = A n , Env , in A [ x 1 ] Standard-reduction rules and some program transformations (SR,lbeta) R [( λx.e 1 ) e 2 ] → R [ letrec x = e 2 in e 1 ] letrec Env 1 in letrec Env 2 in e → letrec Env 1 , Env 2 in e (SR,llet) (T,cpx) T [ letrec x = y, Env in C [ x ]] → T [ letrec x = y, Env in C [ y ]] (T,gc) T [ letrec Env in e ] → T [ e ] if LetVars ( Env ) ∩ FV ( e ) = ∅ Meta-syntax must be capable to represent: contexts of different classes environments Env i , environment chains { x i = A i [ x i +1 ] } n − 1 i =1 4/19
Design Decisions for the Meta-Syntax Operational semantics of typical call-by-need calculi (excerpt) Reduction contexts: A ::= [ · ] | ( A e ) R ::= A | letrec Env in A | letrec { x i = A i [ x i +1 ] } n − 1 i =1 , x n = A n , Env , in A [ x 1 ] Standard-reduction rules and some program transformations (SR,lbeta) R [( λx.e 1 ) e 2 ] → R [ letrec x = e 2 in e 1 ] letrec Env 1 in letrec Env 2 in e → letrec Env 1 , Env 2 in e (SR,llet) (T,cpx) T [ letrec x = y, Env in C [ x ]] → T [ letrec x = y, Env in C [ y ]] (T,gc) T [ letrec Env in e ] → T [ e ] if LetVars ( Env ) ∩ FV ( e ) = ∅ Meta-syntax must be capable to represent: contexts of different classes environments Env i , environment chains { x i = A i [ x i +1 ] } n − 1 i =1 4/19
Design Decisions for the Meta-Syntax Operational semantics of typical call-by-need calculi (excerpt) Reduction contexts: A ::= [ · ] | ( A e ) R ::= A | letrec Env in A | letrec { x i = A i [ x i +1 ] } n − 1 i =1 , x n = A n , Env , in A [ x 1 ] Standard-reduction rules and some program transformations (SR,lbeta) R [( λx.e 1 ) e 2 ] → R [ letrec x = e 2 in e 1 ] letrec Env 1 in letrec Env 2 in e → letrec Env 1 , Env 2 in e (SR,llet) (T,cpx) T [ letrec x = y, Env in C [ x ]] → T [ letrec x = y, Env in C [ y ]] (T,gc) T [ letrec Env in e ] → T [ e ] if LetVars ( Env ) ∩ FV ( e ) = ∅ Meta-syntax must be capable to represent: contexts of different classes environments Env i , environment chains { x i = A i [ x i +1 ] } n − 1 i =1 4/19
Syntax of the Meta-Language LRSX Variables x ∈ Var ::= X (variable meta-variable) | x (concrete variable) Expressions s ∈ Expr ::= S (expression meta-variable) | D [ s ] (context meta-variable) | letrec env in s (letrec-expression) | var x (variable) | ( f r 1 . . . r ar ( f ) ) (function application) where r i is o i , s i , or x i specified by f o ∈ HExpr n ::= x 1 . . . . x n .s (higher-order expression) Environments env ∈ Env ::= ∅ (empty environment) | E ; env (environment meta-variable) | Ch [ x, s ]; env (chain meta-variable) | x.s ; env (binding) 5/19
Binding and Scoping Constraints Operational semantics of typical call-by-need calculi (excerpt) Reduction contexts: A ::= [ · ] | ( A e ) R ::= A | letrec Env in A | letrec { x i = A i [ x i +1 ] } n − 1 i =1 , x n = A n , Env , in A [ x 1 ] Standard-reduction rules and some program transformations (SR,lbeta) R [( λx.e 1 ) e 2 ] → R [ letrec x = e 2 in e 1 ] (SR,llet) letrec Env 1 in letrec Env 2 in e → letrec Env 1 , Env 2 in e T [ letrec x = y, Env in C [ x ]] → T [ letrec x = y, Env in C [ y ]] (T,cpx) (T,gc) T [ letrec Env in e ] → T [ e ] if LetVars ( Env ) ∩ FV ( e ) = ∅ Unification-problems have to treat (implicit) restrictions on scoping and emptiness, e.g.: (gc): Env must not be empty; side condition on variables, (llet): FV ( Env 1 ) ∩ LetVars ( Env 2 ) = ∅ (cpx): x, y are not captured by C in C [ x ] 6/19
Binding and Scoping Constraints Operational semantics of typical call-by-need calculi (excerpt) Reduction contexts: A ::= [ · ] | ( A e ) R ::= A | letrec Env in A | letrec { x i = A i [ x i +1 ] } n − 1 i =1 , x n = A n , Env , in A [ x 1 ] Standard-reduction rules and some program transformations (SR,lbeta) R [( λx.e 1 ) e 2 ] → R [ letrec x = e 2 in e 1 ] (SR,llet) letrec Env 1 in letrec Env 2 in e → letrec Env 1 , Env 2 in e T [ letrec x = y, Env in C [ x ]] → T [ letrec x = y, Env in C [ y ]] (T,cpx) (T,gc) T [ letrec Env in e ] → T [ e ] if LetVars ( Env ) ∩ FV ( e ) = ∅ Unification-problems have to treat (implicit) restrictions on scoping and emptiness, e.g.: (gc): Env must not be empty; side condition on variables, (llet): FV ( Env 1 ) ∩ LetVars ( Env 2 ) = ∅ (cpx): x, y are not captured by C in C [ x ] 6/19
Binding and Scoping Constraints Operational semantics of typical call-by-need calculi (excerpt) Reduction contexts: A ::= [ · ] | ( A e ) R ::= A | letrec Env in A | letrec { x i = A i [ x i +1 ] } n − 1 i =1 , x n = A n , Env , in A [ x 1 ] Standard-reduction rules and some program transformations (SR,lbeta) R [( λx.e 1 ) e 2 ] → R [ letrec x = e 2 in e 1 ] (SR,llet) letrec Env 1 in letrec Env 2 in e → letrec Env 1 , Env 2 in e T [ letrec x = y, Env in C [ x ]] → T [ letrec x = y, Env in C [ y ]] (T,cpx) (T,gc) T [ letrec Env in e ] → T [ e ] if LetVars ( Env ) ∩ FV ( e ) = ∅ Unification-problems have to treat (implicit) restrictions on scoping and emptiness, e.g.: (gc): Env must not be empty; side condition on variables, (llet): FV ( Env 1 ) ∩ LetVars ( Env 2 ) = ∅ (cpx): x, y are not captured by C in C [ x ] 6/19
Binding and Scoping Constraints Operational semantics of typical call-by-need calculi (excerpt) Reduction contexts: A ::= [ · ] | ( A e ) R ::= A | letrec Env in A | letrec { x i = A i [ x i +1 ] } n − 1 i =1 , x n = A n , Env , in A [ x 1 ] Standard-reduction rules and some program transformations (SR,lbeta) R [( λx.e 1 ) e 2 ] → R [ letrec x = e 2 in e 1 ] (SR,llet) letrec Env 1 in letrec Env 2 in e → letrec Env 1 , Env 2 in e T [ letrec x = y, Env in C [ x ]] → T [ letrec x = y, Env in C [ y ]] (T,cpx) (T,gc) T [ letrec Env in e ] → T [ e ] if LetVars ( Env ) ∩ FV ( e ) = ∅ Unification-problems have to treat (implicit) restrictions on scoping and emptiness, e.g.: (gc): Env must not be empty; side condition on variables, (llet): FV ( Env 1 ) ∩ LetVars ( Env 2 ) = ∅ (cpx): x, y are not captured by C in C [ x ] 6/19
Letrec Unification Problem A letrec unification problem is a tuple P = (Γ , ∆ 1 , ∆ 2 , ∆ 3 ) with Γ : unification equations s . = s ′ ∆ 1 : non-empty contexts (set of D -variables) ∆ 2 : non-empty environments (set of E -variables) ∆ 3 : non-capture constraints (set of (expression,context)-pairs) Occurrence restrictions: Each S -variable occurs at most twice in Γ Each E -, Ch -, D -variable occurs at most once in Γ Ch -variables are only allowed in one letrec-environment in Γ 7/19
Recommend
More recommend