program language - fall 2013
TRANSCRIPT
![Page 1: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/1.jpg)
Program LanguageProgram Derivation
郗昀彥 (代)
![Page 2: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/2.jpg)
![Page 3: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/3.jpg)
3
Construction?
Derivation!
Program
![Page 4: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/4.jpg)
4
Algorithm
![Page 5: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/5.jpg)
5
Efficient Program
Specification
Algorithm
![Page 6: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/6.jpg)
6
Specification
Efficient Program
Algorithm
•Hard•Depend on Tools•Correctness by Testing
![Page 7: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/7.jpg)
7
Efficient Program
Specification
Algorithm
• Straightforward•Math. or Logic
![Page 8: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/8.jpg)
8
Algorithm
Efficient Program
Specification
•Correctness by Verification•Model Checking•Automata
![Page 9: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/9.jpg)
9
Algorithm
Efficient Program
Specification • Program Construction•Derivation• Synthesis•Hoare Logic
![Page 10: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/10.jpg)
10
ProgramDerivation
![Page 11: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/11.jpg)
11
• fold
• fold fusion
• list homomorphism
• 3rd list homomorphism theorem
![Page 12: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/12.jpg)
foldr
![Page 13: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/13.jpg)
In computer science, a list or sequence is an abstract data type that implements a finite ordered collection of values, where the same value may occur more than once.
- wikipedia
![Page 14: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/14.jpg)
1 2 3 4 5
1 2 3 4 5: : : : :
data List a = [] | a : (List a)
![Page 15: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/15.jpg)
1 2 3 4 5 [ ]: : : : :
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
![Page 16: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/16.jpg)
1 2 3 4 5: : : : :
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
[ ]
![Page 17: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/17.jpg)
1 2 3 4 5: : : : :
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
[ ]
![Page 18: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/18.jpg)
1 2 3 4 5: : : : :
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
[ ]
![Page 19: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/19.jpg)
1 2 3 4 5: : : : :
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
[ ]
![Page 20: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/20.jpg)
1 2 3 4 5: : : : :
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
[ ]
![Page 21: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/21.jpg)
1 2 3 4 5: : : : : e
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
![Page 22: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/22.jpg)
1 2 3 4 5: : : : e◁
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
![Page 23: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/23.jpg)
1 2 3 4 5: : : e◁◁
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
![Page 24: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/24.jpg)
1 2 3 4 5: : e◁◁◁
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
![Page 25: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/25.jpg)
1 2 3 4 5: e◁◁◁◁
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
![Page 26: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/26.jpg)
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
1 2 3 4 5 e◁◁◁◁◁
![Page 27: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/27.jpg)
sum [1..1000]=>foldr (+) 0 [1..1000]1 + (foldr (+) 0 [2..1000])1 + (2 + (foldr (+) 0 [3..1000]))...1 + (2 + (... + (1000 + (foldr (+) 0 []))...))1 + (2 + (... + (1000 + 0)...)))...1 + (2 + 500497) 1 + 500499 500500
![Page 28: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/28.jpg)
sum [1..1000]=>foldr (+) 0 [1..1000]1 + (foldr (+) 0 [2..1000])1 + (2 + (foldr (+) 0 [3..1000]))...1 + (2 + (... + (1000 + (foldr (+) 0 []))...))1 + (2 + (... + (1000 + 0)...)))...1 + (2 + 500497) 1 + 500499 500500
![Page 29: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/29.jpg)
1 + (2 + (... + (1000 + 0)...)))
![Page 30: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/30.jpg)
length :: [a] → Intsum :: (Num a) ⇒ [a] → amap :: (a → b) → [a] → [b]filter :: (a → Bool) → [a] → [a]inits :: [a] → [[a]]concat :: [[a]] → [a]maximum :: (Ord a) ⇒ [a] → a
e ?(◁) ?
![Page 31: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/31.jpg)
foldl
![Page 32: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/32.jpg)
1 2 3 4 5 [ ]: : : : :
foldr :: (a → b → b) → b → [a] → bfoldl :: (b → a → b) → b → [a] → bfoldl (▷) e [] = efoldl (▷) e (x : xs) = foldl (▷) (e ▷ x) xs
![Page 33: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/33.jpg)
1 2 3 [ ]: : :
foldl (▷) Para0 Para1
{{
e
![Page 34: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/34.jpg)
1 2 3 [ ]: : :
foldl (▷) Para0 Para1
{{
e 1▷
![Page 35: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/35.jpg)
1 2 3 [ ]: : :
foldl (▷) Para0 Para1
{{
e 1▷ 2▷
![Page 36: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/36.jpg)
1 2 3 [ ]: : :
foldl (▷) Para0 Para1
{{
e 1▷ 2▷ 3▷
![Page 37: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/37.jpg)
13
sum [1..1000]=>foldl (+) 0 [1..1000]foldl (+) (0 + 1) [2..1000]foldr (+) (1 + 2) [3..1000]foldr (+) (3 + 3) [4..1000]...foldr (+) (498501 + 999) [1000]foldr (+) (499500 + 1000) []foldr (+) 500500 []500500
![Page 38: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/38.jpg)
13
sum [1..1000]=>foldl (+) 0 [1..1000]foldl (+) (0 + 1) [2..1000]foldr (+) (1 + 2) [3..1000]foldr (+) (3 + 3) [4..1000]...foldr (+) (498501 + 999) [1000]foldr (+) (499500 + 1000) []foldr (+) 500500 []500500
![Page 39: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/39.jpg)
![Page 40: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/40.jpg)
length :: [a] → Intsum :: (Num a) ⇒ [a] → amap :: (a → b) → [a] → [b]filter :: (a → Bool) → [a] → [a]concat :: [[a]] → [a]maximum :: (Ord a) ⇒ [a] → a
e ?(▷) ?
![Page 41: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/41.jpg)
fusion
![Page 42: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/42.jpg)
42
map f ◦ map g
![Page 43: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/43.jpg)
42
map (f ◦ g)
![Page 44: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/44.jpg)
43
filter q ◦ filter p
![Page 45: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/45.jpg)
43
filter (q ∧ p)
![Page 46: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/46.jpg)
44
foldr-fusion
f ◦ foldr (◁) e
![Page 47: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/47.jpg)
44
foldr-fusion
⇐ f (x ◁ z) = x ≪ (f z)
foldr (≪) (f e)
![Page 48: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/48.jpg)
45
f $ foldr (◁) e []= { definition of foldr } f e= { definition of foldr } foldr (≪) (f e) []
foldr-fusion
f ◦ foldr (◁) e = foldr (≪) (f e)
![Page 49: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/49.jpg)
46
f $ foldr (◁) e (x:xs)= f (x ◁ (foldr (◁) e xs))= { f (x ◁ z) = x ≪ (f z) } x ≪ (f (foldr (◁) e xs))= x ≪ (foldr (≪) (f e) xs)= foldr (≪) (f e) (x:xs)
foldr-fusion
f ◦ foldr (◁) e = foldr (≪) (f e)
![Page 50: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/50.jpg)
47
f $ foldr (◁) e (x:xs)= f (x ◁ (foldr (◁) e xs))= { f (x ◁ z) = x ≪ (f z) } x ≪ (f (foldr (◁) e xs))= x ≪ (foldr (≪) (f e) xs)= foldr (≪) (f e) (x:xs)
foldr-fusion
f ◦ foldr (◁) e = foldr (≪) (f e)
![Page 51: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/51.jpg)
48
f $ foldr (◁) e (x:xs)= f (x ◁ (foldr (◁) e xs))= { f (x ◁ z) = x ≪ (f z) } x ≪ (f (foldr (◁) e xs))= x ≪ (foldr (≪) (f e) xs)= foldr (≪) (f e) (x:xs)
foldr-fusion
f ◦ foldr (◁) e = foldr (≪) (f e)
![Page 52: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/52.jpg)
49
f $ foldr (◁) e (x:xs)= f (x ◁ (foldr (◁) e xs))= { f (x ◁ z) = x ≪ (f z) } x ≪ (f (foldr (◁) e xs))= x ≪ (foldr (≪) (f e) xs)= foldr (≪) (f e) (x:xs)
foldr-fusion
f ◦ foldr (◁) e = foldr (≪) (f e)
![Page 53: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/53.jpg)
50
f $ foldr (◁) e (x:xs)= f (x ◁ (foldr (◁) e xs))= { f (x ◁ z) = x ≪ (f z) } x ≪ (f (foldr (◁) e xs))= x ≪ (foldr (≪) (f e) xs)= foldr (≪) (f e) (x:xs)
foldr-fusion
f ◦ foldr (◁) e = foldr (≪) (f e)
![Page 54: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/54.jpg)
51
http://www.openrice.com/restaurant/commentdetail.htm?commentid=2106508
範例
![Page 55: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/55.jpg)
52
inits :: [a] → [[a]]tails :: [a] → [[a]]concat :: [[a]] → [a]filter :: (a → Bool) → [a] → [a]power :: [a] → [[a]]
filter (/=[])◦concat◦map tails◦inits
Suffixes of Prefixes
![Page 56: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/56.jpg)
53
inits :: [a] → [[a]]inits [] = [[]]inits (x:xs) = [] : map (x:) (inits xs)
filter (/=[])◦concat◦map tails◦inits
Suffixes of Prefixes
![Page 57: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/57.jpg)
54
inits :: [a] → [[a]]inits [] = [[]]inits (x:xs) = [] : map (x:) (inits xs)
inits’ = foldr (\x hs → [] : (map (x:) hs)) [[]]
filter (/=[])◦concat◦map tails◦inits
Suffixes of Prefixes
![Page 58: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/58.jpg)
55
filter (/=[])◦concat◦map tails◦inits
map tails ◦ inits = f ◦ foldr (◁) [[]] foldr-fusion= foldr (≪) (map tails [[]])= foldr (≪) [[[]]]
1. f = map tails2. (◁) = \x hs → [] : (map (x:) hs)
Suffixes of Prefixes
![Page 59: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/59.jpg)
56
filter (/=[])◦concat◦map tails◦inits
map tails ◦ inits = f ◦ foldr (◁) [[]] foldr-fusion= foldr (≪) (map tails [[]])= foldr (≪) [[[]]]
1. f = map tails2. (◁) = \x hs → [] : (map (x:) hs)3. (≪) = \x h → [[]] : map (\hs → (x : (head hs)) : hs) h
Suffixes of Prefixes
![Page 60: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/60.jpg)
57
filter (/=[])◦concat◦foldr (≪) [[[]]]
filter (/=[]) ◦ concat= g ◦ foldr (++) [] foldr-fusion= foldr (⋆) []
1. g = filter (/=[])2. (⋆) = \x h → filter (/=[]) x) ++ h
Suffixes of Prefixes
![Page 61: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/61.jpg)
58
filter (/=[]) ◦ concat= g ◦ foldr (++) [] foldr-fusion= foldr (⋆) []
1. g = filter (/=[])2. (⋆) = \x h → filter (/=[]) x) ++ h
filter (/=[])◦concat◦foldr (≪) [[[]]]
Suffixes of Prefixes
![Page 62: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/62.jpg)
59
foldr (⋆) []◦foldr (≪) [[[]]]= foldr (⊙) []
1. (≪) = \x h → [[]] : map (\hs → (x : (head hs)) : hs) h2. (⋆) = \x h → filter (/=[]) x) ++ h
Suffixes of Prefixes
if... foldr (⋆) [] (x ≪ z) = x ⊙ (foldr (⋆) [] z)
![Page 63: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/63.jpg)
60
foldr (⋆) []◦foldr (≪) [[[]]]= foldr (⊙) []
1. (≪) = \x h → [[]] : map (\hs → (x : (head hs)) : hs) h2. (⋆) = \x h → filter (/=[]) x) ++ h
Suffixes of Prefixes
if... foldr (⋆) [] (x ≪ z) = x ⊙ (foldr (⋆) [] z)
... after 3 hours
![Page 64: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/64.jpg)
61
fusion
foldl
foldr
![Page 65: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/65.jpg)
62
1 2 3
1 2 3: : :
data ConsList a = [] | a : (ConsList a)
![Page 66: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/66.jpg)
63
1 2 3
1 2 3‡ ‡‡
data SnocList a = [] | (SnocList a) ‡ a
![Page 67: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/67.jpg)
64
1 2 3‡ ‡‡
f (▷) ee 1▷ 2▷ 3▷
![Page 68: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/68.jpg)
65
1 2 3‡ ‡‡
e 1▷ 2▷ 3▷
folds (▷) e [] = efolds (▷) e (ys ‡ y) = (folds (▷) e ys) ▷ y
foldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
![Page 69: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/69.jpg)
66
1 2 3‡ ‡‡
e 1▷ 2▷ 3▷
folds (▷) e [] = efolds (▷) e (ys ‡ y) = (folds (▷) e ys) ▷ y
1 2 3: : :
foldl (▷) e [] = efoldl (▷) e (x : xs) = foldl (▷) (e ▷ x) xs
![Page 70: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/70.jpg)
67
foldl (▷) e [] = efoldl (▷) e (x : xs) = foldl (▷) (e ▷ x) xsfoldl (▷) e (ys ‡ y) = (foldl (▷) e ys) ▷ y
foldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
![Page 71: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/71.jpg)
68
h is foldl with (▷) e: h [] = e h (ys‡y) = (h ys) ▷ y h ◦ (‡y) = (▷ y) ◦ h
h is foldr with (◁) e: h [] = e h (x:xs) = x ◁ (h xs) h ◦ (x:) = (x ◁) ◦ h
point
free
![Page 72: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/72.jpg)
69
interesting... h = foldr (◁) e = foldl (▷) e
if .... ?
thr 11
![Page 73: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/73.jpg)
70
別管 fold 了你有聽過
List Homomorphism 嗎?!
![Page 74: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/74.jpg)
List Homomorphism
71
•h (xs ++ ys) = (h xs) ⊕ (h ys)
•h ◦ (++ys) = (⊕ h ys) ◦ h
•... parallel
length :: [a] → Intmaximum :: (Ord a) ⇒ [a] → a
![Page 75: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/75.jpg)
to prove h is homo... thr 11
萬全
![Page 76: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/76.jpg)
73
3rd List Homo. Theorem
h is a list homomorphism if h can be foldr (◁) e and foldl (▷) efor some (◁), (▷) and e
![Page 77: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/77.jpg)
74
h ◦ (++ys)={ foldr-fusion, since (++ys) = foldr (∶) ys } foldr (◁) (h ys )={ foldr-fusion (backwards) } (⊕ h ys) ◦ foldr (◁) e = (⊕ h ys) ◦ h
For the second foldr-fusion h ys = e ⊕ h ys and (x ◁ y) ⊕ h ys = x ◁ (y ⊕ h ys)
h ◦ (‡z)={ foldr-fusion, since (‡z) = foldr (∶) [z]} foldr (◁) (h [z])={ foldr-fusion (backwards) } (▷ z) ◦ foldr (◁) e = (▷ z) ◦ h
For the second foldr-fusion z ◁ e = e ▷ z and (x ◁ y) ▷ z = x ◁ (y ▷ z)
h = foldr = foldl
h is homo.
![Page 78: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/78.jpg)
75
h ys = e ⊕ h ys and (x ◁ y) ⊕ h ys = x ◁ (y ⊕ h ys)
z ◁ e = e ▷ z and (x ◁ y) ▷ z = x ◁ (y ▷ z)
h = foldr = foldl
h is homo.
Requirement 1
Requirement 2
![Page 79: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/79.jpg)
h = foldr = foldl
h is list homo.
requirement 1
requirement 2(◁), (▷)
(⊕)
the way to go
![Page 80: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/80.jpg)
77
(x ◁ y) ▷ z = = = = == = x ◁ (y ▷ z)
z zzz
z
(x ◁ y) ▷ z = x ◁ (y ▷ z)
![Page 81: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/81.jpg)
78
(x ◁ y) ▷ z = = = = == = x ◁ (y ▷ z)
(1) (▷ z) => (⊕ (h ys))
z zzz
z
![Page 82: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/82.jpg)
79
(x ◁ y) ⊕ (h ys) = = = = == = x ◁ (y ⊕ (h ys))
(1) (▷ z) => (⊕ (h ys))
z zzz
z
![Page 83: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/83.jpg)
80
(x ◁ y) ⊕ (h ys) = = = = == = x ◁ (y ⊕ (h ys))
(1) (▷ z) => (⊕ (h ys))(2) z => free variables
z zzz
z
![Page 84: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/84.jpg)
81
(x ◁ y) ⊕ (h ys) = = = = == = x ◁ (y ⊕ (h ys))
(1) (▷ z) => (⊕ (h ys))(2) z => free variables
X1 X2X2X1
X1
![Page 85: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/85.jpg)
82
(x ◁ y) ⊕ (h ys) = = = = == = x ◁ (y ⊕ (h ys))
X1 X2X2X1
X1
(x ◁ y) ⊕ (h ys) = x ◁ (y ⊕ (h ys))
![Page 86: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/86.jpg)
83
(x ◁ y) ⊕ (h ys) = = = = == = x ◁ (y ⊕ (h ys))
X1 X2X2X1
X1
(x ◁ y) ⊕ (h ys) = x ◁ (y ⊕ (h ys))
h ys = e ⊕ h ys
requirement 2 (base case)
![Page 87: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/87.jpg)
84
(x ◁ y) ⊕ (h ys) = = = = == = x ◁ (y ⊕ (h ys))
(x ◁ y) ⊕ (h ys) = x ◁ (y ⊕ (h ys))
![Page 88: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/88.jpg)
ಠ_ಠthr 11
必死
tupling
![Page 89: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/89.jpg)
86
Algorithm
Efficient Program
Specification
• total functional• fold • fold-fusion• 3rd list homo.• hoard logic• logic/type
![Page 90: Program Language - Fall 2013](https://reader033.vdocuments.site/reader033/viewer/2022051706/58f3061f1a28abaa2b8b4719/html5/thumbnails/90.jpg)
Reference
87
• Constructing List Homomorphisms from Proofs. [Yun-Yan Chi, Shin-Cheng Mu]
• Introduction to Funcional Programming using Haskell [Richard Bird]