A formal derivation of an executable Krivine machine

Size: px
Start display at page:

Download "A formal derivation of an executable Krivine machine"

Transcription

1 A formal derivation of an executable Krivine machine Wouter Swierstra Universiteit Utrecht IFIP WG 2.1 Meeting 68; Rome, Italy 1

2 β reduction (λx. t0) t1 t0 {t1/x} 2

3 Motivation Implementing β-reduction through substitutions is a terrible idea! Instead, modern compilers evaluate lambda terms using an abstract machine, such as Haskell s STG or OCaml s CAM. Such abstract machines are usually described as tail-recursive functions/finite state machines. 3

4 Who comes up with these things? 4

5 Olivier Danvy and his many students and collaborators 5

6 Most of our implementations of the abstract machines raise compiler warnings about nonexhaustive matches. These are inherent to programming abstract machines in an ML-like language Mads Sig Ager, Dariusz Biernacki, Olivier Danvy, Jan Midtgaard 6

7 Laissez-faire vs nanny state 7

8 Swedish welfare state 8

9 Outline 1. A terminating small-step evaluator 2. A small-step abstract machine (refocusing) 3. A Krivine machine (inlining) 9

10 Small step evaluation 10

11 Types data Ty : Set where O : Ty _=>_ : Ty -> Ty -> Ty Context : Set Context = List Ty 11

12 Terms data Term : Context -> Ty -> Set where Lam : Term (Cons u Γ) v -> Term Γ (u => v) App : Term Γ (u => v) -> Term Γ u -> Term Γ v Var : Ref Γ u -> Term Γ u 12

13 Closed terms only 13

14 Reduction rules 14

15 Closed terms data Closed : Ty -> Set where Closure : Term Γ u -> Env Γ -> Closed u Clapp : Closed (u => v) -> Closed u -> Closed v data Env : Context -> Set where Nil : Env Nil _ _ : Closed u -> Env Γ -> Env (Cons u Γ) 15

16 Reduction rules 16

17 Reduction rules 16

18 Reduction rules 17

19 Head reduction in three steps Decompose the term into a redex and evaluation context; Contract the redex; Plug the result back into the context. 18

20 Redex data Redex : Ty -> Set where Lookup : Ref Γ u -> Env Γ -> Redex u App : Term Γ (u => v) -> Term Γ u -> Env Γ -> Redex v Beta : Term (Cons u Γ) v -> Env Γ -> Closed u -> Redex v 19

21 Contraction contract : Redex u -> Closed u contract (Lookup i env) = env! i contract (App f x env) = Clapp (Closure f env) (Closure x env) contract (Beta body env arg) = Closure body (arg env) 20

22 Decomposition as a view Idea: every closed term is: a value; or a redex in some evaluation context. Define a view on closed terms. 21

23 Views: example Natural numbers are typically defined using the Peano axioms. But sometimes you want to use the fact that every number is even or odd, e.g. when converting to a binary representation; or proving 2 is irrational. But why is that a valid proof principle? 22

24 Views: example How can we derive even-odd induction from Peano induction? Define a data type EvenOdd : Nat -> Set Define a covering function evenodd : (n : Nat) -> EvenOdd n 23

25 The view data type data EvenOdd : Nat -> Set where IsEven : (k : Nat) -> EvenOdd (double k) IsOdd : (k : Nat) -> EvenOdd (Succ (double k)) 24

26 Covering function evenodd : (n : Nat) -> EvenOdd n evenodd Zero = IsEven Zero evenodd (Succ Zero) = IsOdd Zero evenodd (Succ (Succ k)) with evenodd k... IsEven k' = IsEven (Succ k')... IsOdd k' = IsOdd (Succ k') 25

27 Example example: Nat ->... example n with evenodd n example.(double k) IsEven k =... example.(succ (double k)) IsOdd k =... 26

28 Decomposition as a view Idea: every closed term is: a value; or a redex in some evaluation context. Define a view on closed terms. 27

29 Evaluation contexts data EvalContext : Ty -> Ty -> Set where MT : EvalContext u u ARG : Closed u -> EvalContext v w -> EvalContext (u => v) w 28

30 Plug plug : EvalContext u v -> Closed u -> Closed v plug MT f = f plug (ARG x ctx) f = plug ctx (Clapp f x) 29

31 Decomposition data Decomposition : Closed u -> Set where Val : (t : Closed u) -> isval t -> Decomposition t Decompose : (r : Redex v) -> (ctx : EvalContext v u) -> Decomposition (plug ctx (fromredex r)) 30

32 Decompose decompose : (c : Closed u) -> Decomposition c decompose c = load MT c 31

33 load : (ctx : EvalContext u v) (c : Closed u) -> Decomposition (plug ctx c) load ctx (Closure (Lam body) env) = unload ctx body env load ctx (Closure (App f x) env) = Decompose (App f x env) ctx load ctx (Closure (Var i) env) = Decompose (Lookup i env) ctx load ctx (Clapp f x) = load (ARG x ctx) f unload : (ctx : EvalContext (u => v) w) -> (body : Term (Cons u G) v) (env : Env G) -> Decomposition (plug ctx (Closure (Lam body) env)) unload MT body env = Val body env unload (ARG arg ctx) body env = Decompose (Beta body env arg) ctx 32

34 Head-reduction headreduce : Closed u -> Closed u headreduce c with decompose c... Val val p = val... Decompose redex ctx = plug ctx (contract redex) 33

35 Iterated head reduction evaluate : Closed u -> Value u evaluate c = iterate (decompose c) where iterate : Decomposition c -> Value u iterate (Val val p) = Val val p iterate (Decompose r ctx) = iterate (decompose (plug ctx (contract r))) 34

36 Iterated head reduction evaluate : Closed u -> Value u evaluate c = iterate (decompose c) where iterate : Decomposition c -> Value u iterate (Val val p) = Val val p iterate (Decompose r ctx) = iterate (decompose (plug ctx (contract r))) 35

37 Iterated head reduction evaluate : Closed u -> Value u evaluate c = iterate (decompose c) where iterate : Decomposition c -> Value u iterate (Val val p) = Val val p iterate (Decompose r ctx) = iterate (decompose (plug ctx (contract r))) 35

38 The Bove-Capretta method 36

39 Bove-Capretta 37

40 Bove-Capretta data Trace : Decomposition c -> Set where Done : (val : Closed u) -> (p : isval val) -> Trace (Val val p) Step : Trace (decompose (plug ctx (contract r))) -> Trace (Decompose r ctx) 38

41 Iterated head reduction, again iterate : {u : Ty} {c : Closed u} -> (d : Decomposition c) -> Trace d -> Value u iterate (Val val p) Done = Val val p iterate (Decompose r ctx) (Step step) = let d' = decompose (plug ctx (contract r)) in iterate d' step 39

42 Nearly done We still need to find a trace for every term... (c : Closed u) -> Trace (decompose c) 40

43 Nearly done We still need to find a trace for every term... (c : Closed u) -> Trace (decompose c) Fail 40

44 Nearly done We still need to find a trace for every term... (c : Closed u) -> Trace (decompose c) Fail Yet we know that the simply typed lambda calculus is strongly normalizing... 40

45 Logical relation Reducible : (u : Ty) -> (t : Closed u) -> Set Reducible O t = Trace (decompose t) Reducible (u => v) t = Pair (Trace (decompose t)) ((x : Closed u) -> Reducible u x -> Reducible (Clapp t x)) 41

46 Required lemmas lemma1 : (t : Closed u) -> Reducible (headreduce t) -> Reducible t lemma2 : (t : Term G u) (env : Env G) -> ReducibleEnv env -> Reducible (Closure t env) 42

47 Result! theorem : (c : Closed u) -> Reducible c theorem (Closure t env) = lemma2 t env (envtheorem env) theorem (Clapp f x) = snd (theorem f) x (theorem x) termination : (c : Closed u) -> Trace (decompose c)...an easy corollary 43

48 Finally, evaluation evaluate : Closed u -> Value u evaluate t = iterate (decompose t) (termination t) 44

49 The story so far... Data types for terms, closed terms, values, redexes, evaluation contexts. Defined a three step head-reduction function: decompose, contract, plug. Proven that iterated head reduction yields a normal form and used this to define a normalization function. 45

50 What s next? Use Danvy & Nielsen s refocusing transformation to define a small-step abstract machine; Inline the iterate function (and one or two minor changes), yields the Krivine abstract machine. Prove that each transformation preserves the termination behaviour and semantics. 46

51 A term 47

52 A redex and an evaluation context 48

53 Contract the redex 49

54 Plug and repeat 50

55 The drawback To contract a single redex, we need to: traverse the term to find a redex; contract the redex; traverse the context to plug back the contractum. 51

56 Refocusing The refocusing transformation (Danvy & Nielsen) avoids these traversals. Instead, given a decomposition, it navigates to the next redex immediately. Refocusing behaves just the same as decompose. plug 52

57 Refocus summary refocus : (ctx : EvalContext u v) -> (c : Closed u) -> Decomposition (plug ctx c) refocuscorrect : (ctx : EvalContext u v) -> (c : Closed u) -> refocus ctx c == decompose (plug ctx c) 53

58 Refocus, details refocus : (ctx : EvalContext u v) (c : Closed u) -> Decomposition (plug ctx c) refocus MT (Closure (Lam body) env) = Val body env refocus (ARG x ctx) (Closure (Lam body) env) = Decompose (Beta body env x) ctx refocus ctx (Closure (Var i) env) = Decompose (Lookup i env) ctx refocus ctx (Closure (App f x) env) = Decompose (App f x env) ctx refocus ctx (Clapp f x) = refocus (ARG x ctx) f 54

59 What else? It is easy to prove that iteratively refocusing and contracting redexes produces the same result as the small step evaluator. And that if the Trace data type is inhabited, then so is the corresponding data type for the refocussing evaluator. 55

60 The Krivine machine Now inline the iterate function; and disallow closed applications; and compress corridor transitions. 56

61 The Krivine machine refocus : (ctx : EvalContext u v) -> (t : Term Γ u) -> (env : Env Γ) -> Value v refocus ctx (Var i) env = let Closure t env = lookup i env q in refocus ctx t env refocus ctx (App f x) env = refocus (ARG (Closure x env) ctx) f env refocus (ARG x ctx) (Lam body) env = refocus ctx body (x env) refocus MT (Lam body) env = Val (Closure (Lam body) env) 57

62 Once again... We need to prove that this function terminates by adapting the proof we saw for the refocusing evaluator.... and show that it produces the same value as our previous evaluation functions. 58

63 Conclusions Most of our implementations of the abstract machines raise compiler warnings about nonexhaustive matches. These are inherent to programming abstract machines in an ML-like language. 59

64 Conclusions Most of our implementations of the abstract machines raise compiler warnings about nonexhaustive matches. These are inherent to programming abstract machines in an ML-like language. Agda is not an ML-like language. 59

65 Conclusions Most of our implementations of the abstract machines raise compiler warnings about nonexhaustive matches. These are inherent to programming abstract machines in an ML-like language. 60

66 Conclusions Most of our implementations of the abstract machines raise compiler warnings about nonexhaustive matches. These are inherent to programming abstract machines in an ML-like language. Using dependent types exposes structure that is not apparent in ML-like languages. 60

From Math to Machine A formal derivation of an executable Krivine Machine Wouter Swierstra Brouwer Seminar

From Math to Machine A formal derivation of an executable Krivine Machine Wouter Swierstra Brouwer Seminar From Math to Machine A formal derivation of an executable Krivine Machine Wouter Swierstra Brouwer Seminar β reduction (λx. t0) t1 t0 {t1/x} Substitution Realizing β-reduction through substitution is a

More information

Accurate Step Counting

Accurate Step Counting Accurate Step Counting Catherine Hope and Graham Hutton University of Nottingham Abstract Starting with an evaluator for a language, an abstract machine for the same language can be mechanically derived

More information

From Reduction-based to Reduction-free Normalization

From Reduction-based to Reduction-free Normalization From Reduction-based to Reduction-free Normalization Olivier Danvy Department of Computer Science, Aarhus University May 2009 Abstract We document an operational method to construct reduction-free normalization

More information

BRICS. A Concrete Framework for Environment Machines. BRICS RS-06-3 Biernacka & Danvy: A Concrete Framework for Environment Machines

BRICS. A Concrete Framework for Environment Machines. BRICS RS-06-3 Biernacka & Danvy: A Concrete Framework for Environment Machines BRICS RS-06-3 Biernacka & Danvy: A Concrete Framework for Environment Machines BRICS Basic Research in Computer Science A Concrete Framework for Environment Machines Małgorzata Biernacka Olivier Danvy

More information

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam. Programming Languages Fall 2013 Midterm Exam 10/22/13 Time Limit: 100 Minutes Name (Print): Graduate Center I.D. This is a closed-book, closed-notes exam. Various definitions are provided in the exam.

More information

Implementing functional languages with abstract machines

Implementing functional languages with abstract machines Implementing functional languages with abstract machines Hayo Thielecke University of Birmingham http://www.cs.bham.ac.uk/~hxt December 9, 2015 Contents Introduction Lambda calculus and programming languages

More information

arxiv:cs/ v1 [cs.lo] 8 Aug 2005

arxiv:cs/ v1 [cs.lo] 8 Aug 2005 Logical Methods in Computer Science Volume 00, Number 0, Pages 000 000 S 0000-0000(XX)0000-0 arxiv:cs/0508048v1 [cs.lo] 8 Aug 2005 AN OPERATIONAL FOUNDATION FOR DELIMITED CONTINUATIONS IN THE CPS HIERARCHY

More information

A Typed Calculus Supporting Shallow Embeddings of Abstract Machines

A Typed Calculus Supporting Shallow Embeddings of Abstract Machines A Typed Calculus Supporting Shallow Embeddings of Abstract Machines Aaron Bohannon Zena M. Ariola Amr Sabry April 23, 2005 1 Overview The goal of this work is to draw a formal connection between steps

More information

Concepts of programming languages

Concepts of programming languages Concepts of programming languages Lecture 5 Wouter Swierstra 1 Announcements Submit your project proposal to me by email on Friday; The presentation schedule in now online Exercise session after the lecture.

More information

BRICS. A Syntactic Correspondence between Context-Sensitive Calculi and Abstract Machines

BRICS. A Syntactic Correspondence between Context-Sensitive Calculi and Abstract Machines BRICS RS-05-38 Biernacka & Danvy: A Syntactic Correspondence between Context-Sensitive Calculi and Abstract Machines BRICS Basic Research in Computer Science A Syntactic Correspondence between Context-Sensitive

More information

Last class. CS Principles of Programming Languages. Introduction. Outline

Last class. CS Principles of Programming Languages. Introduction. Outline Last class CS6848 - Principles of Programming Languages Principles of Programming Languages V. Krishna Nandivada IIT Madras Interpreters A Environment B Cells C Closures D Recursive environments E Interpreting

More information

MPRI course 2-4 Functional programming languages Exercises

MPRI course 2-4 Functional programming languages Exercises MPRI course 2-4 Functional programming languages Exercises Xavier Leroy October 13, 2016 Part I: Interpreters and operational semantics Exercise I.1 (**) Prove theorem 2 (the unique decomposition theorem).

More information

Accurate Step Counting

Accurate Step Counting Accurate Step Counting Catherine Hope and Graham Hutton School of Computer Science and IT University of Nottingham, UK {cvh,gmh}@cs.nott.ac.uk Abstract Starting with an evaluator for a language, an abstract

More information

Lesson 4 Typed Arithmetic Typed Lambda Calculus

Lesson 4 Typed Arithmetic Typed Lambda Calculus Lesson 4 Typed Arithmetic Typed Lambda 1/28/03 Chapters 8, 9, 10 Outline Types for Arithmetic types the typing relation safety = progress + preservation The simply typed lambda calculus Function types

More information

Programming Languages Lecture 14: Sum, Product, Recursive Types

Programming Languages Lecture 14: Sum, Product, Recursive Types CSE 230: Winter 200 Principles of Programming Languages Lecture 4: Sum, Product, Recursive Types The end is nigh HW 3 No HW 4 (= Final) Project (Meeting + Talk) Ranjit Jhala UC San Diego Recap Goal: Relate

More information

Lecture 13: Subtyping

Lecture 13: Subtyping Lecture 13: Subtyping Polyvios Pratikakis Computer Science Department, University of Crete Type Systems and Programming Languages Pratikakis (CSD) Subtyping CS546, 2018-2019 1 / 15 Subtyping Usually found

More information

CSE-321 Programming Languages 2010 Midterm

CSE-321 Programming Languages 2010 Midterm Name: Hemos ID: CSE-321 Programming Languages 2010 Midterm Score Prob 1 Prob 2 Prob 3 Prob 4 Total Max 15 30 35 20 100 1 1 SML Programming [15 pts] Question 1. [5 pts] Give a tail recursive implementation

More information

Parsing Scheme (+ (* 2 3) 1) * 1

Parsing Scheme (+ (* 2 3) 1) * 1 Parsing Scheme + (+ (* 2 3) 1) * 1 2 3 Compiling Scheme frame + frame halt * 1 3 2 3 2 refer 1 apply * refer apply + Compiling Scheme make-return START make-test make-close make-assign make- pair? yes

More information

Lambda Calculus: Implementation Techniques and a Proof. COS 441 Slides 15

Lambda Calculus: Implementation Techniques and a Proof. COS 441 Slides 15 Lambda Calculus: Implementation Techniques and a Proof COS 441 Slides 15 Last Time: The Lambda Calculus A language of pure functions: values e ::= x \x.e e e v ::= \x.e With a call-by-value operational

More information

CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011

CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011 CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011 In this lecture we introduce the topic of scope in the context of the λ-calculus and define translations from λ-cbv to FL for the two most common

More information

A native compiler for Coq: current status, perspectives and discussion 1

A native compiler for Coq: current status, perspectives and discussion 1 A native compiler for Coq A native compiler for Coq: current status, perspectives and discussion 1 Maxime Dénès, jww M. Boespflug and B. Grégoire Marelle Team, INRIA Sophia-Antipolis February 19, 2013

More information

Hutton, Graham and Bahr, Patrick (2016) Cutting out continuations. In: WadlerFest, April 2016, Edinburgh, Scotland.

Hutton, Graham and Bahr, Patrick (2016) Cutting out continuations. In: WadlerFest, April 2016, Edinburgh, Scotland. Hutton, Graham and Bahr, Patrick (2016) Cutting out continuations. In: WadlerFest, 11-12 April 2016, Edinburgh, Scotland. Access from the University of Nottingham repository: http://eprints.nottingham.ac.uk/32703/1/cutting.pdf

More information

` e : T. Gradual Typing. ` e X. Ronald Garcia University of British Columbia

` e : T. Gradual Typing. ` e X. Ronald Garcia University of British Columbia aaab/hicbvbns8naen34wetxtecvi0xwvbirfe9fd3qs0c9oqplsnu3s3stsbgqh1l/ixymixv0h3vw3btsctpxbwoo9gwbmbslnsjvot7w2vrg5tv3ake/u7r8c2kfhbzvkktawsxgiuweoyllmw5pptruppcactjvb6g7md8zukpbetz2n1bcwifnecggj9e2kdw9capbgiaghpvggn/t21ak5c+bv4hakigo0+vaxfyykeztwhinspddjtt8bqrnhdfr2mkvticmy0j6hmqiq/mn8+ck+m0qio0saijweq78njicuykvgogxoovr2zuj/xi/t0bu/yxgaarqtxaio41gnejyedpmkrppceccsmvsxgyieok1ezrocu/zykmlf1fyn5j5evuu3rrwldijo0tly0rwqowfuqc1eui6e0st6s56sf+vd+li0rlnftax9gfx5a8zmk40=

More information

λ calculus is inconsistent

λ calculus is inconsistent Content Rough timeline COMP 4161 NICTA Advanced Course Advanced Topics in Software Verification Gerwin Klein, June Andronick, Toby Murray λ Intro & motivation, getting started [1] Foundations & Principles

More information

Programming Languages Fall 2014

Programming Languages Fall 2014 Programming Languages Fall 2014 Lecture 7: Simple Types and Simply-Typed Lambda Calculus Prof. Liang Huang huang@qc.cs.cuny.edu 1 Types stuck terms? how to fix it? 2 Plan First I For today, we ll go back

More information

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped) 1 LAMBDA CALCULUS (untyped) 2 The Untyped Lambda Calculus (λ) Designed by Alonzo Church (1930s) Turing Complete (Turing was his doctoral student!) Models functions, always as 1-input Definition: terms,

More information

Programming Languages

Programming Languages CSE 230: Winter 2008 Principles of Programming Languages Ocaml/HW #3 Q-A Session Push deadline = Mar 10 Session Mon 3pm? Lecture 15: Type Systems Ranjit Jhala UC San Diego Why Typed Languages? Development

More information

Introduction to Functional Programming in Haskell 1 / 56

Introduction to Functional Programming in Haskell 1 / 56 Introduction to Functional Programming in Haskell 1 / 56 Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order

More information

An experiment with variable binding, denotational semantics, and logical relations in Coq. Adam Chlipala University of California, Berkeley

An experiment with variable binding, denotational semantics, and logical relations in Coq. Adam Chlipala University of California, Berkeley A Certified TypePreserving Compiler from Lambda Calculus to Assembly Language An experiment with variable binding, denotational semantics, and logical relations in Coq Adam Chlipala University of California,

More information

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods Course 2D1453, 2006-07 Advanced Formal Methods Lecture 2: Lambda calculus Mads Dam KTH/CSC Some material from B. Pierce: TAPL + some from G. Klein, NICTA Alonzo Church, 1903-1995 Church-Turing thesis First

More information

A Derivational Approach to the Operational Semantics of Functional Languages

A Derivational Approach to the Operational Semantics of Functional Languages A Derivational Approach to the Operational Semantics of Functional Languages Ma lgorzata Biernacka PhD Dissertation Department of Computer Science University of Aarhus Denmark A Derivational Approach

More information

CIS 500 Software Foundations Fall September 25

CIS 500 Software Foundations Fall September 25 CIS 500 Software Foundations Fall 2006 September 25 The Lambda Calculus The lambda-calculus If our previous language of arithmetic expressions was the simplest nontrivial programming language, then the

More information

Me and my research. Wouter Swierstra Vector Fabrics, 6/11/09

Me and my research. Wouter Swierstra Vector Fabrics, 6/11/09 Me and my research Wouter Swierstra Vector Fabrics, 6/11/09 Brief bio MSc in Software Technology (Utrecht); PhD entitled A Functional Specification of Effects (University of Nottingham); Postdoc position

More information

Functional Languages. Hwansoo Han

Functional Languages. Hwansoo Han Functional Languages Hwansoo Han Historical Origins Imperative and functional models Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s Different formalizations of the notion of an algorithm

More information

CSE505, Fall 2012, Midterm Examination October 30, 2012

CSE505, Fall 2012, Midterm Examination October 30, 2012 CSE505, Fall 2012, Midterm Examination October 30, 2012 Rules: The exam is closed-book, closed-notes, except for one side of one 8.5x11in piece of paper. Please stop promptly at Noon. You can rip apart

More information

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6 Lambda calculus Advanced functional programming - Lecture 6 Wouter Swierstra and Alejandro Serrano 1 Today Lambda calculus the foundation of functional programming What makes lambda calculus such a universal

More information

Journal of Computer and System Sciences

Journal of Computer and System Sciences Journal of Computer and System Sciences 76 (2010) 302 323 Contents lists available at ScienceDirect Journal of Computer and System Sciences www.elsevier.com/locate/jcss Inter-deriving semantic artifacts

More information

Type Systems Winter Semester 2006

Type Systems Winter Semester 2006 Type Systems Winter Semester 2006 Week 4 November 8 November 15, 2006 - version 1.1 The Lambda Calculus The lambda-calculus If our previous language of arithmetic expressions was the simplest nontrivial

More information

Trees. Solution: type TreeF a t = BinF t a t LeafF 1 point for the right kind; 1 point per constructor.

Trees. Solution: type TreeF a t = BinF t a t LeafF 1 point for the right kind; 1 point per constructor. Trees 1. Consider the following data type Tree and consider an example inhabitant tree: data Tree a = Bin (Tree a) a (Tree a) Leaf deriving Show tree :: Tree Int tree = Bin (Bin (Bin Leaf 1 Leaf ) 2 (Bin

More information

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein COMP 4161 NICTA Advanced Course Advanced Topics in Software Verification Toby Murray, June Andronick, Gerwin Klein λ 1 Last time... λ calculus syntax free variables, substitution β reduction α and η conversion

More information

Metaprogramming assignment 3

Metaprogramming assignment 3 Metaprogramming assignment 3 Optimising embedded languages Due at noon on Thursday 29th November 2018 This exercise uses the BER MetaOCaml compiler, which you can install via opam. The end of this document

More information

BRICS. A Functional Correspondence between Monadic Evaluators and Abstract Machines for Languages with Computational Effects

BRICS. A Functional Correspondence between Monadic Evaluators and Abstract Machines for Languages with Computational Effects BRICS Basic Research in Computer Science BRICS RS-03-35 Ager et al.: A Functional Correspondence between Monadic Evaluators and Abstract Machines A Functional Correspondence between Monadic Evaluators

More information

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes Chapter 13: Reference Why reference Typing Evaluation Store Typings Safety Notes References Computational Effects Also known as side effects. A function or expression is said to have a side effect if,

More information

Exercise 1 ( = 22 points)

Exercise 1 ( = 22 points) 1 Exercise 1 (4 + 3 + 4 + 5 + 6 = 22 points) The following data structure represents polymorphic lists that can contain values of two types in arbitrary order: data DuoList a b = C a (DuoList a b) D b

More information

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine. CMSC 330: Organization of Programming Languages Lambda Calculus Programming Language Features Many features exist simply for convenience Multi-argument functions foo ( a, b, c ) Ø Use currying or tuples

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Lambda Calculus CMSC 330 1 Programming Language Features Many features exist simply for convenience Multi-argument functions foo ( a, b, c ) Ø Use currying

More information

CSE-321 Programming Languages 2012 Midterm

CSE-321 Programming Languages 2012 Midterm Name: Hemos ID: CSE-321 Programming Languages 2012 Midterm Prob 1 Prob 2 Prob 3 Prob 4 Prob 5 Prob 6 Total Score Max 14 15 29 20 7 15 100 There are six problems on 24 pages in this exam. The maximum score

More information

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ August 17, 2007 Lambda Calculus and Type

More information

Exercise 1 ( = 24 points)

Exercise 1 ( = 24 points) 1 Exercise 1 (4 + 5 + 4 + 6 + 5 = 24 points) The following data structure represents polymorphic binary trees that contain values only in special Value nodes that have a single successor: data Tree a =

More information

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

More Untyped Lambda Calculus & Simply Typed Lambda Calculus Concepts in Programming Languages Recitation 6: More Untyped Lambda Calculus & Simply Typed Lambda Calculus Oded Padon & Mooly Sagiv (original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky,

More information

dynamically typed dynamically scoped

dynamically typed dynamically scoped Reference Dynamically Typed Programming Languages Part 1: The Untyped λ-calculus Jim Royer CIS 352 April 19, 2018 Practical Foundations for Programming Languages, 2/e, Part VI: Dynamic Types, by Robert

More information

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements CIS 500 Software Foundations Fall 2004 27 September IS 500, 27 September 1 The Lambda Calculus IS 500, 27 September 3 Announcements Homework 1 is graded. Pick it up from Cheryl Hickey (Levine 502). We

More information

Generalized Refocusing: From Hybrid Strategies to Abstract Machines

Generalized Refocusing: From Hybrid Strategies to Abstract Machines Generalized Refocusing: From Hybrid Strategies to Abstract Machines Małgorzata Biernacka 1, Witold Charatonik 2, and Klara Zielińska 3 1 Institute of Computer Science, University of Wrocław, Wrocław, Poland

More information

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Lambda Calculus. Concepts in Programming Languages Recitation 6: Concepts in Programming Languages Recitation 6: Lambda Calculus Oded Padon & Mooly Sagiv (original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky, S. Tanimoto ) Reference: Types and Programming

More information

Part III. Chapter 15: Subtyping

Part III. Chapter 15: Subtyping Part III Chapter 15: Subtyping Subsumption Subtype relation Properties of subtyping and typing Subtyping and other features Intersection and union types Subtyping Motivation With the usual typing rule

More information

CIS 500 Software Foundations Fall October 2

CIS 500 Software Foundations Fall October 2 CIS 500 Software Foundations Fall 2006 October 2 Preliminaries Homework Results of my email survey: There was one badly misdesigned (PhD) problem and a couple of others that were less well thought through

More information

4.5 Pure Linear Functional Programming

4.5 Pure Linear Functional Programming 4.5 Pure Linear Functional Programming 99 4.5 Pure Linear Functional Programming The linear λ-calculus developed in the preceding sections can serve as the basis for a programming language. The step from

More information

Exercise 1 (2+2+2 points)

Exercise 1 (2+2+2 points) 1 Exercise 1 (2+2+2 points) The following data structure represents binary trees only containing values in the inner nodes: data Tree a = Leaf Node (Tree a) a (Tree a) 1 Consider the tree t of integers

More information

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility λ Calculus Church s λ Calculus: Brief History One of a number of approaches to a mathematical challenge at the time (1930): Constructibility (What does it mean for an object, e.g. a natural number, to

More information

CSE-321 Programming Languages 2011 Final

CSE-321 Programming Languages 2011 Final Name: Hemos ID: CSE-321 Programming Languages 2011 Final Prob 1 Prob 2 Prob 3 Prob 4 Prob 5 Prob 6 Total Score Max 15 15 10 17 18 25 100 There are six problems on 18 pages in this exam, including one extracredit

More information

Exercise 1 ( = 18 points)

Exercise 1 ( = 18 points) 1 Exercise 1 (4 + 5 + 4 + 5 = 18 points) The following data structure represents polymorphic binary trees that contain values only in special Value nodes that have a single successor: data Tree a = Leaf

More information

Part III Chapter 15: Subtyping

Part III Chapter 15: Subtyping Part III Chapter 15: Subtyping Subsumption Subtype relation Properties of subtyping and typing Subtyping and other features Intersection and union types Subtyping Motivation With the usual typing rule

More information

J. Barkley Rosser, 81, a professor emeritus of mathematics and computer science at the University of Wisconsin who had served in government, died

J. Barkley Rosser, 81, a professor emeritus of mathematics and computer science at the University of Wisconsin who had served in government, died Church-Rosser J. Barkley Rosser, 81, a professor emeritus of mathematics and computer science at the University of Wisconsin who had served in government, died Sept. 5, 1989. Along with Alan Turing and

More information

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER 2012 2013 MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS Time allowed TWO hours Candidates may complete the front

More information

PARSIFAL Summer 2011 Internship Report Logically validating logic programs

PARSIFAL Summer 2011 Internship Report Logically validating logic programs PARSIFAL Summer 2011 Internship Report Logically validating logic programs Chris Martens August 22, 2011 1 Overview Logic programs can be used to specify systems, and logic programming languages such as

More information

Generic Programming With Dependent Types: II

Generic Programming With Dependent Types: II Generic Programming With Dependent Types: II Generic Haskell in Agda Stephanie Weirich University of Pennsylvania March 2426, 2010 SSGIP Generic-Haskell style generic programming in Agda Dependently-typed

More information

Theorem Proving Principles, Techniques, Applications Recursion

Theorem Proving Principles, Techniques, Applications Recursion NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion 1 CONTENT Intro & motivation, getting started with Isabelle Foundations & Principles Lambda Calculus Higher Order Logic,

More information

Mikiβ : A General GUI Library for Visualizing Proof Trees

Mikiβ : A General GUI Library for Visualizing Proof Trees Mikiβ : A General GUI Library for Visualizing Proof Trees System Description and Demonstration Kanako Sakurai and Kenichi Asai Ochanomizu University, Japan {sakurai.kanako,asai}@is.ocha.ac.jp Abstract.

More information

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008 CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008 Contents 1 Solution to the Exercise 1 1.1 Semantics for lambda calculus.......................

More information

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations Outline FP Foundations, Scheme In Text: Chapter 15 Mathematical foundations Functional programming λ-calculus LISP Scheme 2 Imperative Languages We have been discussing imperative languages C/C++, Java,

More information

LAZY EVALUATION AND DELIMITED CONTROL

LAZY EVALUATION AND DELIMITED CONTROL Logical Methods in Computer Science Vol. 6 (3:1) 2010, pp. 1 37 www.lmcs-online.org Submitted Oct. 12, 2009 Published Jul. 11, 2010 LAZY EVALUATION AND DELIMITED CONTROL RONALD GARCIA a, ANDREW LUMSDAINE

More information

Typed Lambda Calculus and Exception Handling

Typed Lambda Calculus and Exception Handling Typed Lambda Calculus and Exception Handling Dan Zingaro zingard@mcmaster.ca McMaster University Typed Lambda Calculus and Exception Handling p. 1/2 Untyped Lambda Calculus Goal is to introduce typing

More information

Verified Characteristic Formulae for CakeML. Armaël Guéneau, Magnus O. Myreen, Ramana Kumar, Michael Norrish April 27, 2017

Verified Characteristic Formulae for CakeML. Armaël Guéneau, Magnus O. Myreen, Ramana Kumar, Michael Norrish April 27, 2017 Verified Characteristic Formulae for CakeML Armaël Guéneau, Magnus O. Myreen, Ramana Kumar, Michael Norrish April 27, 2017 Goal: write programs in a high-level (ML-style) language, prove them correct interactively,

More information

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08 Introduction to Lambda Calculus Lecture 5 CS 565 1/24/08 Lambda Calculus So far, we ve explored some simple but non-interesting languages language of arithmetic expressions IMP (arithmetic + while loops)

More information

Polymorphism and System-F (OV)

Polymorphism and System-F (OV) Polymorphism and System-F (OV) Theorie der Programmierung SoSe 2014 FAU the occurrence of something in several different forms Polymorphism? Polymorphic systems Type systems that allow a single piece of

More information

Lecture 5: The Untyped λ-calculus

Lecture 5: The Untyped λ-calculus Lecture 5: The Untyped λ-calculus Syntax and basic examples Polyvios Pratikakis Computer Science Department, University of Crete Type Systems and Static Analysis Pratikakis (CSD) Untyped λ-calculus I CS49040,

More information

Universes. Universes for Data. Peter Morris. University of Nottingham. November 12, 2009

Universes. Universes for Data. Peter Morris. University of Nottingham. November 12, 2009 for Data Peter Morris University of Nottingham November 12, 2009 Introduction Outline 1 Introduction What is DTP? Data Types in DTP Schemas for Inductive Families 2 of Data Inductive Types Inductive Families

More information

Graphical Untyped Lambda Calculus Interactive Interpreter

Graphical Untyped Lambda Calculus Interactive Interpreter Graphical Untyped Lambda Calculus Interactive Interpreter (GULCII) Claude Heiland-Allen https://mathr.co.uk mailto:claude@mathr.co.uk Edinburgh, 2017 Outline Lambda calculus encodings How to perform lambda

More information

Towards Reasoning about State Transformer Monads in Agda. Master of Science Thesis in Computer Science: Algorithm, Language and Logic.

Towards Reasoning about State Transformer Monads in Agda. Master of Science Thesis in Computer Science: Algorithm, Language and Logic. Towards Reasoning about State Transformer Monads in Agda Master of Science Thesis in Computer Science: Algorithm, Language and Logic Viet Ha Bui Department of Computer Science and Engineering CHALMERS

More information

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland COMP 1130 Lambda Calculus based on slides by Jeff Foster, U Maryland Motivation Commonly-used programming languages are large and complex ANSI C99 standard: 538 pages ANSI C++ standard: 714 pages Java

More information

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu, Lambda Calculus Type Systems, Lectures 3 Jevgeni Kabanov Tartu, 13.02.2006 PREVIOUSLY ON TYPE SYSTEMS Arithmetical expressions and Booleans Evaluation semantics Normal forms & Values Getting stuck Safety

More information

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson Lambda Calculus CS 550 Programming Languages Jeremy Johnson 1 Lambda Calculus The semantics of a pure functional programming language can be mathematically described by a substitution process that mimics

More information

Lecture 9: Typed Lambda Calculus

Lecture 9: Typed Lambda Calculus Advanced Topics in Programming Languages Spring Semester, 2012 Lecture 9: Typed Lambda Calculus May 8, 2012 Lecturer: Mooly Sagiv Scribe: Guy Golan Gueta and Shachar Itzhaky 1 Defining a Type System for

More information

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ October 13, 2004 Lambda Calculus and Type

More information

Types and Programming Languages. Lecture 5. Extensions of simple types

Types and Programming Languages. Lecture 5. Extensions of simple types Types and Programming Languages Lecture 5. Extensions of simple types Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao Tong University Fall, 2016 Coming soon Simply typed λ-calculus has enough structure

More information

A Simple Supercompiler Formally Verified in Coq

A Simple Supercompiler Formally Verified in Coq A Simple Supercompiler Formally Verified in Coq IGE+XAO Balkan 4 July 2010 / META 2010 Outline 1 Introduction 2 3 Test Generation, Extensional Equivalence More Realistic Language Use Information Propagation

More information

Lecture Notes on Data Representation

Lecture Notes on Data Representation Lecture Notes on Data Representation 15-814: Types and Programming Languages Frank Pfenning Lecture 9 Tuesday, October 2, 2018 1 Introduction In this lecture we ll see our type system in action. In particular

More information

CIS 500 Software Foundations Midterm I

CIS 500 Software Foundations Midterm I CIS 500 Software Foundations Midterm I October 11, 2006 Name: Student ID: Email: Status: Section: registered for the course not registered: sitting in to improve a previous grade not registered: just taking

More information

Types and Programming Languages. Lecture 6. Normalization, references, and exceptions

Types and Programming Languages. Lecture 6. Normalization, references, and exceptions Types and Programming Languages Lecture 6. Normalization, references, and exceptions Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao Tong University Fall, 2016 Coming soon One more language features:

More information

Homework 3 COSE212, Fall 2018

Homework 3 COSE212, Fall 2018 Homework 3 COSE212, Fall 2018 Hakjoo Oh Due: 10/28, 24:00 Problem 1 (100pts) Let us design and implement a programming language called ML. ML is a small yet Turing-complete functional language that supports

More information

Toward explicit rewrite rules in the λπ-calculus modulo

Toward explicit rewrite rules in the λπ-calculus modulo Toward explicit rewrite rules in the λπ-calculus modulo Ronan Saillard (Olivier Hermant) Deducteam INRIA MINES ParisTech December 14th, 2013 1 / 29 Motivation Problem Checking, in a trustful way, that

More information

On Meaning Preservation of a Calculus of Records

On Meaning Preservation of a Calculus of Records On Meaning Preservation of a Calculus of Records Emily Christiansen and Elena Machkasova Computer Science Discipline University of Minnesota, Morris Morris, MN 56267 chri1101, elenam@morris.umn.edu Abstract

More information

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th Name: CAS ID (e.g., abc01234@pomona.edu): I encourage you to collaborate. collaborations below. Please record your Each question is worth

More information

Programming type-safe transformations using higher-order abstract syntax

Programming type-safe transformations using higher-order abstract syntax Programming type-safe transformations using higher-order abstract syntax OLIVIER SAVARY BELANGER McGill University STEFAN MONNIER Universite de Montreal and BRIGITTE PIENTKA McGill University When verifying

More information

Denotational Semantics. Domain Theory

Denotational Semantics. Domain Theory Denotational Semantics and Domain Theory 1 / 51 Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning

More information

Basic Research in Computer Science BRICS RS O. Danvy: A Rational Deconstruction of Landin s SECD Machine

Basic Research in Computer Science BRICS RS O. Danvy: A Rational Deconstruction of Landin s SECD Machine BRICS Basic Research in Computer Science BRICS RS-03-33 O. Danvy: A Rational Deconstruction of Landin s SECD Machine A Rational Deconstruction of Landin s SECD Machine Olivier Danvy BRICS Report Series

More information

CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction

CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction CITS3211 FUNCTIONAL PROGRAMMING 14. Graph reduction Summary: This lecture discusses graph reduction, which is the basis of the most common compilation technique for lazy functional languages. CITS3211

More information

Adam Chlipala University of California, Berkeley ICFP 2006

Adam Chlipala University of California, Berkeley ICFP 2006 Modular Development of Certified Program Verifiers with a Proof Assistant Adam Chlipala University of California, Berkeley ICFP 2006 1 Who Watches the Watcher? Program Verifier Might want to ensure: Memory

More information

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going?

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going? Types for References, Exceptions and Continuations Annoucements How s the midterm going? Meeting 21, CSCI 5535, Spring 2009 2 One-Slide Summary Review of Subtyping If τ is a subtype of σ then any expression

More information

Functional Programming

Functional Programming Functional Programming COMS W4115 Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science Original version by Prof. Simon Parsons Functional vs. Imperative Imperative programming

More information