-- SIMPLY TYPED LAMBDA CALCULUS in AGDA -- (The HELLO WORLD of AGDA) {- Examples for lambda terms: λx. x (this denotes the identity function) λx. (λy. x) (this denotes a function which returns a function which returns the outer input) λx. (λy. y) (this denotes a function which returns a function which returns the inner input) z (this is a lambda term consisting of just a single variable) f x (this is a lambda term consisting of the application of one variable to another) In pure untyped lambda calculus, we have just the following ingredients: - variables - application - abstraction (λx. ....) A famous extensions adds the following ingredients: - zero : * - succ : * ⇒ * - rec : (τ ⇒ τ) ⇒ τ ⇒ * ⇒ τ (for instance, rec f x zero reduces to x rec f x (succ zero) reduces to f x rec f x (succ (succ zero)) reduces to f (f x)) In the typed lambda calculus, every term has to be well-typed, and there are the following types available: - a base type, typically written "*", denoting natural numbers - function types: if τ and τ' are types, then so is (τ ⇒ τ') -} -- "Ty" is the Agda datatype of types in STLC data Ty : Set where * : Ty _⇒_ : Ty → Ty → Ty -- "Cxt" is the Agda datatypwe of contexts of STLC -- For instance, λx. x is a lambda term in the empty context. -- For instance, λx. y is a lambda term in the context y. -- For instance, f x is a lambda term in the context f, x. data Cxt : Set where ε : Cxt _,_ : Cxt → Ty → Cxt -- this is the same as the Agda datatype of lists of Ty's. -- "τ ∈ Γ" is the type of witnesses that τ occurs in the context Γ. -- In case that τ does not occur in Γ, this type will be empty. -- In case it occurs multiple times, this type will contain multiple inhabitants. data _∈_ : Ty → Cxt → Set where here : {τ : Ty} {Γ : Cxt} → τ ∈ (Γ , τ) there : {τ τ' : Ty} {Γ : Cxt} → τ ∈ Γ → τ ∈ (Γ , τ') -- "Term Γ τ" is the Agda datatype of terms of STLC in context Γ of type τ -- Note: Nowhere we'll have to deal with "raw" (untyped) terms such as λf. f f. -- We are using de Bruijn indices instead of variable names. -- One advantage of this decision is that we will not need to deal with -- α-equivalence (α-equivalence states that λx.x is equivalent to λy.y). -- This is, to some extent, cheating. A development which cheats less -- is available at https://plfa.github.io/. data Term : Cxt → Ty → Set where zero : {Γ : Cxt} → Term Γ * succ : {Γ : Cxt} → Term Γ (* ⇒ *) _·_ : {Γ : Cxt} {τ τ' : Ty} → Term Γ (τ ⇒ τ') → Term Γ τ → Term Γ τ' lam : {Γ : Cxt} {τ τ' : Ty} → Term (Γ , τ) τ' → Term Γ (τ ⇒ τ') var : {Γ : Cxt} {τ : Ty} → τ ∈ Γ → Term Γ τ data ℕ : Set where zero : ℕ succ : ℕ → ℕ Set[_] : Ty → Set Set[ * ] = ℕ Set[ τ ⇒ τ' ] = Set[ τ ] → Set[ τ' ] -- "Env Γ" is the Agda datatype of environments for the context Γ. -- So a value of type Env Γ should contain values for each of the variables occuring in Γ. data Env : Cxt → Set where ∅ : Env ε _,_ : {τ : Ty} {Γ : Cxt} → Env Γ → Set[ τ ] → Env (Γ , τ) -- this is a form of heterogenous lists exampleEnv : Env ((ε , *) , *) exampleEnv = (∅ , (succ (succ (succ zero)))), (succ zero) -- For instance, "eval (var here) exampleEnv" should be succ zero. -- For instance, "eval (var (there here)) exampleEnv" should be succ (succ (succ zero)). lookup : {Γ : Cxt} {τ : Ty} → Env Γ → τ ∈ Γ → Set[ τ ] -- The case "lookup ε here" cannot occur. lookup (env , x) here = x lookup (env , _) (there v) = lookup env v -- "eval t env" evaluates the term t to a value, using the environment -- "env" for looking up values of variables eval : {Γ : Cxt} {τ : Ty} → Term Γ τ → Env Γ → Set[ τ ] eval zero env = zero eval succ env = succ eval (t · s) env = (eval t env) (eval s env) eval (lam t) env = λ x → eval t (env , x) eval (var v) env = lookup env v -- reduce : {Γ : Cxt} {τ : Ty} → Term Γ τ → Term Γ τ -- things we could try to formulate and prove: -- reducing is deterministic, reducing is total, -- (in an approach where non-well-typed terms occur) reducing preserves types -- For instance, "eval zero ∅" should be the natural number zero. -- For instance, "eval succ ∅" should be the function succ between natural numbers. -- For instance, "eval (lam (var here)) ∅" should be the identity function. exampleTerm₁ : Term ε * exampleTerm₁ = zero exampleTerm₂ : Term ε (* ⇒ *) exampleTerm₂ = lam zero -- read this as λx. zero exampleTerm₃ : Term ε (* ⇒ *) exampleTerm₃ = lam (var here) -- read this as λx. x exampleTerm₄ : Term ε (* ⇒ (* ⇒ *)) exampleTerm₄ = lam (lam (var here)) -- read this as λx. λy. y -- the subexpresion "var here" is a value of Term (ε , *, *) * -- the subexpression "lam (var here)" is a value of Term (ε , *) (* ⇒ *) -- the expression "lam (lam (var here))" is a value of Term ε (* ⇒ (* ⇒ *)) exampleTerm₅ : Term ε (* ⇒ (* ⇒ *)) exampleTerm₅ = lam (lam (var (there here))) -- read this as λx. λy. x -- the subexpresion "var (there here)" is a value of Term (ε , *, *) * -- the expression "var (there (there here))" will be rejected by Agda -- POSSIBLE TASKS: -- * extend this development by rec (the recursor) -- * extend this development to System F_ω (this system contains, for instance, the polymorphic -- identity function: ΛA. (λ(x:A). x)) -- * give a definition of the β-reduction relation and prove various lemmas about it -- * write a compiler to your favorite programming language (JavaScript? dominos?) -- zero ∶ ℕ : Set : Set₁ : Set₂ : Set₃ : ... : Setω : Set (ω + 1) : ... -- this is Agda's limit ---------^