Introduction 3 Background functionals factor out common behaviour map applies a function to every element of a list fun map [ ] = [ ] map f ( x : : xs ) = f x : : map f xs filter keeps only those elements of a list for which a predicate holds fun f i l t e r [ ] = [ ] f i l t e r pred ( x : : xs ) = i f pred x then x : : f i l t e r pred xs e l s e f i l t e r pred xs ; 4 Developing a program that detects possible uses of functionals in Standard ML source code and converts them to their higher order equivalents Philipp Mayerhofer March 7, 2003 1 Structure 1. Introduction 2. Problem analysis 3. Design and implementation 4. Conclusion 2
Aim Program detects possible uses of the functionals map and filter in Standard ML source code and converts them to their higher order equivalents Example: Notation possible use of map is expressible by a function fun s q u a r e [ ] = [ ] s q u a r e ( x : : xs ) = ( x x ) : : s q u a r e xs higher order equivalent v a l s q u a r e = map ( fn x = x x ) 5 Motivation educational software engineering optimisation 6., if Definition 1 A function, denoted is h-expressible by a function, if. Definition 2 A function, denoted is a higher order equivalent of a function Definition 3 An expression if. 7 Notation Example fun i n c l [ ] = [ ] i n c l ( x : : xs ) = ( x + 1 ) : : i n c l xs ; x + 1 ) ; v a l i n c l 2 = map ( fn x = incl is expressible by map map (fn x => x + 1) incl incl is h-expressible by map higher order equivalent of incl. map map incl 8
Templates fun f... [ ]... = [ ] f... ( x : : xs )... = some o p e r a t i o n : : f xs Problems: more than one way of expressing same functionality fun f... l... = i f l = [ ] then [ ] e l s e some o p e r a t i o n : : f ( t l l ) need meta variables for some operation 11 Problematic language constructs Pattern matching fun f1 [ ] = [ ] f1 ( x : : xs ) = ( x + 1 ) : : f1 xs ; fun f2 l = case l of [ ] = ( x : : xs ) = [ ] ( x + 1 ) : : f2 xs ; ( Does n o t compile w i t h o u t warning ) fun f3 l = i f ( l = [ ] ) then [ ] e l s e l e t v a l ( x : : xs ) = l in ( x + 1 ) : : f3 xs end ; 12 Problem analysis 9 Determining expressibility cf. matching fingerprints (a) Original fingerprint (b) Skeletal image define templates for structure of functionals match functions to templates to determine expressibility 10
Problematic language constructs Pattern matching (cont.) Normal form: convert to set of conditions and assignments define two meta-functions constr returns the constructor tag of a given type field of a given type field n returns the Example: function on previous slide converted to fun f l = i f ( c o n s t r l = [ ] ) then [ ] e l s e l e t v a l x = f i e l d 1 l v a l xs = f i e l d 2 l in ( x + 1 ) : : f xs end ; 13 Problematic language constructs Multiple arguments tuples vs. curried arguments fun f1 ( [ ], ) = [ ] f1 ( x : : xs, n ) = ( x+n ) : : f1 ( xs, n ) ; fun f2 [ ] = [ ] f2 ( x : : xs ) n = ( x+n ) : : f2 xs n ; tuples = single entity that give effect of multiple arguments restrict templates to curried arguments 14 Problematic language constructs Infix operators i n f i x d i v i d e s ; fun x d i v i d e s y = ( y mod x ) = 0 ; syntactic sugar for functions with two arguments 6 d i v i d e s 3 in non infix form op can apply op d i v i d e s ( 6, 3 ) normal form converts infix to non infix form op 15 Design and implementation 16
Implementation approach Working subset approach: 1. choose subset of Standard ML 2. implement program for the subset 3. gradually extend the subset 19 Conclusion 20 Architecture Input Lexical analysis token stream Parsing abstract syntax tree Normalisation normalised syntax tree Unification Output 17 Architecture (cont.) Unification stage: develop generic unification package data types for first order language terms unification algorithm for the data types divide unification stage into 2 stages: normalised syntax tree Convert to a first order language first order language Unify with templates (function, replacement) pairs 18
Summary introduced notation expressibility, h-expressibility, higher order equivalence showed how to determine expressibility cf. matching fingerprints unification with templates normal forms for pattern matching, infix operators explained architecture and implementation approach working subset approach analysed achievements and further work 23 Additional slides 24 Achievements fun double l = i f l = [ ] then [ ] e l s e ( ( hd l ) 2 ) : : double ( t l l ) ; fun double l = map ( fn x = ( op ( ( x, 2 ) ) ) ) l ; working program for subset of SML notation generic unification package 21 Further work extend subset improve generic unification package syntax for templates fun f... l... = i f ( l = [ ] ) then [ ] e l s e EXPR : : f ( t l l ) 22
Unification algorithm does not occur in such that then 1: disagreement set of 2: if then 3: return 4: else if variable term 5: for all do 6: 7: 8: goto step 1 9: else 10: return not unifiable 25 Algorithm 1: /* is an input source file */ 2: parse 3: normalise 4: set of function definitions in 5: templates of map filter 6: for all do 7: for all do 8: if unifies with then 9: convert to its higher order equivalent Initial subset minimal features to express functionals includes constants + tuples conditional expressions (if... then... else) boolean expressions (andalso, orelse) 26 excludes pattern matching infix operators nested expressions (let... in... end) exceptions entire module language 27