A-calculus with Constants and Let-blocks

Slides:



Advertisements
Similar presentations
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Advertisements

Termination Analysis Math Foundations of Computer Science.
6. Introduction to the Lambda Calculus. © O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap  What is Computability? — Church’s Thesis.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Search in the semantic domain. Some definitions atomic formula: smallest formula possible (no sub- formulas) literal: atomic formula or negation of an.
Last time Proof-system search ( ` ) Interpretation search ( ² ) Quantifiers Equality Decision procedures Induction Cross-cutting aspectsMain search strategy.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
7.1 Systems of Linear Equations: Two Equations Containing Two Variables.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
March 4, 2008 DF4-1http://csg.csail.mit.edu/arvind/ From Id/pH to Dataflow Graphs Arvind Computer Science & Artificial Intelligence Lab Massachusetts Institute.
Sullivan Algebra and Trigonometry: Section 12.1 Systems of Linear Equations Objectives of this Section Solve Systems of Equations by Substitution Solve.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Solving Systems Using Elimination
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
Solve the following system using the elimination method.
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
Lesson 3 Formalizing and Implementing Pure Lambda Calculus 1/15/02 Chapters 5.3, 6, 7.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L04Ext-1 September 21, 2006http:// Some more thoughts.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
Chapter 5 1. Chapter Summary  Mathematical Induction  Strong Induction  Recursive Definitions  Structural Induction  Recursive Algorithms.
A LISP interepreter for the Lambda Calculus Functional Programming
CS5205: Foundation in Programming Languages Type Reconstruction
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Chapter 2: Lambda Calculus
CS5205: Foundations in Programming Languages
Systems of linear equations
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Introduction to Logic for Artificial Intelligence Lecture 2
Propositional Calculus: Boolean Functions and Expressions
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
Introduction to Scheme
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Predicate Calculus Validity
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Systems of First Order Linear Equations
Chapter 5: Systems of Linear Equations
Proving Properties of Recursive List Functions
Carlos Varela Rennselaer Polytechnic Institute September 6, 2016
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
Programming Languages and Compilers (CS 421)
Functional Programming
First Order Logic Rosen Lecture 3: Sept 11, 12.
Maintenance Sheet 18 Due Tomorrow
Carlos Varela Rennselaer Polytechnic Institute September 8, 2017
Systems of Equations and Inequalities
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Class 33: Making Recursion M.C. Escher, Ascending and Descending
Material in the textbook Sections to 1.2.1
There are infinite solutions to the system.
Announcements Exam 2 on Friday, November 2nd Topics
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Bluespec-7: Semantics of Bluespec
This Lecture Substitution model
2 Equations, Inequalities, and Applications.
CSE S. Tanimoto Lambda Calculus
Systems of Linear Equations: An Introduction
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Programming Languages
Carlos Varela Rennselaer Polytechnic Institute September 8, 2015
The student will be able to:
Variables.
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Carlos Varela Rennselaer Polytechnic Institute September 10, 2019
Presentation transcript:

A-calculus with Constants and Let-blocks September 19, 2006

Outline Recursion and Y combinator  The llet Calculus September 19, 2006 http://www.csg.csail.mit.edu/6.827

Recursion fact n = if (n == 0) then 1 else n * fact (n-1) fact can be rewritten as: fact = n. Cond (Zero? n) 1 (Mul n (fact (Sub n 1))) How to get rid of the fact on the RHS? Idea: pass fact as an argument to itself H = f.n.Cond (Zero? n) 1 (Mul n (f f (Sub n 1))) fact = H H Self application! September 19, 2006 http://www.csg.csail.mit.edu/6.827

Self-application and Paradoxes Self application, i.e., (x x) is dangerous. Suppose: u  y. if (y y) = a then b else a What is (u u) ? (u u)  if (u u) = a then b else a Contradiction!!! Any semantics of -calculus has to make sure that functions such as u have the meaning , i.e. “totally undefined” or “no information”. Self application also violates every type discipline. September 19, 2006 http://www.csg.csail.mit.edu/6.827

Recursion and Fixed Point Equations Recursive functions can be thought of as solutions of fixed point equations: fact = n. Cond (Zero? n) 1 (Mul n (fact (Sub n 1))) Suppose H = f.n.Cond (Zero? n) 1 (Mul n (f (Sub n 1))) then fact = H fact fact is a fixed point of function H! September 19, 2006 http://www.csg.csail.mit.edu/6.827

Fixed Point Equations f : D  D A fixed point equation has the form f(x) = x Its solutions are called the fixed points of f because if xp is a solution then xp = f(xp) = f(f(xp)) = f(f(f(xp))) = ... Examples: f: Int  Int Solutions f(x) = x2 – 2 f(x) = x2 + x + 1 f(x) = x x = 2, x = -1 no solutions infinite number of solutions September 19, 2006 http://www.csg.csail.mit.edu/6.827

Least Fixed Point Consider f n = if n=0 then 1 else (if n=1 then f 3 else f (n-2)) H = f.n.Cond(n=0 , 1, Cond(n=1, f 3, f (n-2)) Is there an fp such that fp = H fp ? f1 n = 1 if n is even =  otherwise f2 n = 1 if n is even = 5 otherwise f1 contains no arbitrary information and is called the least fixed point. Unique solution! September 19, 2006 http://www.csg.csail.mit.edu/6.827

Y : A Fixed Point Operator Y  f.(x. (f (x x))) (x.(f (x x))) Notice Y F  x.F (x x)) (x.F (x x))  F (Y F)  F (x.F (x x)) (x.F (x x)) F (x.F (x x)) (x.F (x x)) F (Y F) = Y F (Y F) is a fixed point of F Y computes the least fixed point of any function ! There are many different fixed point operators. September 19, 2006 http://www.csg.csail.mit.edu/6.827

Mutual Recursion odd n = if n==0 then False else even (n-1) even n = if n==0 then True else odd (n-1) odd = H1 even even = H2 odd where H1 = f.n.Cond(n=0, False, f(n-1)) H2 = f.n.Cond(n=0, True, f(n-1)) substituting “H2 odd” for even odd = H1 (H2 odd) = H odd where H =  odd = Y H Can we expressing odd using Y ? f. H1 (H2 f) September 19, 2006 http://www.csg.csail.mit.edu/6.827

-calculus with Combinator Y Recursive programs can be translated into the -calculus with constants and Y combinator. However, Y combinator violates every type discipline translation is messy in case of mutually recursive functions  extend the -calculus with recursive let blocks. September 19, 2006 http://www.csg.csail.mit.edu/6.827

Outline Recursion and Y combinator  The llet Calculus  September 19, 2006 http://www.csg.csail.mit.edu/6.827

-calculus with Constants & Letrec E ::= x | x.E | E E | Cond (E, E, E) | PFk(E1,...,Ek) | CN0 | CNk(E1,...,Ek) | CNk(SE1,...,SEk) | let S in E PF1 ::= negate | not | ... | Prj1| Prj2 | ... PF2 ::= + | ... CN0 ::= Number | Boolean CN2 ::= cons | ... Statements S ::=  | x = E | S; S Variables on the LHS in a let expression must be pairwise distinct not in initial terms September 19, 2006 http://www.csg.csail.mit.edu/6.827

Let-block Statements “ ; “ is associative and commutative S1 ; S2 S2 ; S1 S1 ; (S2 ; S3) (S1 ; S2 ) ; S3  ; S  S let  in E  E September 19, 2006 http://www.csg.csail.mit.edu/6.827

Free Variables of an Expression FV(x) = {x} FV(E1 E2) = FV(E1) U FV(E2) FV(x.E) = FV(E) - {x} FV(let S in E) = FVS(S) U FV(E) – BVS(S) FVS() = {} BVS() = {} FVS(x = E; S) = FV(E) U FVS(S) BVS(x = E; S) = {x} U BVS(S) September 19, 2006 http://www.csg.csail.mit.edu/6.827

-Renaming (to avoid free variable capture) Assuming t is a new variable, rename x to t : x.e t.(e[t/x]) let x = e ; S in e0  let t = e[t/x] ; S[t/x] in e0[t/x] where [t/x] is defined as follows: x[t/x] = t y[t/x] = y if x  y (E1 E2 )[t/x] = (E1[t/x] E2[t/x]) (x.E)[t/x] = x.E (y.E)[t/x] = y.E[t/x] if x  y (let S in E)[t/x] ? = (let S in E) if x  FV(let S in E) = (let S[t/x] in E[t/x]) if x  FV(let S in E) (S1; S2)[t/x] = (y = E)[t/x] = [t/x] = (S1[t/x]; S2[t/x]) (y = E[t/x])  September 19, 2006 http://www.csg.csail.mit.edu/6.827

Primitive Functions and Datastructures -rules +( n, m)  n+m ... Cond-rules Cond(True, e1, e2 )  e1 Cond(False, e1, e2 )  e2 Data-structures CNk(e1,...,ek )  Prji(CNk(a1,...,ak ))  let t1 = e1; ... ; tk = ek in CNk(t1,...,tk ) ai September 19, 2006 http://www.csg.csail.mit.edu/6.827

The -rule The normal -rule (x.e) ea  e [ea/x] is replaced the following -rule (x.e) ea  let t = ea in e[t/x] where t is a new variable and the Instantiation rules which are used to refer to the value of a variable September 19, 2006 http://www.csg.csail.mit.edu/6.827

Values and Simple Expressions V ::= x.E | CN0 | CNk(SE1,...,SEk) Simple expressions SE ::= x | V September 19, 2006 http://www.csg.csail.mit.edu/6.827

Contexts for Expressions A context is an expression (or statement) with a “hole” such that if an expression is plugged in the hole the context becomes a legitimate expression: C[] ::= [] | x.C[] | C[] E | E C[] | let S in C[] | let SC[] in E Statement Context for an expression SC[] ::= x = C[] | SC[] ; S | S; SC[] September 19, 2006 http://www.csg.csail.mit.edu/6.827

let Instantiation Rules A free variable in an expression can be instantiated by a simple expression Instantiation rule 1 (let x = a ; S in C[x])  (let x = a ; S in C’[a]) simple expression free occurrence of x in some context C renamed C[ ] to avoid free-variable capture Instantiation rule 2 (x = a ; SC[x])  (x = a ; SC’[a]) Instantiation rule 3 x = a  where a = C[x] x = C’[C[x]] September 19, 2006 http://www.csg.csail.mit.edu/6.827

Lifting Rules: Motivation let f = let S1 in x.e1 y = f a in ((let S2 in x.e2) e3) How do we juxtapose (x.e1) a or (x.e2) e3 ? September 19, 2006 http://www.csg.csail.mit.edu/6.827

Lifting Rules (let S’ in e’) is the -renamed (let S in e) to avoid name conflicts in the following rules: x = let S in e  let S1 in (let S in e)  (let S in e) e1  Cond((let S in e), e1, e2)  PFk(e1,...(let S in e),...ek) x = e’; S’ let S1; S’ in e’ let S’ in e’ e1 let S’ in Cond(e’, e1, e2) let S’ in PFk(e1,...e’,...ek) September 19, 2006 http://www.csg.csail.mit.edu/6.827

Confluenence and Letrecs odd = n.Cond(n=0, False, even (n-1)) (M) even = n.Cond(n=0, True, odd (n-1)) substitute for even (n-1) in M odd = n.Cond(n=0, False, Cond(n-1 = 0 , True, odd ((n-1)-1))) (M1) even = n.Cond(n=0, True, odd (n-1)) substitute for odd (n-1) in M odd = n.Cond(n=0, False, even (n-1)) (M2) even = n.Cond(n=0, True, Cond( n-1 = 0 , False, even ((n-1)-1))) Can odd in M1 and M2 be reduced to the same expression ? Proposition: let is not confluent. Ariola & Klop 1994 September 19, 2006 http://www.csg.csail.mit.edu/6.827

 versus let Calculus Terms of the let calculus can be translated into terms of the  calculus by systematically eliminating the let blocks. Let T be such a translation. Suppose e e1 in let then does there exist a reduction such that T[[e]] T[[e1]] in  ?   We need a notion of observable values to compare terms in a meaningful way. September 19, 2006 http://www.csg.csail.mit.edu/6.827

Instantaneous Information “Instantaneous information” (info) of a term is defined as a (finite) trees TP ::=  | | CN0 | CNk(TP1,...,TPk) Info: E  TP Info[{S in E}] = Info [E] Info[x.E] =  Info[CN0] = CN0 Info[CNk(a1,...,ak)] = CNk(Info[a1],...,Info[ak]) Info[E] =  otherwise Notice this procedure always terminates September 19, 2006 http://www.csg.csail.mit.edu/6.827

Reduction and Info Terms can be compared by their Info value   t (bottom) t  t (reflexive) CNk(v1,...,vi,...,vk) CNk(v1,...,v’i,...,vk) if vi  v’i Proposition Reduction is monotonic wrt Info: If e e1 then Info[e]  Info[e1].   Proposition Confluence wrt Info: If e  e1 and e e2 then  e3 s.t. e1 e3 and Info[e2]  Info[e3]. September 19, 2006 http://www.csg.csail.mit.edu/6.827