Crash course on SML Wojciech Moczydłowski SML – functional programming language Complete formal semantics Extremely convenient for writing compilers, interpreters,

Slides:



Advertisements
Similar presentations
More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Advertisements

Programming with Lists
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 [3,4]
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.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
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.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
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 =
Chapter ElevenModern Programming Languages1 A Fourth Look At ML.
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.
CSE341: Programming Languages Lecture 5 Pattern-Matching Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Fall 2011.
A Lightning Tour of Haskell Lecture 1, Designing and Using Combinators John Hughes.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Introduction to ML A Quasi-Functional Language With Strong Typing And Type Inference.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
Introduction to ML A Quasi-Functional Language With Strong Typing And Type Inference.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
F28PL1 Programming Languages Lecture 15: Standard ML 5.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
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.
Chapter 9: Functional Programming in a Typed Language.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Cs7120 (prasad)L7-TDEF1 Type Definitions. cs7120 (prasad)L7-TDEF2 Concrete Types Primitive types ( int, bool, char, string, etc ) Type constructors (
Overview of the Haskell 98 Programming Language
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CSE 341 : Programming Languages Lecture 2 Functions, Pairs, Lists Zach Tatlock Spring 2014.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
CSED101 INTRODUCTION TO COMPUTING SUM TYPE 유환조 Hwanjo Yu.
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.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
String is a synonym for the type [Char].
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
A lightening tour in 45 minutes
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
Nicholas Shahan Spring 2016
PROGRAMMING IN HASKELL
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Functions, Patterns and Datatypes
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Functions, Patterns and Datatypes
Types and Classes in Haskell
PROGRAMMING IN HASKELL
Functions, Patterns and Datatypes
CSE 341 Section 3 Nick Mooney Spring 2017.
CSE-321 Programming Languages Introduction to Functional Programming
Functions, Patterns and Datatypes
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Functions, Patterns and Datatypes
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Presentation transcript:

Crash course on SML Wojciech Moczydłowski SML – functional programming language Complete formal semantics Extremely convenient for writing compilers, interpreters, provers and 611 homeworks OCaml, based on SML, is practical and used in industry

SML - ideology 3 worlds – expressions, values and types. Values have types, i.e. 0 : int, “asd” : string Expressions have types as well, i.e. 3+4 : int, fact : int -> int Expressions evaluate to values

SML types Some types: –int, string, char –int*int, int*string –int -> int, int*int -> int –int -> int -> int –int list –‘a list –‘a -> ‘b -> ‘a

Interpreter - 2+2; val it = 4 : int ; val it = 40 : int -“611 rules”; val it = “611 rules” : string - ~ ; val it = 0 : int -(17, “a”); val it = (17, “a”) : int * string

Definitions -val i = 10; val i = 10 : int -val j = i + i; val j = 20 : int -val s = Int.toString(j); val s = “20” : string -val t = (i, i + 1, i + 2); val t = (10, 11, 12) : int * int *int -val q = #2 t; val q = 11 : int

Datatypes -datatype Bool = True | False -datatype Color = Red | Black -datatype Nat = Zero | S of Nat -True; val it = True : Bool -S(S(Zero)); val it = S(S(Zero)) : Nat

Functions fun add(n, m : Nat) : Nat = case n of Zero => m | S(x) => S(add(x, m)); fun mult(n, m) = case n of Zero => Zero |S(x) => add(n, mult(x, m)) Evaluation – call by value. All functions have one argument – add : Nat * Nat -> Nat mult : Nat * Nat -> Nat

Anonymous functions As in lambda calculus, a function can be specified “on fly” -(fn x => x + 17) 26; val it = 43 : int; Large parts of lambda calculus can be expressed.

Polymorphism -val K = fn x => fn y => x; val K = fn : ‘a -> ‘b -> ‘a ‘a ‘b are type variables. -K 6 4; val it = 6 : int -K “611” 170; val it = “611” : string -K K K;

Polymorphism -val I = fn x => x; -val S = fn x => fn y => fn z => (x z) (y z); -val Ap = fn f => fn x => f x; However, the following won’t type-check: -val Y = fn f => (fn x => f (x x)) (fn x => f (x x)); -(fn x => x x) (fn x => x x);

Datatypes reloaded datatype ‘a option = NONE | SOME of ‘a; -NONE; val it = NONE : ‘a option; -SOME; val it = fn : ‘a -> ‘a option -SOME 5; val it = SOME 5 : int option; -SOME “Cornell”; val it = SOME “Cornell” : string option;

Datatypes -fun div(m, n) = if n = 0 then NONE else SOME (m div n); -datatype (‘a, ‘b) Either = Left of ‘a | Right of ‘b;

Recursive polymorphic datatypes -datatype ‘a Tree = Null | Node of (‘a Tree) * ‘a * (‘a Tree) -Node (Node(Null, 5, Null), 3, Null); -fun sum(t) = case t of Null => 0 | Node (a, b, c) => sum(a) + b + sum(c)

RPD’s fun size(t) = case t of Null => 0 |Node(t1, _, t2) => size t1 + (size t2) + 1 fun add(t, n) = case t of Null => Null |Node(t1, m, t2) => Node(add(t1, n), m + n, add(t2, n))

RPD’s fun mul(t, n) = case t of Null => Null |Node(t1, m, t2) => Node(mul(t1, n), m * n, mul(t2, n)) In general?

RPD’s fun mapTree(t, f) = case t of Null => Null |Node(t1, m, t2) => Node(mapTree(t1, f), f m, mapTree(t2, f)) -fun add(t, n) = mapTree(t, fn m => m + n); -val mul = fn (t, n) => mapTree (t, fn m => n * m);

RPD’s datatype ‘a list = nil | cons of (‘a * ‘a list); Notation: [] denotes nil (x::xs) denotes cons (x, xs)

Lists Some lists: [] (1::(2::(3::nil))) (“a”::”b”::”c”::nil) Another notation: [a, b, c] = a::b::c::nil [1,2,3], [“a”, “b”, “c”]

Lists -fun length(l) = case l of [] => 0 |(x::xs) => 1 + length xs; -fun hd [] = raise Fail “Empty” |hd(x::xs) = x; -fun tl [] = raise Fail “Empty” |tl (x::xs) = xs;

Lists -fun map(f, l) = case l of [] => [] |(x::xs) => f x ::(map f xs);

Modules signature SET = sig type ‘‘a set val empty : ‘‘a set val insert : ‘‘a -> ‘‘a set -> ‘‘a set val elem : ‘‘a -> ‘‘a set -> bool end

Modules structure Set :> SET= struct type ’’a set = ‘‘a list val empty : ‘‘a set = [] val insert : ‘‘a -> ‘‘a set -> ‘‘a set = fn x => fn y => x::y val elem : ‘‘a -> ‘‘a set -> bool =... end

Accessing structure elements -Set.insert, Set.add.... -open Set; -insert, add,...

Standard library Many useful structures List :> LIST hd, tl, last, map, find, filter... Option :> OPTION option, isSome, valOf, filter... String :> STRING size, isSubstring, ^, <=...