Implementing Lambda Calculi in Prolog

Implementing Lambda Calculi in Prolog

Resources: Some of the slides of this lecture were derived from [Järvi], with permission of the original author, by copy x = 1 & paste or by selection, annotation, or rewording. [Järvi] is let x = 1 in ... in turn based on [Pierce] as the underlying textbook. x(1). !x(1) x.set(1) Programming Paradigms and Formal Semantics Implementing Lambda Calculi in Prolog Ralf Lämmel <1h [Järvi] Slides by J. Järvi: “Programming Languages”, CPSC 604 @ TAMU (2009) [Pierce] B.C. Pierce: Types and Programming Languages, MIT Press, 2002 Table of contents • Untyped NB Objective: compare grammar- and rule-based formalization • Typed NB with the corresponding Prolog encodings / implementation. • Untyped lambda calculus • Applied, untyped lambda calculus • Applied, typed lambda calculus © Ralf Lämmel, 2009-2011 unless noted otherwise 192 Untyped NB © Ralf Lämmel, 2009-2011 unless noted otherwise 193 This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU. SyntaxMore than one syntactic of category NB Language NB t ::= terms: v value if t1 then t2 else t3 conditional succ t successor pred t predecessor iszero t test for zero v ::= values: true constant true false constant false nv numeric value nv ::= numeric values: 0 zero value succ nv successor value © Ralf Lämmel, 2009-2011 unless noted otherwise 194 54 / 58 Prolog Syntax of NB term(V) :- value(V). term(if(T1,T2,T3)) :- term(T1), term(T2), term(T3). term(succ(T)) :- term(T). term(pred(T)) :- term(T). term(iszero(T)) :- term(T). We are faithful to the distinction of the value(true). syntactical categories. value(false). value(NV) :- nvalue(NV). nvalue(zero). nvalue(succ(NV)) :- nvalue(NV). © Ralf Lämmel, 2009-2011 unless noted otherwise 195 Prolog NB: sample terms supposed to evaluate to 0 if(iszero(pred(succ(zero))),zero,succ(succ(zero))). supposed to evaluate to false if(iszero(zero),iszero(succ(zero)),true). © Ralf Lämmel, 2009-2011 unless noted otherwise 196 This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU. Typ es for EvaluationNB rules of NB Reminder evaluation rules NB (SOS) E-Iszero E-IszeroZero t t → iszero 0 true iszero t iszero t → → E-Succ E-Pred E-IszeroSucc t t t t iszero (succ nv) false → → → succ t succ t pred t pred t → → E-PredZero E-PredSucc E-IfTrue pred 0 0 pred (succ nv) nv if true then t else t t → → 2 3 → 2 E-IfFalse Exercise: what if false then t2 else t3 t3 → happens to our type system E-If t t when we omit 1 → 1 if t then t else t if t then t else t the rule? 1 2 3 → 1 2 3 14 / 28 © Ralf Lämmel, 2009-2011 unless noted otherwise 197 Prolog Evaluation rules of NB (SOS) % eval(pred(zero),zero). Disfavored eval(pred(succ(NV)),NV) :- nvalue(NV). semantics eval(succ(T1),succ(T2)) :- eval(T1,T2). eval(pred(T1),pred(T2)) :- eval(T1,T2). Note: appearances of eval(iszero(zero),true). metavariables in SOS eval(iszero(succ(NV)),false) :- nvalue(NV). translate into tests for values. eval(iszero(T1),iszero(T2)) :- eval(T1,T2). eval(if(true,T2,_),T2). eval(if(false,_,T3),T3). eval(if(T1,T2,T3),if(T4,T2,T3)) :- eval(T1,T4). © Ralf Lämmel, 2009-2011 unless noted otherwise 198 Prolog Reflexive, transitive closure manysteps(V,V) :- value(V). manysteps(T1,V) :- eval(T1,T2), manysteps(T2,V). This is like ➝* in the formal setup, and the predicate works for any language with a binary reduction relation eval/2. © Ralf Lämmel, 2009-2011 unless noted otherwise 199 Prolog Composing everything :- [...]. % Import syntax and semantics :- [...]. % Import main predicate :- current_prolog_flag(argv,Argv), ( append(_,['--',Input],Argv), main(Input), halt; true ). Hence, we can invoke the language processor from the command-line prompt. © Ralf Lämmel, 2009-2011 unless noted otherwise 200 Prolog main(Input) :- see(Input), read(Term), seen, format('Input term: ~w~n',[Term]), manysteps(Term,X), show(X,Y), format('Value of term: ~w~n',[Y]). show(zero,0) :- !. show(succ(X),Z) :- !, show(X,Y), Z is Y + 1. show(X,X). © Ralf Lämmel, 2009-2011 unless noted otherwise 201 Running the NB interpreter $ swipl -q -f main.pro -- ../samples/sample1.nb Input term: if(iszero(pred(succ(zero))),zero,succ(succ(zero))) Value of term: 0 $ © Ralf Lämmel, 2009-2011 unless noted otherwise 202 Typed NB © Ralf Lämmel, 2009-2011 unless noted otherwise 203 This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU. Typ es for NB typing rules NB Types in NB First, we need a few new syntactic forms T ::= types: Bool the Boolean type Nat the type of numeric values And typing rules: T-If T-True T-False t1 : Bool t2 : Tt3 : T true : Bool false : Bool if t1 then t2 else t3 : T T-Succ T-Pred T-Iszero © Ralf Lämmel, T-Zero2009-2011 unless noted otherwise 204 t : Nat t : Nat t : Nat 0 : Nat succ t : Nat pred t : Nat iszero t : Bool 15 / 28 Typ es for NB typing rules NB This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU. First, we need a few new syntactic forms T ::= types: Bool typingthe Boolean typerules NatNBthe type of numeric values And typing rules: T-If T-True T-False t1 : Bool t2 : Tt3 : T true : Bool false : Bool if t1 then t2 else t3 : T T-Succ T-Pred T-Iszero T-Zero t : Nat t : Nat t : Nat 0 : Nat succ t : Nat pred t : Nat iszero t : Bool 15 / 28 © Ralf Lämmel, 2009-2011 unless noted otherwise 205 Prolog NB typing rules welltyped(true,bool). 1:1 mapping welltyped(false,bool). welltyped(zero,nat). welltyped(succ(T),nat) :- welltyped(T,nat). welltyped(pred(T),nat) :- welltyped(T,nat). welltyped(iszero(T),bool) :- welltyped(T,nat). welltyped(if(T1,T2,T3),T) :- welltyped(T1,bool), welltyped(T2,T), welltyped(T3,T). © Ralf Lämmel, 2009-2011 unless noted otherwise 206 Prolog Conditional evaluation for typed NB main(Input) :- see(Input), read(Term), seen, format('Input term: ~w~n',[Term]), welltyped(Term, Type), format('Type of term: ~w~n',[Type]), manysteps(Term,X), show(X,Y), format('Value of term: ~w~n',[Y]). © Ralf Lämmel, 2009-2011 unless noted otherwise 207 The Untyped Lambda Calculus © Ralf Lämmel, 2009-2011 unless noted otherwise 208 Summarizing Summarizing the system This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU. The grammar and (small-step) operational semantics of lambda-calculus Formalizationlambda-calculus of the lambda calculus Evaluation rules (call-by-name) Syntax Summarizing Summarizing the system Syntax t ::=x • t ::=x t t t t t1 → t1 t → t λx.t Terms → → t1 t2 t1 t2 vt vt 1 2 → 1 2 → The grammar and (small-step) operational semantics of tt (λx.t) v [v/x]t lambda-calculus → v ::=λx.t Values (normal forms) Evaluation rules (call-by-name)v ::=λx.t Syntax is the smallest binary relation on • Evaluation → terms satisfying the rules t ::=x t t t t 1 → 1 → λx.t 34 / 36 t1 t2 t1 t2 vt vt tt → → (λx.t) v [v/x]t → v ::=λx.t © Ralf Lämmel, 2009-2011 unless noted otherwise 209 is the smallest binary relation on → terms satisfying the rules 34 / 36 Prolog Syntax of the untyped lambda calculus term(var(X)) :- variable(X). term(app(T1,T2)) :- term(T1), term(T2). term(lam(X,T)) :- variable(X), term(T). value(lam(X,T)) :- variable(X), term(T). variable(X) :- atom(X). Variables are Prolog atoms. © Ralf Lämmel, 2009-2011 unless noted otherwise 210 Prolog λ: sample term app(app(app( % TEST (if-then-else) lam(l,lam(m,lam(n,app(app(var(l),var(m)),var(n))))), % Church Boolean True lam(t,lam(f,var(t)))), We illustrate Church Booleans % Church Numeral 0 and numerals. That is, we use a conditional (TEST) to select lam(s,lam(z,var(z)))), either C0 or C1. % Church Numeral 1 lam(s,lam(z,app(var(s),var(z))))). © Ralf Lämmel, 2009-2011 unless noted otherwise 211 Prolog Evaluation rules of the untyped lambda calculus eval(app(T1,T2),app(T3,T2)) :- eval(T1,T3). eval(app(V,T1),app(V,T2)) :- Substitution (as needed for beta reduction) is the value(V), interesting part--both in the eval(T1,T2). formal setting. and in Prolog. eval(app(lam(X,T1),V),T2) :- value(V), substitute(V,X,T1,T2). © Ralf Lämmel, 2009-2011 unless noted otherwise 212 This slide is derived from Jaakko Järvi’s slides for his course ”ProgrammingIntroduction Languages”, CPSC 604 @ TAMU. InductiveSubstitution definition of substitution in λ calculus [N/x]x = N [N/x]y = y, y any variable different from x [N/x](M1 M2)=([N/x]M1)([N/x]M2) [N/x](λx.M)=λx.MIntroduction Variable[N/x binding](λy.M)= —λ precisey.([N/x definition]M), y not free in N Alpha renaming can be applied freely, variables drawn from the infinite FV(M) defines the set of free variables in the term M pool of variable names FV(x)=Examples:x { } FV(MN)=FV(M) FV(N) [z/x]x ∪ FV(λx.M)=FV(M) x [z/x](λx.xx) \{ } © Ralf Lämmel, 2009-2011[z/x unless](λ ynoted.yx otherwise) 213 [Spotz/x]( freeλz. andxz) bound occurrences here: 12 / 36 (λx.y)(λy.y) λx.(λy.xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9 / 36 Substitution 1/3 Prolog substitute(N,X,var(X),N).

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    45 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us