Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Substitution & Evaluation Order cos 441 David Walker.
Closures & Environments CS153: Compilers Greg Morrisett.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Proofs and Programs Wei Hu 11/01/2007. Outline  Motivation  Theory  Lambda calculus  Curry-Howard Isomorphism  Dependent types  Practice  Coq Wei.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
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.
1 PROPERTIES OF A TYPE ABSTRACT INTERPRETATER. 2 MOTIVATION OF THE EXPERIMENT § a well understood case l type inference in functional programming à la.
Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CLF: A Concurrent Logical Framework David Walker Princeton (with I. Cervesato, F. Pfenning, K. Watkins)
1 Meta-Programming through Typeful Code Representation Chiyan Chen and Hongwei Xi Boston University.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 3 LECTURE: LAMBDA CALCULUS PRACTICAL/TUTORIAL: (i) Do exercises given out.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
Control Flow Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Twelf: The Quintessential Proof Assistant for Language Metatheory Karl Crary Carnegie Mellon University Joint work with Robert Harper and Michael Ashley-Rollman.
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.
POPLmark 1A in Cinic Evan Austin, Aaron Stump, and Edwin Westbrook 1.
Syntax With Binders COS 441 Princeton University Fall 2004.
Dynamic Semantics COS 441 Princeton University Fall 2004.
CS 280 Data Structures Professor John Peterson. Lexer Project Questions? Must be in by Friday – solutions will be posted after class The next project.
C. Varela1 Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators Carlos Varela Rennselaer.
10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Lambda Calculus History and Syntax. History The lambda calculus is a formal system designed to investigate function definition, function application and.
CS 280 Data Structures Professor John Peterson. How Does Parsing Work? You need to know where to start (“statement”) This grammar is constructed so that.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
1 ML fun x -> e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors let f x = e variables These.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
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.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
12/2/20151 GC16/3011 Functional Programming Lecture 2 The Lambda Calculus: A Simple Introduction.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
CSE-321 Programming Languages Abstract Machine E POSTECH May 1, 2006 박성우.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
Review for Test 2 Chapters 5 (start at 5.4), 6.1, , 12, 13, 15.1, Python.
1 Interactive Computer Theorem Proving CS294-9 November 30, 2006 Adam Chlipala UC Berkeley Lecture 14: Twelf.
Operational Semantics of Scheme
Chapter 2: Lambda Calculus
Abstract Syntax cs7100 (Prasad) L7AST.
Programming Languages and Compilers (CS 421)
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
A Verified Compiler for an Impure Functional Language
Typed Arithmetic Expressions
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
Programming Languages and Compilers (CS 421)
FP Foundations, Scheme In Text: Chapter 14.
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
Chapter 1 Review: BNF Grammar for lists:
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Abstract Syntax cs7100 (Prasad) L7AST.
6.001 SICP Variations on a Scheme
CSE S. Tanimoto Lambda Calculus
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Presentation transcript:

Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg

Outline λ-calculus. Implementation strategies Typed λ-calculus Polymorphic types, System F Dependent types Current work

Motivations λ-calculus expresses function abstractions ◦ Powerful, Turing-complete. ◦ Foundation of functional programming languages. ◦ Strong connection with logic. This project: ◦ Study types ◦ Explore different implementations

The λ-calculus Very simple language: ◦ Terms M = x | λ x. M | M N ◦ Reductions (λ x. M) N => [N/x] M Examples ◦ Identity: λ x. x ◦ Constant function: λ x. λ y. x ◦ Omega: λ x. (x x)

Implementation strategies Explicit substitution ◦ term = var “x” | lam “x” M | app M N ◦ “Natural way” of representing lambda expressions. ◦ Problems :  No immediate α-renaming  Free variable capture problem

Implementation strategies (cont’d) De Bruijn indices ◦ term M = Var n | lam M | app M N ◦ e.g. Identity: λ. 1, Constant: λ. λ. 2 ◦ Advantages:  Unique representation ◦ Problems:  Not simple to manipulate. λ x. ( λ y. x) x = λ. (λ. 2). 1  Need to shift indices in substitution to avoid capture.

Implementation strategies (cont’d) Higher Order Abstract Syntax (HOAS) ◦ Use the features of the meta-language ! ◦ term M = lam f | app M N where f is a function term → term ◦ Term substitution : sub (lam f) N => lam (f N) ◦ α-renaming already provided! ◦ WARNING : Leads to ridiculously short implementations.

Our implementations De Bruijn in SML datatype exp = var of int | lam of exp | app of exp * exp fun shift (var x) l b = if x > b then var (x + l) else var x | shift (lam m) l b = lam (shift m l (b + 1)) | shift (app (m, n)) l b = app(shift m l b, shift n l b) fun sub (var y) s x l = if y = x then shift s l 0 (* Substitution *) else if y > x then var (y-1) (* Free variable *) else var y (* Bound variable *) | sub (lam m) s x l = lam (sub m s (x + 1) (l + 1)) | sub (app (m, n)) s x l = app (sub m s x l, sub n s x l) fun eval (var x) = var x | eval (lam m) = lam (eval m) | eval (app (m, n)) = case eval m of lam m' => eval (sub m' n 1 0) | m' => (app (m', eval n)) HOAS in SML datatype exp = lam of (exp -> exp) | app of exp * exp; fun eval (lam m) = lam (fn x => eval (m x)) | eval (app (m, n)) = case eval m of lam m' => eval (m' n) | m' => app (m', n)

Our implementations Twelf exp : type. lam : (exp -> exp) -> exp. app : exp -> exp -> exp. eval : exp -> exp -> type. eval/lam : eval (lam M) (lam M). eval/app : eval (app M N) N' <- eval M (lam M') <- eval (M' N) N'. SML datatype exp = lam of (exp -> exp) | app of exp * exp; fun eval (lam m) = lam (fn x => eval (m x)) | eval (app (m, n)) = case eval m of lam m' => eval (m' n) | m' => app (m', n)

The ω problem Consider the function: ω = λ x. ( x x ) What happens if we apply it to itself? ω ω = (λ x. ( x x )) ω => ω ω => ω ω =>... Evaluation never terminates! We say (ω ω) doesn’t have a normal form. To solve this problem, we introduce types.

Types Each function f has a fixed domain A and co-domain B (like sets in math). types τ = a | τ → σ where a is a base type (e.g. nat) Typing rules ◦ XXXXXXXXXXXXXX

Return of the ω What happens to ω ? ◦ ω = λ x. xx ω : α → β x : α, but also x : α → β α ≠ α → β ω cannot have a valid type! Simply-typed lambda calculus is strongly normalizing.

Implementing types Much easier in Twelf: use HOAS. This corresponds to Curry style. t : type. a : t. arrow : t -> t -> t. %infix right 10 arrow. exp : type. lam : (exp -> exp) -> exp. app : exp -> exp -> exp. check : exp -> t -> type. check/lam : check (lam M) (A arrow B) check (M x) B). check/app : check (app M N) B <- check M (A arrow B) <- check N A.

Examples Type checking: ◦ check (lam ([x] x)) (A arrow A) Secret weapon: we can use Twelf to do type inference ! ◦ %query 1 * check (lam ([x] x)) T  T = X1 arrow X2 arrow X1. ω ? ◦ %query 1 * check (lam ([x] app x x)) T  Query error -- wrong number of solutions: expected 1 in * tries, but found 0

Another implementation Cute trick : Then ill-typed terms cannot even be constructed ! This corresponds to Church style. t : type. a : t. arrow : t -> t -> t. %infix right 10 arrow. exp : t -> type. lam : (exp T1 -> exp T2) -> exp (T1 arrow T2). app : exp (T1 arrow T2) -> exp T1 -> exp T2.

Limitations of simple types Decidable, but no longer Turing complete. id = λ x : a. x id = λ x : b. x id = λ x : nat. x id = λ x : (a → a). x We need to write a different identity function for each type. ◦ To avoid that we need polymorphism.

Polymorphism: System F Idea: Allow abstraction over types. Then, we can write a single identity function for any type a: Λ a. λ x : a. x We extend our definition of types and terms: ◦ τ = a | τ → τ | Π a. τ ◦ M = x | λ x : τ. M | M N | Λ a. M | M {τ} Implementations follow the same idea.

Dependent types : LF Idea: Let types depend on values. Keep information about our types. Examples: ◦ Vector : Nat → type. ◦ Vector n is the type of vectors of length n. ◦ Exp t Advantages: ◦ We won’t need to prove properties about our programs !

Current work Polymorphic types in Twelf Dependent types in Twelf Types in SML ◦ Problems with HOAS.

Problems with HOAS Current functional languages (like SML) present a few problems: ◦ Do not allow us to look inside functions. ◦ Hard to generate λ terms. Solutions: ◦ Improve functional programming languages.  Beluga ◦ Try different paradigms.  Python

Why Python? Dynamically typed. Very flexible code generation. User-friendly interface. I = Lam(lambda x : x) K = Lam(lambda x : Lam (lambda y : x)) O = Lam(lambda x : App(x, x)) print "I = ", I print "K = ", K print "O = ", O >>> I = Lam u. (u) K = Lam w. (Lam v. (w)) O = Lam y. ((y)(y))

Conclusions and future work Many type systems. Various implementations. Besides implementing, our goals include: ◦ Proving equivalence of representations.