Zelfgemaakt datatype voor bomen PowerPoint PPT Presentation

presentation player overlay
1 / 32
About This Presentation
Transcript and Presenter's Notes

Title: Zelfgemaakt datatype voor bomen


1
Zelfgemaakt datatypevoor bomen
data Tree a Bin (Tree a) (Tree a) Leaf a
  • Met functies

foldTree Tree a ? bfoldTree (b,lf)
(Bin le ri) b (foldTree (b,lf) le) (foldTree
(b,lf) ri) foldTree (b,lf) (Leaf x) lf x
foldTree (b?b?b , a?b)?Tree a ? bfoldTree
(b,lf) f where f (Bin le ri) b (f le) (f
ri) f (Leaf x) lf x
b?b?b
( , )?
a?b
2
Voorbeelden van algebras
data Tree a Bin (Tree a) (Tree a) Leaf a
type TreeAlgebra a b ( b ? b ? b ,
a ? b )
data Expr Add Expr Expr Mul Expr
Expr Con Int
type ExprAlgebra b ( b ? b ? b , b ?
b ? b , Int ? b )
data Expr Add Expr Expr Mul Expr
Expr Con Int Var String
type ExprAlgebra b ( b ? b ? b , b ?
b ? b , Int ? b , String ? b
)
3
Definitie een algebra
  • Een algebrabestaat uit
  • een type
  • functies in een tupel
  • Een algebrabestaat uit
  • een type dat het resultaat is van een fold, die
  • functies in een tupel neerzet in plaats van
    constructorfuncties
  • Een algebra voor een datatypebestaat uit
  • een type dat het resultaat is van een fold, die
  • functies in een tupel neerzet in plaats van
    constructorfuncties van dat datatype

carrier set
semantiek
countLeafsFuns TreeAlgebra a
Int countLeafsFuns ( () , \x?1 )
4
Algebras voor wederzijdsrecursieve datatypes
data Stat a Assign String (Expr a) Print
(Expr a) Block Stat a data Expr a Con
a Var String Add (Expr a) (Expr a)
type StatExprAlgebra a s e ( ( String ? e
? s , e ? s , s ?
s ) , ( a ? e ,
String ? e , e ? e ? e ) )
foldStatExpr StatExprAlgebra a s e ? Stat a
? s foldStatExpr ((f1,f2,f3),(g1,g2,g3)) f
where f (Assign x e) f1 x (g e) f (Print
e) f2 (g e) f (Block ss) f3 (map f ss) g
(Con c) g1 c g (Var x) g2 x g (Add e1
e2) g3 (g e1) (g e2)
5
Definitie van foldExpr
data Expr Add Expr Expr Mul Expr
Expr Con Int
type ExprAlgebra b ( b ? b ? b , b ?
b ? b , Int ? b )
foldExpr ExprAlgebra b ? Expr ? b foldExpr
(a,m,c) f where f (Add e1 e2) a (f
e1) (f e2) f (Mul e1 e2) m (f e1) (f e2)
f (Con n) c n
6
Gebruik van ExprAlgebra
data Expr Add Expr Expr Mul Expr
Expr Con Int
type ExprAlgebra b ( b ? b ? b , b ?
b ? b , Int ? b )
evalExpr Expr ? Int evalExpr foldExpr
evalExprAlgebra
evalExprAlgebra ExprAlgebra Int evalExprAlgebra
( () , () , id )
7
Taal syntax en semantiek
3 4 5
start p where p ltgtltgt
Add (Con 3) (Mul (Con 4) (Con 5))
fold a where a (,,,)
23
8
Compositionaliteit
  • Een semantiek is compositioneel als de
    betekenis van een geheel een
    functie is van de betekenissen van de delen

eval (Add x y) add (eval x) (eval y)
  • Een compositionele semantiekkun je schrijven als
    fold over de expressiewaarbij een algebra
    vervangingen geeftvoor de constructoren

9
Verschillende semantieken
3 4 5
String
Add (Con 3) (Mul (Con 4) (Con 5))
Expr
fold a where a (,,,)
aExprAlgebra Int
fold a where a (,,,)
aExprAlgebra Code
23
Push 3 Push 4 Push 5 Apply () Apply ()
Int
Code
10
De compileer-semantiek
  • Wat is machinecode ?

type Code Instr
  • Wat is een machine-instructie ?

data Instr Push Int Apply
(Int?Int?Int)
11
Compilergenereren van Code
data Expr Add Expr Expr Mul Expr
Expr Con Int
type ExprAlgebra b ( b ? b ? b , b ?
b ? b , Int ? b )
evalExpr Expr ? Int evalExpr foldExpr
evalExprAlgebra where evalExprAlgebra
ExprAlgebra Int evalExprAlgebra ( () ,
() , id )
compExpr Expr ? Code compExpr foldExpr
compExprAlgebra where compExprAlgebra
ExprAlgebra Code compExprAlgebra ( add ,
mul , con )
mul Code ? Code ? Code mul c1 c2
c1 c2 Apply ()
con n Push n
12
Verschillende semantieken
3 4 5
String
Add (Con 3) (Mul (Con 4) (Con 5))
Expr
fold a where a (,,,)
aExprAlgebra Int
fold a where a (,,,)
aExprAlgebra Code
23
Push 3 Push 4 Push 5 Apply () Apply ()
Int
Code
13
Runnersimulatie van processor
run Code ? Stack ? Stack run
stack stack run (instrrest) stack
exec instr stack
run rest ( )
exec Instr ? Stack ? Stack exec (Push x)
stack x stack exec (Apply f)
(yxstack) f x y stack
runExpr Code ? Int runExpr prog
run prog
head ( )
14
Compiler correctheid
runExpr (compileExpr e) evalExpr e
15
Uitrekenen vanexpressies met variabelen
data Expr Add Expr Expr Mul Expr
Expr Con Int
type ExprAlgebra b ( b ? b ? b , b ?
b ? b , Int ? b )
evalExpr Expr ? Int evalExpr foldExpr
eAlgebra where eAlgebra ExprAlgebra Int
eAlgebra ( () , () , id )
evalExpr Env ? Expr ? Int evalExpr env
foldExpr eAlgebra where eAlgebra
ExprAlgebra Int eAlgebra ( () , () , id
, ???? )
, ???? )
, (env ?) )
BAD !!!
16
Uitrekenen vanexpressies met variabelen
evalExpr Expr ? Env ? Int evalExpr env
foldExpr eAlgebra where eAlgebra
ExprAlgebra Int eAlgebra ( () , () , id
, (env?) )
evalExpr Expr ? (Env?Int) evalExpr
foldExpr eAlgebra where eAlgebra
ExprAlgebra Int eAlgebra ( add, mul, con,
var )
(Env?Int)
(Env?Int)
evalExpr Expr ? Int evalExpr expr
evalExpr expr
17
Uitrekenen vanexpressies met definities
evalExpr Expr ? Env ? Int evalExpr env
foldExpr eAlgebra where eAlgebra
ExprAlgebra (Env?Int) eAlgebra ( add , mul
, con , var )
, def )
18
Uitrekenen vanexpressies met definities
add b ? b ?
b
add f g e
f e g e
mul f g e f e g e
con const
con n e
n
var flip (?)
var x e
e ? x
def (ltgt)
def x fd fb e
fb e
(x, )
( )
fd e
19
Verschillende semantieken
3 4 5
String
Add (Con 3) (Mul (Con 4) (Con 5))
Expr
fold a where a (,,,)
aExprAlgebra Int
fold a where a (,,,)
aExprAlgebra Code
23
Push 3 Push 4 Push 5 Apply () Apply ()
Int
Code
20
Compileren vanexpressies met definities
add f g e
f e g e Apply ()
mul f g e
f e g e Apply ()
con n e
Push n
var x e
e ? x
def x fd fb e
fb ( (x, fd e) e )
21
Wat zit er in het Env ?
  • evalExpr
  • compExpr

type Env (String, Int)
type Env (String, Code)
22
Compiler correctheidexpressies met definities
hd (run (compileExpr e) s) evalExpr e
runExpr (compileExpr e env) evalExpr e env
23
Voorbeeld compileren van expressie
345
Push 3 Push 4 Push 5 Apply () Apply ()
Push 3 Push 2 Push 2 Apply () Push 5 Apply
() Apply ()
24
Voorbeeld compileren van expressie
Push 3 Push 2 Push 2 Apply () Push 5 Apply
() Apply ()
Push 3 Push 2 Push 2 Apply () Push 2 Push
2 Apply () Apply () Apply ()
25
De compileer-semantiek
Aanpassing van
  • Wat is machinecode ?

type Code Instr
  • Wat is een machine-instructie ?

data Instr Push Int Apply
(Int?Int?Int)
data Instr Push Int Apply
(Int?Int?Int) Load Adres Store
Adres
26
Efficient compileren vanexpressies met
definities
add f g e
f e g e Apply ()
mul f g e
f e g e Apply ()
con n e
Push n
Load (e?x)
var x e
e ? x
where a length e
def x fd fb e
fb ( (x, fd e) e )
fd e Store a fb ((x,a)e)
27
Wat zit er in het Env ?
  • evalExpr
  • compExpr
  • efficientCompExpr

type Env (String, Int)
type Env (String, Code)
type Env (String, Adres)
28
Runnersimulatie van processor
run Code ? Stack ? Stack run
stack stack run (instrrest) stack
exec instr stack
run rest ( )
exec Instr ? Stack ? Stack exec (Push x)
stack x stack exec (Apply f)
(yxstack) f x y stack
runExpr Code ? Int runExpr prog
run prog
head ( )
29
Runner aangepastesimulatie van processor
run Code ? (Mem,Stack) ? (Mem,Stack) run
ms ms run (instrrest) ms
exec instr ms
run rest ( )
exec Instr ? (Mem,Stack) ? (Mem,Stack) exec
(Push x) (m, st) (m, x st
) exec (Apply f) (m, yxst) (m, f x y st
) exec (Load a) (m, st) (m, m!a st
) exec (Store a) (m, x st) (update m a x,
st )
30
Voorbeeld Blokgestructureerde talen
Enter (0,2) Access(0,0) Enter (1,2) Access
(1,1) Access (0,1) Access (1,0) Leave
(1,2) Access(0,1) Leave (0,2)
use x dcl x use z use y dcl x dcl
z use x dcl y use y
31
Definitie van Block-type, -algebra -fold
data Block Cons Stat Block Empty data
Stat Decl Naam Use Naam Blk Block
type BlockAlgebra b s ( ( s ? b ? b
, b ) , ( Naam ? s , Naam
? s , b ? s ) )
foldBlock BlockAlgebra b s ? Block ?
b foldBlock ((c,e),(d,u,b)) f where f
(Cons (sb)) c (g s) (f b) f Empty e
g (Decl x) d x g (Use x) u x g (Blk
n) b (f n)
32
Compileren van een Block
compBlock Block? Code compBlock
foldBlock cAlg where cAlg
BlockAlgebra Code cAlg ( (c,e), (d,u,b))
where c e d u b
Env ? Code
(Env?Code)
Write a Comment
User Comments (0)
About PowerShow.com