Computer Science 538 Lecture Notes 4-12-00 Type Inference in SML (cont) How does it work with patterns? Fun Leng [] = 0 | Leng (a::b) = 1 + Leng b; Need to determine the type of Leng, a, and b. First each item is given a separate type identifier T(Leng) => ‘x -> ‘y refined to ‘z List -> int because Leng[] and returns zero T(a) => ‘v => ‘z T(b) => ‘w => ‘z list because the cons operator both are the same type Finding errors with type inference. f() + f(1) Type of F: ‘a -> ‘b ‘a = unit from f() and ‘a = int from f(1) so this produces an error Where type inference fails Fun F x = x x; This can not be type infered. x = ‘a = ‘b ->c which changes to ‘a = ‘a -> ‘c which can not be solved Fun f(g) = (g(3), g(true)) Type of g = ? You have a function that takes some types but not all types. This example will take int or bool You can get a type error if you give something other than a int or bool Solution: Fun f(g) = (g(I (3)), g(b(bool))) T(g) = T -> ‘b Prolog - Programming in Logic, 1972 by Kowalski and Comeraver Conventinal programming model "Algorithms + Data structures = Programs". Prolog: "Algorithms = Logic + Control" , Logic is esscential and control is an efficiency issue. Example prolog program. Is a list in order? iNORDER ([]). is the empty list in order: yes iNORDER ([_]). is a single element in order: yes iNORDER ([a, b | c] :- (a =< b, iNORDER([b|c]))). :- equals if, | = the reminder of the list Terminology in prolog Elementary data objects = Integers and Atoms Integer is a numeric literal: 1 2 3 ~17 Atoms are identifiers that begin with lowercase Prolog data objects = Terms to program, we define relations among Terms. Ex: iNORDER([]) A Predicate names a relation. Ex: iNORDER To define a relation using a predicate we write clauses made up of either a fact or rule.