Introduction them to their higher order equivalents Philipp - - PowerPoint PPT Presentation

introduction
SMART_READER_LITE
LIVE PREVIEW

Introduction them to their higher order equivalents Philipp - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

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

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

slide-2
SLIDE 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:

  • possible use of map

fun square [ ] = [ ]

square ( x : : xs ) = ( x

  • x ) : :

square xs

  • higher order equivalent

val square = map ( fn x =

x

  • x )

5

Motivation

  • educational
  • software engineering
  • ptimisation

6

Notation

Definition 1 A function

✂☎✄ ✆ ✝ ✆ ✞

is expressible by a function

✟ ✄ ✆ ✞ ✞ ✝ ✠ ✆ ✝ ✆ ✞✡

, denoted

✂ ☛ ✟

, if

☞✌
  • ✂✎✍
✟ ✌

. Definition 2 A function

✂☎✄ ✆ ✝ ✆ ✞

is h-expressible by a function

✟ ✄ ✆ ✞ ✞ ✝ ✠ ✆ ✝ ✆ ✞✡

, denoted

✂ ☛ ✏ ✟

, if

✂ ✍ ✟ ✌

. Definition 3 An expression

✟ ✌

is a higher order equivalent of a function

if

✂ ☛ ✏ ✟

.

7

Notation – Example

fun i n c l [ ] = [ ]

i n c l ( x : : xs ) = ( x + 1 ) : : i n c l xs ; val i n c l 2 = map ( fn x =

x +1);

  • incl is expressible by map
☞✌ ✍

(fn x => x + 1)

  • incl

map

  • incl is h-expressible by map
  • incl
☛ ✏

map

map

✌ ✍

higher order equivalent of incl.

8

slide-3
SLIDE 3

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

Templates

fun f . . . [ ] . . . = [ ]

f . . . ( x : : xs ) . . . =

  • some
  • 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
  • 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

  • f [ ] =

[ ]

( x : : xs ) =

( x + 1 ) : : f2 xs ; (

  • Does not

compile without warning

  • )

fun f3 l = i f ( l = [ ] ) then [ ] e l s e l e t val ( x : : xs ) = l in ( x + 1 ) : : f3 xs end ;

12

slide-4
SLIDE 4

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 n returns the

field of a given type 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 val x = f i e l d 1 l val 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

  • perators

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

  • can apply
✄ ☎ ✆

in non infix form op

☎ ✠ ✄✞✝ ✆ ✡
  • p

d i v i d e s ( 6 , 3 )

  • normal form converts infix to non infix form
✄ ☎ ✆ ✑
  • p
☎ ✠ ✄✞✝ ✆ ✡

15

Design and implementation

16

slide-5
SLIDE 5

Architecture

Normalisation Unification Lexical analysis Parsing

normalised syntax tree token stream abstract syntax tree

Output Input

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:

(function, replacement) pairs first order language normalised syntax tree

Convert to a first order language Unify with templates

18

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

slide-6
SLIDE 6

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

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

slide-7
SLIDE 7

Unification algorithm

1:

disagreement set of

2: if

✄☎

then

3:

return

4: else if

variable

✝✞✝

term

✆ ☎

such that

does not occur in

then

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

26

Initial subset

  • minimal features to express functionals
  • includes

– constants + tuples – conditional expressions (if ... then ... else) – boolean expressions (andalso, orelse)

  • excludes

– pattern matching – infix operators – nested expressions (let ... in ... end) – exceptions – entire module language

27