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