2020-07-18 21:22:03 +03:00
|
|
|
module DerivingEq
|
|
|
|
|
|
|
|
import Language.Reflection
|
|
|
|
|
|
|
|
%language ElabReflection
|
|
|
|
|
|
|
|
public export
|
|
|
|
countArgs : (ty : TTImp) -> Nat
|
|
|
|
countArgs (IPi _ _ ExplicitArg _ _ retTy) = 1 + countArgs retTy
|
|
|
|
countArgs (IPi _ _ _ _ _ retTy) = countArgs retTy
|
|
|
|
countArgs _ = 0
|
|
|
|
|
|
|
|
-- %logging 5
|
|
|
|
public export
|
2021-03-09 20:23:05 +03:00
|
|
|
genEq : {t : _} -> Name -> Elab (t -> t -> Bool)
|
2020-07-18 21:22:03 +03:00
|
|
|
genEq typeName = do
|
2021-06-05 14:53:22 +03:00
|
|
|
let pos : FC = MkFC (Virtual Interactive) (0,0) (0,0)
|
2020-07-18 21:22:03 +03:00
|
|
|
[(n, _)] <- getType typeName
|
|
|
|
| _ => fail "Ambiguous name"
|
|
|
|
constrs <- getCons n
|
|
|
|
let and : TTImp -> TTImp -> TTImp
|
|
|
|
and x y = `(~(x) && ~(y))
|
|
|
|
compareEq : String -> String -> TTImp
|
2021-09-15 15:20:58 +03:00
|
|
|
compareEq x y = `(~(IVar pos $ UN (Basic x)) == ~(IVar pos $ UN (Basic y)))
|
2020-07-18 21:22:03 +03:00
|
|
|
makeClause : Name -> Elab Clause
|
|
|
|
makeClause constr = do
|
|
|
|
[(_, ty)] <- getType constr
|
|
|
|
| _ => fail "ambiguous name for constr"
|
|
|
|
let nArgs = countArgs ty
|
|
|
|
let xs = map (\i => "x_" ++ show i) $ take nArgs [1..]
|
|
|
|
let ys = map (\i => "y_" ++ show i) $ take nArgs [1..]
|
|
|
|
let px = foldl (IApp pos) (IVar pos constr) $ map (IBindVar pos) xs
|
|
|
|
let py = foldl (IApp pos) (IVar pos constr) $ map (IBindVar pos) ys
|
|
|
|
pure $ PatClause pos `(MkPair ~(px) ~(py))
|
|
|
|
$ foldl and `(True) $ zipWith compareEq xs ys
|
|
|
|
finalClause : Clause
|
|
|
|
finalClause = PatClause pos `(_) `(False)
|
|
|
|
clauses <- traverse makeClause constrs
|
|
|
|
let allClauses = clauses ++ [finalClause]
|
2023-09-01 13:35:52 +03:00
|
|
|
caseExpr = ICase pos [] `(MkPair x y) (Implicit pos True) allClauses
|
2020-07-18 21:22:03 +03:00
|
|
|
result = `(\x, y => ~(caseExpr))
|
|
|
|
check result
|
|
|
|
%logging 0
|
|
|
|
|
|
|
|
-- This tree works
|
|
|
|
|
|
|
|
data TreeOne a = BranchOne (TreeOne a) a (TreeOne a)
|
|
|
|
| Leaf
|
|
|
|
|
2021-11-02 18:34:52 +03:00
|
|
|
covering
|
2020-07-18 21:22:03 +03:00
|
|
|
Eq a => Eq (TreeOne a) where
|
2021-01-12 15:23:28 +03:00
|
|
|
(==) = %runElab genEq `{ TreeOne }
|