eval (Add x y) = add (eval x) (eval y) 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 semantiek kun je schrijven als fold over de expressie waarbij een algebra vervangingen geeft voor de constructoren
Zelfgemaakt datatype voor bomen data Tree a = Bin (Tree a) (Tree a) | Leaf a Met functies foldTree :: (bbb , ab)Tree a b foldTree (b,lf) = f where f (Bin le ri) = b (f le) (f ri) f (Leaf x) = lf x foldTree :: Tree a b foldTree (b,lf) (Bin le ri) = b (foldTree (b,lf) le) (foldTree (b,lf) ri) foldTree (b,lf) (Leaf x) = lf x ( , ) bbb ab
Functies op bomen foldTree :: ( bbb , ab ) Tree a b Specialisaties: countLeafs :: Tree a Int countLeafs = foldTree ( (+) , \x1 ) sumLeafs :: Tree Int Int sumLeafs = foldTree ( (+) , \xx ) listLeafs :: Tree a [a] listLeafs = foldTree ( (++) , \x[x] )
De essentie van Tree-functies type TreeAlgebra a b = ( bbb , ab ) countLeafsFuns :: ( IntIntInt , aInt ) countLeafsFuns :: TreeAlgebra a Int countLeafsFuns = ( (+) , \x1 ) countLeafs :: Tree a Int countLeafs = foldTree ( (+) , \x1 ) countLeafs :: Tree a Int countLeafs = foldTree countLeafsFuns
Tree, TreeAlgebra en functie foldTree data Tree a = Bin (Tree a) (Tree a) | Leaf a type TreeAlgebra a b = ( bbb , ab ) foldTree :: TreeAlgebra a bTree a b foldTree (b,lf) = f where f (Bin le ri) = b (f le) (f ri) f (Leaf x) = lf x
Voorbeelden van TreeAlgebra type TreeAlgebra a b = ( bbb , ab ) countLeafsFuns :: TreeAlgebra a Int countLeafsFuns = ( (+) , \x1 ) sumLeafsFuns :: TreeAlgebra Int Int sumLeafsFuns = ( (+) , \xx ) listLeafsFuns :: TreeAlgebra a [a] listLeafsFuns = ( (++) , \x[x] )
Hoofdstuk 8: eenvoudige niet-polymorfe versie data Tree = Leaf Int | Bin Tree Tree type TreeAlgebra b = (Intb , bbb) foldTree :: TreeAlgebra bTree b foldTree (b,lf) = f where f (Leaf x) = lf x f (Bin le ri) = b (f le) (f ri)
Publieksvraag Maak een algebra die het minimum van een Tree bepaalt Maak een algebra die alle bladeren van een Tree vervangt door 0 (geef ook de types aan) data Tree = Leaf Int | Bin Tree Tree
Repmin, versie 1 repmin t = b where b = fold subalg t m = fold minalg t “pass 2” “pass 1” minalg :: Algebra Int minalg = ( \x x , \m1 m2 min m1 m2 ) subalg :: Algebra Tree subalg = ( \x Leaf 0 , \b1 b2 Bin b1 b2 ) m
weg met deze globale variabele! Repmin, versie 12 repmin t = b where b = fold subalg t m = fold minalg t minalg :: Algebra Int minalg = ( \x x , \m1 m2 min m1 m2 ) subalg :: Algebra Tree subalg = ( \x Leaf m , \b1 b2 Bin b1 b2 ) weg met deze globale variabele!
Repmin, versie 1 2 repmin t = r m where r = fold repalg t m = fold minalg t repmin t = b where b = fold subalg t m = fold minalg t minalg :: Algebra Int minalg = ( \x x , \m1 m2 min m1 m2 ) repalg :: Algebra (Int Tree) repalg = ( \x \m Leaf m , \r1 r2 \m Bin (r1 m) (r2 m)) repalg :: Algebra (Int Tree) repalg = ( \x Leaf m , \b1 b2 Bin b1 b2 ) repalg :: Algebra (Int Tree) repalg = ( \x \m Leaf m , \b1 b2 \m Bin b1 b2 )
Passes zijn nu onafhankelijk van elkaar! Repmin, versie 2 repmin t = r m where r = fold repalg t m = fold minalg t Passes zijn nu onafhankelijk van elkaar! minalg :: Algebra Int minalg = ( \x x , \m1 m2 min m1 m2 ) repalg :: Algebra (Int Tree) repalg = ( \x \m Leaf m , \r1 r2 \m Bin (r1 m) (r2 m))
één gecombineerde pass met tupelresultaat Repmin, versie 2 3 repmin t = r m where (m,r)= fold tupalg t één gecombineerde pass met tupelresultaat tupalg :: Algebra ( Int , Int Tree ) tupalg = ( \x ( x , \m Leaf m ) , \t1 t2 ( min m1 m2 , \m Bin (r1 m) (r2 m) ) ) tupalg :: Algebra ( Int , Int Tree ) tupalg = ( \x ( x , \m Leaf m ) , \t1 t2 let (m1,r1) = t1 (m2,r2) = t2 in ( min m1 m2 , \m Bin (r1 m) (r2 m) ) ) tupalg :: Algebra ( Int , Int Tree ) tupalg = ( \x ( x , \m Leaf m ) , \t1 t2 ) tupalg :: Algebra ( Int , Int Tree ) tupalg = ( \x , \t1 t2 ) minalg :: Algebra Int minalg = ( \x x , \m1 m2 min m1 m2 ) repalg :: Algebra (Int Tree) repalg = ( \x \m Leaf m , \r1 r2 \m Bin (r1 m) (r2 m))
Repmin, versie 3 repmin t = r m where (m,r)= fold tupalg t Int (Int,Tree) tupalg :: Algebra ( Int , Int Tree ) tupalg = ( \x ( x , \m Leaf m ) , \t1 t2 let (m1,r1) = t1 (m2,r2) = t2 in ( min m1 m2 , \m Bin (r1 m) (r2 m) ) ) tupalg :: Algebra ( Int , Int Tree ) tupalg = ( \x ( x , \m Leaf m ) , \t1 t2 let (m1,r1) = t1 (m2,r2) = t2 in ( min m1 m2 , \m Bin (r1 m) (r2 m) ) )
Repmin, versie 3 4 repmin t = where f = fold meralg t repmin t = b (m,b) = f m repmin t = r m where (m,r)= fold tupalg t meralg :: Algebra (Int (Int,Tree)) meralg = ( \x \m ( x , Leaf m ) , \f1 f2 \m let (m1,b1) = f1 m (m2,b2) = f2 m in ( min m1 m2 , Bin b1 b2 ) ) meralg :: Algebra (Int (Int,Tree)) meralg = ( \x \m ( x , Leaf m ) , \f1 f2 \m ( min m1 m2 , Bin b1 b2 ) ) meralg :: Algebra (Int (Int,Tree)) meralg = ( \x \m , \f1 f2 \m ) meralg :: Algebra (Int (Int,Tree)) meralg = ( \x , \f1 f2 ) meralg :: Algebra (Int (Int,Tree)) meralg = ( \x \m ( x , Leaf m ) , \f1 f2 \m )