make overloaded reduce stuff into interfaces

this is kinda a pain so i might change it back i guess
This commit is contained in:
rhiannon morris 2023-02-20 21:42:31 +01:00
parent 56791e286d
commit cb5bd6c98c

View file

@ -9,120 +9,129 @@ import Data.Maybe
%default total %default total
namespace Elim
public export %inline
isClo : Elim {} -> Bool
isClo (CloE {}) = True
isClo (DCloE {}) = True
isClo _ = False
public export
0 NotClo : Pred $ Elim {}
NotClo = No . isClo
namespace Term
public export %inline
isClo : Term {} -> Bool
isClo (CloT {}) = True
isClo (DCloT {}) = True
isClo (E e) = isClo e
isClo _ = False
public export
0 NotClo : Pred $ Term {}
NotClo = No . isClo
public export public export
0 NonCloElim : TermLike 0 CloTest : TermLike -> Type
NonCloElim q d n = Subset (Elim q d n) NotClo CloTest tm = forall q, d, n. tm q d n -> Bool
interface PushSubsts (0 tm : TermLike) (0 isClo : CloTest tm) | tm where
pushSubstsWith : DSubst dfrom dto -> TSubst q dto from to ->
tm q dfrom from -> Subset (tm q dto to) (No . isClo)
public export public export
0 NonCloTerm : TermLike 0 NotClo : {isClo : CloTest tm} -> PushSubsts tm isClo => Pred (tm q d n)
NonCloTerm q d n = Subset (Term q d n) NotClo NotClo = No . isClo
public export
0 NonClo : (tm : TermLike) -> {isClo : CloTest tm} ->
PushSubsts tm isClo => TermLike
NonClo tm q d n = Subset (tm q d n) NotClo
public export %inline public export %inline
ncloT : (t : Term q d n) -> (0 nc : NotClo t) => NonCloTerm q d n nclo : {isClo : CloTest tm} -> (0 _ : PushSubsts tm isClo) =>
ncloT t = Element t nc (t : tm q d n) -> (0 nc : NotClo t) => NonClo tm q d n
nclo t = Element t nc
public export %inline parameters {0 isClo : CloTest tm} {auto _ : PushSubsts tm isClo}
ncloE : (e : Elim q d n) -> (0 nc : NotClo e) => NonCloElim q d n
ncloE e = Element e nc
mutual
namespace Term
||| if the input term has any top-level closures, push them under one layer of ||| if the input term has any top-level closures, push them under one layer of
||| syntax ||| syntax
export %inline export %inline
pushSubsts : Term q d n -> NonCloTerm q d n pushSubsts : tm q d n -> NonClo tm q d n
pushSubsts s = pushSubstsWith id id s pushSubsts s = pushSubstsWith id id s
export %inline
pushSubstsWith' : DSubst dfrom dto -> TSubst q dto from to ->
tm q dfrom from -> tm q dto to
pushSubstsWith' th ph x = fst $ pushSubstsWith th ph x
mutual
public export
isCloT : CloTest Term
isCloT (CloT {}) = True
isCloT (DCloT {}) = True
isCloT (E e) = isCloE e
isCloT _ = False
public export
isCloE : CloTest Elim
isCloE (CloE {}) = True
isCloE (DCloE {}) = True
isCloE _ = False
mutual
export export
pushSubstsWith : DSubst dfrom dto -> TSubst q dto from to -> PushSubsts Term Reduce.isCloT where
Term q dfrom from -> NonCloTerm q dto to
pushSubstsWith th ph (TYPE l) = pushSubstsWith th ph (TYPE l) =
ncloT $ TYPE l nclo $ TYPE l
pushSubstsWith th ph (Pi qty x a body) = pushSubstsWith th ph (Pi qty x a body) =
ncloT $ Pi qty x (subs a th ph) (subs body th ph) nclo $ Pi qty x (subs a th ph) (subs body th ph)
pushSubstsWith th ph (Lam x body) = pushSubstsWith th ph (Lam x body) =
ncloT $ Lam x $ subs body th ph nclo $ Lam x $ subs body th ph
pushSubstsWith th ph (Sig x a b) = pushSubstsWith th ph (Sig x a b) =
ncloT $ Sig x (subs a th ph) (subs b th ph) nclo $ Sig x (subs a th ph) (subs b th ph)
pushSubstsWith th ph (Pair s t) = pushSubstsWith th ph (Pair s t) =
ncloT $ Pair (subs s th ph) (subs t th ph) nclo $ Pair (subs s th ph) (subs t th ph)
pushSubstsWith th ph (Eq i ty l r) = pushSubstsWith th ph (Eq i ty l r) =
ncloT $ Eq i (subs ty th ph) (subs l th ph) (subs r th ph) nclo $ Eq i (subs ty th ph) (subs l th ph) (subs r th ph)
pushSubstsWith th ph (DLam i body) = pushSubstsWith th ph (DLam i body) =
ncloT $ DLam i $ subs body th ph nclo $ DLam i $ subs body th ph
pushSubstsWith th ph (E e) = pushSubstsWith th ph (E e) =
let Element e nc = pushSubstsWith th ph e in ncloT $ E e let Element e nc = pushSubstsWith th ph e in nclo $ E e
pushSubstsWith th ph (CloT s ps) = pushSubstsWith th ph (CloT s ps) =
pushSubstsWith th (comp th ps ph) s pushSubstsWith th (comp th ps ph) s
pushSubstsWith th ph (DCloT s ps) = pushSubstsWith th ph (DCloT s ps) =
pushSubstsWith (ps . th) ph s pushSubstsWith (ps . th) ph s
namespace Elim
||| if the input elimination has any top-level closures, push them under one
||| layer of syntax
export %inline
pushSubsts : Elim q d n -> NonCloElim q d n
pushSubsts e = pushSubstsWith id id e
export export
pushSubstsWith : DSubst dfrom dto -> TSubst q dto from to -> PushSubsts Elim Reduce.isCloE where
Elim q dfrom from -> NonCloElim q dto to
pushSubstsWith th ph (F x) = pushSubstsWith th ph (F x) =
ncloE $ F x nclo $ F x
pushSubstsWith th ph (B i) = pushSubstsWith th ph (B i) =
let res = ph !! i in let res = ph !! i in
case nchoose $ isClo res of case nchoose $ isCloE res of
Left yes => assert_total pushSubsts res Left yes => assert_total pushSubsts res
Right no => Element res no Right no => Element res no
pushSubstsWith th ph (f :@ s) = pushSubstsWith th ph (f :@ s) =
ncloE $ subs f th ph :@ subs s th ph nclo $ subs f th ph :@ subs s th ph
pushSubstsWith th ph (CasePair pi p x r y z b) = pushSubstsWith th ph (CasePair pi p x r y z b) =
ncloE $ CasePair pi (subs p th ph) x (subs r th ph) y z (subs b th ph) nclo $ CasePair pi (subs p th ph) x (subs r th ph) y z (subs b th ph)
pushSubstsWith th ph (f :% d) = pushSubstsWith th ph (f :% d) =
ncloE $ subs f th ph :% (d // th) nclo $ subs f th ph :% (d // th)
pushSubstsWith th ph (s :# a) = pushSubstsWith th ph (s :# a) =
ncloE $ subs s th ph :# subs a th ph nclo $ subs s th ph :# subs a th ph
pushSubstsWith th ph (CloE e ps) = pushSubstsWith th ph (CloE e ps) =
pushSubstsWith th (comp th ps ph) e pushSubstsWith th (comp th ps ph) e
pushSubstsWith th ph (DCloE e ps) = pushSubstsWith th ph (DCloE e ps) =
pushSubstsWith (ps . th) ph e pushSubstsWith (ps . th) ph e
parameters (th : DSubst dfrom dto) (ph : TSubst q dto from to)
namespace Term
public export %inline
pushSubstsWith' : Term q dfrom from -> Term q dto to
pushSubstsWith' s = (pushSubstsWith th ph s).fst
namespace Elim public export
public export %inline 0 RedexTest : TermLike -> Type
pushSubstsWith' : Elim q dfrom from -> Elim q dto to RedexTest tm = forall q, d, n, g. Definitions' q g -> tm q d n -> Bool
pushSubstsWith' e = (pushSubstsWith th ph e).fst
public export
interface Whnf (0 tm : TermLike) (0 isRedex : RedexTest tm) | tm where
whnf : (defs : Definitions' q g) ->
tm q d n -> Subset (tm q d n) (No . isRedex defs)
public export
0 IsRedex, NotRedex : {isRedex : RedexTest tm} -> Whnf tm isRedex =>
Definitions' q g -> Pred (tm q d n)
IsRedex defs = So . isRedex defs
NotRedex defs = No . isRedex defs
public export
0 NonRedex : (tm : TermLike) -> {isRedex : RedexTest tm} -> Whnf tm isRedex =>
(q : Type) -> (d, n : Nat) -> {g : _} ->
(defs : Definitions' q g) -> Type
NonRedex tm q d n defs = Subset (tm q d n) (NotRedex defs)
public export %inline
nred : {0 isRedex : RedexTest tm} -> (0 _ : Whnf tm isRedex) =>
(t : tm q d n) -> (0 nr : NotRedex defs t) =>
NonRedex tm q d n defs
nred t = Element t nr
public export %inline public export %inline
@ -150,118 +159,99 @@ isAnn : Elim {} -> Bool
isAnn (_ :# _) = True isAnn (_ :# _) = True
isAnn _ = False isAnn _ = False
parameters (defs : Definitions' q g)
mutual mutual
namespace Elim
public export public export
isRedex : Elim q d n -> Bool isRedexE : RedexTest Elim
isRedex (F x) {d, n} = isJust $ lookupElim x defs {d, n} isRedexE defs (F x) {d, n} =
isRedex (B _) = False isJust $ lookupElim x defs {d, n}
isRedex (f :@ _) = isRedex f || isLamHead f isRedexE _ (B _) = False
isRedex (CasePair {pair, _}) = isRedex pair || isPairHead pair isRedexE defs (f :@ _) =
isRedex (f :% _) = isRedex f || isDLamHead f isRedexE defs f || isLamHead f
isRedex (t :# a) = isE t || isRedex t || isRedex a isRedexE defs (CasePair {pair, _}) =
isRedex (CloE {}) = True isRedexE defs pair || isPairHead pair
isRedex (DCloE {}) = True isRedexE defs (f :% _) =
isRedexE defs f || isDLamHead f
isRedexE defs (t :# a) =
isE t || isRedexT defs t || isRedexT defs a
isRedexE _ (CloE {}) = True
isRedexE _ (DCloE {}) = True
namespace Term
public export public export
isRedex : Term q d n -> Bool isRedexT : RedexTest Term
isRedex (CloT {}) = True isRedexT _ (CloT {}) = True
isRedex (DCloT {}) = True isRedexT _ (DCloT {}) = True
isRedex (E e) = isAnn e || isRedex e isRedexT defs (E e) = isAnn e || isRedexE defs e
isRedex _ = False isRedexT _ _ = False
namespace Elim mutual
public export
0 IsRedex, NotRedex : Pred $ Elim q d n
IsRedex = So . isRedex
NotRedex = No . isRedex
namespace Term
public export
0 IsRedex, NotRedex : Pred $ Term q d n
IsRedex = So . isRedex
NotRedex = No . isRedex
parameters (q : Type) (d, n : Nat) {g : q -> Type} (defs : Definitions' q g)
public export
0 NonRedexElim, NonRedexTerm : Type
NonRedexElim = Subset (Elim q d n) (NotRedex defs)
NonRedexTerm = Subset (Term q d n) (NotRedex defs)
parameters (defs : Definitions' q g)
mutual
namespace Elim
export covering export covering
whnf : Elim q d n -> NonRedexElim q d n defs Whnf Elim Reduce.isRedexE where
whnf (F x) with (lookupElim x defs) proof eq whnf defs (F x) with (lookupElim x defs) proof eq
_ | Just y = whnf y _ | Just y = whnf defs y
_ | Nothing = Element (F x) $ rewrite eq in Ah _ | Nothing = Element (F x) $ rewrite eq in Ah
whnf (B i) = Element (B i) Ah whnf _ (B i) = nred $ B i
whnf (f :@ s) = whnf defs (f :@ s) =
let Element f fnf = whnf f in let Element f fnf = whnf defs f in
case nchoose $ isLamHead f of case nchoose $ isLamHead f of
Left _ => Left _ =>
let Lam {body, _} :# Pi {arg, res, _} = f let Lam {body, _} :# Pi {arg, res, _} = f
s = s :# arg s = s :# arg
in in
whnf $ sub1 body s :# sub1 res s whnf defs $ sub1 body s :# sub1 res s
Right nlh => Element (f :@ s) $ fnf `orNo` nlh Right nlh => Element (f :@ s) $ fnf `orNo` nlh
whnf (CasePair pi pair r ret x y body) = whnf defs (CasePair pi pair r ret x y body) =
let Element pair pairnf = whnf pair in let Element pair pairnf = whnf defs pair in
case nchoose $ isPairHead pair of case nchoose $ isPairHead pair of
Left _ => Left _ =>
let Pair {fst, snd} :# Sig {fst = tfst, snd = tsnd, _} = pair let Pair {fst, snd} :# Sig {fst = tfst, snd = tsnd, _} = pair
fst = fst :# tfst fst = fst :# tfst
snd = snd :# sub1 tsnd fst snd = snd :# sub1 tsnd fst
in in
whnf $ subN body [fst, snd] :# sub1 ret pair whnf defs $ subN body [fst, snd] :# sub1 ret pair
Right np => Right np =>
Element (CasePair pi pair r ret x y body) $ pairnf `orNo` np Element (CasePair pi pair r ret x y body) $ pairnf `orNo` np
whnf (f :% p) = whnf defs (f :% p) =
let Element f fnf = whnf f in let Element f fnf = whnf defs f in
case nchoose $ isDLamHead f of case nchoose $ isDLamHead f of
Left _ => Left _ =>
let DLam {body, _} :# Eq {ty, l, r, _} = f let DLam {body, _} :# Eq {ty, l, r, _} = f
body = endsOr l r (dsub1 body p) p body = endsOr l r (dsub1 body p) p
in in
whnf $ body :# dsub1 ty p whnf defs $ body :# dsub1 ty p
Right ndlh => Right ndlh =>
Element (f :% p) $ fnf `orNo` ndlh Element (f :% p) $ fnf `orNo` ndlh
whnf (s :# a) = whnf defs (s :# a) =
let Element s snf = whnf s in let Element s snf = whnf defs s in
case nchoose $ isE s of case nchoose $ isE s of
Left _ => let E e = s in Element e $ noOr2 snf Left _ => let E e = s in Element e $ noOr2 snf
Right ne => Right ne =>
let Element a anf = whnf a in let Element a anf = whnf defs a in
Element (s :# a) $ ne `orNo` snf `orNo` anf Element (s :# a) $ ne `orNo` snf `orNo` anf
whnf (CloE el th) = whnf $ pushSubstsWith' id th el whnf defs (CloE el th) = whnf defs $ pushSubstsWith' id th el
whnf (DCloE el th) = whnf $ pushSubstsWith' th id el whnf defs (DCloE el th) = whnf defs $ pushSubstsWith' th id el
namespace Term
export covering export covering
whnf : Term q d n -> NonRedexTerm q d n defs Whnf Term Reduce.isRedexT where
whnf t@(TYPE {}) = Element t Ah whnf _ t@(TYPE {}) = nred t
whnf t@(Pi {}) = Element t Ah whnf _ t@(Pi {}) = nred t
whnf t@(Lam {}) = Element t Ah whnf _ t@(Lam {}) = nred t
whnf t@(Sig {}) = Element t Ah whnf _ t@(Sig {}) = nred t
whnf t@(Pair {}) = Element t Ah whnf _ t@(Pair {}) = nred t
whnf t@(Eq {}) = Element t Ah whnf _ t@(Eq {}) = nred t
whnf t@(DLam {}) = Element t Ah whnf _ t@(DLam {}) = nred t
whnf (E e) = whnf defs (E e) =
let Element e enf = whnf e in let Element e enf = whnf defs e in
case nchoose $ isAnn e of case nchoose $ isAnn e of
Left _ => let tm :# _ = e in Element tm $ noOr1 $ noOr2 enf Left _ => let tm :# _ = e in Element tm $ noOr1 $ noOr2 enf
Right na => Element (E e) $ na `orNo` enf Right na => Element (E e) $ na `orNo` enf
whnf (CloT tm th) = whnf $ pushSubstsWith' id th tm whnf defs (CloT tm th) = whnf defs $ pushSubstsWith' id th tm
whnf (DCloT tm th) = whnf $ pushSubstsWith' th id tm whnf defs (DCloT tm th) = whnf defs $ pushSubstsWith' th id tm