2020-05-18 15:59:07 +03:00
|
|
|
module Decidable.Equality
|
|
|
|
|
2021-11-26 13:55:17 +03:00
|
|
|
import Control.Function
|
2020-05-18 15:59:07 +03:00
|
|
|
import Data.Maybe
|
2020-07-26 00:58:03 +03:00
|
|
|
import Data.Either
|
2020-05-18 15:59:07 +03:00
|
|
|
import Data.Nat
|
2020-06-30 04:18:40 +03:00
|
|
|
import Data.List
|
2020-09-05 11:41:31 +03:00
|
|
|
import Data.List1
|
2022-03-23 16:33:13 +03:00
|
|
|
import Data.List1.Properties
|
2022-05-19 13:45:03 +03:00
|
|
|
import Data.These
|
2020-05-18 15:59:07 +03:00
|
|
|
|
2020-12-04 04:45:27 +03:00
|
|
|
import public Decidable.Equality.Core as Decidable.Equality
|
2020-05-18 15:59:07 +03:00
|
|
|
|
2020-12-04 04:45:27 +03:00
|
|
|
%default total
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
--- Unit
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2021-01-17 15:07:04 +03:00
|
|
|
public export
|
2020-06-30 04:18:40 +03:00
|
|
|
DecEq () where
|
2020-05-18 15:59:07 +03:00
|
|
|
decEq () () = Yes Refl
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Booleans
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2021-01-17 15:07:04 +03:00
|
|
|
public export
|
2020-06-30 04:18:40 +03:00
|
|
|
DecEq Bool where
|
2020-05-18 15:59:07 +03:00
|
|
|
decEq True True = Yes Refl
|
|
|
|
decEq False False = Yes Refl
|
2020-06-30 04:18:40 +03:00
|
|
|
decEq False True = No absurd
|
|
|
|
decEq True False = No absurd
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Nat
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2021-01-17 15:07:04 +03:00
|
|
|
public export
|
2020-06-30 04:18:40 +03:00
|
|
|
DecEq Nat where
|
2020-05-18 15:59:07 +03:00
|
|
|
decEq Z Z = Yes Refl
|
2022-05-19 21:00:29 +03:00
|
|
|
decEq (S n) (S m) = decEqCong $ decEq n m
|
2020-06-30 04:18:40 +03:00
|
|
|
decEq Z (S _) = No absurd
|
|
|
|
decEq (S _) Z = No absurd
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Maybe
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2021-01-17 15:07:04 +03:00
|
|
|
public export
|
2020-06-30 04:18:40 +03:00
|
|
|
DecEq t => DecEq (Maybe t) where
|
2020-05-18 15:59:07 +03:00
|
|
|
decEq Nothing Nothing = Yes Refl
|
2022-05-19 21:00:29 +03:00
|
|
|
decEq (Just x) (Just y) = decEqCong $ decEq x y
|
2020-06-30 04:18:40 +03:00
|
|
|
decEq Nothing (Just _) = No absurd
|
|
|
|
decEq (Just _) Nothing = No absurd
|
2020-05-18 15:59:07 +03:00
|
|
|
|
2020-07-26 00:58:03 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Either
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2021-01-17 15:07:04 +03:00
|
|
|
public export
|
2020-07-26 00:58:03 +03:00
|
|
|
(DecEq t, DecEq s) => DecEq (Either t s) where
|
2022-05-19 21:00:29 +03:00
|
|
|
decEq (Left x) (Left y) = decEqCong $ decEq x y
|
|
|
|
decEq (Right x) (Right y) = decEqCong $ decEq x y
|
2020-07-26 00:58:03 +03:00
|
|
|
decEq (Left x) (Right y) = No absurd
|
|
|
|
decEq (Right x) (Left y) = No absurd
|
|
|
|
|
2022-05-19 13:45:03 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- These (inclusive or)
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public export
|
|
|
|
DecEq t => DecEq s => DecEq (These t s) where
|
|
|
|
decEq (This x) (This y) = decEqCong $ decEq x y
|
|
|
|
decEq (That x) (That y) = decEqCong $ decEq x y
|
|
|
|
decEq (Both x z) (Both y w) with (decEq x y)
|
|
|
|
decEq (Both x z) (Both x w) | Yes Refl = decEqCong $ decEq z w
|
|
|
|
_ | No ctr = No $ \case Refl => ctr Refl
|
|
|
|
decEq (This x) (That y) = No $ \case Refl impossible
|
|
|
|
decEq (This x) (Both y z) = No $ \case Refl impossible
|
|
|
|
decEq (That x) (This y) = No $ \case Refl impossible
|
|
|
|
decEq (That x) (Both y z) = No $ \case Refl impossible
|
|
|
|
decEq (Both x z) (This y) = No $ \case Refl impossible
|
|
|
|
decEq (Both x z) (That y) = No $ \case Refl impossible
|
|
|
|
|
2020-05-18 15:59:07 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Tuple
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2020-06-30 04:18:40 +03:00
|
|
|
pairInjective : (a, b) = (c, d) -> (a = c, b = d)
|
|
|
|
pairInjective Refl = (Refl, Refl)
|
2020-05-18 15:59:07 +03:00
|
|
|
|
2021-01-17 15:07:04 +03:00
|
|
|
public export
|
2020-06-30 04:18:40 +03:00
|
|
|
(DecEq a, DecEq b) => DecEq (a, b) where
|
|
|
|
decEq (a, b) (a', b') with (decEq a a')
|
|
|
|
decEq (a, b) (a', b') | (No contra) =
|
|
|
|
No $ contra . fst . pairInjective
|
|
|
|
decEq (a, b) (a, b') | (Yes Refl) with (decEq b b')
|
|
|
|
decEq (a, b) (a, b) | (Yes Refl) | (Yes Refl) = Yes Refl
|
|
|
|
decEq (a, b) (a, b') | (Yes Refl) | (No contra) =
|
|
|
|
No $ contra . snd . pairInjective
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- List
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2021-01-17 15:07:04 +03:00
|
|
|
public export
|
2020-06-30 04:18:40 +03:00
|
|
|
DecEq a => DecEq (List a) where
|
2020-05-18 15:59:07 +03:00
|
|
|
decEq [] [] = Yes Refl
|
2020-06-30 04:18:40 +03:00
|
|
|
decEq (x :: xs) [] = No absurd
|
|
|
|
decEq [] (x :: xs) = No absurd
|
2020-05-18 15:59:07 +03:00
|
|
|
decEq (x :: xs) (y :: ys) with (decEq x y)
|
2020-06-30 04:18:40 +03:00
|
|
|
decEq (x :: xs) (y :: ys) | No contra =
|
|
|
|
No $ contra . fst . consInjective
|
2020-05-18 15:59:07 +03:00
|
|
|
decEq (x :: xs) (x :: ys) | Yes Refl with (decEq xs ys)
|
|
|
|
decEq (x :: xs) (x :: xs) | (Yes Refl) | (Yes Refl) = Yes Refl
|
2020-06-30 04:18:40 +03:00
|
|
|
decEq (x :: xs) (x :: ys) | (Yes Refl) | (No contra) =
|
|
|
|
No $ contra . snd . consInjective
|
|
|
|
|
2020-09-05 11:41:31 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- List1
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2021-01-17 15:07:04 +03:00
|
|
|
public export
|
2020-09-05 11:41:31 +03:00
|
|
|
DecEq a => DecEq (List1 a) where
|
|
|
|
|
2020-09-22 17:07:40 +03:00
|
|
|
decEq (x ::: xs) (y ::: ys) with (decEq x y)
|
|
|
|
decEq (x ::: xs) (y ::: ys) | No contra = No (contra . fst . consInjective)
|
|
|
|
decEq (x ::: xs) (y ::: ys) | Yes eqxy with (decEq xs ys)
|
2021-11-26 13:55:17 +03:00
|
|
|
decEq (x ::: xs) (y ::: ys) | Yes eqxy | No contra = No (contra . (rewrite sym eqxy in injective))
|
2021-02-22 21:46:35 +03:00
|
|
|
decEq (x ::: xs) (y ::: ys) | Yes eqxy | Yes eqxsys = Yes (cong2 (:::) eqxy eqxsys)
|
2020-09-05 11:41:31 +03:00
|
|
|
|
2020-06-30 04:18:40 +03:00
|
|
|
-- TODO: Other prelude data types
|
|
|
|
|
|
|
|
-- For the primitives, we have to cheat because we don't have access to their
|
|
|
|
-- internal implementations. We use believe_me for the inequality proofs
|
|
|
|
-- because we don't them to reduce (and they should never be needed anyway...)
|
|
|
|
-- A postulate would be better, but erasure analysis may think they're needed
|
|
|
|
-- for computation in a higher order setting.
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
|
2021-10-08 14:07:11 +03:00
|
|
|
||| An unsafe decidable equality implementation based on boolean equality.
|
|
|
|
||| Useful for builtin types.
|
2021-01-17 15:07:04 +03:00
|
|
|
public export
|
2021-10-08 14:07:11 +03:00
|
|
|
[FromEq] Eq a => DecEq a where
|
2021-09-30 11:48:06 +03:00
|
|
|
decEq x y = case x == y of -- Blocks if x or y not concrete
|
|
|
|
True => Yes primitiveEq
|
|
|
|
False => No primitiveNotEq
|
|
|
|
where primitiveEq : forall x, y . x = y
|
|
|
|
primitiveEq = believe_me (Refl {x})
|
|
|
|
primitiveNotEq : forall x, y . Not (x = y)
|
|
|
|
primitiveNotEq prf = believe_me {b = Void} ()
|
|
|
|
|
2021-10-08 14:07:11 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Int
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public export
|
|
|
|
DecEq Int where
|
|
|
|
decEq = decEq @{FromEq}
|
|
|
|
|
2021-09-30 11:48:06 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Bits8
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public export
|
2021-10-08 14:07:11 +03:00
|
|
|
DecEq Bits8 where
|
|
|
|
decEq = decEq @{FromEq}
|
2021-09-30 11:48:06 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Bits16
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public export
|
2021-10-08 14:07:11 +03:00
|
|
|
DecEq Bits16 where
|
|
|
|
decEq = decEq @{FromEq}
|
2021-09-30 11:48:06 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Bits32
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public export
|
2021-10-08 14:07:11 +03:00
|
|
|
DecEq Bits32 where
|
|
|
|
decEq = decEq @{FromEq}
|
2021-09-30 11:48:06 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Bits64
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public export
|
2021-10-08 14:07:11 +03:00
|
|
|
DecEq Bits64 where
|
|
|
|
decEq = decEq @{FromEq}
|
2021-09-30 11:48:06 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Int8
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public export
|
2021-10-08 14:07:11 +03:00
|
|
|
DecEq Int8 where
|
|
|
|
decEq = decEq @{FromEq}
|
2021-09-30 11:48:06 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Int16
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public export
|
2021-10-08 14:07:11 +03:00
|
|
|
DecEq Int16 where
|
|
|
|
decEq = decEq @{FromEq}
|
2021-09-30 11:48:06 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Int32
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public export
|
2021-10-08 14:07:11 +03:00
|
|
|
DecEq Int32 where
|
|
|
|
decEq = decEq @{FromEq}
|
2021-09-30 11:48:06 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Int64
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public export
|
2021-10-08 14:07:11 +03:00
|
|
|
DecEq Int64 where
|
|
|
|
decEq = decEq @{FromEq}
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Char
|
|
|
|
--------------------------------------------------------------------------------
|
2021-01-17 15:07:04 +03:00
|
|
|
public export
|
2021-10-08 14:07:11 +03:00
|
|
|
DecEq Char where
|
|
|
|
decEq = decEq @{FromEq}
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Integer
|
|
|
|
--------------------------------------------------------------------------------
|
2021-01-17 15:07:04 +03:00
|
|
|
public export
|
2021-10-08 14:07:11 +03:00
|
|
|
DecEq Integer where
|
|
|
|
decEq = decEq @{FromEq}
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- String
|
|
|
|
--------------------------------------------------------------------------------
|
2021-01-17 15:07:04 +03:00
|
|
|
public export
|
2021-10-08 14:07:11 +03:00
|
|
|
DecEq String where
|
|
|
|
decEq = decEq @{FromEq}
|