Outline Functions on Lists Amtoft from Hatcliff from Leavens Inductive Definitions Specifications Derivations Recursive Functions A recursive function Patterns Typical Templates follows the structure Map of inductively-defined data. Filter Fold Representing Sets Equality Types With lists as our example, we shall study Assocation Lists 1. inductive definitions (to specify data) 2. recursive functions (to process data) 3. frequent function templates
Specifying Types/Sets Functions on Lists Amtoft from Hatcliff from Leavens Extensional Inductive Definitions { n | n is a multiple of 3 } Specifications Derivations Recursive Functions { p | p has red hair } Patterns Typical Templates ◮ defined by giving characteristics Map Filter ◮ no info about how to generate elements Fold Representing Sets Equality Types Intensional Let S be the smallest set of natural numbers Assocation Lists satisfying 1. 0 ∈ S , 2. x + 3 ∈ S whenever x ∈ S . ◮ defined inductively ◮ describes how to generate elements
Why require smallest solution? Functions on Lists Amtoft from Hatcliff from Leavens Let S be a set of natural numbers satisfying Inductive Definitions Specifications 1. 0 ∈ S , Derivations Recursive Functions 2. x + 3 ∈ S whenever x ∈ S . Patterns Typical Templates Which sets satisfy this specification? Map Filter ◮ { 0,3,6,9, . . . } Fold Representing Sets ◮ { 0,1,3,4,6,7,9,10, . . . } Equality Types Assocation Lists ◮ ... By choosing the smallest solution, we ◮ get exactly those elements explicitly generated by the specification ◮ we can give a derivation showing why each element belongs in the set.
Derivation of Set Elements Functions on Lists Amtoft from Hatcliff from Leavens Let S be the smallest set of natural numbers satisfying Inductive Definitions Specifications 1. 0 ∈ S , Derivations Recursive Functions 2. x + 3 ∈ S whenever x ∈ S . Patterns Typical Templates Example: Map Filter ◮ 0 ∈ S (by rule 1) Fold Representing Sets ◮ 3 ∈ S (by rule 2) Equality Types Assocation Lists ◮ 6 ∈ S (by rule 2) ◮ 9 ∈ S (by rule 2) Non-example: ◮ 10 Letting set be defined as the smallest gives us constructive information about the set.
BNF Inductive Specifications Functions on Lists Amtoft from Hatcliff from Leavens Integer lists: Inductive Definitions < int − l i s t > ::= n i l | < int > : : < int − l i s t > Specifications Derivations Example: Recursive Functions Patterns Typical Templates 1 :: 2 :: 3 :: nil ≡ [1, 2, 3] Map Filter Fold Derivation: Representing Sets Equality Types Assocation Lists nil is an <int-list> (by rule 1) ⇒ 3 :: nil is an <int-list> (by rule 2) ⇒ 2 :: 3 :: nil is an <int-list> (by rule 2) ⇒ 1 :: 2 :: 3 :: nil is an <int-list> (by rule 2) Note: ◮ recursion in grammar ◮ each use of :: increases list length by 1
Approximating Recursion Functions on Lists Amtoft from Hatcliff Grammar: from Leavens < int − l i s t > ::= n i l | < int > : : < int − l i s t > Inductive Definitions Specifications We write a family of functions list sum i , with i the Derivations length of the argument: Recursive Functions Patterns Typical Templates l i s t s u m 0 ( l s ) = 0; Map fun Filter Fold l i s t s u m 1 ( l s ) = Representing Sets fun Equality Types hd ( l s ) + l i s t s u m 0 ( t l ( l s ) ) ; Assocation Lists fun l i s t s u m 2 ( l s ) = hd ( l s ) + l i s t s u m 1 ( t l ( l s ) ) ; fun l i s t s u m 3 ( l s ) = hd ( l s ) + l i s t s u m 2 ( t l ( l s ) ) ; . . . − l i s t s u m 3 ( [ 1 , 2 , 3 ] ) ; val i t = 6 : i n t
Putting It Together Functions on Lists Amtoft from Hatcliff We had from Leavens Inductive Definitions fun l i s t s u m 0 ( l s ) = 0; Specifications Derivations Recursive Functions fun l i s t s u m 1 ( l s ) = Patterns hd ( l s ) + l i s t s u m 0 ( t l ( l s ) ) ; Typical Templates Map Filter Fold fun l i s t s u m 2 ( l s ) = Representing Sets hd ( l s ) + l i s t s u m 1 ( t l ( l s ) ) ; Equality Types Assocation Lists fun l i s t s u m 3 ( l s ) = hd ( l s ) + l i s t s u m 2 ( t l ( l s ) ) ; . . . Recursive function: fun l i s t s u m ( l s ) = i f l s = n i l then 0 else hd ( l s ) + l i s t s u m ( t l ( l s ) ) ;
Using Patterns Functions on Lists Amtoft from Hatcliff For the grammar from Leavens Inductive Definitions < int − l i s t > ::= n i l | < int > : : < int − l i s t > Specifications Derivations we wrote Recursive Functions Patterns fun l i s t s u m ( l s ) = Typical Templates i f l s = n i l Map Filter then 0 Fold else hd ( l s ) + l i s t s u m ( t l ( l s ) ) ; Representing Sets Equality Types but the correspondence is clearer by the ML patterns Assocation Lists fun l i s t s u m ( l s ) = case l s of n i l = > 0 | (n : : ns ) = > n + l i s t s u m ( ns ) ; or even better l i s t s u m ( n i l ) = 0 fun | l i s t s u m (n : : ns ) = n + l i s t s u m ( ns ) ;
Recursion Template Functions on Lists Amtoft from Hatcliff from Leavens Data Structure directs Function Structure Inductive Definitions Specifications Derivations Recursive Functions Grammar: Patterns < int − l i s t > ::= n i l | < int > : : < int − l i s t > Typical Templates Map Filter Template: Fold Representing Sets fun l i s t r e c ( n i l ) = . . . . Equality Types Assocation Lists | l i s t r e c (n : : ns ) = . . . . l i s t r e c ( ns ) . . . . . ; Key points: ◮ for each case in BNF there is a case in function ◮ recursion occurs in function exactly where recursion occurs in BNF ◮ we may assume function “works” for sub-structures of the same type
More Examples Functions on Lists Amtoft from Hatcliff from Leavens Add one to each element of list: Inductive Definitions Specifications Derivations fun l i s t i n c ( n i l ) = n i l Recursive Functions | l i s t i n c (n : : ns ) = (n +1):: l i s t i n c ( ns ) ; Patterns Typical Templates Select those elements greater than five: Map Filter g t f i v e ( n i l ) = n i l fun Fold | g t f i v e (n : : ns ) = Representing Sets Equality Types n > 5 i f Assocation Lists then n : : g t f i v e ( ns ) g t f i v e ( ns ) ; else Append two lists: fun append ( n i l , l 2 ) = l 2 | append (n : : ns , l 2 ) = n : : append ( ns , l 2 ) ;
Map Functions on Lists Amtoft from Hatcliff from Leavens Adding one to each element of list: Inductive Definitions fun l i s t i n c ( n i l ) = n i l Specifications Derivations | l i s t i n c (n : : ns ) = (n +1):: l i s t i n c ( ns ) ; Recursive Functions Generalization: apply arbitrary function to each element Patterns Typical Templates fun l i s t m a p f n i l = n i l Map Filter | l i s t m a p f (n : : ns ) = Fold f (n) : : l i s t m a p f ns ; Representing Sets Equality Types Assocation Lists Type of list map : fn : (’a -> ’b) -> ’a list -> ’b list Instantiation: add one to each element m y l i s t i n c = l i s t m a p ( fn x = > x + 1 ) ; val Instantiation: square each element s q u a r e l i s t = l i s t m a p ( fn x = > x ∗ x ) ; val
Filter Functions on Lists Amtoft from Hatcliff Selecting only the elements greater than five: from Leavens g t f i v e ( n i l ) = n i l fun Inductive Definitions | g t f i v e (n : : ns ) = Specifications Derivations n > 5 then n : : g t f i v e ( ns ) i f Recursive Functions g t f i v e ( ns ) ; else Patterns Generalization: select using arbitrary predicate Typical Templates Map Filter fun l i s t f i l t e r p n i l = n i l Fold | l i s t f i l t e r p (n : : ns ) = Representing Sets i f p(n) then n : : l i s t f i l t e r p ns Equality Types Assocation Lists else l i s t f i l t e r p ns ; Type of list filter : (’a -> bool) -> ’a list -> ’a list Instantiation: select those greater than five m y g t f i v e = l i s t f i l t e r ( fn n = > n > 5 ) ; val Instantiation: select the even elements evens = l i s t f i l t e r ( fn n = > n mod 2 = 0 ) ; val
Foldr Functions on Lists Amtoft from Hatcliff “Folding” all elements up by adding them from Leavens fun l i s t s u m ( n i l ) = 0 Inductive Definitions | l i s t s u m (n : : ns ) = n + l i s t s u m ( ns ) ; Specifications Derivations Generalization: fold in arbitrary way Recursive Functions Patterns fun f o l d r f e n i l = e Typical Templates Map | f o l d r f e ( x : : xs ) = f ( x , ( f o l d r f e xs )) Filter Fold Type of foldr : Representing Sets Equality Types (’a * ’b -> ’b) -> ’b -> ’a list -> ’b Assocation Lists Instantiation: my addlist my a d dl i st xs = f o l d r op + 0 xs fun Instantiation: my identity m y i d e n t i t y xs = f o l d r op : : n i l xs fun Instantiation: my append fun my append xs ys = f o l d r op : : ys xs
Recommend
More recommend