-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArgPrelude.agda
177 lines (138 loc) · 5.03 KB
/
ArgPrelude.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
module ArgPrelude where
open import Agda.Builtin.Float
open import Data.Bool
open import Data.Empty
open import Data.Float public hiding (_==_; _-_; _+_)
open import Data.Integer hiding (_*_)
open import Data.List
open import Data.Maybe
open import Data.Nat as ℕ using (suc; ℕ; _∸_; _⊔_)
open import Data.Product
open import Data.String as S using (String) renaming (_++_ to _+++_)
open import Data.Unit
open import Function using (id)
open import Level public renaming (zero to lzero; suc to lsuc; _⊔_ to _l⊔_)
open import Relation.Binary
open import Relation.Binary.PropositionalEquality public using (_≡_; _≢_; refl)
open import Relation.Nullary
open import WLPretty public
-- boolean equality
record BEq {ℓ} (A B : Set ℓ): Set ℓ where
infixl 7 _=ᵇ_
field
_=ᵇ_ : A → B → Bool
-- isEquivalence = IsEquivalence _=ᵇ_
open BEq {{...}} public
-- boolean filter
filterb : ∀ {ℓ} {A : Set ℓ} → (P : A → Bool) → List A → List A
filterb P [] = []
filterb P (x ∷ xs) with P x
... | true = x ∷ filterb P xs
... | false = filterb P xs
-- preliminary
data Proposition : Set where
mkProp : String → Proposition
NOT : Proposition → Proposition
_AND_ : Proposition → Proposition → Proposition
_OR_ : Proposition → Proposition → Proposition
private
_=P_ : Proposition → Proposition → Bool
(mkProp x) =P (mkProp y) = x S.== y
NOT x =P NOT y = x =P y
(x₁ AND x₂) =P (y₁ AND y₂) = (x₁ =P y₁) ∧ (x₂ =P y₂)
(x₁ OR x₂) =P (y₁ OR y₂) = (x₁ =P y₁) ∧ (x₂ =P y₂)
_ =P _ = false
infix 4 _≡P_ _≟P_
_≡P_ : Proposition → Proposition → Set
(mkProp x) ≡P (mkProp y) = x ≡ y
NOT x ≡P NOT y = x ≡P y
(x₁ AND x₂) ≡P (y₁ AND y₂) = (x₁ ≡P y₁) × (x₂ ≡P y₂)
(x₁ OR x₂) ≡P (y₁ OR y₂) = (x₁ ≡P y₁) × (x₂ ≡P y₂)
_ ≡P _ = ⊥
_≟P_ : Decidable _≡P_
(mkProp x) ≟P (mkProp y) = x S.≟ y
NOT x ≟P NOT y = x ≟P y
(x₁ AND x₂) ≟P (y₁ AND y₂) with (x₁ ≟P y₁) | (x₂ ≟P y₂)
... | yes p₁ | yes p₂ = yes (p₁ , p₂)
... | yes _ | no ¬p₂ = no λ x → ¬p₂ (proj₂ x)
... | no ¬p₁ | yes _ = no λ x → ¬p₁ (proj₁ x)
... | no ¬p₁ | no _ = no λ x → ¬p₁ (proj₁ x)
(x₁ OR x₂) ≟P (y₁ OR y₂) with (x₁ ≟P y₁) | (x₂ ≟P y₂)
... | yes p₁ | yes p₂ = yes (p₁ , p₂)
... | yes _ | no ¬p₂ = no λ x → ¬p₂ (proj₂ x)
... | no ¬p₁ | yes _ = no λ x → ¬p₁ (proj₁ x)
... | no ¬p₁ | no _ = no λ x → ¬p₁ (proj₁ x)
(mkProp _) ≟P NOT _ = no id
(mkProp _) ≟P _ AND _ = no id
(mkProp _) ≟P _ OR _ = no id
(NOT _) ≟P (mkProp _) = no id
(NOT _) ≟P _ AND _ = no id
(NOT _) ≟P _ OR _ = no id
(_ AND _) ≟P (mkProp _) = no id
(_ AND _) ≟P NOT _ = no id
(_ AND _) ≟P _ OR _ = no id
(_ OR _) ≟P (mkProp _) = no id
(_ OR _) ≟P NOT _ = no id
(_ OR _) ≟P _ AND _ = no id
instance
PEq : BEq Proposition Proposition
_=ᵇ_ {{PEq}} x y = x =P y
-- Text fragments
record Fragment : Set where
constructor mkFrag
field
ftext : String
-- Statement consists of Proposition and a particular text this proposition is stated in.
record Statement : Set where
constructor st
field
sttext : Maybe Fragment -- the statement text
stprop : Proposition -- it's meaning (proposition)
infix 4 _≡S_ _≟S_
_≡S_ : Statement → Statement → Set
x ≡S y = (Statement.stprop x) ≡P (Statement.stprop y)
_≟S_ : Decidable _≡S_
x ≟S y = (Statement.stprop x) ≟P (Statement.stprop y)
-- bool equality
private
_=S_ : Statement → Statement → Bool
(st _ x) =S (st _ y) = x =P y
instance
SEq : BEq Statement Statement
_=ᵇ_ {{SEq}} x y = x =S y
-- float arithmetics
infix 5 _[<]_ _[≤]_ _[=]_
infixl 6 _[+]_ _[-]_
infixl 7 _[*]_
_[+]_ : Float → Float → Float
x [+] y = primFloatPlus x y
_[-]_ : Float → Float → Float
x [-] y = primFloatMinus x y
_[*]_ : Float → Float → Float
x [*] y = primFloatTimes x y
_[/]_ : Float → Float → Float
x [/] y = primFloatDiv x y
_[=]_ : Float → Float → Bool
x [=] y = primFloatEquality x y
_[<]_ : Float → Float → Bool
x [<] y = primFloatLess x y
_[≤]_ : Float → Float → Bool
x [≤] y = primFloatLess x y ∨ primFloatEquality x y
-- Some Docs
docSection : ℕ → String → Doc
docSection n s = line <> text (s +++ " ")
<> text (S.replicate (0 ℕ.⊔ ((n ∸ 1) ∸ S.length s)) '.')
docFloat : Float → Doc
docFloat x = text (Data.Float.show x)
-- rounded to n decimal places
docFloatRounded : ℕ → Float → Doc
docFloatRounded n x = text (Data.Float.show ((primRound (x * 10^n)) /10^n))
where
10^n = tofloat (primRound (primExp ((primNatToFloat n) * (primLog 10.0))))
where
tofloat : ℤ → Float
tofloat (+ n) = (primNatToFloat n) * 1.0
tofloat (-[1+ n ]) = (primNatToFloat (n ∸ 1)) * 1.0
_/10^n : ℤ → Float
(+ n) /10^n = (primNatToFloat n) ÷ 10^n
(-[1+ n ]) /10^n = primFloatNegate ((primNatToFloat (n ∸ 1)) ÷ 10^n)