summaryrefslogtreecommitdiff
path: root/src/Inky/Decidable.idr
blob: 7fba6761efdba8605cbe7ae61ab9deb1812a2ad1 (plain)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
module Inky.Decidable

import public Inky.Decidable.Either

import Data.Bool
import Data.Either
import Data.Maybe
import Data.List
import Data.List1
import Data.List1.Properties
import Data.Nat
import Data.SnocList
import Data.So
import Data.These
import Data.Vect

import Decidable.Equality

public export
When : Type -> Bool -> Type
When a = Union a (Not a)

public export
Dec' : (0 _ : Type) -> Type
Dec' a = LazyEither a (Not a)

-- Operations ------------------------------------------------------------------

-- Conversion to Dec

public export
fromDec : Dec a -> Dec' a
fromDec (Yes prf) = True `Because` prf
fromDec (No contra) = False `Because` contra

public export
toDec : Dec' a -> Dec a
toDec (True `Because` prf) = Yes prf
toDec (False `Because` contra) = No contra

-- Negation

public export
notWhen : {b : Bool} -> a `When` b -> Not a `When` not b
notWhen = Union.map id (\prf, contra => contra prf) . Union.not

public export
notDec : Dec' a -> Dec' (Not a)
notDec p = not p.does `Because` notWhen p.reason

-- Maps

public export
mapWhen : (a -> a') -> (0 _ : a' -> a) -> {b : Bool} -> a `When` b -> a' `When` b
mapWhen f g = Union.map f (\contra, prf => void $ contra $ g prf)

public export
mapDec : (a -> b) -> (0 _ : b -> a) -> Dec' a -> Dec' b
mapDec f g = map f (\contra, prf => void $ contra $ g prf)

-- Conjunction

public export
andWhen : {b1, b2 :  Bool} -> a1 `When` b1 -> Lazy (a2 `When` b2) -> (a1, a2) `When` (b1 && b2)
andWhen x y =
  Union.map {d = Not (a1, a2)} id (\x, (y, z) => either (\f => f y) (\g => g z) x) $
  Union.both x y

public export
andDec : Dec' a -> Dec' b -> Dec' (a, b)
andDec p q = (p.does && q.does) `Because` andWhen p.reason q.reason

-- Disjunction

public export
elseWhen : {b1, b2 : Bool} -> a1 `When` b1 -> Lazy (a2 `When` b2) -> Either a1 a2 `When` (b1 || b2)
elseWhen x y =
  Union.map {b = (Not a1, Not a2)} id (\(f, g) => either f g) $
  Union.first x y

public export
elseDec : Dec' a -> Dec' b -> Dec' (Either a b)
elseDec p q = (p.does || q.does) `Because` elseWhen p.reason q.reason

public export
orWhen : {b1, b2 : Bool} -> a1 `When` b1 -> a2 `When` b2 -> These a1 a2 `When` (b1 || b2)
orWhen x y =
  Union.map {b = (Not a1, Not a2)} id (\(f, g) => these f g (const g)) $
  Union.any x y

public export
orDec : Dec' a -> Dec' b -> Dec' (These a b)
orDec p q = (p.does || q.does) `Because` orWhen p.reason q.reason

-- Dependent Versions

public export
thenDec :
  (0 b : a -> Type) ->
  (0 unique : (x, y : a) -> b x -> b y) ->
  Dec' a -> ((x : a) -> Dec' (b x)) -> Dec' (x ** b x)
thenDec b unique p f =
  map id
    (\contra, (x ** prf) =>
      either
        (\contra => contra x)
        (\yContra => void $ snd yContra $ unique x (fst yContra) prf)
        contra) $
  andThen p f

-- Equality --------------------------------------------------------------------

public export
interface DecEq' (0 a : Type) where
  does : (x, y : a) -> Bool
  reason : (x, y : a) -> (x = y) `When` (does x y)

  decEq : (x, y : a) -> Dec' (x = y)
  decEq x y = does x y `Because` reason x y

public export
whenCong : (0 _ : Injective f) => {b : Bool} -> (x = y) `When` b -> (f x = f y) `When` b
whenCong = mapWhen (\prf => cong f prf) (\prf => inj f prf)

public export
whenCong2 :
  (0 _ : Biinjective f) =>
  {b1, b2 : Bool} ->
  (x = y) `When` b1 -> (z = w) `When` b2 ->
  (f x z = f y w) `When` (b1 && b2)
whenCong2 p q =
  mapWhen {a = (_, _)} (\prfs => cong2 f (fst prfs) (snd prfs)) (\prf => biinj f prf) $
  andWhen p q

[ToEq] DecEq' a => Eq a where
  x == y = does x y

-- Instances -------------------------------------------------------------------

public export
DecEq' () where
  does _ _ = True
  reason () () = Refl

public export
DecEq' Bool where
  does b1 b2 = b1 == b2

  reason False False = Refl
  reason False True = absurd
  reason True False = absurd
  reason True True = Refl

public export
DecEq' Nat where
  does k n = k == n

  reason 0 0 = Refl
  reason 0 (S n) = absurd
  reason (S k) 0 = absurd
  reason (S k) (S n) = whenCong (reason k n)

public export
(d : DecEq' a) => DecEq' (Maybe a) where
  does x y = let _ = ToEq @{d} in x == y

  reason Nothing Nothing = Refl
  reason Nothing (Just y) = absurd
  reason (Just x) Nothing = absurd
  reason (Just x) (Just y) = whenCong (reason x y)

public export
(d1 : DecEq' a) => (d2 : DecEq' b) => DecEq' (Either a b) where
  does x y = let _ = ToEq @{d1} in let _ = ToEq @{d2} in x == y

  reason (Left x) (Left y) = whenCong (reason x y)
  reason (Left x) (Right y) = absurd
  reason (Right x) (Left y) = absurd
  reason (Right x) (Right y) = whenCong (reason x y)


public export
(d1 : DecEq' a) => (d2 : DecEq' b) => DecEq' (These a b) where
  does x y = let _ = ToEq @{d1} in let _ = ToEq @{d2} in x == y

  reason (This x) (This y) = whenCong (reason x y)
  reason (That x) (That y) = whenCong (reason x y)
  reason (Both x z) (Both y w) = whenCong2 (reason x y) (reason z w)
  reason (This x) (That y) = \case Refl impossible
  reason (This x) (Both y z) = \case Refl impossible
  reason (That x) (This y) = \case Refl impossible
  reason (That x) (Both y z) = \case Refl impossible
  reason (Both x z) (This y) = \case Refl impossible
  reason (Both x z) (That y) = \case Refl impossible

public export
(d1 : DecEq' a) => (d2 : DecEq' b) => DecEq' (a, b) where
  does x y = let _ = ToEq @{d1} in let _ = ToEq @{d2} in x == y

  reason (x, y) (z, w) = whenCong2 (reason x z) (reason y w)

public export
(d : DecEq' a) => DecEq' (List a) where
  does x y = let _ = ToEq @{d} in x == y

  reason [] [] = Refl
  reason [] (y :: ys) = absurd
  reason (x :: xs) [] = absurd
  reason (x :: xs) (y :: ys) = whenCong2 (reason x y) (reason xs ys)

public export
(d : DecEq' a) => DecEq' (List1 a) where
  does x y = let _ = ToEq @{d} in x == y

  reason (x ::: xs) (y ::: ys) = whenCong2 (reason x y) (reason xs ys)

public export
(d : DecEq' a) => DecEq' (SnocList a) where
  does x y = let _ = ToEq @{d} in x == y

  reason [<] [<] = Refl
  reason [<] (sy :< y) = absurd
  reason (sx :< x) [<] = absurd
  reason (sx :< x) (sy :< y) =
    rewrite sym $ andCommutative (does sx sy) (does x y) in
    whenCong2 (reason sx sy) (reason x y)

-- Other Primitives

%unsafe
public export
primitiveEq : Eq a => (x, y : a) -> (x = y) `When` (x == y)
primitiveEq x y with (x == y)
  _ | True = believe_me (Refl {x})
  _ | False = \prf => believe_me {b = Void} ()

%unsafe
public export
[FromEq] Eq a => DecEq' a where
  does x y = x == y
  reason x y = primitiveEq x y

public export
DecEq' Int where
  does = does @{FromEq}
  reason = reason @{FromEq}

public export
DecEq' Char where
  does = does @{FromEq}
  reason = reason @{FromEq}

public export
DecEq' Integer where
  does = does @{FromEq}
  reason = reason @{FromEq}

public export
DecEq' String where
  does = does @{FromEq}
  reason = reason @{FromEq}