-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExamplesReaderT.agda
189 lines (123 loc) · 3.68 KB
/
ExamplesReaderT.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
module ExamplesReaderT where
open import Agda.Primitive
open import Relation.Binary.PropositionalEquality using (_≡_; _≢_; refl)
open import Data.Unit using (⊤; tt)
open import Data.Empty using (⊥; ⊥-elim)
open import Function using (_∘_)
open import Monad.Maybe
open import Monad.Reader
data World : Set where
w1 w2 : World
IntMonad = MonadReaderT {World} {{MonadMaybe}}
-- intensional
∧ : ∀ {a} → Set a → Set a
∧ = ReaderT World Maybe
-- extensional
∨ : ∀ {a} {A : Set a} → ∧ A → World → Maybe A
∨ = runReaderT
-- extension in the world w
∨_/_ : ∀ {a} {A : Set a} → ∧ A → World → Maybe A
∨ ma / w = ∨ ma w
rigid = return
infixl 5 _⋆_
-- simple lifting
_⋆_ : ∀ {i k} {A : Set i} {B : Set k} → ∧ A → (A → B) → ∧ B
ma ⋆ f = liftM f ma
data Human : Set where
John Mary Bill : Human
----------------------------------------------------
-- Example: intension for the tallest human
itH : ∧ Human
itH = mkReaderT f
where
f : World → Maybe Human
f w1 = just John
f w2 = just Mary
_ : ∨ itH w1 ≡ just John
_ = refl
_ : ∨ itH w2 ≡ just Mary
_ = refl
_ : ∨ itH ≡ λ w → ∨ itH w
_ = refl
-- -- iRun' is itself intensional
-- -- h Run/ w = h runs in the world w
postulate
_Run : Human → Set
Jr : John Run -- proof of "John runs"
Mr : Mary Run
Br : Bill Run
_Run/_ : Human → World → Maybe Set
h Run/ w = return (h Run)
iRun' : Human → (∧ Set)
iRun' h = mkReaderT (λ w → return (h Run))
_ : ∨ (itH >>= iRun') / w1 ≡ just (John Run)
_ = refl
_ : ∨ (itH >>= iRun') / w2 ≡ just (Mary Run)
_ = refl
_ : ∨ (rigid John >>= iRun') / w1 ≡ just (John Run)
_ = refl
_ : ∨ (rigid John >>= iRun') / w2 ≡ just (John Run)
_ = refl
-- composition of calculations -------------------------------------------------
-- (using Maybe and Monad transformer)
iFather : Human → (∧ Human)
iFather h = mkReaderT (Father h)
where
Father : Human → World → Maybe Human
Father John w1 = just Bill
Father Mary w1 = just Bill
Father Bill w1 = nothing
Father John w2 = just Bill
Father Mary w2 = just John
Father Bill w2 = nothing
-- the father of the tallest human runs
_ : ∨ (itH >>= iFather >>= iRun') / w1 ≡ (Bill Run/ w1)
_ = refl
_ : ∨ (itH >>= iFather >>= iRun') / w1 ≡ just (Bill Run)
_ = refl
_ : ∨ (itH >>= iFather >>= iRun') / w2 ≡ (John Run/ w2)
_ = refl
_ : ∨ (itH >>= iFather >>= iRun') / w2 ≡ just (John Run)
_ = refl
Father : Human → Maybe Human
Father John = just Bill
Father Mary = just Bill
Father Bill = nothing
-- the father of the tallest human runs
-- _ : ∨ (itH ⋆ Father ⋆ Run) / w1 ≡ (Run Bill)
-- _ = refl
-- _ : ∨ (itH ⋆ Father ⋆ Run) / w1
-- _ = Br
-- singleton "John"
-- ⊤ is the intension (=meaning) of singleton
-- or unique object
sJ : ⊤ → Human
sJ tt = John
isJ : ⊤ → ∧ Human
isJ tt = rigid John
_ : ∨ (rigid tt >>= isJ) / w1 ≡ just John
_ = refl
_ : ∨ (liftM sJ (rigid tt)) / w1 ≡ just John
_ = refl
_ : ∨ ((rigid tt) ⋆ sJ) / w1 ≡ return John
_ = refl
_ : ∨ (rigid tt >>= isJ >>= iRun') / w1 ≡ John Run/ w1
_ = refl
_ : ∨ (rigid tt >>= (λ x → rigid John) >>= iRun') / w1 ≡ John Run/ w1
_ = refl
_ : ∨ (rigid tt >>= isJ >>= iRun') / w1 ≡ just (John Run)
_ = refl
_ : ∨ (rigid tt >>= (λ x → rigid John) >>= iRun') / w1 ≡ just (John Run)
_ = refl
itH' : ⊤ → ∧ Human
itH' tt = mkReaderT f
where
f : World → Maybe Human
f w1 = just John
f w2 = just Mary
_ : ∨ (rigid tt >>= itH') / w1 ≡ just John
_ = refl
_ : ∨ (rigid tt >>= itH') / w2 ≡ just Mary
_ = refl
_ : ∨ (rigid tt >>= itH') / w1 ≡ return (sJ tt)
_ = refl