Lecture 7: The Untyped Lambda Calculus

Similar documents
Lecture 13 CIS 341: COMPILERS

Lecture 5: The Untyped λ-calculus

Lecture 13: Subtyping

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

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

> module Lambda where > import Data.List -- I need some standard list functions

CMSC 330: Organization of Programming Languages

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

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

CSE-321 Programming Languages 2012 Midterm

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

Fundamentals and lambda calculus

The Substitution Model

CSE-321 Programming Languages 2010 Midterm

The Substitution Model. Nate Foster Spring 2018

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Programming Languages Lecture 15: Recursive Types & Subtyping

The Untyped Lambda Calculus

Lambda Calculus and Extensions as Foundation of Functional Programming

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

CSE 505: Concepts of Programming Languages

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Variables. Substitution

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

CS 312. Lecture April Lazy Evaluation, Thunks, and Streams. Evaluation

CMSC 330: Organization of Programming Languages

Pure Lambda Calculus. Lecture 17

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Metaprogramming assignment 3

Lambda Calculus. Lecture 4 CS /26/10

Introduction to Lambda Calculus. Lecture 7 CS /08/09

CMSC330 Spring 2017 Midterm 2

CMSC330. Objects, Functional Programming, and lambda calculus

CS153: Compilers Lecture 15: Local Optimization

Lecture 14: Recursive Types

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

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

Concepts of programming languages

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

1.3. Conditional expressions To express case distinctions like

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

7. Introduction to Denotational Semantics. Oscar Nierstrasz

The Untyped Lambda Calculus

EPITA - Practical Programming 06 - Advanced Module Usage

OCaml Language Choices CMSC 330: Organization of Programming Languages

Lambda Calculus. Variables and Functions. cs3723 1

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Untyped Lambda Calculus

CS153: Compilers Lecture 14: Type Checking

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

Lists. Michael P. Fourman. February 2, 2010

Chapter 5: The Untyped Lambda Calculus

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

1 Introduction. 3 Syntax

Functional Programming. Pure Functional Programming

CSE-321 Programming Languages 2010 Final

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

Mutable References. Chapter 1

CITS3211 FUNCTIONAL PROGRAMMING

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

Lambda Calculus.

Untyped Lambda Calculus

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

Graphical Untyped Lambda Calculus Interactive Interpreter

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CS1 Recitation. Week 2

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

Functions & First Class Function Values

Writing code that I'm not smart enough to write. A funny thing happened at Lambda Jam

CSE 341 Section 7. Ethan Shea Autumn Adapted from slides by Nicholas Shahan, Dan Grossman, Tam Dang, and Eric Mullen

(Refer Slide Time: 4:00)

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

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Lazy Modules. Keiko Nakata Institute of Cybernetics at Tallinn University of Technology

Programming Languages

Some Advanced ML Features

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

Lecture Notes on Data Representation

Harvard School of Engineering and Applied Sciences Computer Science 152

Lambda Calculus. Concepts in Programming Languages Recitation 6:

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

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

CMSC 336: Type Systems for Programming Languages Lecture 4: Programming in the Lambda Calculus Acar & Ahmed 22 January 2008.

Accurate Step Counting

Getting Started with Haskell (10 points)

CS-XXX: Graduate Programming Languages. Lecture 22 Class-Based Object-Oriented Programming. Dan Grossman 2012

Programming Languages Lecture 14: Sum, Product, Recursive Types

Begin at the beginning

Chapter 5: The Untyped Lambda Calculus

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

Transcription:

Lecture 7: The Untyped Lambda Calculus Writing the interpreter Polyvios Pratikakis Computer Science Department, University of Crete Type Systems and Static Analysis Pratikakis (CSD) Untyped λ-calculus III CS49040, 2015-2016 1 / 13

Last class Semantics: Small-step: e e, return the AST rewritten by interpreting one instruction Big-step: e v, go all the way to a value, evaluate Lazy, call-by-name: defer β-reduction until latest possible point Eager, call-by-value: perform β-reduction inside sub-terms (but not under λ) compute argument before applying the function Implementation: Free variables α-renaming Substitution (capture-avoiding) Pratikakis (CSD) Untyped λ-calculus III CS49040, 2015-2016 2 / 13

Definitions Syntax: type var = string type value = VFun of var * exp and exp = EVar of var EVal of value EApp of exp * exp Pratikakis (CSD) Untyped λ-calculus III CS49040, 2015-2016 3 / 13

Free variables (alternative) FV(x) = x FV(λxe) = FV(e) \ {x} FV(e 1 e 2 ) = FV(e 1 ) FV(e 2 ) module VarSet = SetMake(String) let rec freevar ( ast : exp) : VarSett = match ast with EVar(x) -> VarSet singleton x EVal(VFun(x, e)) -> VarSetremove x ( freevar e) EApp(e1, e2) -> VarSetunion ( freevar e1) ( freevar e2) Pratikakis (CSD) Untyped λ-calculus III CS49040, 2015-2016 4 / 13

A little more about modules More than namespace Abstraction for values (including functions) and types Modules have types module type OrderedType = sig type t val compare : t -> t -> int end Pratikakis (CSD) Untyped λ-calculus III CS49040, 2015-2016 5 / 13

Module types Example module Str : OrderedType = struct type t = string let compare = Pervasivescompare end Actually, built-in: module type SetOrderedType = sig end module String = struct end String defines at least the names and types from SetOrderedType Pratikakis (CSD) Untyped λ-calculus III CS49040, 2015-2016 6 / 13

Functors: functions on modules module Set : sig module type OrderedType = sig end module Make : functor (O: OrderedType) -> sig type elt = Ot type t val singleton : elt -> t val union : t -> t -> t end end Pratikakis (CSD) Untyped λ-calculus III CS49040, 2015-2016 7 / 13

Back to implementation: α-renaming One interesting case: (λye 1 )[e/x] Don t recompute FV(e) each time let subst ast e x = let fv = freevars e in let rec helper ast = in helper ast Might need to replace y in e 1 with fresh variable Create fresh name y different from free vars and x Keep alpha-renamings in a map: MapMake(OrderedType), Listassoc, etc Renaming map is argument to substitution function Pratikakis (CSD) Untyped λ-calculus III CS49040, 2015-2016 8 / 13

Creating fresh names Use a reference to create unique numbers But don t use references if you can avoid it! let next = let counter = ref 0 in fun str -> let n =!counter in incr counter; str ^( string_of_int n) A function that takes a string and appends a unique number Pratikakis (CSD) Untyped λ-calculus III CS49040, 2015-2016 9 / 13

A more efficient substitution module StrMap = MapMake(String) let subst ast e x = let fv = freevars e in let rec helper map ast = match ast with EVar(v) -> let v = StrMapfind v map in in helper ast StrMapempty Pratikakis (CSD) Untyped λ-calculus III CS49040, 2015-2016 10 / 13

Other utility functions Test integers let church_from_int n : exp = let scott_from_int n : exp = let church_succ : exp = ast_of_string fun n fun s fun z s (n s z) let church_plus : exp = ast_of_string fun m fun n fun s fun z m s (n s z) Also try Scott encoding Pratikakis (CSD) Untyped λ-calculus III CS49040, 2015-2016 11 / 13

Other utility functions, cont d Keep repeating small-step until done: let rec stepper ( f : exp -> exp) (e: exp) : exp = try (stepper f (f e)) with Cannot_step _ -> e Throw away the exception argument: We don t want the subexpression that cannot step (_), we want the whole stopped program e Might not terminate Pratikakis (CSD) Untyped λ-calculus III CS49040, 2015-2016 12 / 13

Next time Pure calculus is like assembly Many terms have the same representation Ad-hoc, depending on usage Easy to mix representations, use bool instead of int Introduce types A way to separate values into sets, ensure isolation Eg never mix an integer and a boolean Even when the underlying representation is the same Types as relations Prove type-safety: program will not go wrong Property over all executions Pratikakis (CSD) Untyped λ-calculus III CS49040, 2015-2016 13 / 13