Functional Programming

Similar documents
Specification and Verification in Higher Order Logic

Programming in Standard ML: Continued

A Brief Introduction to Standard ML

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

Functional Programming and Modeling

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

CSE 341 Section 5. Winter 2018

SML A F unctional Functional Language Language Lecture 19

Products and Records

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

Lists. Michael P. Fourman. February 2, 2010

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Chapter 3 Linear Structures: Lists

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

Chapter 3 Linear Structures: Lists

CMSC 330, Fall 2013, Practice Problems 3

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

A quick introduction to SML

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

Begin at the beginning

Type Checking and Type Inference

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

Handout 2 August 25, 2008

Datatype declarations

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

LECTURE 16. Functional Programming

Functional Programming

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

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

CSCI-GA Scripting Languages

Homework 3 COSE212, Fall 2018

Functional programming Primer I

CSCC24 Functional Programming Typing, Scope, Exceptions ML

CSE-321 Programming Languages 2010 Midterm

Data Types The ML Type System

Lecture 2: The Basis of SML

Introduction to SML Getting Started

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

SMURF Language Reference Manual Serial MUsic Represented as Functions

L3 Programming September 19, OCaml Cheatsheet

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

CMSC 330, Fall 2013, Practice Problem 3 Solutions

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

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

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.

Introduction to OCaml

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

A general introduction to Functional Programming using Haskell

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)

Principles of Functional Programming

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

CSE-321 Programming Languages 2012 Midterm

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

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

Introduction to Objective Caml

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

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

Programming Languages

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

Chapter 15. Functional Programming Languages

Isabelle s meta-logic. p.1

Programming Languages

Recap: Functions as first-class values

Types-2. Polymorphism

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

Functional Programming

F28PL1 Programming Languages. Lecture 11: Standard ML 1

Exercises on ML. Programming Languages. Chanseok Oh

15 150: Principles of Functional Programming. Exceptions

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

Overview. A Compact Introduction to Isabelle/HOL. Tobias Nipkow. System Architecture. Overview of Isabelle/HOL

Programming Paradigms

Programming Languages

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

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

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

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Overloading, Type Classes, and Algebraic Datatypes

COMP 105 Assignment: Hindley-Milner Type Inference

1.3. Conditional expressions To express case distinctions like

Modules and Representation Invariants

ITT8060: Advanced Programming (in F#)

Typed Racket: Racket with Static Types

The Substitution Model

Lecture Notes on Induction and Recursion

COS 320. Compiling Techniques

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

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

CS3110 Spring 2016 Lecture 6: Modules

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

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

Implementing nml: Hindley-Milner Type Inference

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

Typed Scheme: Scheme with Static Types

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

CS 457/557: Functional Languages

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

Inductive Data Types

Transcription:

Functional Programming A Brief Introduction to Standard ML Arnd Poetzsch-Heffter Software Technology Group Fachbereich Informatik Technische Universität Kaiserslautern Sommersemester 2010 Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 1 / 101

Outline I 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 3 Cases and Pattern Matching Tuples Case Analysis 4 Data Types Simple Data Types Recursive Data Types Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 2 / 101

Outline II Overview 5 Modules Structures Signatures Modules in Moscow ML 6 Implementing a Simple Theorem Prover Introduction Basic Data Structures Substitution and Unification 7 Summary Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 3 / 101

Functional Programming Overview Functional Programming Fact A functional program consists of function declarations data type declarations an expression Functional Programs do not have variables, assigments, statements, loops,... instead: let-expressions recursive functions higher-order functions Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 4 / 101

Functional Programming Overview Functional Programming Advantages clearer semantics corresponds more directly to abstract mathematical objects more freedom in implementation Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 5 / 101

Overview Standard ML The SML Programming Language Overview functional programming language interpreter and compiler available strongly typed, with: type inference abstract data types parametric polymorphism exception-handling mechanisms Motivation ML is similar to functional core of Isabelle/HOL specification language ML is the implementation language of the theorem prover Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 6 / 101

Outline I SML in Examples 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 3 Cases and Pattern Matching Tuples Case Analysis 4 Data Types Simple Data Types Recursive Data Types Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 7 / 101

Outline II SML in Examples 5 Modules Structures Signatures Modules in Moscow ML 6 Implementing a Simple Theorem Prover Introduction Basic Data Structures Substitution and Unification 7 Summary Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 8 / 101

Evaluation and Bindings SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 9 / 101

Bindings SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 10 / 101

Local Bindings SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 11 / 101

Booleans SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 12 / 101

Integers SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 13 / 101

Applying Functions SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 14 / 101

Defining Functions SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 15 / 101

Currying SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 16 / 101

Higher-Order Functions SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 17 / 101

Recursive Functions SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 18 / 101

Lambda Abstractions SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 19 / 101

Lambda Abstractions SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 20 / 101

Clausal Definitions SML in Examples Operations and Functions Example (Fibonacci) fun fib 0 = 1 fib 1 = 1 fib n = fib (n 1) + fib(n 2); Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 21 / 101

Exceptions SML in Examples Operations and Functions Example (Failure) hd []; uncaught exception Hd 1 div 0; uncaught exception Div Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 22 / 101

User-Defined Exceptions SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 23 / 101

Unit SML in Examples Standard Data Types Example (Unit) (); val it = () : unit close_theory; val it = fn : unit > unit Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 24 / 101

Character Strings SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 25 / 101

List Constructors SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 26 / 101

List Operations SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 27 / 101

Pattern Matching SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 28 / 101

Pairs SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 29 / 101

Records SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 30 / 101

Polymorphism SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 31 / 101

Polymorphism SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 32 / 101

Type Inference SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 33 / 101

Standard List Operations SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 34 / 101

Type Inference SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 35 / 101

Some System Functions SML in Examples 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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 36 / 101

Outline I Cases and Pattern Matching 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 3 Cases and Pattern Matching Tuples Case Analysis 4 Data Types Simple Data Types Recursive Data Types Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 37 / 101

Outline II Cases and Pattern Matching 5 Modules Structures Signatures Modules in Moscow ML 6 Implementing a Simple Theorem Prover Introduction Basic Data Structures Substitution and Unification 7 Summary Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 38 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 39 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 40 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 41 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 42 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 43 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 44 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 45 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 46 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 47 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 48 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 49 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 50 / 101

Outline I Data Types 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 3 Cases and Pattern Matching Tuples Case Analysis 4 Data Types Simple Data Types Recursive Data Types Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 51 / 101

Outline II Data Types 5 Modules Structures Signatures Modules in Moscow ML 6 Implementing a Simple Theorem Prover Introduction Basic Data Structures Substitution and Unification 7 Summary Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 52 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 53 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 54 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 55 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 56 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 57 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 58 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 59 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 60 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 61 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 62 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 63 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 64 / 101

Outline I Modules 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 3 Cases and Pattern Matching Tuples Case Analysis 4 Data Types Simple Data Types Recursive Data Types Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 65 / 101

Outline II Modules 5 Modules Structures Signatures Modules in Moscow ML 6 Implementing a Simple Theorem Prover Introduction Basic Data Structures Substitution and Unification 7 Summary Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 66 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 67 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 68 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 69 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 70 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 71 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 72 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 73 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 74 / 101

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 ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 75 / 101

Outline I Implementing a Theorem Prover 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 3 Cases and Pattern Matching Tuples Case Analysis 4 Data Types Simple Data Types Recursive Data Types Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 76 / 101

Outline II Implementing a Theorem Prover 5 Modules Structures Signatures Modules in Moscow ML 6 Implementing a Simple Theorem Prover Introduction Basic Data Structures Substitution and Unification 7 Summary Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 77 / 101

Overview Implementing a Theorem Prover Introduction Theorem Prover theorem prover implements a proof system used for proof checking and automated theorem proving Goals build your own theorem prover for propositional logic understanding the fundamental structure of a theorem prover Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 78 / 101

Data Types Implementing a Theorem Prover Basic Data Structures Data Types of a Theorem Prover formulas, terms and types axioms and theorems deduction rules proofs Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 79 / 101

Implementing a Theorem Prover Formulas, Terms and Types Basic Data Structures Propositional Logic each term is a formula each term has the type B Data Type Definition datatype Term = Variable of string Constant of bool Negation of Term Conjunction of Term Term Disjunction of Term Term Implication of Term Term; Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 80 / 101

Implementing a Theorem Prover Syntactical Operations on Terms Basic Data Structures Determining the Topmost Operator fun isvar (Variable x) = true isvar _ = false ; fun isconst (Constant b) = true isconst _ = false ; fun isneg (Negation t1) = true isneg _ = false ; fun iscon (Conjunction (t1, t2 )) = true iscon _ = false ; fun isdis (Disjunction (t1, t2 )) = true isdis _ = false ; fun isimp ( Implication (t1,t2 )) = true isimp _ = false ; Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 81 / 101

Implementing a Theorem Prover Syntactical Operations on Terms Basic Data Structures Composition combine several subterms with an operator to a new one Composition of Terms fun mkvar s1 = Variable s1; fun mkconst b1 = Constant b1; fun mkneg t1 = Negation t1; fun mkcon (t1,t2) = Conjunction(t1,t2 ); fun mkdis (t1,t2) = Disjunction(t1, t2 ); fun mkimp (t1,t2) = Implication (t1,t2 ); Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 82 / 101

Implementing a Theorem Prover Syntactical Operations on Terms Basic Data Structures Decomposition decompose a term Decomposition of Terms exception SyntaxError; fun destneg (Negation t1) = t1 destneg _ = raise SyntaxError ; fun destcon (Conjunction (t1,t2)) = (t1, t2) destcon _ = raise SyntaxError ; fun destdis (Disjunction (t1,t2 )) = (t1,t2) destdis _ = raise SyntaxError ; fun destimp (Implication (t1,t2 )) = (t1,t2) destimp _ = raise SyntaxError ; Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 83 / 101

Term Examples Implementing a Theorem Prover Basic Data Structures Example (Terms) t 1 = a b c; t 2 = true (x y) z; t 3 = ((a b) c) val t1 = Disjunction(Conjunction(Variable "a",variable "b" ), Negation(Variable "c") ); val t2 = Disjunction( Conjunction( Constant true, Conjunction (Variable "x",variable "y" )), Negation(Variable "z" )); val t3 = Negation(Conjunction( Disjunction(Variable "a",variable "b" ), Negation(Variable "c" ))); Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 84 / 101

Theorems Implementing a Theorem Prover Basic Data Structures Data Type Definition datatype Theorem = Theorem of Term list Term; Syntactical Operations fun assumptions (Theorem (assums,concl)) = assums; fun conclusion (Theorem (assums,concl)) = concl; fun mktheorem(assums,concl) = Theorem(assums,concl); fun desttheorem (Theorem (assums,concl)) = (assums,concl); Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 85 / 101

Rules Implementing a Theorem Prover Basic Data Structures Data Type Definition datatype Rule = Rule of Theorem list Theorem; Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 86 / 101

Application of Rules Implementing a Theorem Prover Basic Data Structures Application of Rules form a new theorem from several other theorems Application (Version 1) exception DeductionError; fun applyrule rule thms = let val Rule (prem,concl) = rule in if prem=thms then concl else raise DeductionError end; Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 87 / 101

Application of Rules Implementing a Theorem Prover Basic Data Structures Application of Rules premises and given theorems do not need to be identical premises only need to be in the given theorems Application (Version 2) fun mem x [] = false mem x (h::t) = (x=h) orelse (contains t x); fun sublist [] l2 = true sublist (h1:: t1) l2 = (contains l2 h1) andalso (sublist t1 l2 ); fun applyrule rule thms = let val Rule (prem,concl) = rule in if sublist prem thms then concl else raise DeductionError end; Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 88 / 101

Application of Rules Implementing a Theorem Prover Basic Data Structures Example (Rule Application) val axiom1 = Theorem( [], (Variable "a" )); val axiom2 = Theorem( [], Implication((Variable "a" ),( Variable "b" ))); val axiom3 = Theorem( [], Implication((Variable "b" ),( Variable "c" ))); val modusponens = Rule( [Theorem( [], Implication ((Variable "a" ),( Variable "b")) ), Theorem( [], (Variable "a") )], Theorem( [], (Variable "b") ) ); Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 89 / 101

Application of Rules Implementing a Theorem Prover Basic Data Structures Example (Rule Application) val thm1 = applyrule modusponens [axiom1,axiom2]; val thm2 = applyrule modusponens [thm1,axiom3]; Problem axioms and rules should work for arbitrary variables axiom scheme, rule scheme definition of substitution and unification needed Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 90 / 101

Support Functions Implementing a Theorem Prover Substitution and Unification Support Functions fun insert x l = if mem x l then l else x :: l ; fun assoc [] a = NONE assoc ((x,y ):: t ) a = if (x=a) then SOME y else assoc t a; fun occurs v (w as Variable _) = (v=w) occurs v (Constant b) = false occurs v (Negation t) = occurs v t occurs v (Conjunction (t1,t2 )) = occurs v t1 orelse occurs v t2 occurs v (Disjunction (t1,t2 )) = occurs v t1 orelse occurs v t2 occurs v ( Implication (t1,t2 )) = occurs v t1 orelse occurs v t2; Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 91 / 101

Substitution Implementing a Theorem Prover Substitution and Unification Substitution fun subst theta (v as Variable _) = (case assoc theta v of NONE => v SOME w => w) subst theta (Constant b) = Constant b subst theta (Negation t) = Negation(subst theta t) subst theta (Conjunction (t1, t2 )) = Conjunction(subst theta t1, subst theta t2) subst theta (Disjunction (t1, t2 )) = Disjunction(subst theta t1, subst theta t2) subst theta ( Implication (t1, t2 )) = Implication (subst theta t1, subst theta t2 ); Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 92 / 101

Substitution Implementing a Theorem Prover Substitution and Unification Example (Substitution) val theta1 = [( Variable "a",variable "b" ),( Variable "b",constant true )]; Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 93 / 101

Unification Implementing a Theorem Prover Substitution and Unification Definition (Matching) A term matches another if the latter can be obtained by instantiating the former. matches(m,n) θ.subst(θ,m) = N Definition (Unifier, Unifiability) A substitution is a unifier of two terms, if it makes them equal. unifier(θ,m,n) subst(θ,m) = subst(θ,n) Two terms are unifiable if they have a unifier. unifiable(m,n) θ.unifier(θ,m,n) Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 94 / 101

Unification Algorithm Implementing a Theorem Prover Substitution and Unification General Idea traverse two terms in exactly the same way eliminating as much common structure as possible things actually happen when a variable is encountered (in either term) when a variable is encoutered, make a binding with the corresponding subterm in the other term, and substitute through important: making a binding (x,m) where x occurs in M must be disallowed since the resulting substitution will not be a unifier Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 95 / 101

Unification Algorithm Implementing a Theorem Prover Substitution and Unification Unification exception UnificationException; fun unifyl [] [] theta = theta unifyl ((v as Variable _ ):: L) (M::R) theta = if v=m then unifyl L R theta else if occurs v M then raise UnificationException else unifyl (map (subst [(v,m)]) L) (map (subst [(v,m)]) R) (combinesubst [(v,m)] theta) unifyl L1 (L2 as (Variable _::_)) theta = unifyl L2 L1 theta... Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 96 / 101

Unification Algorithm Implementing a Theorem Prover Substitution and Unification Unification... unifyl (Negation tl :: L) (Negation tr :: R) theta = unifyl ( tl :: L) ( tr :: R) theta unifyl (Conjunction ( tl1, tl2 ):: L) (Conjunction (tr1, tr2 ):: R) theta = unifyl ( tl1 :: tl2 :: L) ( tr1 :: tr2 :: R) theta unifyl (Disjunction ( tl1, tl2 ):: L) (Disjunction ( tr1, tr2 ):: R) theta = unifyl ( tl1 :: tl2 :: L) ( tr1 :: tr2 :: R) theta unifyl ( Implication ( tl1, tl2 ):: L) ( Implication ( tr1, tr2 ):: R) theta = unifyl ( tl1 :: tl2 :: L) ( tr1 :: tr2 :: R) theta unifyl _ = raise UnificationException; fun unify M N = unifyl [M] [N] []; Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 97 / 101

Implementing a Theorem Prover Combining Substitutions Substitution and Unification Combining Substitutions fun combinesubst theta sigma = let val (dsigma,rsigma) = ListPair.unzip sigma val sigma1 = ListPair. zip(dsigma,(map (subst theta) rsigma)) val sigma2 = List. filter (op <>) sigma1 val theta1 = List. filter (fn (a,_) => not (mem a dsigma)) theta in sigma2 @ theta1 end; Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 98 / 101

Outline I Summary 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 3 Cases and Pattern Matching Tuples Case Analysis 4 Data Types Simple Data Types Recursive Data Types Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 99 / 101

Outline II Summary 5 Modules Structures Signatures Modules in Moscow ML 6 Implementing a Simple Theorem Prover Introduction Basic Data Structures Substitution and Unification 7 Summary Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 100 / 101

Summary Summary Summary programming in Standard ML evaluation and bindings defining functions standard data types type inference case analysis and pattern matching data type definitions modules primitive theorem prover kernel terms theorems rules substitution unification Arnd Poetzsch-Heffter ( Software Technology Group Fachbereich Functional InformatikProgramming Technische Universität Kaiserslautern) Sommersemester 2010 101 / 101