-calculus Motiv ation: primitiv es of and pro cedural - - PDF document

calculus motiv ation primitiv es of and pro cedural
SMART_READER_LITE
LIVE PREVIEW

-calculus Motiv ation: primitiv es of and pro cedural - - PDF document

-calculus Motiv ation: primitiv es of and pro cedural programming com binatory logic Lam b da calculus: Jan Ma luszy nski A formalism that captures the mec hanism E++ 1o or ro om 210 of Phone:


slide-1
SLIDE 1
  • calculus
and com binatory logic Jan Ma luszy
  • nski
E++ 1o
  • r
ro
  • m
210 Phone: 1483 Mail: janma@ida.liu.se
  • Motiv
ation
  • Computation
as rewriting
  • Lam
b da calculus
  • Com
binatory logic
  • Relating
them 1 Motiv ation: primitiv es
  • f
pro cedural programming Lam b da calculus:
  • A
formalism that captures the mec hanism
  • f
{ making expressions to pro cedures, { pro cedure calls
  • A
general mo del
  • f
computation.
  • Reference
concepts for studying pro cedu- ral languages. Pla y ed imp
  • rtan
t role in dev elopmen t
  • f
pro- gramming languages, ALGOL, LISP ,... Com binatory logic:
  • A
related formalism
  • Imp
  • rtan
t concept
  • f
c
  • mbinator
  • Inuenced
implemen tation tec hniques Computation seen as Rewriting 2 An Example: Simplication
  • f
P
  • lynomials
Rules: + x ! x
  • x
! x(y + z ) ! xy + xz (x + y )z ! xz + y z Applied to an expression: (0 + a)(a + b) ! a(a + b) ! a 2 + ab An alternativ e reduction: (0 + a)(a + b) ! (0 + a)a + (0 + a)b ! (0
  • a
+ a 2 ) + (0 + a)b ! (0 + a 2 ) + (0 + a)b ! a 2 + (0 + a)b ! a 2 + (0
  • b
+ ab) ! a 2 + (0 + ab) ! a 2 + ab 3 Simplication
  • f
P
  • lynomials:
a Summary W e ha v e:
  • a
domain: the p
  • lynomials
  • a
binary rewrite relation
  • n
the domain: sp ecied b y rules Desirable prop erties:
  • an
y sequence
  • f
reductions is nite (termination).
  • for
eac h expression the
  • utcome
is unique. 4
slide-2
SLIDE 2 Abstract Reduction Systems An ars is a pair (A; !), where
  • A
is a set,
  • !
is a binary relation
  • n
A: a r ewrite r elation. Notation
  • a
! b: "b is a r e duct
  • f
a"
  • !
!: the reexiv e and transitiv e closure
  • f
!
  • :
the in v erse
  • f
!
  • :
the in v erse
  • f
! !
  • =:
(! ! [ )
  • .
5 T uring Mac hines : : : : : : 1 1 Read/write head 6 S tap e alphab et; Q states;
  • :
Q
  • S
) Q
  • S
  • fleft
; right g. s 1 : : : s i1 q s i : : : s n represen ts a conguration with
  • the
tap e s 1 : : : s i1 s i : : : s n
  • the
state q
  • the
p
  • sition
  • f
the head at s i c
  • mputation
, r ewriting: If
  • (q
; s i ) = (q ; t; right ), then s 1 : : : s i1 q s i : : : s n ` s 1 : : : s i1 t q s i+1 : : : s n 6 The con v ertibilit y relation a and b are c
  • nvertible
(a = b) if a can b e rewritten in to b using the ! relation forw ards
  • r
bac kw ards a nite n um b er
  • f
times. a b q q q q q q q q q q : : :
  • @
@ R @ @ R @ @ R
  • @
@ R @ @ R
  • 7
Normalizing ars Giv en an ars (A; !):
  • a
is a normal form : there is no b suc h that a ! b.
  • b
is a normal form
  • f
a: a ! ! b and b is a normal form.
  • !
is we akly normalizing if ev ery a 2 A has a normal form.
  • !
is str
  • ngly
normalizing (or terminat- ing , if there is no innite sequence a ! a 1 ! a 2 ! : : : a a 1 b a 2 b 1 a 3 b 2 . . .
  • @
@ R
  • @
@ R
  • @
@ R
  • @
@ R 8
slide-3
SLIDE 3 Conuen t ars Giv en an ars (A; !):
  • !
is Chur ch-R
  • sser
(CR) (or c
  • nuent
) if for all a; b; c 2 A suc h that a ! ! b and a ! ! c, there is a d 2 A suc h that b ! ! d and c ! ! d.
  • !
is we akly Chur ch-R
  • sser
(WCR) (or we akly c
  • nuent
) if for all a; b; c 2 A suc h that a ! b and a ! c, there is a d 2 A suc h that b ! ! d and c ! ! d. a b c d CR
  • @
@ @ @ R @ @ @ @ R @ @ @ @ R @ @ R
  • a
b c d W CR
  • @
@ @ @ R @ @ @ @ R @ @ R
  • a
b c d
  • ?
  • 6
9 Conuen t ars's sp ecify functions Giv en a conuen t ars (A; !):
  • ev
ery a 2 A has at most
  • ne
normal form
  • !
denes a partial function f
  • n
A: f (a) = 8 > > < > > : b; if b 2 NF (A) and a ! ! b undened else
  • if
! is w eakly normalizing, then f is a total function.
  • if
! is strongly normalizing, then f can b e computed with an y reduction strategy . 10 The ars b elo w is W CR but not CR. It is also non-terminating. a b c d
  • ?
  • 6
Newman's lemma: If ! is w eakly con- uen t and terminating, then ! is conuen t. 11 Lam b da calculus Lam b da calculus is an ars
  • Domain:
lamb da terms
  • Rewrite
relation: r e duction rules 12
slide-4
SLIDE 4 Lam b da terms In tuition
  • Making
an expression to a function, e.g. fun twice x = 2*x;
  • Using
the function, e.g. twice 2; F
  • rmal
denition:
  • a
v ariable (x; y ; z ; :::) is a lam b da term,
  • if
M is a lam b da term, then (x:M ) is a lam b da term (functional abstr action)
  • if
M and N are lam b da terms then (M N ) is a lam b da term (functional applic ation) 13 Notational con v en tions for lam b da terms
  • Left-asso
ciativit y
  • f
application ((xy )z ) ) xy z
  • Skipping
m ultiple s (x:(y :(M ))) ) xy :M Example: write xy :xy (xy ) instead
  • f
(x:(y :((xy )(xy )))) 14 In tuition
  • f
rewriting
  • x:M
is a function with parameter x: x is b
  • und
in M , non-b
  • und
v ariables are fr e e.
  • (x:M
)N is a \call": r eplac e x in M b y N . (x:x)y ! y (x:xx)(x:x)y ! (x:x)(x:x)y ! (x:x)y ! y But ho w to handle: (xy :xy )y Replacemen t m ust not bind free v ariables. 15 The replacemen t
  • p
eration [x 7! N ]M (i) [x 7! N ]x is N , (ii) [x 7! N ]y is y for an y v ariable y dieren t from x, (iii) [x 7! N ](P Q) is ([x 7! N ]P [x 7! N ]Q), (iv) [x 7! N ]x:P is x:P , (v) [x 7! N ]y :P is y :[x 7! N ]P if y is dieren t from x and has no free
  • ccurrence
in N ,
  • r
if x has no free
  • ccurrence
in P , (vi) [x 7! N ]y :P is z [x 7! N ]([y 7! z ]P ) if y is dieren t from x and has a free
  • c-
currence in N , and x has a free
  • ccurrence
in P . z is a v ariable that do es not
  • ccur
free in N nor in P . (xy :xy )y ! [x 7! y ](y :xy ) = z [x 7! y ]([y 7! z ]y :xy ) = z [x 7! y ](z :xz ) = (z :y z ) 16
slide-5
SLIDE 5 ( ) If y is not free in M , then x:M !
  • y
:[x 7! y ]M . ( ) (x:M )N !
  • [x
7! N ]M
  • In
tuition
  • f
!
  • :
renaming; x:xy !
  • z
:z y
  • Restriction
  • n
!
  • :
\fresh" v ariables are to b e used: x:xy 6!
  • y
:y y
  • !
!
  • is
symmetric, e.g.: xy :xy ! !
  • z
w :z w z w :z w ! !
  • xy
:xy Hence the
  • rule
is called
  • c
  • nversion
rule . If s ! !
  • t
then s and t are called
  • c
  • ngruent.
17 The
  • reduction
rule
  • In
tuition
  • f
!
  • :
function call.
  • Computation
b y
  • reduction
ma y
  • r
ma y not terminate, e.g. (y :y y )(y :y y ) ! (y :y y )(y :y y )
  • Ch
urc h-Rosser theorem: if P ! !
  • M
and P ! !
  • N
then there exist
  • congruen
t terms T 1 and T 2 suc h that M ! !
  • T
1 and N ! !
  • T
2 .
  • Hence,
ev ery lam b da term has a unique (up to
  • con
v ersion) normal form,
  • r
no normal form at all. 18 Some rewrite strategies
  • The
normal-or der r e duction (lazy ev aluation): reduce the left-most redex,e.g.: (x:xx)((y :y )(y :y )) ! ((y :y )(y :y ))((y :y )(y :y )) ! (y :y )((y :y )(y :y )) ! ((y :y )(y :y )) ! y :y
  • The
applic ative-or der r e duction (call b y v alue): reduce the left-most innermost redex, e.g.: (x:xx)((y :y )(y :y )) ! (x:xx)(y :y ) ! ((y :y )(y :y )) ! y :y 19 The rewrite strategies and termination
  • If
the normal form exists, the normal-order reduction will compute it.
  • The
applicativ e
  • rder
reduction ma y not terminate ev en if there is a normal form, e.g.: (xz :z )((y :y y )(y :y y )) ! (xz :z )((y :y y )(y :y y )) using the applicativ e-order reduction. But: (xz :z )((y :y y )(y :y y )) ! (z :z ) using the normal-order reduction. 20
slide-6
SLIDE 6 Represen ting n um b ers as lam b da terms Chur ch numer als: Represen t a natural n um b er n b y xy :x n y where x n y denotes n applications
  • f
x to y . Th us:
  • xy
:y abbreviated as 0, denotes 0.
  • xy
:xy abbreviated as 1, denotes 1.
  • xy
:x(xy ) abbreviated as 2, denotes 2.
  • etc.
... Notice:
  • Eac
h Ch urc h n umeral is normal form.
  • It
can b e seen as a function
  • n
lam b da terms: giv en lam b da terms M and N : nM N ! ! M n N 21 The p
  • w
er
  • f
lam b da calculus
  • :
an m-ary function
  • n
N at
  • is
lamb da-dene d b y a lam b da term F : i for arbitrary natural n um b ers n 1 ; :::; n m F n 1 ::: n m ! ! n i (n 1 ; :::; n m ) = n.
  • is
lamb da-denable if it is lam b da-dened b y some lam b da term. The class
  • f
lam b da-denable functions coin- cides with the class
  • f
  • T
uring-computable functions 22 Discussion Lam b da terms dening functions are complex Example
  • Lam
b da term s dening successor: uxy :x(uxy ) sn= (uxy :x(uxy ))(z v :z n v ) ! xy :x((z v :z n v )xy ) ! xy :x((v :x n v )y ) ! xy :x(x n y ) =n+1
  • Addition:
nxy ! ! x n y , x + y = s x (s y (0)), hence xy :xs(y s 0) denes addition. Its extended form is: xy :x(uxy :x(uxy )(y (uxy :x(uxy ))(xy :y )). 23 Expressing xp
  • in
ts
  • Fixp
  • in
ts
  • f
functions can b e represen ted b y lam b da terms: { Existence
  • f
xp
  • in
ts: F
  • r
ev ery term M there exists P s.t.: M P =
  • P
{ Ho w to represen t them: There exists Y s. that for ev ery M M (Y M ) =
  • Y
M e.g. tak e x:((y :x(y y ))(y :x(y y ))) as Y , then: Y M = x:(y :x(y y ))(y :x(y y ))M ! (y :M (y y ))(y :M (y y )) ! M ((y :M (y y ))(y :M (y y ))) = M (Y M ) 24
slide-7
SLIDE 7 Com binatory logic
  • CL
is y et another rewrite system
  • Equiv
alen t to lam b da calculus
  • Do
es not use b
  • und
v ariables
  • Used
in implemen tation
  • f
functional lan- guages
  • In
tuition: to represen t comp
  • sition
  • f
func- tions 25 Com binatory terms (i) A v ariable is a com binatory term, (ii) The basic com binators K and S are com- binatory terms, (iii) If X and Y are com binatory terms, then the applic ation (X Y ) is a com binatory term with subterms X and Y . e.g.: ((S K )K ), (((S K )K )x) Notation: the application assumed left-asso ciativ e, e.g. S S (K (S K K )) denotes ((S S )(K ((S K )K ))) In tuition:
  • C-terms
represen t (partial) functions
  • n
C-terms computed b y rewriting.
  • The
v alue
  • f
F for A, is the normal form
  • f
the term (F A).
  • K
allo ws for creation
  • f
constan t func- tions.
  • S
describ es a sp ecic w a y
  • f
comp
  • sition
  • f
functions. 26 Com binatory reduction
  • K
X Y ! X
  • S
X Y Z ! X Z (Y Z ) S (K S )K xy z ! (K S )x(K x)y z ! S (K x)y z ! (K x)z (y z ) ! x(y z )
  • Com
binatory logic is conuen t.
  • If
a C-term X has a normal form then re- duction
  • f
X under normal-order strategy terminates. 27 Some natural com binators A c
  • mbinator
is a v ariable-free C-term. The iden tit y com binator I : S K K S K K y ! K y (K y ) ! y The comp
  • sition
com binator B : S (K S )K F
  • r
an y C-terms X ; Y ; Z : B X Y Z = S (K S )K X Y Z ! (K S )X (K X )Y Z ! S (K X )Y Z ! (K X )Z (Y Z ) ! X (Y Z ) 28
slide-8
SLIDE 8 Sim ulation
  • f
the functional abstraction in CL Giv en x and C-term M construct a C-term
  • c
x:M s.t.: ( c x:M )N ! ! C L [x 7! N ]M Dene:
  • c
x:x = S K K
  • c
x:P = K P if x do es not app ear in P ,
  • c
x:P Q = S ( c x:P )( c x:Q) if the previous cases do not apply .
  • c
x:K x = S ( c x:K )( c x:x) = S (K K )(S K K ): No w, for an y term P , S (K K )(S K K )P ! (K K P )(S K K P ) ! ! K P 29 Compiling lam b da terms to C-terms In ten tion: Sim ulate lam b da calculus in CL The transformation H :
  • x
H = x for ev ery v ariable x,
  • (P
Q) H = P H Q H for an y lam b da terms P and Q,
  • (x:P
) H =
  • c
x:(P ) H (xy :y ) H =
  • c
x:(y :y ) H =
  • c
x:( c y :y ) =
  • c
x:S K K = K (S K K ) (xy :y )z u !
  • (y
:y )u !
  • u
K (S K K )z u ! C L S K K u ! ! C L u 30