A Brief Introduction to Standard ML

Similar documents
Functional Programming

Programming in Standard ML: Continued

Specification and Verification in Higher Order Logic

Higher-Order Logic. Specification and Verification with Higher-Order Logic

A quick introduction to SML

Functional Programming and Modeling

CSE 341 Section 5. Winter 2018

CS109A ML Notes for the Week of 1/16/96. Using ML. ML can be used as an interactive language. We. shall use a version running under UNIX, called

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

Introduction to SML Getting Started

Chapter 3 Linear Structures: Lists

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

Chapter 3 Linear Structures: Lists

CMSC 330, Fall 2013, Practice Problems 3

Products and Records

SML A F unctional Functional Language Language Lecture 19

Note that pcall can be implemented using futures. That is, instead of. we can use

Lists. Michael P. Fourman. February 2, 2010

Exercises on ML. Programming Languages. Chanseok Oh

CS Lectures 2-3. Introduction to OCaml. Polyvios Pratikakis

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

CSCC24 Functional Programming Typing, Scope, Exceptions ML

If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

The type checker will complain that the two branches have different types, one is string and the other is int

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Computer Science CSC324 Wednesday February 13, Homework Assignment #3 Due: Thursday February 28, 2013, by 10 p.m.

Functional Programming

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

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

Fall Lecture 3 September 4. Stephen Brookes

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Lecture 2: The Basis of SML

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

F28PL1 Programming Languages. Lecture 14: Standard ML 4

Datatype declarations

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

Introduc)on To Standard ML

Begin at the beginning

CSE341 Autumn 2017, Midterm Examination October 30, 2017

Homework 3 COSE212, Fall 2018

Online Resource. Online introductory book on programming in SML (Standard ML): online.pdf

Announcements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML

Imperative languages

Haskell Types COMP360

A list is a finite sequence of elements. Elements may appear more than once

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

Type Checking and Type Inference

Lecture Notes on Induction and Recursion

Currying fun f x y = expression;

CSE 341: Programming Languages

Product types add could take a single argument of the product type int * int fun add(,y):int = +y; > val add = fn : int * int -> int add(3,4); > val i

Functional Programming

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Functional programming Primer I

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

Sample Exam; Solutions

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

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

F28PL1 Programming Languages. Lecture 11: Standard ML 1

ITT8060: Advanced Programming (in F#)

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

SML. CSE 307 Principles of Programming Languages Stony Brook University

Functional Paradigm II

Introduction to OCaml

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen

CSCI-GA Scripting Languages

Higher-Order Functions

Programming Paradigms

Chapter 3 Programming with Recursion

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Combining Static and Dynamic Contract Checking for Curry

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Zach Tatlock Winter 2018

Chapter 2 SML, a Functional Programming Language

Example Scheme Function: equal

2.1. Expressions. Chapter 2 SML, a Functional Programming Language. Integers. Interacting with ML

Function definitions. CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Example, extended. Some gotchas. Zach Tatlock Winter 2018

02157 Functional Programming Lecture 1: Introduction and Getting Started

Data Types The ML Type System

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Dan Grossman Fall 2011

CSE-321 Programming Languages 2010 Midterm

Programming Languages and Compilers (CS 421)

Sometimes it s good to be lazy

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

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources

CPS 506 Comparative Programming Languages. Programming Language Paradigm

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

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

CSC/MAT-220: Lab 6. Due: 11/26/2018

Course outline. CSE 341: Programming Languages. Why study programming languages? Course motivation and objectives. 1 lecture: Concepts

2.1. Expressions. Chapter 2 ML, a Functional Programming Language. Integers. Interacting with ML

More eamples with half half(0) handle Zero => 1000; > val it = 1000 : int half(1) handle Zero => 1000; > ncaght eception Odd half(0) handle Zero => 10

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CSE341: Programming Languages Lecture 7 First-Class Functions. Dan Grossman Winter 2013

Exercise 1 ( = 24 points)

Transcription:

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 Kaiserslautern Sommersemester 2008 Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 1 / 35

Outline Overview 1 Overview Functional Programming Standard ML 2 Standard ML in Examples Evalutation and Bindings Operations and Functions Standard Data Types Polymorphism and Type Inference Miscellaneous Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 2 / 35

Functional Programming Overview Functional Programming Fact functional program = expression Functional Programs do not have instead variables assigments sequences repetition let-expressions recursive functions higher-order functions Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 3 / 35

Functional Programming Overview Functional Programming Advantages clearer semantics corresponds more directly to abstract mathematical objects more freedom in implementation Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 4 / 35

Overview Standard ML The SML Programming Language Overview functional programming language normally used interactively strongly typed, with: type inference abstract data types polymorphic types exception-handling mechanisms Motivation SML is the meta-language for the HOL theorem prover Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 5 / 35

Outline 1 Overview Functional Programming Standard ML 2 Standard ML in Examples Evalutation and Bindings Operations and Functions Standard Data Types Polymorphism and Type Inference Miscellaneous Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 6 / 35

Evaluation and Bindings Evalutation and Bindings Example (Evaluation) 2 + 3; val it = 5 : int rev [1,2,3,4,5]; val it = [5,4,3,2,1] : int list Example (Simple Bindings) val n = 8 2 + 5; val n = 21 : int n 2; val it = 42 : int Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 7 / 35

Bindings Evalutation and Bindings Example (Special Identifier it) it ; val it = 42 : int Example (Multiple Bindings) val one = 1 and two = 2; val one = 1 : int val two = 2 : int Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 8 / 35

Local Bindings Evalutation and Bindings Example (Simple Local Binding) val n = 0; val n = 0 : int let val n = 12 in n div 6 end; val it = 2 : int n; val it = 0 : int Example (Multiple Local Bindings) let val n = 5 val m = 6 in n + m end; val it = 11 : int Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 9 / 35

Booleans Operations and Functions Example (Operations) val b1 = true and b2 = false; val b1 = true : bool val b2 = false : bool 1 = (1 + 1); val it = false : bool not (b1 orelse b2); val it = false : bool (7 < 3) andalso (false orelse 2 > 0); val it = false : bool Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 10 / 35

Integers Operations and Functions Example (Operations) val n = 2 + (3 4); val n = 14 : int val n = (10 div 2) 7; val n = ~2 : int Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 11 / 35

Applying Functions Operations and Functions General Rules type of functions from σ 1 to σ 2 is σ 1 σ 2 application f x applies function f to argument x call-by-value (obvious!) left associative: m n o p = (((m n)o)p) Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 12 / 35

Defining Functions Operations and Functions Example (One Argument) fun f n = n + 2; val f = fn : int > int f 22; val it = 24 : int Example (Two or More Arguments) fun plus n (m:int) = n + m; val plus = fn : int > int > int plus 2 3; val it = 5 : int Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 13 / 35

Currying Operations and Functions Example (Curried Addition) fun plus n (m:int) = n + m; val plus = fn : int > int > int plus 1 2; val it = 3 : int Example (Partial Application) val inc = plus 1; val inc = fn : int > int inc 7; val it = 8 : int Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 14 / 35

Higher-Order Functions Operations and Functions Example (Higher-Order Functions) fun foo f n = ( f (n+1)) div 2 ; val foo = fn : ( int > int) > int > int foo inc 3; val it = 2 : int Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 15 / 35

Recursive Functions Operations and Functions Example (Defining Recursive Functions) fun f n = if (n=0) then 1 else n f (n 1); val f = fn : int > int f 3; val it = 6 : int fun member x [] = false member x (h::t) = (x=h) orelse (member x t); val member = fn : a > a list > bool member 3 [1,2,3,4]; val it = true : bool Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 16 / 35

Lambda Abstractions Operations and Functions Example (The Increment Function) fn x=> x + 1; val it = fn : int > int (fn x=> x + 1) 2; val it = 3 : int Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 17 / 35

Lambda Abstractions Operations and Functions Example (Curried Multiplication) fn x=> fn y=> x (y: int ); val it = fn : int > int > int val double = (fn x=> fn y=> x (y: int )) 2; val double = fn : int > int double 22; val it = 44 : int Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 18 / 35

Clausal Definitions Operations and Functions Example (Fibonacci) fun fib 0 = 1 fib 1 = 1 fib n = fib (n 1) + fib(n 2); Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 19 / 35

Exceptions Operations and Functions Example (Failure) hd []; uncaught exception Hd 1 div 0; uncaught exception Div Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 20 / 35

User-Defined Exceptions Operations and Functions Example (Explicitly Generating Failure) exception negative_argument_to_fact; exception negative_argument_to_fact fun fact n = if (n<0) then raise negative_argument_to_fact else if (n=0) then 1 else n fact (n 1); val fact = fn : int > int fact (~1); uncaught exception negative_argument_to_fact Example (Exception Handling) (fact(~1)) handle negative_argument_to_fact => 0; val it = 0 : int Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 21 / 35

Unit Standard Data Types Example (Unit) (); val it = () : unit close_theory; val it = fn : unit > unit Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 22 / 35

Character Strings Standard Data Types Example (String Operations) "abc"; val it = "abc" : string chr; val it = fn : int > string chr 97; val it = "a" : string Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 23 / 35

List Constructors Standard Data Types Example (Empty Lists) null l ; val it = false : bool null []; val it = true : bool Example (Construction and Concatenation) 9 :: l ; val it = [9,2,3,5] : int list [true, false ] @ [false,true ]; val it = [true, false, false,true] : bool list Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 24 / 35

List Operations Standard Data Types Example (Head and Tail) val l = [2,3,2+3]; val l = [2,3,5] : int list hd l; val it = 2 : int tl l ; val it = [3,5] : int list Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 25 / 35

Pattern Matching Standard Data Types Example (Pattern Matching and Lists) fun bigand [] = true bigand (h:: t ) = h andalso bigand t; val bigand = fn : bool list > bool Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 26 / 35

Pairs Standard Data Types Example (Pair Functions) val p = (2,3); val p = (2,3) : int int fst p; val it = 2 : int snd p; val it = 3 : int Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 27 / 35

Records Standard Data Types Example (Date Record) val date = {day=4,month="february",year=1967} : {day: int, month:string, year: int } val {day=d,month=m,year=y} = date; val d = 4 : int val m = "february" : string val y = 1967 : int #month date; val it = "february" : string Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 28 / 35

Polymorphism Polymorphism and Type Inference Example (Head Function) hd [2,3]; val it = 2 : int hd [true, false ]; val it = true : bool Problem What is the type of hd? int list > int or bool list > bool Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 29 / 35

Polymorphism Polymorphism and Type Inference Example (Type of Head Function) hd; val it = fn : a list > a Example (Polymorphic Head Function) head function has both types a is a type variable. hd can have any type of the form σ list > σ (where σ is an SML type) Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 30 / 35

Type Inference Polymorphism and Type Inference Example (Mapping Function) fun map f l = if ( null l ) then [] else f (hd l )::( map f ( tl l )); val map = fn : ( a > b) > a list > b list map (fn x=>0); val it = fn : a list > int list Fact (ML Type Inference) SML infers the most general type. Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 31 / 35

Standard List Operations Polymorphism and Type Inference Example (Mapping) fun map f [] = [] map f (h:: t ) = f h :: map f t ; val ( a, b) map = fn : ( a > b) > a list > b list Example (Filtering) fun filter P [] = [] filter P (h:: t ) = if P h then h:: filter P t else filter P t ; val a filter = fn : ( a > bool) > a list > a list Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 32 / 35

Type Inference Polymorphism and Type Inference Example (Function Composition) fun comp f g x = f(g x); val comp = fn:( a > b) > ( c > a) > c > b comp null (map (fn y=> y+1)); val it = fn : int list > bool Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 33 / 35

Some System Functions Miscellaneous Example (Load a file called file.sml) use; val it = fn : string > unit use " file.sml"; [opening file.sml]... Key Commands terminate the session: <Ctrl> D interrupt: <Ctrl> C Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 34 / 35

Summary Summary functional programming in Standard ML evaluation and bindings defining Functions standard data types type inference Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Technology A Brief Introduction Group Fachbereich to StandardInformatik ML Technische Universität Sommersemester Kaiserslautern) 2008 35 / 35