-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calc.agda
205 lines (169 loc) · 6.96 KB
/
Calc.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
module Calc where
open import Data.Integer
open import Relation.Binary.PropositionalEquality
open import Relation.Nullary
open import Relation.Nullary.Negation
open import Data.Empty
open import Data.Product
open import Data.Sum
open import Data.Unit
data Expr : Set where
Lit : ℤ → Expr
Add : Expr → Expr → Expr
Print : Expr → Expr
Seq : Expr → Expr → Expr
data Output : Set where
∅ : Output -- Empty output
_∷_ : ℤ → Output → Output -- Put another item in an existing output
-- First one-step relation
data ⟨_,_⟩⟶⟨_,_⟩ : Expr → Output → Expr → Output → Set where
E-Add-1 : ∀ {a a′ b ω ω′} →
⟨ a , ω ⟩⟶⟨ a′ , ω′ ⟩ →
-------------
⟨ Add a b , ω ⟩⟶⟨ Add a′ b , ω′ ⟩
E-Add-2 : ∀ {i b b′ ω ω′} →
⟨ b , ω ⟩⟶⟨ b′ , ω′ ⟩ →
-------------
⟨ Add (Lit i) b , ω ⟩⟶⟨ Add (Lit i) b′ , ω′ ⟩
E-Add : ∀ {i j k ω} →
k ≡ i + j →
-------------
⟨ Add (Lit i) (Lit j) , ω ⟩⟶⟨ Lit k , ω ⟩
E-Print-1 : ∀ {a a′ ω ω′} →
⟨ a , ω ⟩⟶⟨ a′ , ω′ ⟩ →
-------------
⟨ Print a , ω ⟩⟶⟨ Print a′ , ω′ ⟩
E-Print : ∀ {i ω} →
-------------
⟨ Print (Lit i) , ω ⟩⟶⟨ Lit i , (i ∷ ω) ⟩
E-Seq-1 : ∀ {a a′ b ω ω′} →
⟨ a , ω ⟩⟶⟨ a′ , ω′ ⟩ →
-------------
⟨ Seq a b , ω ⟩⟶⟨ Seq a′ b , ω′ ⟩
E-Seq-2 : ∀ {i b b′ ω ω′} →
⟨ b , ω ⟩⟶⟨ b′ , ω′ ⟩ →
-------------
⟨ Seq (Lit i) b , ω ⟩⟶⟨ Seq (Lit i) b′ , ω′ ⟩
E-Seq : ∀ {i j ω} →
-------------
⟨ Seq (Lit i) (Lit j) , ω ⟩⟶⟨ Lit j , ω ⟩
data ⟨_,_⟩⟶*⟨_,_⟩ : Expr → Output → Expr → Output → Set where
E-Done : ∀ {a ω} →
-------------
⟨ a , ω ⟩⟶*⟨ a , ω ⟩
E-Step : ∀ {a a′ a′′ ω ω′ ω′′} →
⟨ a , ω ⟩⟶⟨ a′ , ω′ ⟩ →
⟨ a′ , ω′ ⟩⟶*⟨ a′′ , ω′′ ⟩ →
-------------
⟨ a , ω ⟩⟶*⟨ a′′ , ω′′ ⟩
--
-- The new one-step relation
data ⟨_,_⟩⟶′⟨_,_⟩ : Expr → Output → Expr → Output → Set where
E-Add-1′ : ∀ {a a′ b ω ω′} →
⟨ a , ω ⟩⟶′⟨ a′ , ω′ ⟩ →
-------------
⟨ Add a b , ω ⟩⟶′⟨ Add a′ b , ω′ ⟩
E-Add-2′ : ∀ {i b b′ ω ω′} →
⟨ b , ω ⟩⟶′⟨ b′ , ω′ ⟩ →
-------------
⟨ Add (Lit i) b , ω ⟩⟶′⟨ Add (Lit i) b′ , ω′ ⟩
E-Add′ : ∀ {i j k ω} →
k ≡ i + j →
-------------
⟨ Add (Lit i) (Lit j) , ω ⟩⟶′⟨ Lit k , ω ⟩
E-Print-1′ : ∀ {a a′ ω ω′} →
⟨ a , ω ⟩⟶′⟨ a′ , ω′ ⟩ →
-------------
⟨ Print a , ω ⟩⟶′⟨ Print a′ , ω′ ⟩
E-Print′ : ∀ {i ω} →
-------------
⟨ Print (Lit i) , ω ⟩⟶′⟨ Lit i , (i ∷ ω) ⟩
E-Seq-1′ : ∀ {a a′ b ω ω′} →
⟨ a , ω ⟩⟶′⟨ a′ , ω′ ⟩ →
-------------
⟨ Seq a b , ω ⟩⟶′⟨ b , ω′ ⟩
E-Seq′ : ∀ {i b ω} →
⟨ Seq (Lit i) b , ω ⟩⟶′⟨ b , ω ⟩
data ⟨_,_⟩⟶′*⟨_,_⟩ : Expr → Output → Expr → Output → Set where
E-Done′ : ∀ {a ω} →
-------------
⟨ a , ω ⟩⟶′*⟨ a , ω ⟩
E-Step′ : ∀ {a a′ a′′ ω ω′ ω′′} →
⟨ a , ω ⟩⟶′⟨ a′ , ω′ ⟩ →
⟨ a′ , ω′ ⟩⟶′*⟨ a′′ , ω′′ ⟩ →
-------------
⟨ a , ω ⟩⟶′*⟨ a′′ , ω′′ ⟩
--
one-step′ : ∀ {a a′ ω ω′} →
⟨ a , ω ⟩⟶′⟨ a′ , ω′ ⟩ →
⟨ a , ω ⟩⟶′*⟨ a′ , ω′ ⟩
one-step′ p = E-Step′ p E-Done′
one-step : ∀ {a a′ ω ω′} →
⟨ a , ω ⟩⟶⟨ a′ , ω′ ⟩ →
⟨ a , ω ⟩⟶*⟨ a′ , ω′ ⟩
one-step p = E-Step p E-Done
lift′* : ∀ {a a′ ω ω′} {f : Expr → Expr} →
(∀ {a c ω ω′′} → ⟨ a , ω ⟩⟶′⟨ c , ω′′ ⟩ →
⟨ f a , ω ⟩⟶′⟨ f c , ω′′ ⟩
) →
⟨ a , ω ⟩⟶′*⟨ a′ , ω′ ⟩ →
⟨ f a , ω ⟩⟶′*⟨ f a′ , ω′ ⟩
lift′* _ E-Done′ = E-Done′
lift′* op (E-Step′ x p) =
let z = op {_} x
in
E-Step′ z (lift′* op p)
data Is-Value : Expr → Set where
Is-Value-Lit : ∀ {i} →
-------------
Is-Value (Lit i)
value-not-step : ∀ {a b ω ω′} →
Is-Value a →
¬ (⟨ a , ω ⟩⟶⟨ b , ω′ ⟩)
value-not-step Is-Value-Lit ()
value-or-can-step : ∀ {a ω} →
(Is-Value a) ⊎ (∃[ a′ ] ∃[ ω′ ] ⟨ a , ω ⟩⟶⟨ a′ , ω′ ⟩)
value-or-can-step {Lit x} = inj₁ Is-Value-Lit
value-or-can-step {Add a b} {ω} with value-or-can-step {a} {ω}
value-or-can-step {Add .(Lit _) b} {ω} | inj₁ Is-Value-Lit with value-or-can-step {b} {ω}
value-or-can-step {Add .(Lit _) .(Lit _)} {ω} | inj₁ Is-Value-Lit | inj₁ Is-Value-Lit = inj₂ (Lit _ , ω , E-Add refl)
value-or-can-step {Add .(Lit _) b} {ω} | inj₁ Is-Value-Lit | inj₂ (fst , fst₁ , snd) = inj₂ (Add _ fst , fst₁ , E-Add-2 snd)
value-or-can-step {Add a b} {ω} | inj₂ (fst , fst₁ , snd) = inj₂ (Add fst b , fst₁ , E-Add-1 snd)
value-or-can-step {Print a} {ω} with value-or-can-step {a} {ω}
... | inj₁ Is-Value-Lit = inj₂ (Lit _ , _ , E-Print)
... | inj₂ (fst , fst₁ , snd) = inj₂ (Print fst , fst₁ , E-Print-1 snd)
value-or-can-step {Seq a b} {ω} with value-or-can-step {a} {ω}
value-or-can-step {Seq .(Lit _) b} {ω} | inj₁ Is-Value-Lit with value-or-can-step {b} {ω}
value-or-can-step {Seq .(Lit _) .(Lit _)} {ω} | inj₁ Is-Value-Lit | inj₁ Is-Value-Lit = inj₂ (Lit _ , ω , E-Seq)
value-or-can-step {Seq .(Lit _) b} {ω} | inj₁ Is-Value-Lit | inj₂ (fst , fst₁ , snd) = inj₂ (Seq (Lit _) fst , fst₁ , E-Seq-2 snd)
value-or-can-step {Seq a b} {ω} | inj₂ (fst , fst₁ , snd) = inj₂ (Seq fst b , fst₁ , E-Seq-1 snd)
open import Data.Nat
---- {
-- Numeric literals as integers
record Number {a} (A : Set a) : Set a where
field fromNat : ℕ → A
open Number {{...}} public
{-# BUILTIN FROMNAT fromNat #-}
instance
NumInt : Number ℤ
NumInt .Number.fromNat zero = +0
NumInt .Number.fromNat (suc n) = Data.Integer.suc (fromNat n)
---- }
counterexample : ∃[ a ] ∃[ v ] ∃[ ω′ ]
Is-Value v ×
⟨ a , ∅ ⟩⟶*⟨ v , ω′ ⟩ ×
¬ (⟨ a , ∅ ⟩⟶′*⟨ v , ω′ ⟩)
counterexample =
a , Lit 3 ,
ω′ , Is-Value-Lit ,
E-Step (E-Seq-1 (E-Add-1 E-Print))
(E-Step (E-Seq-1 (E-Add-2 E-Print))
(E-Step (E-Seq-1 (E-Add refl))
(E-Step (E-Seq-2 E-Print) (E-Step E-Seq E-Done)))) ,
no-derivation
where
a = Seq (Add (Print (Lit 1)) (Print (Lit 2))) (Print (Lit 3))
ω′ = 3 ∷ (2 ∷ (1 ∷ ∅))
no-derivation :
¬ (⟨ a , ∅ ⟩⟶′*⟨ Lit 3 , ω′ ⟩)
no-derivation (E-Step′ (E-Seq-1′ (E-Add-1′ E-Print′)) (E-Step′ E-Print′ (E-Step′ () p)))