![Page 1: BindersUnbound( - Information and Computer Sciencesweirich/talks/icfp2011.pdf · BindersUnbound(Stephanie(Weirich(Brent Yorgey (Tim(Sheard(Noah(David(Yorgey , SatSept17 th,(10:24(PM](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06b9b17e708231d4196c58/html5/thumbnails/1.jpg)
Binders Unbound
Stephanie Weirich
Brent Yorgey
Tim Sheard
Noah David Yorgey, Sat Sept 17th, 10:24 PM
![Page 2: BindersUnbound( - Information and Computer Sciencesweirich/talks/icfp2011.pdf · BindersUnbound(Stephanie(Weirich(Brent Yorgey (Tim(Sheard(Noah(David(Yorgey , SatSept17 th,(10:24(PM](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06b9b17e708231d4196c58/html5/thumbnails/2.jpg)
From InspiraGon to …
data N = String data Exp = Var N | Lam N Exp | App Exp Exp
Alpha-‐equivalence & Capture-‐avoiding subsGtuGon
fv (Var x) = [x] fv (Lam x e) = fv e // [x] fv (App e e’) = fv e ++ fv e’
FrustraGon! This should be “easy”
Bug source: may need to freshen when recurring under binders
![Page 3: BindersUnbound( - Information and Computer Sciencesweirich/talks/icfp2011.pdf · BindersUnbound(Stephanie(Weirich(Brent Yorgey (Tim(Sheard(Noah(David(Yorgey , SatSept17 th,(10:24(PM](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06b9b17e708231d4196c58/html5/thumbnails/3.jpg)
DeclaraGve SpecificaGon for Binding
data N = Name Exp data Exp = Var N | Lam (Bind N Exp) | App Exp Exp
Generic programming available for fv, aeq, subsGtuGon Monadic destructor for binding ensures
freshness unbind :: Fresh m => Bind N Exp -> m (N,Exp)
Get right to the interesGng part of the implementaGon!!
James Cheney, Scrap Your Nameplate ICFP 2005
![Page 4: BindersUnbound( - Information and Computer Sciencesweirich/talks/icfp2011.pdf · BindersUnbound(Stephanie(Weirich(Brent Yorgey (Tim(Sheard(Noah(David(Yorgey , SatSept17 th,(10:24(PM](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06b9b17e708231d4196c58/html5/thumbnails/4.jpg)
Unbound Library
aeq :: (Alpha a) => a -> a -> Bool
data N = Name Exp data Exp = Var N | Lam (Bind N Exp) | App Exp Exp
$(derive [‘’Exp]) instance Alpha Exp > let x = string2Name “x” :: N
> let y = string2Name “y” :: N > Lam x (Var x) `aeq` Lam y (Var y) True
cabal install unbound
![Page 5: BindersUnbound( - Information and Computer Sciencesweirich/talks/icfp2011.pdf · BindersUnbound(Stephanie(Weirich(Brent Yorgey (Tim(Sheard(Noah(David(Yorgey , SatSept17 th,(10:24(PM](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06b9b17e708231d4196c58/html5/thumbnails/5.jpg)
Unbound library • Several small improvements to FreshLib:
– DocumentaGon and cabal distribuGon – Support for mulGple atom sorts
– Improved subsGtuGon interface – Two different monads for “freshness”
• Expressive general binding specifica2on language bind :: (Alpha b) => N -> b -> Bind N b bind :: (Alpha a, Alpha b) => a -> b -> Bind a b
What sort of binding paZerns can be
specified by type structure?
![Page 6: BindersUnbound( - Information and Computer Sciencesweirich/talks/icfp2011.pdf · BindersUnbound(Stephanie(Weirich(Brent Yorgey (Tim(Sheard(Noah(David(Yorgey , SatSept17 th,(10:24(PM](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06b9b17e708231d4196c58/html5/thumbnails/6.jpg)
Beyond Single Binding
data N = Name Exp data Exp = Var N | Lam (Bind [N] Exp) | App Exp [Exp]
data N = Name Exp data Pat = PVar N | PCon String [Pat] data Exp = Var N | Lam (Bind Pat Exp) | App Exp Exp | Con String [Exp]
\ x y z -‐> (x z) (y z)
\ (x, Just y) -‐> x + y
All names in the paZern expression are bound in the body of the Bind
![Page 7: BindersUnbound( - Information and Computer Sciencesweirich/talks/icfp2011.pdf · BindersUnbound(Stephanie(Weirich(Brent Yorgey (Tim(Sheard(Noah(David(Yorgey , SatSept17 th,(10:24(PM](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06b9b17e708231d4196c58/html5/thumbnails/7.jpg)
let x = e in e’
x bound in e’ but not e
Embedded Terms in PaZerns
data Exp = ... | Let Exp (Bind N Exp)
data Exp = ... | Let (Bind (N, Embed Exp) Exp)
data Exp = ... | Let (Bind [(N, Embed Exp)] Exp)
All names in the paZern except in Embeds are bound in the body of the Bind
let x1 = e1 x2 = e2 ... in e’
x1,x2… bound in e’
Can enforce equal number of LHSs and RHSs
data Exp = ... | Let [Exp] (Bind [N] Exp)
![Page 8: BindersUnbound( - Information and Computer Sciencesweirich/talks/icfp2011.pdf · BindersUnbound(Stephanie(Weirich(Brent Yorgey (Tim(Sheard(Noah(David(Yorgey , SatSept17 th,(10:24(PM](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06b9b17e708231d4196c58/html5/thumbnails/8.jpg)
Double Binding (recursive)
data Exp = ... | Let (Bind (Rec (N, Embed Exp)) Exp)
data Exp = ... | Let (Bind (Rec [(N, Embed Exp)]) Exp)
All names in a rec paZern are bound in both the Embeds and the body of the Bind
let rec x = e in e’
x bound in e and e’
let rec x1 = e1 x2 = e2
... in e’
x1, x2… bound in e1,e2… and e’
data Exp = … | Let (Bind N (Exp, Exp))
data Exp = ... | Let (Bind [N] ([Exp], Exp))
![Page 9: BindersUnbound( - Information and Computer Sciencesweirich/talks/icfp2011.pdf · BindersUnbound(Stephanie(Weirich(Brent Yorgey (Tim(Sheard(Noah(David(Yorgey , SatSept17 th,(10:24(PM](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06b9b17e708231d4196c58/html5/thumbnails/9.jpg)
Double Binding (non-‐recursive)
data Exp = ... | Let (Bind LetPat Exp)
data LetPat = Nil
| Cons (Rebind (N, Embed Exp) LetPat)
let* x1 = e1 x2 = e2 x3 = e3
... in e’
x1 bound in e2, e3, e’ x2 bound in e3, e’ x3 bound in e’
![Page 10: BindersUnbound( - Information and Computer Sciencesweirich/talks/icfp2011.pdf · BindersUnbound(Stephanie(Weirich(Brent Yorgey (Tim(Sheard(Noah(David(Yorgey , SatSept17 th,(10:24(PM](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06b9b17e708231d4196c58/html5/thumbnails/10.jpg)
Binding SpecificaGon Language • T ::= (Terms)
| Primitive types, Int, Char, etc.
| Regular datatypes of terms, i.e. [T], (T,T)
| Name T
| Bind P T
• P ::= (Patterns) | Name T
| Primitive types, Int, Char, etc.
| Regular datatypes of patterns, i.e. [P], (P,P)
| Embed T
| Rec P
| Rebind P P
| Shift P
![Page 11: BindersUnbound( - Information and Computer Sciencesweirich/talks/icfp2011.pdf · BindersUnbound(Stephanie(Weirich(Brent Yorgey (Tim(Sheard(Noah(David(Yorgey , SatSept17 th,(10:24(PM](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06b9b17e708231d4196c58/html5/thumbnails/11.jpg)
SemanGcs • Paper gives precise semanGcs for fv, aeq and subst for terms
and paZerns composed of these types
• SemanGcs based on locally nameless representa2on – Simple definiGons of operaGons
– Rec/Shil inspired by semanGcs
• Proofs of basic properGes of the operaGons
• ImplementaGon follows semanGcs & uses RepLib library for generic programming (~2500 loc)
![Page 12: BindersUnbound( - Information and Computer Sciencesweirich/talks/icfp2011.pdf · BindersUnbound(Stephanie(Weirich(Brent Yorgey (Tim(Sheard(Noah(David(Yorgey , SatSept17 th,(10:24(PM](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06b9b17e708231d4196c58/html5/thumbnails/12.jpg)
Future work
• Scope preservaGon (see Pouillard and Westbrook)
• DeclaraGve semanGcs, independent of variable representaGon
• AlternaGve implementaGons (nominal, canonical, opGmized?)
• IntegraGon with theorem prover
![Page 13: BindersUnbound( - Information and Computer Sciencesweirich/talks/icfp2011.pdf · BindersUnbound(Stephanie(Weirich(Brent Yorgey (Tim(Sheard(Noah(David(Yorgey , SatSept17 th,(10:24(PM](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06b9b17e708231d4196c58/html5/thumbnails/13.jpg)
Related Work
• Cheney, FreshLib
• Pooer, CαML
• Charguéraud, The Locally Nameless Representa2on
• Sewell et al. OTT Urban, General Bindings and Alpha-‐Equivalence
in Nominal Isabelle
![Page 14: BindersUnbound( - Information and Computer Sciencesweirich/talks/icfp2011.pdf · BindersUnbound(Stephanie(Weirich(Brent Yorgey (Tim(Sheard(Noah(David(Yorgey , SatSept17 th,(10:24(PM](https://reader034.vdocument.in/reader034/viewer/2022042317/5f06b9b17e708231d4196c58/html5/thumbnails/14.jpg)
Summary
• Separate specificaGon of binding structure from implementaGon
• Abstract types define a EDSL for binding
• Type-‐generic programming automates boilerplate
• Locally nameless representaGon simplifies semanGcs