binding free variables and all that
play

Binding, Free Variables, and All That CS4450/7450 Syntax for Basic - PowerPoint PPT Presentation

Binding, Free Variables, and All That CS4450/7450 Syntax for Basic Scheme <expr> ::= <ident> <expr> ::= ( lambda ( <ident>* ) <expr> ) <expr> ::= ( <expr>* ) data Expr = Ident String | Lambda [String]


  1. Binding, Free Variables, and All That CS4450/7450

  2. Syntax for Basic Scheme <expr> ::= <ident> <expr> ::= ( lambda ( <ident>* ) <expr> ) <expr> ::= ( <expr>* ) data Expr = Ident String | Lambda [String] Expr | Funcall [Expr]

  3. Binding rule for Scheme ( lambda ( <ident> ) <expr> ) • The binder " lambda (<ident>) " binds all occurrences of <ident> within <expr> unless – there is an intervening declaraKon of the <ident> Ex: (lambda (x) (+ x ((lambda (x) x) 4)))

  4. Free & bound occurrences • A variable x occurs free in expression E iff there is some use of x not bound by a declaraKon in E • A variable x occurs bound in E iff there is some occurrence of x bound in E – Which variable occurrences are free/bound in: • ((lambda (x) x) y) • (lambda (y) ((lambda (x) x) y)) • ((lambda (x) x) x)

  5. CalculaKng the free variables data Lam = Ident String | Lambda [String] Lam | Apply [Lam] (* (lambda (x) (x y)) *) e = Lambda ["x”] (Apply [Ident "x”,Ident "y"]) free (Ident x) = … free (Lambda args e) = … free (Apply es) = … Q: how do we tell whether x is free or bound?

  6. CalculaKng the free variables lkup x [] = False lkup x (y:ys) = x==y || lkup x ys free seen (Ident x) = … free seen (Lambda args e) = … free seen (Apply es) = … A: include a list of variables known to be bound

  7. CalculaKng the free variables lkup x [] = False lkup x (y:ys) = x==y || lkup x ys free seen (Ident x) = if lkup x seen then [] else [x] free seen (Lambda args e) = free (seen ++ args) e free seen (Apply es) = foldr (++) [] (map (free seen) es)

  8. Abstract & Concrete Syntax of Imp type Name = String type FunDefn = (Name,[Name],[Stmt]) type Prog = ([FunDefn],[Stmt]) data Stmt = Assign Name Exp function double(n) { | If BExp [Stmt] [Stmt] return n+n; … } | Return Exp y := double(5); data Exp = Add Exp Exp … | FunCall Name [Exp] data BExp = IsEq Exp Exp … | LitBool Bool

  9. Variable Occurrences function iseven(n) { if n==0 {return 1;} else {return isodd(n-1);} } function isodd(n) { if n==0 {return 0;} else {return iseven (n-1);} } x := iseven (m+n);

  10. Variable Binders function iseven(n) { if n==0 {return 1;} else {return isodd(n-1);} } function isodd(n) { if n==0 {return 0;} else {return iseven (n-1);} } x := iseven (m+n); let sum := 0 in { ... } for i := e1,e2 { ... } "Binders" are language constructs that define a name or variable.

  11. Scope of Variable Binders function iseven(n) { if n==0 {return 1;} else {return isodd(n-1);} } function isodd(n) { if n==0 {return 0;} else {return iseven (n-1);} } x := iseven (m+n); let sum := 0 in { ... } for i := e1,e2 { ... } Scopes of isodd , sum , and i binders in blue

  12. Scope of Variable Binders function iseven(n) { if n==0 {return 1;} else {return isodd(n-1);} } function isodd(n) { if n==0 {return 0;} else {return iseven (n-1);} } x := iseven (m+n);

  13. Free Variable Occurrences function iseven(n) { if n==0 {return 1;} else {return isodd(n-1);} } function isodd(n) { if n==0 {return 0;} else {return iseven (n-1);} } x := iseven (m+n); Free variable occurrences Not free variable occurrences

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend