Upload
yun-yan-chi
View
147
Download
0
Embed Size (px)
Citation preview
Program LanguageProgram Derivation
郗昀彥 (代)
3
Construction?
Derivation!
Program
4
Algorithm
5
Efficient Program
Specification
Algorithm
6
Specification
Efficient Program
Algorithm
•Hard•Depend on Tools•Correctness by Testing
7
Efficient Program
Specification
Algorithm
• Straightforward•Math. or Logic
8
Algorithm
Efficient Program
Specification
•Correctness by Verification•Model Checking•Automata
9
Algorithm
Efficient Program
Specification • Program Construction•Derivation• Synthesis•Hoare Logic
10
ProgramDerivation
11
• fold
• fold fusion
• list homomorphism
• 3rd list homomorphism theorem
foldr
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
1 2 3 4 5
1 2 3 4 5: : : : :
data List a = [] | a : (List a)
1 2 3 4 5 [ ]: : : : :
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
1 2 3 4 5: : : : :
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
[ ]
1 2 3 4 5: : : : :
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
[ ]
1 2 3 4 5: : : : :
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
[ ]
1 2 3 4 5: : : : :
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
[ ]
1 2 3 4 5: : : : :
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
[ ]
1 2 3 4 5: : : : : e
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
1 2 3 4 5: : : : e◁
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
1 2 3 4 5: : : e◁◁
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
1 2 3 4 5: : e◁◁◁
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
1 2 3 4 5: e◁◁◁◁
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
foldr :: (a → b → b) → b → [a] → bfoldr (◁) e [] = efoldr (◁) e (x : xs) = x ◁ (foldr (◁) e xs)
1 2 3 4 5 e◁◁◁◁◁
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
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
1 + (2 + (... + (1000 + 0)...)))
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 ?(◁) ?
foldl
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
1 2 3 [ ]: : :
foldl (▷) Para0 Para1
{{
e
1 2 3 [ ]: : :
foldl (▷) Para0 Para1
{{
e 1▷
1 2 3 [ ]: : :
foldl (▷) Para0 Para1
{{
e 1▷ 2▷
1 2 3 [ ]: : :
foldl (▷) Para0 Para1
{{
e 1▷ 2▷ 3▷
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
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
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 ?(▷) ?
fusion
42
map f ◦ map g
42
map (f ◦ g)
43
filter q ◦ filter p
43
filter (q ∧ p)
44
foldr-fusion
f ◦ foldr (◁) e
44
foldr-fusion
⇐ f (x ◁ z) = x ≪ (f z)
foldr (≪) (f e)
45
f $ foldr (◁) e []= { definition of foldr } f e= { definition of foldr } foldr (≪) (f e) []
foldr-fusion
f ◦ foldr (◁) e = foldr (≪) (f e)
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)
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)
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)
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)
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)
51
http://www.openrice.com/restaurant/commentdetail.htm?commentid=2106508
範例
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
53
inits :: [a] → [[a]]inits [] = [[]]inits (x:xs) = [] : map (x:) (inits xs)
filter (/=[])◦concat◦map tails◦inits
Suffixes of Prefixes
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
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
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
57
filter (/=[])◦concat◦foldr (≪) [[[]]]
filter (/=[]) ◦ concat= g ◦ foldr (++) [] foldr-fusion= foldr (⋆) []
1. g = filter (/=[])2. (⋆) = \x h → filter (/=[]) x) ++ h
Suffixes of Prefixes
58
filter (/=[]) ◦ concat= g ◦ foldr (++) [] foldr-fusion= foldr (⋆) []
1. g = filter (/=[])2. (⋆) = \x h → filter (/=[]) x) ++ h
filter (/=[])◦concat◦foldr (≪) [[[]]]
Suffixes of Prefixes
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)
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
61
fusion
foldl
foldr
62
1 2 3
1 2 3: : :
data ConsList a = [] | a : (ConsList a)
63
1 2 3
1 2 3‡ ‡‡
data SnocList a = [] | (SnocList a) ‡ a
64
1 2 3‡ ‡‡
f (▷) ee 1▷ 2▷ 3▷
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)
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
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)
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
69
interesting... h = foldr (◁) e = foldl (▷) e
if .... ?
thr 11
70
別管 fold 了你有聽過
List Homomorphism 嗎?!
List Homomorphism
71
•h (xs ++ ys) = (h xs) ⊕ (h ys)
•h ◦ (++ys) = (⊕ h ys) ◦ h
•... parallel
length :: [a] → Intmaximum :: (Ord a) ⇒ [a] → a
to prove h is homo... thr 11
萬全
73
3rd List Homo. Theorem
h is a list homomorphism if h can be foldr (◁) e and foldl (▷) efor some (◁), (▷) and e
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.
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
h = foldr = foldl
h is list homo.
requirement 1
requirement 2(◁), (▷)
(⊕)
the way to go
77
(x ◁ y) ▷ z = = = = == = x ◁ (y ▷ z)
z zzz
z
(x ◁ y) ▷ z = x ◁ (y ▷ z)
78
(x ◁ y) ▷ z = = = = == = x ◁ (y ▷ z)
(1) (▷ z) => (⊕ (h ys))
z zzz
z
79
(x ◁ y) ⊕ (h ys) = = = = == = x ◁ (y ⊕ (h ys))
(1) (▷ z) => (⊕ (h ys))
z zzz
z
80
(x ◁ y) ⊕ (h ys) = = = = == = x ◁ (y ⊕ (h ys))
(1) (▷ z) => (⊕ (h ys))(2) z => free variables
z zzz
z
81
(x ◁ y) ⊕ (h ys) = = = = == = x ◁ (y ⊕ (h ys))
(1) (▷ z) => (⊕ (h ys))(2) z => free variables
X1 X2X2X1
X1
82
(x ◁ y) ⊕ (h ys) = = = = == = x ◁ (y ⊕ (h ys))
X1 X2X2X1
X1
(x ◁ y) ⊕ (h ys) = x ◁ (y ⊕ (h ys))
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)
84
(x ◁ y) ⊕ (h ys) = = = = == = x ◁ (y ⊕ (h ys))
(x ◁ y) ⊕ (h ys) = x ◁ (y ⊕ (h ys))
ಠ_ಠthr 11
必死
tupling
86
Algorithm
Efficient Program
Specification
• total functional• fold • fold-fusion• 3rd list homo.• hoard logic• logic/type
Reference
87
• Constructing List Homomorphisms from Proofs. [Yun-Yan Chi, Shin-Cheng Mu]
• Introduction to Funcional Programming using Haskell [Richard Bird]