Slide 1 Vitaly Shmatikov CS 345 Introduction to ML.

Slides:



Advertisements
Similar presentations
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
Advertisements

Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
The Algol Family and ML John Mitchell CS 242. Language Sequence Algol 60 Algol 68 Pascal MLModula Lisp Many other languages: Algol 58, Algol W, Euclid,
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Semantics Static semantics Dynamic semantics attribute grammars
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
Introduction to ML – Part 1 Kenny Zhu. Assignment 2 chive/fall07/cos441/assignments/a2.ht m
CS 312 Spring 2004 Lecture 18 Environment Model. Substitution Model Represents computation as doing substitutions for bound variables at reduction of.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
CS 312 Spring 2002 Lecture 16 The Environment Model.
Crash course on SML Wojciech Moczydłowski SML – functional programming language Complete formal semantics Extremely convenient for writing compilers, interpreters,
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
The Algol Family and ML John Mitchell CS 242 Reading: Chapter 5.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages” Chapter 5 except “Real World Haskell”, Chapter 0 and Chapter 1 (
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
Introduction to Haskell Mooly Sagiv (original slides by Kathleen Fisher & John Mitchell) Spring 2012.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Brief history of “programming” “Programming” in the sense of devising rules, methods, procedures, recipes, algorithms, has always been present in human.
Midterm Review John Mitchell CS 242. Midterm uThurs Nov 7, 7-9PM Terman Aud Closed book uClass schedule Thurs Oct 31, Tues Nov 5 – topics not on midterm.
CSE S. Tanimoto Introduction to ML 1 Introduction to ML History Special features Interacting with ML ML’s basic types ML’s composite types Math.
Chapter 9: Functional Programming in a Typed Language.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03EX - ML Programming Language Design and Implementation.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
COMP313A Functional Programming (1)
1 Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002 ML (Meta Language) a brief introduction Alex Proctor and Brian Lee For CSCI 431 at the University.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Slide 1 Dr. Mohammad El-Ramly Fall 2010 Set 7- II - Functions Slides by Vitaly Shmatikov Cairo University Faculty of Computers and Information CS317 Concepts.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Slide 1 Vitaly Shmatikov CS 345 Exceptions. slide 2 Reading Assignment uMitchell, Chapter 8.2.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
Cs776 (Prasad)L2HOF1 Higher-Order Functions. cs776 (Prasad)L2HOF2 Higher-Order Functions A function that takes a function as argument and/or returns a.
CSE-321 Programming Languages Introduction to Functional Programming POSTECH March 8, 2006 박성우.
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Functional Programming
Programming Languages and Compilers (CS 421)
Functional Programming
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
CS 242 The Algol Family and ML John Mitchell Reading: Chapter 5.
ML Programming Language Design and Implementation (4th Edition)
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
FP Foundations, Scheme In Text: Chapter 14.
Functions, Patterns and Datatypes
CSE 341 Section 5 Winter 2018.
CSE S. Tanimoto Introduction to ML
Case Study: ML John Mitchell Reading: Chapter 5.
CSE-321 Programming Languages Introduction to Functional Programming
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
CSE S. Tanimoto Introduction to ML
CSE-321 Programming Languages Introduction to Functional Programming
Functions, Patterns and Datatypes
CSE S. Tanimoto Introduction to ML
CSE S. Tanimoto Introduction to ML
Functions, Patterns and Datatypes
Drew Wyborski Programming Languages
Presentation transcript:

slide 1 Vitaly Shmatikov CS 345 Introduction to ML

slide 2 Reading Assignment uMitchell, Chapter 5.3-4

slide 3 ML uGeneral-purpose, non-C-like, non-OO language Related languages: Haskell, Ocaml, F#, … uCombination of Lisp and Algol-like features Expression-oriented Higher-order functions Garbage collection Abstract data types Module system Exceptions uOriginally intended for interactive use

slide 4 Why Study ML ? uTypes and type checking General issues in static/dynamic typing Polymorphic type inference uMemory management Static scope and block structure, activation records Higher-order functions uControl Type-safe exceptions Tail recursion and continuations

slide 5 History of ML uRobin Milner Stanford, U. of Edinburgh, Cambridge 1991 Turing Award uLogic for Computable Functions (LCF) One of the first automated theorem provers uMeta-Language of the LCF system

slide 6 Logic for Computable Functions uDana Scott (1969) Formulated a logic for proving properties of typed functional programs uRobin Milner (1972) Project to automate logic Notation for programs Notation for assertions and proofs Need to write programs that find proofs –Too much work to construct full formal proof by hand Make sure proofs are correct

slide 7 LCF Proof Search uTactic: function that tries to find proof succeed and return proof tactic(formula) = search forever fail uExpress tactics in the Meta-Language (ML) uUse type system to facilitate correctness

slide 8 Tactics in ML Type System uTactic has a functional type tactic : formula  proof uType system must allow “failure” succeed and return proof tactic(formula) = search forever fail and raise exception

slide 9 Function Types in ML f : A  B means for every x  A, some element y=f(x)  B f(x) = run forever terminate by raising an exception In words, “if f(x) terminates normally, then f(x)  B.” Addition never occurs in f(x)+3 if f(x) raises exception. This form of function type arises directly from motivating application for ML. Integration of type system and exception mechanism mentioned in Milner’s 1991 Turing Award lecture.

slide 10 Higher-Order Functions uTactic is a function uMethod for combining tactics is a function on functions uExample: f(tactic 1, tactic 2 ) = formula. try tactic 1 (formula) else tactic 2 (formula) We haven’t seen -expressions yet (think of them as functions for now)

slide 11 Basic Overview of ML uInteractive compiler: read-eval-print Compiler infers type before compiling or executing Type system does not allow casts or other loopholes uExamples - (5+3)-2; > val it = 6 : int - if 5>3 then “Bob” else “Fido”; > val it = “Bob” : string - 5=4; > val it = false : bool

slide 12 Basic Types uBooleans true, false : bool if … then … else … (types must match) uIntegers 0, 1, 2, … : int +, *, … : int * int  int and so on … uStrings “Austin Powers” uReals 1.0, 2.2, , … decimal point used to disambiguate

slide 13 Compound Types uTuples (4, 5, “noxious”) : int * int * string uLists nil 1 :: [2, 3, 4] uRecords {name = “Fido”, hungry=true} : {name : string, hungry : bool} type

slide 14 Patterns and Declarations uPatterns can be used in place of variables ::= | | | … uValue declarations General form: val = val myTuple = (“Conrad”, “Lorenz”); val (x,y) = myTuple; val myList = [1, 2, 3, 4]; val x::rest = myList; Local declarations let val x = 2+3 in x*4 end;

slide 15 Functions and Pattern Matching uAnonymous function fn x => x+1; like function (…) in JavaScript uDeclaration form fun = | = … uExamples fun f (x,y) = x+y; actual argument must match pattern (x,y) fun length nil = 0 | length (x::s) = 1 + length(s);

slide 16 Functions on Lists uApply function to every element of list fun map (f, nil) = nil | map (f, x::xs) = f(x) :: map (f,xs); uReverse a list fun reverse nil = nil | reverse (x::xs) = append ((reverse xs), [x]); uAppend lists fun append (nil, ys) = ys | append (x::xs, ys) = x :: append(xs, ys); Example: map (fn x => x+1, [1,2,3]); [2,3,4] How efficient is this? Can you do it with only one pass through the list?

slide 17 More Efficient Reverse Function fun reverse xs = let fun rev(nil, z) = z | rev(y::ys, z) = rev(ys, y::z) in rev( xs, nil ) end;

slide 18 Datatype Declarations uGeneral form datatype = | … | ::= | of uExamples datatype color = red | yellow | blue –Elements are red, yellow, blue datatype atom = atm of string | nmbr of int –Elements are atm(“A”), atm(“B”), …, nmbr(0), nmbr(1),... datatype list = nil | cons of atom*list –Elements are nil, cons(atm(“A”), nil), … cons(nmbr(2), cons(atm(“ugh”), nil)),...

slide 19 Datatypes and Pattern Matching uRecursively defined data structure datatype tree = leaf of int | node of int*tree*tree node(4, node(3,leaf(1), leaf(2)), node(5,leaf(6), leaf(7)) ) uRecursive function fun sum (leaf n) = n | sum (node(n,t1,t2)) = n + sum(t1) + sum(t2)

slide 20 Example: Evaluating Expressions uDefine datatype of expressions datatype exp = Var of int | Const of int | Plus of exp*exp; Write (x+3)+y as Plus(Plus(Var(1),Const(3)), Var(2)) uEvaluation function fun ev(Var(n)) = Var(n) | ev(Const(n)) = Const(n) | ev(Plus(e1,e2)) = … ev(Plus(Const(3),Const(2))) Const(5) ev(Plus(Var(1),Plus(Const(2),Const(3)))) ev(Plus(Var(1), Const(5))

slide 21 Case Expression uDatatype datatype exp = Var of int | Const of int | Plus of exp*exp; uCase expression case e of Var(n) => … | Const(n) => …. | Plus(e1,e2) => …

slide 22 Evaluation by Cases datatype exp = Var of int | Const of int | Plus of exp*exp; fun ev(Var(n)) = Var(n) | ev(Const(n)) = Const(n) | ev(Plus(e1,e2)) = ( case ev(e1) of Var(n) => Plus(Var(n),ev(e2)) | Const(n) => ( case ev(e2) of Var(m) => Plus(Const(n),Var(m)) | Const(m) => Const(n+m) | Plus(e3,e4) => Plus(Const(n),Plus(e3,e4)) ) | Plus(e3,e4) => Plus(Plus(e3,e4),ev(e2)) ) ;

slide 23 ML Imperative Features uRemember l-values and r-values? Assignment y := x+3 uML reference cells and assignment Different types for location and contents x : int non-assignable integer value y : int ref location whose contents must be integer !y the contents of cell y ref x expression creating new cell initialized to x ML form of assignment y := x + 3 place value of x+3 in location (cell) y y := !y + 3 add 3 to contents of y and store in location y Refers to contents (r-value) Refers to location (l-value)

slide 24 Reference Cells in ML uVariables in most languages Variable names a storage location Contents of location can be read, can be changed uML reference cells A mutable cell is another type of value Explicit operations to read contents or change contents Separates naming (declaration of identifiers) from “variables”

slide 25 BobBill Imperative Examples in ML uCreate cell and change contents val x = ref “Bob”; x := “Bill”; uCreate cell and increment val y = ref 0; y := !y + 1; u“while” loop val i = ref 0; while !i < 10 do i := !i +1; !i; x 0 y 1

slide 26 Core ML uBasic Types Unit Booleans Integers Strings Reals Tuples Lists Records uPatterns uDeclarations uFunctions uPolymorphism uOverloading uType declarations uExceptions uReference cells

slide 27 Related Languages uML family Standard ML – Edinburgh, Bell Labs, Princeton, … CAML, OCAML – INRIA (France) –Some syntactic differences from Standard ML (SML) –Object system uHaskell Lazy evaluation, extended type system, monads uF# ML-like language for Microsoft.NET platform –“Combining the efficiency, scripting, strong typing and productivity of ML with the stability, libraries, cross-language working and tools of.NET. “ Compiler produces.NET intermediate language