annotate Paper/src/stack-product.agda @ 12:e20725cd6d8a default tip

fix slides
author ryokka
date Mon, 21 May 2018 00:29:29 +0900
parents bf2887cd22c1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
1 module stack-product where
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
2
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
3 open import product
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
4 open import Data.Product
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
5 open import Data.Nat
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
6 open import Function using (id)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
7 open import Relation.Binary.PropositionalEquality
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
8
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
9 -- definition based from Gears(209:5708390a9d88) src/parallel_execution
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
10 goto = executeCS
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
11
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
12 data Bool : Set where
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
13 True : Bool
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
14 False : Bool
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
15
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
16 data Maybe (a : Set) : Set where
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
17 Nothing : Maybe a
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
18 Just : a -> Maybe a
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
19
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
20
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
21 record Stack {a t : Set} (stackImpl : Set) : Set where
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
22 field
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
23 stack : stackImpl
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
24 push : CodeSegment (stackImpl × a × (CodeSegment stackImpl t)) t
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
25 pop : CodeSegment (stackImpl × (CodeSegment (stackImpl × Maybe a) t)) t
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
26
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
27
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
28 data Element (a : Set) : Set where
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
29 cons : a -> Maybe (Element a) -> Element a
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
30
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
31 datum : {a : Set} -> Element a -> a
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
32 datum (cons a _) = a
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
33
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
34 next : {a : Set} -> Element a -> Maybe (Element a)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
35 next (cons _ n) = n
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
36
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
37 record SingleLinkedStack (a : Set) : Set where
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
38 field
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
39 top : Maybe (Element a)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
40 open SingleLinkedStack
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
41
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
42 emptySingleLinkedStack : {a : Set} -> SingleLinkedStack a
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
43 emptySingleLinkedStack = record {top = Nothing}
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
44
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
45
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
46
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
47
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
48 pushSingleLinkedStack : {a t : Set} -> CodeSegment ((SingleLinkedStack a) × a × (CodeSegment (SingleLinkedStack a) t)) t
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
49 pushSingleLinkedStack = cs push
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
50 where
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
51 push : {a t : Set} -> ((SingleLinkedStack a) × a × (CodeSegment (SingleLinkedStack a) t)) -> t
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
52 push (stack , datum , next) = goto next stack1
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
53 where
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
54 element = cons datum (top stack)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
55 stack1 = record {top = Just element}
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
56
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
57 popSingleLinkedStack : {a t : Set} -> CodeSegment (SingleLinkedStack a × (CodeSegment (SingleLinkedStack a × Maybe a) t)) t
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
58 popSingleLinkedStack = cs pop
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
59 where
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
60 pop : {a t : Set} -> (SingleLinkedStack a × (CodeSegment (SingleLinkedStack a × Maybe a) t)) -> t
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
61 pop (record { top = Nothing } , nextCS) = goto nextCS (emptySingleLinkedStack , Nothing)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
62 pop (record { top = Just x } , nextCS) = goto nextCS (stack1 , (Just datum1))
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
63 where
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
64 datum1 = datum x
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
65 stack1 = record { top = (next x) }
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
66
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
67
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
68
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
69
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
70
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
71 createSingleLinkedStack : {a b : Set} -> Stack {a} {b} (SingleLinkedStack a)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
72 createSingleLinkedStack = record { stack = emptySingleLinkedStack
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
73 ; push = pushSingleLinkedStack
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
74 ; pop = popSingleLinkedStack
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
75 }
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
76
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
77
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
78
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
79
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
80 test01 : {a : Set} -> CodeSegment (SingleLinkedStack a × Maybe a) Bool
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
81 test01 = cs test01'
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
82 where
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
83 test01' : {a : Set} -> (SingleLinkedStack a × Maybe a) -> Bool
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
84 test01' (record { top = Nothing } , _) = False
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
85 test01' (record { top = Just x } , _) = True
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
86
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
87
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
88 test02 : {a : Set} -> CodeSegment (SingleLinkedStack a) (SingleLinkedStack a × Maybe a)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
89 test02 = cs test02'
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
90 where
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
91 test02' : {a : Set} -> SingleLinkedStack a -> (SingleLinkedStack a × Maybe a)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
92 test02' stack = goto popSingleLinkedStack (stack , (cs id))
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
93
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
94
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
95 test03 : {a : Set} -> CodeSegment a (SingleLinkedStack a)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
96 test03 = cs test03'
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
97 where
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
98 test03' : {a : Set} -> a -> SingleLinkedStack a
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
99 test03' a = goto pushSingleLinkedStack (emptySingleLinkedStack , a , (cs id))
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
100
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
101
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
102 lemma : {A : Set} {a : A} -> goto (test03 ◎ test02 ◎ test01) a ≡ False
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
103 lemma = refl
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
104
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
105
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
106 n-push : {A : Set} {a : A} -> CodeSegment (ℕ × SingleLinkedStack A) (ℕ × SingleLinkedStack A)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
107 n-push {A} {a} = cs (push {A} {a})
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
108 where
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
109 push : {A : Set} {a : A} -> (ℕ × SingleLinkedStack A) -> (ℕ × SingleLinkedStack A)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
110 push {A} {a} (zero , s) = (zero , s)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
111 push {A} {a} (suc n , s) = goto pushSingleLinkedStack (s , a , {!!} {- n-push -}) -- needs subtype
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
112
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
113
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
114 {-
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
115
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
116 n-push : {A : Set} {a : A} -> Nat -> SingleLinkedStack A -> SingleLinkedStack A
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
117 n-push zero s = s
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
118 n-push {A} {a} (suc n) s = pushSingleLinkedStack (n-push {A} {a} n s) a (\s -> s)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
119
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
120 n-pop : {A : Set} {a : A} -> Nat -> SingleLinkedStack A -> SingleLinkedStack A
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
121 n-pop zero s = s
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
122 n-pop {A} {a} (suc n) s = popSingleLinkedStack (n-pop {A} {a} n s) (\s _ -> s)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
123
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
124 open ≡-Reasoning
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
125
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
126 push-pop-equiv : {A : Set} {a : A} (s : SingleLinkedStack A) -> popSingleLinkedStack (pushSingleLinkedStack s a (\s -> s)) (\s _ -> s) ≡ s
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
127 push-pop-equiv s = refl
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
128
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
129 push-and-n-pop : {A : Set} {a : A} (n : Nat) (s : SingleLinkedStack A) -> n-pop {A} {a} (suc n) (pushSingleLinkedStack s a id) ≡ n-pop {A} {a} n s
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
130 push-and-n-pop zero s = refl
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
131 push-and-n-pop {A} {a} (suc n) s = begin
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
132 n-pop (suc (suc n)) (pushSingleLinkedStack s a id)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
133 ≡⟨ refl ⟩
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
134 popSingleLinkedStack (n-pop (suc n) (pushSingleLinkedStack s a id)) (\s _ -> s)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
135 ≡⟨ cong (\s -> popSingleLinkedStack s (\s _ -> s)) (push-and-n-pop n s) ⟩
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
136 popSingleLinkedStack (n-pop n s) (\s _ -> s)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
137 ≡⟨ refl ⟩
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
138 n-pop (suc n) s
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
139
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
140
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
141
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
142 n-push-pop-equiv : {A : Set} {a : A} (n : Nat) (s : SingleLinkedStack A) -> (n-pop {A} {a} n (n-push {A} {a} n s)) ≡ s
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
143 n-push-pop-equiv zero s = refl
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
144 n-push-pop-equiv {A} {a} (suc n) s = begin
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
145 n-pop (suc n) (n-push (suc n) s)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
146 ≡⟨ refl ⟩
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
147 n-pop (suc n) (pushSingleLinkedStack (n-push n s) a (\s -> s))
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
148 ≡⟨ push-and-n-pop n (n-push n s) ⟩
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
149 n-pop n (n-push n s)
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
150 ≡⟨ n-push-pop-equiv n s ⟩
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
151 s
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
152
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
153
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
154
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
155 n-push-pop-equiv-empty : {A : Set} {a : A} -> (n : Nat) -> n-pop {A} {a} n (n-push {A} {a} n emptySingleLinkedStack) ≡ emptySingleLinkedStack
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
156 n-push-pop-equiv-empty n = n-push-pop-equiv n emptySingleLinkedStack
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
157 -}
bf2887cd22c1 fix Paper
ryokka
parents:
diff changeset
158