2022-02-26 20:18:16 -05:00
|
|
|
module Quox.Equal
|
|
|
|
|
|
|
|
import public Quox.Syntax
|
2022-05-06 15:23:58 -04:00
|
|
|
import Control.Monad.Either
|
2022-05-13 01:05:55 -04:00
|
|
|
import Generics.Derive
|
2022-02-26 20:18:16 -05:00
|
|
|
|
|
|
|
%default total
|
2022-05-13 01:05:55 -04:00
|
|
|
%language ElabReflection
|
2022-02-26 20:18:16 -05:00
|
|
|
|
|
|
|
|
|
|
|
public export
|
2022-04-23 18:21:30 -04:00
|
|
|
data Mode = Equal | Sub
|
2022-05-13 01:05:55 -04:00
|
|
|
%runElab derive "Mode" [Generic, Meta, Eq, Ord, DecEq, Show]
|
2022-04-27 08:56:39 -04:00
|
|
|
|
2022-04-23 18:21:30 -04:00
|
|
|
public export
|
|
|
|
data Error
|
|
|
|
= ClashT Mode (Term d n) (Term d n)
|
|
|
|
| ClashU Mode Universe Universe
|
|
|
|
| ClashQ Qty Qty
|
2022-02-26 20:18:16 -05:00
|
|
|
|
|
|
|
private %inline
|
2022-04-23 18:21:30 -04:00
|
|
|
ClashE : Mode -> Elim d n -> Elim d n -> Error
|
|
|
|
ClashE mode = ClashT mode `on` E
|
2022-02-26 20:18:16 -05:00
|
|
|
|
2022-05-06 15:23:58 -04:00
|
|
|
parameters {auto _ : MonadError Error m}
|
2022-02-26 20:18:16 -05:00
|
|
|
private %inline
|
2022-04-23 18:21:30 -04:00
|
|
|
clashT : Mode -> Term d n -> Term d n -> m a
|
2022-05-06 15:23:58 -04:00
|
|
|
clashT mode = throwError .: ClashT mode
|
2022-02-26 20:18:16 -05:00
|
|
|
|
|
|
|
private %inline
|
2022-04-23 18:21:30 -04:00
|
|
|
clashE : Mode -> Elim d n -> Elim d n -> m a
|
2022-05-06 15:23:58 -04:00
|
|
|
clashE mode = throwError .: ClashE mode
|
2022-02-26 20:18:16 -05:00
|
|
|
|
|
|
|
mutual
|
|
|
|
private covering
|
2022-04-23 18:21:30 -04:00
|
|
|
compareTN' : Mode ->
|
|
|
|
(s, t : Term 0 n) ->
|
|
|
|
(0 _ : NotRedexT s) -> (0 _ : NotRedexT t) -> m ()
|
|
|
|
|
|
|
|
compareTN' mode (TYPE k) (TYPE l) _ _ =
|
|
|
|
case mode of
|
2022-05-06 15:23:58 -04:00
|
|
|
Equal => unless (k == l) $ throwError $ ClashU Equal k l
|
|
|
|
Sub => unless (k <= l) $ throwError $ ClashU Sub k l
|
2022-04-23 18:21:30 -04:00
|
|
|
compareTN' mode s@(TYPE _) t _ _ = clashT mode s t
|
|
|
|
|
2022-04-27 15:58:09 -04:00
|
|
|
compareTN' mode (Pi qty1 _ arg1 res1) (Pi qty2 _ arg2 res2) _ _ = do
|
|
|
|
-- [todo] this should probably always be ==, right..?
|
2022-05-06 15:23:58 -04:00
|
|
|
unless (qty1 == qty2) $ throwError $ ClashQ qty1 qty2
|
2022-04-23 18:21:30 -04:00
|
|
|
compareT0 mode arg2 arg1 -- reversed for contravariant Sub
|
|
|
|
compareTS0 mode res1 res2
|
|
|
|
compareTN' mode s@(Pi {}) t _ _ = clashT mode s t
|
2022-02-26 20:18:16 -05:00
|
|
|
|
|
|
|
-- [todo] eta
|
2022-04-23 18:21:30 -04:00
|
|
|
compareTN' _ (Lam _ body1) (Lam _ body2) _ _ =
|
|
|
|
compareTS0 Equal body1 body2
|
|
|
|
compareTN' mode s@(Lam {}) t _ _ = clashT mode s t
|
2022-02-26 20:18:16 -05:00
|
|
|
|
2022-04-23 18:21:30 -04:00
|
|
|
compareTN' mode (E e) (E f) ps pt = compareE0 mode e f
|
|
|
|
compareTN' mode s@(E _) t _ _ = clashT mode s t
|
2022-02-26 20:18:16 -05:00
|
|
|
|
2022-04-23 18:21:30 -04:00
|
|
|
compareTN' _ (CloT {}) _ ps _ = void $ ps IsCloT
|
|
|
|
compareTN' _ (DCloT {}) _ ps _ = void $ ps IsDCloT
|
2022-02-26 20:18:16 -05:00
|
|
|
|
|
|
|
private covering
|
2022-04-23 18:21:30 -04:00
|
|
|
compareEN' : Mode ->
|
|
|
|
(e, f : Elim 0 n) ->
|
|
|
|
(0 _ : NotRedexE e) -> (0 _ : NotRedexE f) -> m ()
|
2022-02-26 20:18:16 -05:00
|
|
|
|
2022-04-23 18:21:30 -04:00
|
|
|
compareEN' mode e@(F x) f@(F y) _ _ =
|
|
|
|
unless (x == y) $ clashE mode e f
|
|
|
|
compareEN' mode e@(F _) f _ _ = clashE mode e f
|
2022-02-26 20:18:16 -05:00
|
|
|
|
2022-04-23 18:21:30 -04:00
|
|
|
compareEN' mode e@(B i) f@(B j) _ _ =
|
|
|
|
unless (i == j) $ clashE mode e f
|
|
|
|
compareEN' mode e@(B _) f _ _ = clashE mode e f
|
2022-02-26 20:18:16 -05:00
|
|
|
|
2022-04-23 18:21:30 -04:00
|
|
|
-- [todo] tracking variance of functions? maybe???
|
|
|
|
-- probably not
|
|
|
|
compareEN' _ (fun1 :@ arg1) (fun2 :@ arg2) _ _ = do
|
|
|
|
compareE0 Equal fun1 fun2
|
|
|
|
compareT0 Equal arg1 arg2
|
|
|
|
compareEN' mode e@(_ :@ _) f _ _ = clashE mode e f
|
2022-02-26 20:18:16 -05:00
|
|
|
|
2022-04-23 18:21:30 -04:00
|
|
|
-- [todo] is always checking the types are equal correct?
|
|
|
|
compareEN' mode (tm1 :# ty1) (tm2 :# ty2) _ _ = do
|
|
|
|
compareT0 mode tm1 tm2
|
|
|
|
compareT0 Equal ty1 ty2
|
|
|
|
compareEN' mode e@(_ :# _) f _ _ = clashE mode e f
|
2022-02-26 20:18:16 -05:00
|
|
|
|
2022-04-23 18:21:30 -04:00
|
|
|
compareEN' _ (CloE {}) _ pe _ = void $ pe IsCloE
|
|
|
|
compareEN' _ (DCloE {}) _ pe _ = void $ pe IsDCloE
|
2022-02-26 20:18:16 -05:00
|
|
|
|
|
|
|
|
|
|
|
private covering %inline
|
2022-04-23 18:21:30 -04:00
|
|
|
compareTN : Mode -> NonRedexTerm 0 n -> NonRedexTerm 0 n -> m ()
|
|
|
|
compareTN mode s t = compareTN' mode s.fst t.fst s.snd t.snd
|
2022-02-26 20:18:16 -05:00
|
|
|
|
|
|
|
private covering %inline
|
2022-04-23 18:21:30 -04:00
|
|
|
compareEN : Mode -> NonRedexElim 0 n -> NonRedexElim 0 n -> m ()
|
|
|
|
compareEN mode e f = compareEN' mode e.fst f.fst e.snd f.snd
|
|
|
|
|
|
|
|
|
|
|
|
export covering %inline
|
|
|
|
compareT : Mode -> DimEq d -> Term d n -> Term d n -> m ()
|
|
|
|
compareT mode eqs s t =
|
|
|
|
for_ (splits eqs) $ \th => compareT0 mode (s /// th) (t /// th)
|
|
|
|
|
|
|
|
export covering %inline
|
|
|
|
compareE : Mode -> DimEq d -> Elim d n -> Elim d n -> m ()
|
|
|
|
compareE mode eqs e f =
|
|
|
|
for_ (splits eqs) $ \th => compareE0 mode (e /// th) (f /// th)
|
2022-02-26 20:18:16 -05:00
|
|
|
|
|
|
|
|
|
|
|
export covering %inline
|
2022-04-23 18:21:30 -04:00
|
|
|
compareT0 : Mode -> Term 0 n -> Term 0 n -> m ()
|
|
|
|
compareT0 mode s t = compareTN mode (whnfT s) (whnfT t)
|
2022-02-26 20:18:16 -05:00
|
|
|
|
|
|
|
export covering %inline
|
2022-04-23 18:21:30 -04:00
|
|
|
compareE0 : Mode -> Elim 0 n -> Elim 0 n -> m ()
|
|
|
|
compareE0 mode e f = compareEN mode (whnfE e) (whnfE f)
|
2022-02-26 20:18:16 -05:00
|
|
|
|
2022-04-23 18:21:30 -04:00
|
|
|
export covering %inline
|
|
|
|
compareTS0 : Mode -> ScopeTerm 0 n -> ScopeTerm 0 n -> m ()
|
|
|
|
compareTS0 mode (TUnused body1) (TUnused body2) =
|
|
|
|
compareT0 mode body1 body2
|
|
|
|
compareTS0 mode body1 body2 =
|
|
|
|
compareT0 mode (fromScopeTerm body1) (fromScopeTerm body2)
|
|
|
|
|
|
|
|
|
|
|
|
export covering %inline
|
|
|
|
equalTWith : DimEq d -> Term d n -> Term d n -> m ()
|
|
|
|
equalTWith = compareT Equal
|
|
|
|
|
|
|
|
export covering %inline
|
|
|
|
equalEWith : DimEq d -> Elim d n -> Elim d n -> m ()
|
|
|
|
equalEWith = compareE Equal
|
|
|
|
|
|
|
|
export covering %inline
|
|
|
|
subTWith : DimEq d -> Term d n -> Term d n -> m ()
|
|
|
|
subTWith = compareT Sub
|
|
|
|
|
|
|
|
export covering %inline
|
|
|
|
subEWith : DimEq d -> Elim d n -> Elim d n -> m ()
|
|
|
|
subEWith = compareE Sub
|
|
|
|
|
|
|
|
|
|
|
|
export covering %inline
|
|
|
|
equalT : {d : Nat} -> Term d n -> Term d n -> m ()
|
|
|
|
equalT = equalTWith DimEq.new
|
2022-02-26 20:18:16 -05:00
|
|
|
|
2022-04-06 14:31:38 -04:00
|
|
|
export covering %inline
|
2022-04-23 18:21:30 -04:00
|
|
|
equalE : {d : Nat} -> Elim d n -> Elim d n -> m ()
|
|
|
|
equalE = equalEWith DimEq.new
|
2022-02-26 20:18:16 -05:00
|
|
|
|
2022-04-06 14:31:38 -04:00
|
|
|
export covering %inline
|
2022-04-23 18:21:30 -04:00
|
|
|
subT : {d : Nat} -> Term d n -> Term d n -> m ()
|
|
|
|
subT = subTWith DimEq.new
|
2022-04-09 12:54:47 -04:00
|
|
|
|
|
|
|
export covering %inline
|
2022-04-23 18:21:30 -04:00
|
|
|
subE : {d : Nat} -> Elim d n -> Elim d n -> m ()
|
|
|
|
subE = subEWith DimEq.new
|