-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllOrNothing.agda
194 lines (147 loc) · 5.38 KB
/
AllOrNothing.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
module AllOrNothing where
open import Data.Nat public
open import Data.Nat.Properties public
open import Data.String using (String) public
open import Relation.Binary.PropositionalEquality using (_≡_; _≢_; refl; cong; sym; ≢-sym) public
Id : Set
Id = String
infixr 5 ƛ_⇒_
infixl 7 _·_
infix 9 `_
infixr 6 _⦂_
infixr 8 _⇒_
data Type : Set where
Int : Type
_⇒_ : Type → Type → Type
data Term : Set where
lit : ℕ → Term
`_ : Id → Term
ƛ_⇒_ : Id → Term → Term
_·_ : Term → Term → Term
_⦂_ : Term → Type → Term
infixl 5 _,_⦂_
data Context : Set where
∅ : Context
_,_⦂_ : Context → Id → Type → Context
infix 4 _∋_⦂_
data _∋_⦂_ : Context → Id → Type → Set where
Z : ∀ {Γ x A}
→ Γ , x ⦂ A ∋ x ⦂ A
S : ∀ {Γ x y A B}
→ x ≢ y
→ Γ ∋ x ⦂ A
→ Γ , y ⦂ B ∋ x ⦂ A
----------------------------------------------------------------------
--+ +--
--+ Traditional Bidirectional Typing +--
--+ +--
----------------------------------------------------------------------
-- we add App2 and switch the direction of Lit rule to inference
data Mode : Set where
c : Mode
i : Mode
infix 4 _⊢b_#_⦂_
data _⊢b_#_⦂_ : Context → Mode → Term → Type → Set where
⊢b-int : ∀ {Γ n}
→ Γ ⊢b i # (lit n) ⦂ Int
⊢b-var : ∀ {Γ x A}
→ Γ ∋ x ⦂ A
→ Γ ⊢b i # ` x ⦂ A
⊢b-ann : ∀ {Γ e A}
→ Γ ⊢b c # e ⦂ A
→ Γ ⊢b i # (e ⦂ A) ⦂ A
⊢b-lam-∞ : ∀ {Γ e x A B}
→ Γ , x ⦂ A ⊢b c # e ⦂ B
→ Γ ⊢b c # (ƛ x ⇒ e) ⦂ A ⇒ B
⊢b-app₁ : ∀ {Γ e₁ e₂ A B}
→ Γ ⊢b i # e₁ ⦂ A ⇒ B
→ Γ ⊢b c # e₂ ⦂ A
→ Γ ⊢b i # e₁ · e₂ ⦂ B
⊢b-app₂ : ∀ {Γ e₁ e₂ A B}
→ Γ ⊢b c # e₁ ⦂ A ⇒ B
→ Γ ⊢b i # e₂ ⦂ A
→ Γ ⊢b c # e₁ · e₂ ⦂ B
⊢b-sub : ∀ {Γ e A B}
→ Γ ⊢b i # e ⦂ A
→ A ≡ B
→ Γ ⊢b c # e ⦂ B
----------------------------------------------------------------------
--+ +--
--+ QTAS +--
--+ +--
----------------------------------------------------------------------
data Counter : Set where
∞ : Counter
Z : Counter
infix 4 _⊢d_#_⦂_
data _⊢d_#_⦂_ : Context → Counter → Term → Type → Set where
⊢d-int : ∀ {Γ i}
→ Γ ⊢d Z # (lit i) ⦂ Int
⊢d-var : ∀ {Γ x A}
→ Γ ∋ x ⦂ A
→ Γ ⊢d Z # ` x ⦂ A
⊢d-ann : ∀ {Γ e A}
→ Γ ⊢d ∞ # e ⦂ A
→ Γ ⊢d Z # (e ⦂ A) ⦂ A
⊢d-lam-∞ : ∀ {Γ e x A B}
→ Γ , x ⦂ A ⊢d ∞ # e ⦂ B
→ Γ ⊢d ∞ # (ƛ x ⇒ e) ⦂ A ⇒ B
⊢d-app₁ : ∀ {Γ e₁ e₂ A B}
→ Γ ⊢d Z # e₁ ⦂ A ⇒ B
→ Γ ⊢d ∞ # e₂ ⦂ A
→ Γ ⊢d Z # e₁ · e₂ ⦂ B
⊢d-app₂ : ∀ {Γ e₁ e₂ A B}
→ Γ ⊢d ∞ # e₁ ⦂ A ⇒ B
→ Γ ⊢d Z # e₂ ⦂ A
→ Γ ⊢d ∞ # e₁ · e₂ ⦂ B
⊢d-sub : ∀ {Γ e A B}
→ Γ ⊢d Z # e ⦂ A
→ A ≡ B
→ Γ ⊢d ∞ # e ⦂ B
----------------------------------------------------------------------
--+ +--
--+ Sound and Complete +--
--+ +--
----------------------------------------------------------------------
data R : Mode → Counter → Set where
R-Z : R i Z
R-∞ : R c ∞
complete : ∀ {Γ m n e A}
→ Γ ⊢b m # e ⦂ A
→ R m n
→ Γ ⊢d n # e ⦂ A
complete (⊢b-var x) R-Z = ⊢d-var x
complete (⊢b-ann ⊢e) R-Z = ⊢d-ann (complete ⊢e R-∞)
complete (⊢b-app₁ ⊢e ⊢e₁) R-Z = ⊢d-app₁ (complete ⊢e R-Z) (complete ⊢e₁ R-∞)
complete ⊢b-int R-Z = ⊢d-int
complete (⊢b-lam-∞ ⊢e) R-∞ = ⊢d-lam-∞ (complete ⊢e R-∞)
complete (⊢b-app₂ ⊢e ⊢e₁) R-∞ = ⊢d-app₂ (complete ⊢e R-∞) (complete ⊢e₁ R-Z)
complete (⊢b-sub ⊢e x) R-∞ = ⊢d-sub (complete ⊢e R-Z) x
sound : ∀ {Γ m n e A}
→ Γ ⊢d n # e ⦂ A
→ R m n
→ Γ ⊢b m # e ⦂ A
sound ⊢d-int R-Z = ⊢b-int
sound (⊢d-var x) R-Z = ⊢b-var x
sound (⊢d-ann ⊢e) R-Z = ⊢b-ann (sound ⊢e R-∞)
sound (⊢d-app₁ ⊢e ⊢e₁) R-Z = ⊢b-app₁ (sound ⊢e R-Z) (sound ⊢e₁ R-∞)
sound (⊢d-lam-∞ ⊢e) R-∞ = ⊢b-lam-∞ (sound ⊢e R-∞)
sound (⊢d-app₂ ⊢e ⊢e₁) R-∞ = ⊢b-app₂ (sound ⊢e R-∞) (sound ⊢e₁ R-Z)
sound (⊢d-sub ⊢e x) R-∞ = ⊢b-sub (sound ⊢e R-Z) x
-- corollaries
sound-inf : ∀ {Γ e A}
→ Γ ⊢d Z # e ⦂ A
→ Γ ⊢b i # e ⦂ A
sound-inf ⊢e = sound ⊢e R-Z
sound-chk : ∀ {Γ e A}
→ Γ ⊢d ∞ # e ⦂ A
→ Γ ⊢b c # e ⦂ A
sound-chk ⊢e = sound ⊢e R-∞
complete-inf : ∀ {Γ e A}
→ Γ ⊢b i # e ⦂ A
→ Γ ⊢d Z # e ⦂ A
complete-inf ⊢e = complete ⊢e R-Z
complete-chk : ∀ {Γ e A}
→ Γ ⊢b c # e ⦂ A
→ Γ ⊢d ∞ # e ⦂ A
complete-chk ⊢e = complete ⊢e R-∞