Programming in Standard ML: Continued

Similar documents
Functional Programming

Specification and Verification in Higher Order Logic

A Brief Introduction to Standard ML

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

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

Principles of Functional Programming

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

Programming Languages

Functional Programming and Modeling

Programming Languages

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

Programming Languages

Recap: Functions as first-class values

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

CSCI-GA Scripting Languages

Handout 2 August 25, 2008

Begin at the beginning

Lists. Michael P. Fourman. February 2, 2010

CMSC 330, Fall 2013, Practice Problems 3

CSE 130 [Winter 2014] Programming Languages

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

All the operations used in the expression are over integers. a takes a pair as argument. (a pair is also a tuple, or more specifically, a 2-tuple)

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

CSE 130, Fall 2006: Final Examination

Datatype declarations

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

Programming Languages

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

CSE 341: Programming Languages

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

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

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

Chapter 3 Linear Structures: Lists

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

Chapter 3 Linear Structures: Lists

CS 11 Ocaml track: lecture 2

Programming Languages

Metaprogramming assignment 3

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

Introduction to SML Getting Started

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

Programming Languages

CS 11 Haskell track: lecture 1

Products and Records

Standard ML. Data types. ML Datatypes.1

Why Standard ML? A language particularly suited to compiler implementation.

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

SML A F unctional Functional Language Language Lecture 19

Types and Programming Languages. Lecture 5. Extensions of simple types

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

CSE 341 Section 5. Winter 2018

Introduction to Objective Caml

Why OCaml? Introduction to Objective Caml. Features of OCaml. Applications written in OCaml. Stefan Wehr

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Which of the following expressions has the type int list?

Variables and Bindings

Lecture 2: The Basis of SML

CSE 130 Programming Languages. Lecture 3: Datatypes. Ranjit Jhala UC San Diego

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

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

An introduction introduction to functional functional programming programming using usin Haskell

Lecture 3: Recursion; Structural Induction

So what does studying PL buy me?

Functional programming Primer I

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

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

Recap: ML s Holy Trinity

CSC324 Functional Programming Typing, Exceptions in ML

Any questions. Say hello to OCaml. Say hello to OCaml. Why readability matters. History, Variants. Plan (next 4 weeks)

CS 11 Ocaml track: lecture 4. Today: modules

CS3110 Spring 2016 Lecture 6: Modules

Programming Languages and Compilers (CS 421)

Introduction to OCaml

Some Advanced ML Features

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

CSE 130, Fall 2005: Final Examination

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

Generic polymorphism on steroids

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

Programming Languages and Techniques (CIS120)

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

LECTURE 16. Functional Programming

Verified Characteristic Formulae for CakeML. Armaël Guéneau, Magnus O. Myreen, Ramana Kumar, Michael Norrish April 27, 2017

ITT8060: Advanced Programming (in F#)

Sample Exam; Solutions

15 150: Principles of Functional Programming. Exceptions

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

CSCC24 Functional Programming Typing, Scope, Exceptions ML

Two approaches to writing interfaces

CMSC 330: Organization of Programming Languages

Programming Languages and Techniques (CIS120)

Functional Programming

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

Cornell University 12 Oct Solutions. (a) [9 pts] For each of the 3 functions below, pick an appropriate type for it from the list below.

A quick introduction to SML

Lists. Prof. Clarkson Fall Today s music: "Blank Space" by Taylor Swift

SMURF Language Reference Manual Serial MUsic Represented as Functions

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

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

Transcription:

Programming in Standard ML: Continued 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 Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 1 / 41

Outline Overview 1 Overview 2 Cases and Pattern Matching Tuples Case Analysis 3 Data Types Simple Data Types Recursive Data Types 4 Modules Structures Signatures Modules in Moscow ML 5 Summary Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 2 / 41

Outline Overview Outline Previous Lecture: Fundamentals built-in data types functions type inference exceptions This Lecture: Deeper Insight pattern matching case analysis data type definitions modules Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 3 / 41

Outline Cases and Pattern Matching 1 Overview 2 Cases and Pattern Matching Tuples Case Analysis 3 Data Types Simple Data Types Recursive Data Types 4 Modules Structures Signatures Modules in Moscow ML 5 Summary Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 4 / 41

Tuples Cases and Pattern Matching Tuples Example (Tuples) val pair = (2,3); > val pair = (2, 3) : int int val triple = (2,2.0, "2" ); > val triple = (2, 2.0, "2") : int real string val pairs_of_pairs = ((2,3),(2.0,3.0)); > val pairs_of_pairs = ((2, 3), (2.0, 3.0)) : ( int int ) ( real real ) Example (Unit Type) val null_tuple = (); > val null_tuple = () : unit Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 5 / 41

Cases and Pattern Matching Accessing Components Tuples Example (Navigating to the Position) val xy1 = #1 pairs_of_pairs; > val xy1 = (2, 3) : int int val y1 = #2 (#1 pairs_of_pairs ); > val y1 = 3 : int Example (Using Pattern Matching) val ((x1,y1 ),( x2,y2)) = pairs_of_pairs; > val x1 = 2 : int val y1 = 3 : int val x2 = 2.0 : real val y2 = 3.0 : real Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 6 / 41

Pattern Matching Cases and Pattern Matching Tuples Example (Granularity) val ((x1,y1),xy2) = pairs_of_pairs; > val x1 = 2 : int val y1 = 3 : int val xy2 = (2.0, 3.0) : real real Example (Wildcard Pattern) val ((_,y1 ),( _,_)) = pairs_of_pairs; > val y1 = 3 : int val ((_,y1),_) = pairs_of_pairs; > val y1 = 3 : int Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 7 / 41

Pattern Matching Cases and Pattern Matching Tuples Example (Value Patterns) val 0 = 1 1; val (0,x) = (1 1,34); > val x = 34 : int val (0,x) = (2 1,34);! Uncaught exception:! Bind Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 8 / 41

Binding Values Cases and Pattern Matching Tuples General Rules The variable binding val var = val is irreducible. The wildcard binding val _ = val is discarded. The tuple binding val(pat1,..., patn) = (val1,..., valn ) is reduced to val pat1 = valn... val patn = valn Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 9 / 41

Cases and Pattern Matching Clausal Function Expressions Case Analysis Example (Clausal Function Expressions) fun not true = false not false = true; > val not = fn : bool > bool Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 10 / 41

Redundant Cases Cases and Pattern Matching Case Analysis Example (Redundant Cases) fun not True = false not False = true;! Warning: some cases are unused in this match. > val a not = fn : a > bool not false; > val it = false : bool not 3; > val it = false : bool Fact (Redundant Cases) Redundant cases are always a mistake! Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 11 / 41

Inexhaustive Matches Cases and Pattern Matching Case Analysis Example (Inexhaustive Matches) fun first_ten 0 = true first_ten 1 = true first_ten 2 = true first_ten 3 = true first_ten 4 = true first_ten 5 = true first_ten 6 = true first_ten 7 = true first_ten 8 = true first_ten 9 = true;! Warning: pattern matching is not exhaustive > val first_ten = fn : int > bool first_ten 5; > val it = true : bool first_ten ~1;! Uncaught exception: Match Fact (Inexhaustive Matches) Inexhaustive matches may be a problem. Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 12 / 41

Catch-All Clauses Cases and Pattern Matching Case Analysis Example (Catch-All Clauses) fun first_ten 0 = true first_ten 1 = true first_ten 2 = true first_ten 3 = true first_ten 4 = true first_ten 5 = true first_ten 6 = true first_ten 7 = true first_ten 8 = true first_ten 9 = true first_ten _ = false ; > val first_ten = fn : int > bool Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 13 / 41

Overlapping Cases Cases and Pattern Matching Case Analysis Example (Overlapping Cases) fun foo1 1 _ = 1 foo1 _ 1 = 2 foo1 = 0; > val foo1 = fn : int > int > int fun foo2 _ 1 = 1 foo2 1 _ = 2 foo2 = 0; > val foo2 = fn : int > int > int foo1 1 1; > val it = 1 : int foo2 1 1; > val it = 1 : int Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 14 / 41

Cases and Pattern Matching Recursively Defined Functions Case Analysis Example (Recursively Defined Function) fun factorial 0 = 1 factorial n = n factorial (n 1); > val factorial = fn : int > int val rec factorial = fn Example (Recursively Defined Lambda Abstraction) val rec factorial = fn 0 => 1 n => n factorial (n 1); Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 15 / 41

Mutual Recursion Cases and Pattern Matching Case Analysis Example (Mutual Recursion) fun even 0 = true even n = odd (n 1) and odd 0 = false odd n = even (n 1); > val even = fn : int > bool val odd = fn : int > bool (even 5,odd 5); > val it = ( false, true) : bool bool Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 16 / 41

Outline Data Types 1 Overview 2 Cases and Pattern Matching Tuples Case Analysis 3 Data Types Simple Data Types Recursive Data Types 4 Modules Structures Signatures Modules in Moscow ML 5 Summary Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 17 / 41

Type Abbreviations Data Types Simple Data Types type Keyword type abbreviations record definitions Example (Type Abbreviation) type boolpair = bool bool; > type boolpair = bool bool (true,true ): boolpair; > val it = (true, true) : bool bool Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 18 / 41

Defining a Record Type Data Types Simple Data Types Example (Record) type hyperlink = { protocol : string, address : string, display : string }; > type hyperlink = {address : string, display : string, protocol : string } val hol_webpage = { protocol="http", address="rsg.informatik.uni kl.de/teaching/hol", display="hol Course" }; > val hol_webpage = { address = "rsg. informatik. uni kl.de/teaching/hol", display = "HOL Course", protocol = "http"} :{ address : string, display : string, protocol : string } Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 19 / 41

Data Types Accessing Record Components Simple Data Types Example (Type Abbreviation) val {protocol=p, display=d, address=a } = hol_webpage; > val p = "http" : string val d = "HOL Course" : string val a = "rsg. informatik. uni kl.de/teaching/hol" : string val {protocol=_, display=_, address=a } = hol_webpage; > val a = "rsg. informatik. uni kl.de/teaching/hol" : string val {address=a,...} = hol_webpage; > val a = "rsg. informatik. uni kl.de/teaching/hol" : string val {address,...} = hol_webpage; > val address = "rsg. informatik. uni kl.de/teaching/hol" : string Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 20 / 41

Data Types Defining Really New Data Types Simple Data Types datatype Keyword programmer-defined (recursive) data types, introduces one or more new type constructors one or more new value constructors Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 21 / 41

Non-Recursive Data Type Data Types Simple Data Types Example (Non-Recursive Datatype) datatype threeval = TT UU FF; > New type names: =threeval datatype threeval = (threeval,{ con FF : threeval, con TT : threeval, con UU : threeval}) con FF = FF : threeval con TT = TT : threeval con UU = UU : threeval fun not3 TT = FF not3 UU = UU not3 FF = TT; > val not3 = fn : threeval > threeval not3 TT; > val it = FF : threeval Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 22 / 41

Data Types Simple Data Types Parameterised Non-Recursive Data Types Example (Option Type) datatype a option = NONE SOME of a; > New type names: =option datatype a option = ( a option,{ con a NONE : a option, con a SOME : a > a option}) con a NONE = NONE : a option con a SOME = fn : a > a option constant NONE values of the form SOME v (where v has the type a) Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 23 / 41

Option Types Data Types Simple Data Types Example (Option Type) fun reciprocal 0.0 = NONE reciprocal x = SOME (1.0/x) > val reciprocal = fn : real > real option fun inv_reciprocal NONE = 0.0 inv_reciprocal (SOME x) = 1.0/x; > val inv_reciprocal = fn : real option > real fun identity x = inv_reciprocal ( reciprocal x); > val identity = fn : real > real identity 42.0; > val it = 42.0 : real identity 0.0; > val it = 0.0 : real Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 24 / 41

Recursive Data Types Data Types Recursive Data Types Example (Binary Tree) datatype a tree = Empty Node of a tree a a tree ; > New type names: =tree datatype a tree = ( a tree, {con a Empty : a tree, con a Node : a tree a a tree > a tree}) con a Empty = Empty : a tree con a Node = fn : a tree a a tree > a tree Empty is an empty binary tree (Node (t 1,v,t 2 ) is a tree if t 1 and t 2 are trees and v has the type a nothing else is a binary tree Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 25 / 41

Data Types Recursive Data Types Functions and Recursive Data Types Example (Binary Tree) fun treeheight Empty = 0 treeheight (Node (leftsubtree, _, rightsubtree)) = 1 + max(treeheight leftsubtree, treeheight rightsubtree); > val a treeheight = fn : a tree > int Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 26 / 41

Data Types Mutually Recursive Datatypes Recursive Data Types Example (Binary Tree) datatype a tree = Empty Node of a a forest and a forest = None Tree of a tree a forest ; > New type names: =forest, =tree... Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 27 / 41

Abstract Syntax Data Types Recursive Data Types Example (Defining Expressions) datatype expr = Numeral of int Plus of expr expr Times of expr expr; > New type names: =expr datatype expr = (expr, {con Numeral : int > expr, con Plus : expr expr > expr, con Times : expr expr > expr}) con Numeral = fn : int > expr con Plus = fn : expr expr > expr con Times = fn : expr expr > expr Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 28 / 41

Abstract Syntax Data Types Recursive Data Types Example (Evaluating Expressions) fun eval (Numeral n) = Numeral n eval (Plus(e1,e2)) = let val Numeral n1 = eval e1 val Numeral n2 = eval e2 in Numeral(n1+n2) end eval (Times (e1,e2)) = let val Numeral n1 = eval e1 val Numeral n2 = eval e2 in Numeral(n1 n2) end; > val eval = fn : expr > expr eval( Plus( Numeral 2, Times( Numeral 5, Numeral 8 ) ) ); > val it = Numeral 42 : expr Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 29 / 41

Outline Modules 1 Overview 2 Cases and Pattern Matching Tuples Case Analysis 3 Data Types Simple Data Types Recursive Data Types 4 Modules Structures Signatures Modules in Moscow ML 5 Summary Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 30 / 41

Structuring ML Programs Modules Structures Modules structuring programs into separate units program units in ML: structures contain a collection of types, exceptions and values (incl. functions) parameterised units possible composition of structures mediated by signatures Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 31 / 41

Structures Modules Structures Purpose structures = implementation Example (Structure) structure Queue = struct type a queue = a list a list val empty = (nil, nil ) fun insert ( x, (bs,fs )) = (x :: bs, fs) exception Empty fun remove (nil, nil ) = raise Empty remove (bs, f :: fs) = ( f, (bs,fs )) remove (bs, nil)= remove (nil, rev bs) end Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 32 / 41

Modules Structures Accessing Structure Components Identifier Scope components of a structure: local scope must be accessed by qualified names Example (Accessing Structure Components) Queue.empty; > val ( a, b) it = ([], []) : a list b list open Queue; >... empty; > val ( a, b) it = ([], []) : a list b list Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 33 / 41

Modules Structures Accessing Structure Components Usage of open open a structure to incorporate its bindings directly cannot open two structures with components that share a common names prefer to use open in let and local blocks Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 34 / 41

Signatures Modules Signatures Purpose signatures = interface Example (Signature) signature QUEUE = sig type a queue exception Empty val empty : a queue val insert : a a queue > a queue val remove: a queue > q a queue end Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 35 / 41

Signature Ascription Modules Signatures Transparent Ascription descriptive ascription extract principal signature always existing for well-formed structures most specific description everything needed for type checking source code needed Opaque Ascription restrictive ascription enforce data abstraction Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 36 / 41

Opaque Ascription Modules Signatures Example (Opaque Ascription) structure Queue :> QUEUE struct type a queue = a list a list val empty = (nil, nil ) fun insert ( x, (bs,fs )) = (x :: bs, fs) exception Empty fun remove (nil, nil ) = raise Empty remove (bs, f :: fs) = ( f, (bs,fs )) remove (bs, nil)= remove (nil, rev bs) end Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 37 / 41

Signature Matching Modules Signatures Conditions structure may provide more components structure may provide more general types than required structure may provide a concrete datatype instead of a type declarations in any order Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 38 / 41

Modules Modular Compilation in Moscow ML Modules in Moscow ML Compiler mosmlc save structure Foo to file Foo.sml compile module: mosmlc Foo.sml compiled interface in Foo.ui and compiled bytecode Foo.uo load module load "Foo.ui" load "Queue"; > val it = () : unit open Queue; > type a queue = a list a list val ( a, b) insert = fn : a ( a list b) > a list b exn Empty = Empty : exn val ( a, b) empty = ([], []) : a list b list val a remove = fn : a list a list > a ( a list a list ) Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 39 / 41

Outline Summary 1 Overview 2 Cases and Pattern Matching Tuples Case Analysis 3 Data Types Simple Data Types Recursive Data Types 4 Modules Structures Signatures Modules in Moscow ML 5 Summary Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 40 / 41

Summary Summary Summary programming in Standard ML case analysis and pattern matching data type definitions modules This is the end of the ML programming course. :-) Outlook: Next Week theorem-proving fundamentals theorem-prover principles and architecture Arnd Poetzsch-Heffter (Slides by Jens Brandt) ( Software Programming Technology in Group Standard Fachbereich ML: Continued Informatik Technische Universität Sommersemester Kaiserslautern) 2008 41 / 41