How do people check polymorphic types?

Size: px
Start display at page:

Download "How do people check polymorphic types?"

Transcription

1 In A.F. Blackwell & E. Bilotta (Eds). Proc. PPIG 12 Pages How do people check polymorphic types? Yang Jun, Greg Michaelson and Phil Trinder Department of Computing and Electrical Engineering Heriot-Watt University, Riccarton, EH14 4AS, UK Keywords: POP-II.B. problem comprehension, POP-II.B. types of programmer behaviour Abstract Polymorphic typechecking algorithms efficiently locate type errors in programs, but users find error reporting from such algorithms hard to comprehend. We are investigating the development of a new polymorphic type checker that reports type errors in a more understandable form. Here we present the results of experiments into human checking of both correct and incorrect polymorphic typed programs, and briefly discuss their implications for our proposed new checker. Introduction Polymorphic typechecking The Hindley-Milner typechecking algorithm is widely used in contemporary functional language implementations, for example Standard ML (Milner et al 1997) and Haskell (Peyton Jones et al 1998). The algorithm traverses a program s abstract syntax tree (AST) 1, assigning type variables at each node. It then applies rules corresponding to syntactic constructs to attempt to resolve the assigned type variables against specific type information deduced from program contents and context. For a full account, see Milner (1978). For example, consider checking the following Standard ML-ish function, which returns a string depending on the sign of its integer argument: fun sign 0 = "zero" sign n = if (> n) 0 then "positive" else "negative" This function has type int -> string, i.e. it takes an integer argument and returns a string result. Prior to type checking, it is assumed that: 0 : int i.e. 0 is an integer "zero" : string Titolo convegno-volume 2000 Edizioni Memoria "positive" : string i.e. "zero" is a string "negative" : string > : int -> int -> bool i.e. > compares two integers to return a boolean 1 An abstract syntax tree is a representation of the meaningful syntactic structure of a language utterance. 12 th Workshop of the Psychology of Programming Interest Group, Cozenza Italy, April 2000

2 ii The rule for a function: checks the first case: concludes 0 is int from assumptions checks the body i.e. deduces "zero" is string from assumptions concludes that the first case is int -> string checks the second case: assumes parameter n has type α checks the body i.e. checks the if expression: checks the condition i.e. checks the application of > n to 0: checks the application of > to n: concludes that > is int -> int -> bool from assumptions assigns the type variable β to the result of the application concludes that n is α from assumptions unifies the anticipated type α -> β with the type of >, int -> int -> bool concludes that α is int and β is int -> bool assigns the type variable γ to the result of the application concludes that 0 is int from assumptions unifies the anticipated type int -> γ with the type of int -> bool concludes that γ is bool unifies the condition type bool with the required type for a condition, bool checks the then branch i.e. concludes that "positive" is string from assumptions checks the else branch i.e. concludes that "negative" is string from assumptions unifies the then and else branches, which must have the same type concludes that the if has type string concludes that the second case has type int -> string unifies the cases To summarise, the algorithm: is exhaustive, considering all parts of a program; is top-down, left to right; introduces a type variable for many constructs before instantiating a construct s type. In contrast, in our experience people appear to be far less rigid and rigorous in their checking of types. They seem to:

3 iii be partial, assuming a construct s type on the minimum necessary evidence; be opportunistic, scanning a program as 2D text for sources of evidence; seek concrete evidence (e.g. ground types) before introducing type variables. We might caricature a typical human check of the above example as: 0 is int so n must be int "zero" is string so the function result must be string so the function must be int -> string We may note that the caricature human check: concludes that every case must be int -> string because the first case is int -> string; scans down the columns of patterns as well as across each case; focuses on the ground type objects 0 and "zero". Note that we find that this caricature is not wholly accurate. Hindley-Milner Error Reporting We have observed that the Hindley-Milner algorithm is a poor source of guidance for humans in the presence of errors (Yang & Michaelson 2000). For example, consider the following incorrect definition of the Standard ML function map. The map function is supposed to apply a function argument f to every element of an argument list, for example, given: fun double x = 2*x then: map double [1,2,3,4] ==> [2,4,6,8] The incorrect definition is: fun map f [] = 0 map f (h::t) = f h::map f t; where: [] ==> empty list; (h::t)==> pattern where h matches the head of a list argument and t matches the tail of that list;

4 iv f h::map f t ==>apply f to h and put result on front of list from applying map with f to t. In SML, all function cases are supposed to return the same type. Here, however, the first case returns the int 0 but the second case returns a polymorphic list. Given this definition, the SML of New Jersey system (Version 0.93) reports: example.sml: Error: rules don t agree (tycon mismatch) expected: ( Z -> Y) * Z list -> int found: ( Z -> Y) * Z list -> Y list rule: (f,h :: t) => :: (f h,<exp> <exp> t) example.sml: Error: pattern and expression in val rec dec don t agree (tycon mismatch) pattern: ( Z -> Y) -> Z list -> Y list expression: ( Z -> Y) -> Z list -> int in declaration: map = (fn arg => (fn <pat> => <exp>)) Note the: use of the system type variables 2 Y and Z, which do not appear in the original function; failure to identify explicitly the clash between the types of the first and second cases as the source of the error; error messages refer to the abstract rather than the concrete syntax; same error is reported in two different ways. Edinburgh SML is somewhat more succinct, reporting: Type clash in: (f,(h :: t))=>((f h) :: ((% %) t)) Looking for a: int I have found a: a list Both systems use the Hindley-Milner algorithm as the bases of their typecheckers. 2 i.e. α and β for non-greek keyboards...

5 v Experiment We are interested in automating the provision of helpful explanations of polymorphic type checking, especially for naive users. To that end we have conducted a small study of human polymorphic type checking, to try to identify best practice as the basis for an automated system. Based on 12 years experience of teaching functional languages to undergraduate and postgraduate students, we hypothesised that human type checkers would: focus on ground or known types first; use vertical relationships between patterns and cases in resolving and assigning types, which we term 2D text inspection; only introduce type variables as a last resort; only partially check functions. We have carried out two experiments where experts were video taped type checking sets of SML functions, following a speak-aloud protocol. Question classification We have been unable to locate standard sets of type checking problems either for evaluating automated type checkers or for use with people. The questions in our experiments are drawn from the type checking problems that we set our 1st year Functional Programming students (Michaelson 1995), augmented with additional questions. Each question consists of an untyped function definition and the subject is required to identify the type, or explain why there is a type error. Questions are all formed in a pure functional subset of SML, comprising base types, lists and tuples. The questions were chosen to reflect a range of function types. For the 34 error free questions, the function types were: ground types (1) ground and list types (3) ground, list and tuple types (4) ground types and type variables (1) ground and list types, and type variables (1) ground, list and tuple types, and type variables (2) function argument and ground types (2) function argument, ground and list types (4) function argument, list and tuple types, and type variables (2) function argument, ground, list and tuple types (1) function argument types and type variables (2) function argument and ground types, and type variables (1) function argument and list types, and type variables (4) function argument, ground and list types, and type variables (3) function argument, ground, list and tuple types, and type variables (1)

6 vi list and tuple types, and type variables (1) type error... (1) The 34 error questions may contain multiple errors. Identifying the errors might involve locating: unresolved overloading (6) ground type conflicts (8) constructor type conflicts (3) inconsistencies between left and right hand sides of definitions (12) swapped bound variables (6) inconsistencies between cases (7) inconsistent numbers of arguments (3) non-universally quantified type variable (1) Subject classification 8 subjects took part in the type correct experiment and 7 in the type error experiment. 6 people took part in both experiments. The subjects all have: at least post-graduate Computer Science experience; programmed extensively in polymorphic typed languages; implemented Hindley-Milner type checkers or worked extensively with them when implementing functional languages; tutored undergraduate students. and may thus be considered experts rather than beginners. Subjects were given up to 30 minutes to complete each set. Analysis and results Initial inspection of the video taped sessions led to identification of 13 major inference techniques: 1. locate ground types 2. locate overloaded operators 3. locate other system operators of known types e.g. constructors 4. construct type skeleton for whole function corresponding to the number of arguments 5. locate commonalities across patterns 6. analyse top-down, from nodes to leaves of AST 7. analyse bottom up, from leaves to nodes of AST 8. search forwards and backwards from a known type 9. use type variable 10. identify argument type from use in function body

7 vii 11. identify body construct type from known argument type 12. construct and refine type skeleton for local construct 13. refine function type skeleton used during type checking. Each subject s attempt at each question was then analysed and sequences of the above techniques were recorded. The small number of subjects is not enough to form a basis for statistical analysis. Nonetheless, we are able to identify a number of clear trends by combining technique counts for all subjects. Overall comparison of attempts at questions with and without errors Table 1 summarises the numbers of questions attempted and behaviour instances identified: subjects question attempts attempts/ subject techniqu e count count/ attempt no errors errors Table 1 - Total questions attempted and technique counts. Far more questions were attempted per subject and less techniques were used per question for those with type errors than for those lacking errors. Naively, this implies that the subjects found identifying type errors easier than inferring types, but we would need to assume that both tasks require equal effort. However, as we shall see, subjects are slightly more likely to take a more exhaustive approach to type inference for error free questions. Furthermore, it may be that locating dissonance is intrinsically easier than verifying consistency, but we cannot substantiate this speculation without further experimentation. Error free question set Table 2 shows the use of techniques in the error free questions, in descending order of technique counts: Inference Technique tech. counts count/ total% 3. locate known system operators 13. refine type skeleton identify argument type construct/refine local skeleton 4. construct function skeleton top down locate ground types across patterns bottom up use type variable

8 viii 8. forward search locate overloaded operators backwards search body from argument use Table 2 - Questions with no type errors: technique counts for each technique and as a percentage of all technique uses. Considerable use is made of type signature skeletons, which we did not anticipate. Typically, a subject starts with a minimal outline signature (8.25%), with a slot for each bound variable, and for the result. They then fill in the slots to finer degrees of detail as checking progresses (15.30%), identifying argument types (8.80%), introducing sub-skeletons for structured bound variables (8.52%) and identifying the function body type from argument use (0.87%), totaling 40.95% of technique use. For example, consider inferring the type of: fun count0 [] = 0 count0 (0::t) = 1+count0 t count0 (h::t) = count0 t which counts how often 0 appears in a list of integers: initial skeleton: -> identify [] as empty list: list -> identify 0 as integer: list -> int identify (0::t) as integer list: int list -> int As hypothesised, much use is made of concrete type information. Type constructors (17.32%) often appear in patterns in our problem set and will be located early on in a left to right, or left top to left bottom, scan. Ground types (6.72%) are used less than constructors, which we did not anticipate. There is little use of overloaded operators (3.01%), reflecting their general absence from our question set. Concrete type technique use totals 27.05%. 2D inspection of function text, comprising top down (7.43%), across pattern (6.12%), bottom up (5.74%), forward search (4.48%) and backwards search (1.97%), represents 25.74% of technique use confirming our hypothesis. Low use is made of type variables (5.46%), as we hypothesised. Contrary to our expectations, full checking was carried out for 114 questions (77.55%) and partial checking for 33 (22.45%). Error question set Table 3 shows the use of techniques in the error questions, in descending order of technique counts:

9 ix Inference Technique tech. counts count/ total% 3. locate known system operators 13. refine type skeleton locate ground types across patterns forward search construct function skeleton locate overloaded operators construct/refine local skeleton 6. top down backwards search bottom up identify argument type body from argument use use type variable Table3 - Questions with type errors: technique counts for each technique and as a percentage of all technique uses. For the type error questions, less use was made of type signature skeletons than for the error free problems, through the introduction of an initial outline (6.85%), subsequent refinement (12.02%), local skeletons (6.01%), argument identification (2.59%) and body from argument use (1.86%), comprising 29.33% of technique use. However, more use was made of concrete type information, through the identification of known operators(22.3%),ground types (11.73%) and overloaded operators (6.01%), comprising 39.34% of technique use. 2D inspection of text, involving across pattern (10.94%), forward search (7.58%), top down (5.05%), backwards search (3.85%) and bottom up (2.77%), comprised 30.19% of technique use. Note that cross pattern comparison was used far more than for the error free problems. Type variable introduction (0.36%) was the least used technique, and used far less than for the error free questions. Full checking was carried out for 145 questions (61.70%) and partial checking for 90 (38.28%) questions. There was less use of full checking than for error free questions. Discussion and conclusions We have observed a number of important differences between human type checking, as characterised by our composite expert subject group, and the W algorithm. First of all, people find the elaboration of a skeletal type during checking extremely helpful in structuring the process. Furthermore, as we

10 x hypothesised, people rely on identification of constructs of known type to guide checking and make use of the 2D textual form to locate them. Finally, as we hypothesised, people use explicit type variables as a last resort. However, our prediction of people performing partial checking was confounded. We noticed that partial checking was most prevalent where the final type lacks type variables. This may suggest that expert type checkers are more punctilious than the novice students whom we observe from day to day. We have not found any comparable research specifically on human type checking. However, there are interesting correspondences with work on program comprehension. In particular, our observed use of type skeletons to guide type checking may be an instance of Brooks top-down, hypothesis driven comprehension (Brooks 1983). Similarly, our observed use of 2D inspection and concrete type features is similar to Wiedenbeck and Scholtz s identification of the use of surface beacons to guide successful program comprehension (Wiedenbeck and Scholz 1989). Wiedenbeck and Scholz note that beacon use is particularly helpful for comprehension of programs with unknown purposes: our questions are uncommented and provide no semantic information through meaningful identifiers. Here, we have presented a brief overview of our experimental results. We intend to analyse them in more detail, to try and identify which technique sequences people find useful in general, and to relate such sequences to the different categories of error and error-free questions. We are now constructing a type explanation system for our SML subset which will incorporate heuristics based on these conclusions. The system will be much less efficient than the linear W algorithm as heuristics may be applied repeatedly. However, we anticipate that system explanations will prove more useful than those from the W algorithm, especially to naive users. It is likely that there will be alternative explanations, corresponding to different traversals of the tree of W algorithm deductions, and that full explanations will include repetitive information. The system will seek to provide succinct explanations by minimising the explanation length, using explanation pruning heuristics whose choice will be guided by our observations of human behaviour. Acknowledgements We wish to thank Andrew Cook, Mohammad Hamdan, Kevin Hammond, Hans-Wolfgang Loidl, Robert Pointon and Joe Wells for taking part in our experiments. Yang Jun wishes to thank ORS and Heriot-Watt University for continuing support. References Brooks,R. (1983) Towards a Theory of the Comprehension of Computer Programs, International Journal of Man-Machine Studies, 18: Michaelson,G. (1995), Elementary Standard ML, UCL Press Milner,R. (1978) A Theory of Type Polymorphism in Programming, Journal of Computer and Systems Sciences, 17(3): Milner,R., Tofte,M., Harper,R., and McQueen,D. (1997) The Definition of Standard ML: Revised 1997, MIT Press Peyton Jones,S.L. et al (1998) Report on the Programming Language Haskell 98: A Non-strict, Purely Functional Language, Wiedenbeck,S. and Scholz,J. (1989) Beacons: a Knowledge Structure in Program Comprehension, in G.Salvendy and M.J.Smith (Editors), Designing and Using Human-Computer Interfaces and Knowledge Based Systems, Elsevier, Amsterdam, 82-87

11 xi Yang,J. and Michaelson,G. (2000) A visualisation of polymorphic type checking, Journal of Functional Programming, Vol. 10, No. 1:57-75

CIS24 Project #3. Student Name: Chun Chung Cheung Course Section: SA Date: 4/28/2003 Professor: Kopec. Subject: Functional Programming Language (ML)

CIS24 Project #3. Student Name: Chun Chung Cheung Course Section: SA Date: 4/28/2003 Professor: Kopec. Subject: Functional Programming Language (ML) CIS24 Project #3 Student Name: Chun Chung Cheung Course Section: SA Date: 4/28/2003 Professor: Kopec Subject: Functional Programming Language (ML) 1 Introduction ML Programming Language Functional programming

More information

Simple Unification-based Type Inference for GADTs

Simple Unification-based Type Inference for GADTs Simple Unification-based Type Inference for GADTs Stephanie Weirich University of Pennsylvania joint work with Dimitrios Vytiniotis, Simon Peyton Jones and Geoffrey Washburn Overview Goal: Add GADTs to

More information

Types and Type Inference

Types and Type Inference CS 242 2012 Types and Type Inference Notes modified from John Mitchell and Kathleen Fisher Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on Web!! Outline General discussion of

More information

Lists. Michael P. Fourman. February 2, 2010

Lists. Michael P. Fourman. February 2, 2010 Lists Michael P. Fourman February 2, 2010 1 Introduction The list is a fundamental datatype in most functional languages. ML is no exception; list is a built-in ML type constructor. However, to introduce

More information

Type Error Slicing What is a type error and how do you locate one?

Type Error Slicing What is a type error and how do you locate one? Type Error Slicing p.1/38 Type Error Slicing What is a type error and how do you locate one? Christian Haack DePaul University fpl.cs.depaul.edu/chaack Joe Wells Heriot-Watt University www.macs.hw.ac.uk/

More information

Flang typechecker Due: February 27, 2015

Flang typechecker Due: February 27, 2015 CMSC 22610 Winter 2015 Implementation of Computer Languages I Flang typechecker Due: February 27, 2015 Project 3 February 9, 2015 1 Introduction The third project is to implement a type checker for Flang,

More information

Introduction to SML Getting Started

Introduction to SML Getting Started Introduction to SML Getting Started Michael R. Hansen mrh@imm.dtu.dk Informatics and Mathematical Modelling Technical University of Denmark c Michael R. Hansen, Fall 2004 p.1/15 Background Standard Meta

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2018 Lecture 7b Andrew Tolmach Portland State University 1994-2018 Dynamic Type Checking Static type checking offers the great advantage of catching errors early And

More information

Arbitrary-rank polymorphism in (GHC) Haskell

Arbitrary-rank polymorphism in (GHC) Haskell Arbitrary-rank polymorphism in (GHC) Haskell CAS 743 Stephen Forrest 20 March 2006 Damas-Milner Type System A Damas-Milner type system (also called Hindley-Milner) is a traditional type system for functional

More information

(Refer Slide Time: 4:00)

(Refer Slide Time: 4:00) Principles of Programming Languages Dr. S. Arun Kumar Department of Computer Science & Engineering Indian Institute of Technology, Delhi Lecture - 38 Meanings Let us look at abstracts namely functional

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Type Inference Some statically typed languages, like ML (and to a lesser extent Scala), offer alternative

More information

Type Checking and Type Inference

Type Checking and Type Inference Type Checking and Type Inference Principles of Programming Languages CSE 307 1 Types in Programming Languages 2 Static Type Checking 3 Polymorphic Type Inference Version: 1.8 17:20:56 2014/08/25 Compiled

More information

TYPE INFERENCE. François Pottier. The Programming Languages Mentoring ICFP August 30, 2015

TYPE INFERENCE. François Pottier. The Programming Languages Mentoring ICFP August 30, 2015 TYPE INFERENCE François Pottier The Programming Languages Mentoring Workshop @ ICFP August 30, 2015 What is type inference? What is the type of this OCaml function? let f verbose msg = if verbose then

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

More information

Mutable References. Chapter 1

Mutable References. Chapter 1 Chapter 1 Mutable References In the (typed or untyped) λ-calculus, or in pure functional languages, a variable is immutable in that once bound to a value as the result of a substitution, its contents never

More information

CSE-321: Assignment 8 (100 points)

CSE-321: Assignment 8 (100 points) CSE-321: Assignment 8 (100 points) gla@postech.ac.kr Welcome to the final assignment of CSE-321! In this assignment, you will implement a type reconstruction algorithm (the algorithm W discussed in class)

More information

CSCI-GA Scripting Languages

CSCI-GA Scripting Languages CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation

More information

CS153: Compilers Lecture 14: Type Checking

CS153: Compilers Lecture 14: Type Checking CS153: Compilers Lecture 14: Type Checking Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Project 4 out Due Thursday Oct 25 (7 days) Project 5 out Due Tuesday Nov 13 (26 days) Project

More information

Type Processing by Constraint Reasoning

Type Processing by Constraint Reasoning , Martin Sulzmann, Jeremy Wazny 8th November 2006 Chameleon Chameleon is Haskell-style language treats type problems using constraints gives expressive error messages has a programmable type system Developers:

More information

Types, Type Inference and Unification

Types, Type Inference and Unification Types, Type Inference and Unification Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Cornell CS 6110 Summary (Functional Programming) Lambda Calculus Basic ML Advanced ML: Modules, References,

More information

Data Types The ML Type System

Data Types The ML Type System 7 Data Types 7.2.4 The ML Type System The following is an ML version of the tail-recursive Fibonacci function introduced Fibonacci function in ML in Section 6.6.1: EXAMPLE 7.96 1. fun fib (n) = 2. let

More information

Types and Static Type Checking (Introducing Micro-Haskell)

Types and Static Type Checking (Introducing Micro-Haskell) Types and Static (Introducing Micro-Haskell) Informatics 2A: Lecture 13 Alex Simpson School of Informatics University of Edinburgh als@inf.ed.ac.uk 16 October, 2012 1 / 21 1 Types 2 3 4 2 / 21 Thus far

More information

Where is ML type inference headed?

Where is ML type inference headed? 1 Constraint solving meets local shape inference September 2005 2 Types are good A type is a concise description of the behavior of a program fragment. Typechecking provides safety or security guarantees.

More information

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy 06-02552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 6: Polymorphic Type Systems 1. Polymorphic

More information

Types and Type Inference

Types and Type Inference Types and Type Inference Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on the course homepage Outline General discussion

More information

The design of a programming language for provably correct programs: success and failure

The design of a programming language for provably correct programs: success and failure The design of a programming language for provably correct programs: success and failure Don Sannella Laboratory for Foundations of Computer Science School of Informatics, University of Edinburgh http://homepages.inf.ed.ac.uk/dts

More information

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1 A First Look at ML Chapter Five Modern Programming Languages, 2nd ed. 1 ML Meta Language One of the more popular functional languages (which, admittedly, isn t saying much) Edinburgh, 1974, Robin Milner

More information

Type Systems, Type Inference, and Polymorphism

Type Systems, Type Inference, and Polymorphism 6 Type Systems, Type Inference, and Polymorphism Programming involves a wide range of computational constructs, such as data structures, functions, objects, communication channels, and threads of control.

More information

A quick introduction to SML

A quick introduction to SML A quick introduction to SML CMSC 15300 April 9, 2004 1 Introduction Standard ML (SML) is a functional language (or higherorder language) and we will use it in this course to illustrate some of the important

More information

Types and Static Type Checking (Introducing Micro-Haskell)

Types and Static Type Checking (Introducing Micro-Haskell) Types and Static (Introducing Micro-Haskell) Informatics 2A: Lecture 14 John Longley School of Informatics University of Edinburgh jrl@inf.ed.ac.uk 17 October 2017 1 / 21 1 Types 2 3 4 2 / 21 So far in

More information

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually Plan (next 4 weeks) 1. Fast forward Rapid introduction to what s in OCaml 2. Rewind 3. Slow motion Go over the pieces individually History, Variants Meta Language Designed by Robin Milner @ Edinburgh Language

More information

SMURF Language Reference Manual Serial MUsic Represented as Functions

SMURF Language Reference Manual Serial MUsic Represented as Functions SMURF Language Reference Manual Serial MUsic Represented as Functions Richard Townsend, Lianne Lairmore, Lindsay Neubauer, Van Bui, Kuangya Zhai {rt2515, lel2143, lan2135, vb2363, kz2219}@columbia.edu

More information

Fall Lecture 3 September 4. Stephen Brookes

Fall Lecture 3 September 4. Stephen Brookes 15-150 Fall 2018 Lecture 3 September 4 Stephen Brookes Today A brief remark about equality types Using patterns Specifying what a function does equality in ML e1 = e2 Only for expressions whose type is

More information

Exercises on ML. Programming Languages. Chanseok Oh

Exercises on ML. Programming Languages. Chanseok Oh Exercises on ML Programming Languages Chanseok Oh chanseok@cs.nyu.edu Dejected by an arcane type error? - foldr; val it = fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b - foldr (fn x=> fn y => fn z => (max

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

Harvard School of Engineering and Applied Sciences Computer Science 152

Harvard School of Engineering and Applied Sciences Computer Science 152 Harvard School of Engineering and Applied Sciences Computer Science 152 Lecture 17 Tuesday, March 30, 2010 1 Polymorph means many forms. Polymorphism is the ability of code to be used on values of different

More information

JOURNAL OF OBJECT TECHNOLOGY

JOURNAL OF OBJECT TECHNOLOGY JOURNAL OF OBJECT TECHNOLOGY Online at www.jot.fm. Published by ETH Zurich, Chair of Software Engineering JOT, 2002 Vol. 1, No. 2, July-August 2002 The Theory of Classification Part 2: The Scratch-Built

More information

A Brief Introduction to Standard ML

A Brief Introduction to Standard ML A Brief Introduction to Standard ML Specification and Verification with Higher-Order Logic Arnd Poetzsch-Heffter (Slides by Jens Brandt) Software Technology Group Fachbereich Informatik Technische Universität

More information

FUNCTIONAL AND LOGIC PROGRAMS

FUNCTIONAL AND LOGIC PROGRAMS FUNCTIONAL AND LOGIC PROGRAMS Contents Language Specific Compilation Context Handling Identification Scope Overloading Imported Scope Type Checking Type table Type equivalence Coercions Casts and conversions

More information

Mini-ML. CS 502 Lecture 2 8/28/08

Mini-ML. CS 502 Lecture 2 8/28/08 Mini-ML CS 502 Lecture 2 8/28/08 ML This course focuses on compilation techniques for functional languages Programs expressed in Standard ML Mini-ML (the source language) is an expressive core subset of

More information

Induction and Semantics in Dafny

Induction and Semantics in Dafny 15-414 Lecture 11 1 Instructor: Matt Fredrikson Induction and Semantics in Dafny TA: Ryan Wagner Encoding the syntax of Imp Recall the abstract syntax of Imp: a AExp ::= n Z x Var a 1 + a 2 b BExp ::=

More information

COS 320. Compiling Techniques

COS 320. Compiling Techniques Topic 5: Types COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 Types: potential benefits (I) 2 For programmers: help to eliminate common programming mistakes, particularly

More information

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

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context Types 1 / 15 Outline Introduction Concepts and terminology The case for static typing Implementing a static type system Basic typing relations Adding context 2 / 15 Types and type errors Type: a set of

More information

Overloading, Type Classes, and Algebraic Datatypes

Overloading, Type Classes, and Algebraic Datatypes Overloading, Type Classes, and Algebraic Datatypes Delivered by Michael Pellauer Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 28, 2006 September 28, 2006 http://www.csg.csail.mit.edu/6.827

More information

Introduction. chapter Functions

Introduction. chapter Functions chapter 1 Introduction In this chapter we set the stage for the rest of the book. We start by reviewing the notion of a function, then introduce the concept of functional programming, summarise the main

More information

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides. cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides. We are looking for homework graders. If you are interested, send mail to cs242cs.stanford.edu

More information

F28PL1 Programming Languages. Lecture 11: Standard ML 1

F28PL1 Programming Languages. Lecture 11: Standard ML 1 F28PL1 Programming Languages Lecture 11: Standard ML 1 Imperative languages digital computers are concrete realisations of von Neumann machines stored program memory associations between addresses and

More information

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

PROGRAMMING IN HASKELL. Chapter 2 - First Steps PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 The Hugs System Hugs is an implementation of Haskell 98, and is the most widely used Haskell system; The interactive nature of Hugs makes it well suited

More information

Types-2. Polymorphism

Types-2. Polymorphism Types-2 Polymorphism Type reconstruction (type inference) for a simple PL Typing functions Coercion, conversion, reconstruction Rich area of programming language research as people try to provide safety

More information

Agenda. CS301 Session 11. Common type constructors. Things we could add to Impcore. Discussion: midterm exam - take-home or inclass?

Agenda. CS301 Session 11. Common type constructors. Things we could add to Impcore. Discussion: midterm exam - take-home or inclass? Agenda CS301 Session 11 Discussion: midterm exam - take-home or inclass? Interlude: common type constructors Type soundness 1 2 Things we could add to Impcore Common type constructors Array is a type constructor,

More information

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD* OCaml 1. Introduction Rapid introduction to what s in OCaml 2. Focus on Features Individually as Needed as Semester Progresses *Notes from Sorin Lerner at UCSD* History, Variants Meta Language Designed

More information

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference. CS 3110 Spring 2015 Problem Set 3 Version 0 (last modified March 12, 2015) Soft deadline: 3/26/15 11:59pm Hard deadline: 3/28/15 1:00pm Overview In this assignment you will implement several functions

More information

CLF: A logical framework for concurrent systems

CLF: A logical framework for concurrent systems CLF: A logical framework for concurrent systems Thesis Proposal Kevin Watkins Carnegie Mellon University Committee: Frank Pfenning, CMU (Chair) Stephen Brookes, CMU Robert Harper, CMU Gordon Plotkin, University

More information

Handout 10: Imperative programs and the Lambda Calculus

Handout 10: Imperative programs and the Lambda Calculus 06-02552 Princ of Progr Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 10: Imperative programs and the Lambda Calculus

More information

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml COSE212: Programming Languages Lecture 3 Functional Programming in OCaml Hakjoo Oh 2017 Fall Hakjoo Oh COSE212 2017 Fall, Lecture 3 September 18, 2017 1 / 44 Why learn ML? Learning ML is a good way of

More information

Edinburgh Research Explorer

Edinburgh Research Explorer Edinburgh Research Explorer System Description: CyNTHIA Citation for published version: Whittle, J, Bundy, A, Boulton, R & Lowe, H 1999, System Description: CyNTHIA. in Automated Deduction CADE-16: 16th

More information

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 17: Types and Type-Checking 25 Feb 08 CS 412/413 Spring 2008 Introduction to Compilers 1 What Are Types? Types describe the values possibly

More information

Introduction to OCaml

Introduction to OCaml Fall 2018 Introduction to OCaml Yu Zhang Course web site: http://staff.ustc.edu.cn/~yuzhang/tpl References Learn X in Y Minutes Ocaml Real World OCaml Cornell CS 3110 Spring 2018 Data Structures and Functional

More information

Polymorphic Types in ACL2

Polymorphic Types in ACL2 Polymorphic Types in ACL2 Benjamin Selfridge University of Texas at Austin Austin, TX benself@cs.utexas.edu Eric Smith Kestrel Institute Palo Alto, CA eric.smith@kestrel.edu This paper describes a tool

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages Lesson 14 Type Checking Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1 Type Checking We return to the issue of type safety we discussed informally,

More information

Polymorphism and Type Inference

Polymorphism and Type Inference Polymorphism and Type Inference Volker Stolz stolz@ifi.uio.no Department of Informatics University of Oslo Initially by Gerardo Schneider. Based on John C. Mitchell s slides (Stanford U.) Compile-time

More information

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27 CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter 2.1-2.7 p. 1/27 CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp-16/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 24! Type inference in ML / Haskell 1 Type Checking vs

More information

An Object Oriented Programming with C

An Object Oriented Programming with C An Object Oriented Programming with C By Tanmay Kasbe Dr. Ravi Singh Pippal IDEA PUBLISHING WWW.ideapublishing.in i Publishing-in-support-of, IDEA PUBLISHING Block- 9b, Transit Flats, Hudco Place Extension

More information

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type. OCaml The PL for the discerning hacker. ML Flow Expressions (Syntax) Compile-time Static 1. Enter expression 2. ML infers a type Exec-time Dynamic Types 3. ML crunches expression down to a value 4. Value

More information

Products and Records

Products and Records Products and Records Michael P. Fourman February 2, 2010 1 Simple structured types Tuples Given a value v 1 of type t 1 and a value v 2 of type t 2, we can form a pair, (v 1, v 2 ), containing these values.

More information

Heuristics for type error discovery and recovery

Heuristics for type error discovery and recovery Heuristics for type error discovery and recovery Jurriaan Hage Bastiaan Heeren institute of information and computing sciences, utrecht university technical report UU-CS-2005-029 www.cs.uu.nl Abstract.

More information

Structural Typing for Structured Products

Structural Typing for Structured Products Structural Typing for Structured Products Tim Williams Peter Marks 8th October 2014 Background The FPF Framework A standardized representation for describing payoffs A common suite of tools for trades

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

Tracing Ambiguity in GADT Type Inference

Tracing Ambiguity in GADT Type Inference Tracing Ambiguity in GADT Type Inference ML Workshop 2012, Copenhagen Jacques Garrigue & Didier Rémy Nagoya University / INRIA Garrigue & Rémy Tracing ambiguity 1 Generalized Algebraic Datatypes Algebraic

More information

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 6 Robert Grimm, New York University 1 Review Last week Function Languages Lambda Calculus SCHEME review 2 Outline Promises, promises, promises Types,

More information

7. Introduction to Denotational Semantics. Oscar Nierstrasz

7. Introduction to Denotational Semantics. Oscar Nierstrasz 7. Introduction to Denotational Semantics Oscar Nierstrasz Roadmap > Syntax and Semantics > Semantics of Expressions > Semantics of Assignment > Other Issues References > D. A. Schmidt, Denotational Semantics,

More information

Compilers and computer architecture: Semantic analysis

Compilers and computer architecture: Semantic analysis 1 / 1 Compilers and computer architecture: Semantic analysis Martin Berger Alex Jeffery October 2018 Recall the function of compilers 2 / 1 3 / 1 Recall the structure of compilers Source program Lexical

More information

Programming Languages and Compilers (CS 421)

Programming Languages and Compilers (CS 421) Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC http://courses.engr.illinois.edu/cs421 Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha 10/3/17

More information

l e t print_name r = p r i n t _ e n d l i n e ( Name : ^ r. name)

l e t print_name r = p r i n t _ e n d l i n e ( Name : ^ r. name) Chapter 8 Row polymorphism Consider the following code: type name_home = {name : s t r i n g ; home : s t r i n g } type name_mobile = {name : s t r i n g ; mobile : s t r i n g } l e t jane = {name =

More information

Let Arguments Go First

Let Arguments Go First Let Arguments Go First Ningning Xie and Bruno C. d. S. Oliveira The University of Hong Kong {nnxie,bruno}@cs.hku.hk Abstract. Bi-directional type checking has proved to be an extremely useful and versatile

More information

Optimising Functional Programming Languages. Max Bolingbroke, Cambridge University CPRG Lectures 2010

Optimising Functional Programming Languages. Max Bolingbroke, Cambridge University CPRG Lectures 2010 Optimising Functional Programming Languages Max Bolingbroke, Cambridge University CPRG Lectures 2010 Objectives Explore optimisation of functional programming languages using the framework of equational

More information

todo ML-TID A Type Inference Debugger for ML in Education

todo ML-TID A Type Inference Debugger for ML in Education todo ML-TID A Type Inference Debugger for ML in Education Ben Thorner University of Cambridge ben.thorner@cl.cam.ac.uk Kathryn Gray University of Cambridge kathryn.gray@cl.cam.ac.uk Abstract Student programmers

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

Types, Expressions, and States

Types, Expressions, and States 8/27: solved Types, Expressions, and States CS 536: Science of Programming, Fall 2018 A. Why? Expressions represent values in programming languages, relative to a state. Types describe common properties

More information

Functional Programming Language Haskell

Functional Programming Language Haskell Functional Programming Language Haskell Mohammed Aslam CIS 24 Prof. Kopec Presentation: 03 Date: 05/05/2003 Haskell is a general purpose, purely functional programming language named after the logician

More information

Lecture 12: Data Types (and Some Leftover ML)

Lecture 12: Data Types (and Some Leftover ML) Lecture 12: Data Types (and Some Leftover ML) COMP 524 Programming Language Concepts Stephen Olivier March 3, 2009 Based on slides by A. Block, notes by N. Fisher, F. Hernandez-Campos, and D. Stotts Goals

More information

An Efficient Staging Algorithm for Binding-Time Analysis

An Efficient Staging Algorithm for Binding-Time Analysis An Efficient Staging Algorithm for Binding-Time Analysis Takuma Murakami 1, Zhenjiang Hu 1,2, Kazuhiko Kakehi 1, and Masato Takeichi 1 1 Department of Mathematical Informatics, Graduate School of Information

More information

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012 CS-XXX: Graduate Programming Languages Lecture 9 Simply Typed Lambda Calculus Dan Grossman 2012 Types Major new topic worthy of several lectures: Type systems Continue to use (CBV) Lambda Caluclus as our

More information

Polymorphism and Type Inference

Polymorphism and Type Inference Polymorphism and Type Inference Volker Stolz stolz@ifi.uio.no INF 3110-2008 Department of Informatics University of Oslo Initially by Gerardo Schneider. Based on John C. Mitchell s slides (Stanford U.)

More information

RSL Reference Manual

RSL Reference Manual RSL Reference Manual Part No.: Date: April 6, 1990 Original Authors: Klaus Havelund, Anne Haxthausen Copyright c 1990 Computer Resources International A/S This document is issued on a restricted basis

More information

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#, Introduction to ML Based on materials by Vitaly Shmatikov slide 1 ML General-purpose, non-c-like, non-oo language Related languages: Haskell, Ocaml, F#, Combination of Lisp and Algol-like features (1958)

More information

Operational Semantics

Operational Semantics 15-819K: Logic Programming Lecture 4 Operational Semantics Frank Pfenning September 7, 2006 In this lecture we begin in the quest to formally capture the operational semantics in order to prove properties

More information

A Fourth Look At ML. Chapter Eleven Modern Programming Languages, 2nd ed. 1

A Fourth Look At ML. Chapter Eleven Modern Programming Languages, 2nd ed. 1 A Fourth Look At ML Chapter Eleven Modern Programming Languages, 2nd ed. 1 Type Definitions Predefined, but not primitive in ML: datatype bool = true false; Type constructor for lists: datatype 'element

More information

Adding GADTs to OCaml the direct approach

Adding GADTs to OCaml the direct approach Adding GADTs to OCaml the direct approach Jacques Garrigue & Jacques Le Normand Nagoya University / LexiFi (Paris) https://sites.google.com/site/ocamlgadt/ Garrigue & Le Normand Adding GADTs to OCaml 1

More information

An update on XML types

An update on XML types An update on XML types Alain Frisch INRIA Rocquencourt (Cristal project) Links Meeting - Apr. 2005 Plan XML types 1 XML types 2 3 2/24 Plan XML types 1 XML types 2 3 3/24 Claim It is worth studying new

More information

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II) Processadors de Llenguatge II Functional Paradigm Pratt A.7 Robert Harper s SML tutorial (Sec II) Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra Paradigm Shift Imperative Paradigm State Machine

More information

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

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011 CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011 1 Introduction Type checking is a lightweight technique for proving simple properties of programs. Unlike theorem-proving techniques based on axiomatic

More information

CS 6110 S14 Lecture 1 Introduction 24 January 2014

CS 6110 S14 Lecture 1 Introduction 24 January 2014 CS 6110 S14 Lecture 1 Introduction 24 January 2014 1 Introduction What is a program? Is it just something that tells the computer what to do? Yes, but there is much more to it than that. The basic expressions

More information

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

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 14 Tuesday, March 24, 2015 1 Parametric polymorphism Polymorph means many forms. Polymorphism is the ability of

More information

Type Classes and Instance Chains: A Relational Approach. John Garrett Morris

Type Classes and Instance Chains: A Relational Approach. John Garrett Morris Type Classes and Instance Chains: A Relational Approach by John Garrett Morris A dissertation submitted in partial fulfillment of the requirements for the degree of Doctor of Philosophy in Computer Science

More information

Generic polymorphism on steroids

Generic polymorphism on steroids Generic polymorphism on steroids or How to Solve the Expression Problem with Polymorphic Variants Claudio Sacerdoti Coen Dipartimento di Informatica Scienza e Ingegneria

More information

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

SEMANTIC ANALYSIS TYPES AND DECLARATIONS SEMANTIC ANALYSIS CS 403: Type Checking Stefan D. Bruda Winter 2015 Parsing only verifies that the program consists of tokens arranged in a syntactically valid combination now we move to check whether

More information

Overview. Elements of Programming Languages. Another example. Consider the humble identity function

Overview. Elements of Programming Languages. Another example. Consider the humble identity function Overview Elements of Programming Languages Lecture 8: Polymorphism and type inference James Cheney University of Edinburgh October 21, 2016 This week and next week, we will cover di erent forms of abstraction

More information

CS 321 Homework 4 due 1:30pm, Thursday, March 15, 2012 This homework specification is copyright by Andrew Tolmach. All rights reserved.

CS 321 Homework 4 due 1:30pm, Thursday, March 15, 2012 This homework specification is copyright by Andrew Tolmach. All rights reserved. CS 321 Homework 4 due 1:30pm, Thursday, March 15, 2012 This homework specification is copyright 2002-2012 by Andrew Tolmach. All rights reserved. Typechecking In this assignment, you will build a type-checker

More information