2023-02-19 12:22:53 -05:00
|
|
|
module Quox.Reduce
|
2022-04-23 18:21:30 -04:00
|
|
|
|
2023-01-22 18:53:34 -05:00
|
|
|
import Quox.No
|
2023-02-19 12:22:53 -05:00
|
|
|
import Quox.Syntax
|
|
|
|
import Quox.Definition
|
2023-01-26 13:54:46 -05:00
|
|
|
import Data.Vect
|
2023-01-22 18:53:34 -05:00
|
|
|
import Data.Maybe
|
2023-02-22 01:45:10 -05:00
|
|
|
import Data.List
|
2022-04-23 18:21:30 -04:00
|
|
|
|
2022-05-02 16:38:37 -04:00
|
|
|
%default total
|
|
|
|
|
2022-04-23 18:21:30 -04:00
|
|
|
|
2023-01-22 18:53:34 -05:00
|
|
|
|
2023-02-20 15:42:31 -05:00
|
|
|
public export
|
|
|
|
0 CloTest : TermLike -> Type
|
|
|
|
CloTest tm = forall q, d, n. tm q d n -> Bool
|
2022-04-23 18:21:30 -04:00
|
|
|
|
2023-02-20 15:42:31 -05:00
|
|
|
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)
|
2022-04-23 18:21:30 -04:00
|
|
|
|
2023-01-08 14:44:25 -05:00
|
|
|
public export
|
2023-02-20 15:42:31 -05:00
|
|
|
0 NotClo : {isClo : CloTest tm} -> PushSubsts tm isClo => Pred (tm q d n)
|
|
|
|
NotClo = No . isClo
|
2022-04-23 18:21:30 -04:00
|
|
|
|
2023-01-08 14:44:25 -05:00
|
|
|
public export
|
2023-02-20 15:42:31 -05:00
|
|
|
0 NonClo : (tm : TermLike) -> {isClo : CloTest tm} ->
|
|
|
|
PushSubsts tm isClo => TermLike
|
|
|
|
NonClo tm q d n = Subset (tm q d n) NotClo
|
2022-04-23 18:21:30 -04:00
|
|
|
|
|
|
|
public export %inline
|
2023-02-20 15:42:31 -05:00
|
|
|
nclo : {isClo : CloTest tm} -> (0 _ : PushSubsts tm isClo) =>
|
|
|
|
(t : tm q d n) -> (0 nc : NotClo t) => NonClo tm q d n
|
|
|
|
nclo t = Element t nc
|
|
|
|
|
|
|
|
parameters {0 isClo : CloTest tm} {auto _ : PushSubsts tm isClo}
|
|
|
|
||| if the input term has any top-level closures, push them under one layer of
|
|
|
|
||| syntax
|
|
|
|
export %inline
|
|
|
|
pushSubsts : tm q d n -> NonClo tm q d n
|
|
|
|
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
|
2022-04-23 18:21:30 -04:00
|
|
|
|
2023-02-20 15:42:31 -05:00
|
|
|
mutual
|
|
|
|
public export
|
|
|
|
isCloT : CloTest Term
|
|
|
|
isCloT (CloT {}) = True
|
|
|
|
isCloT (DCloT {}) = True
|
|
|
|
isCloT (E e) = isCloE e
|
|
|
|
isCloT _ = False
|
2022-04-23 18:21:30 -04:00
|
|
|
|
2023-02-20 15:42:31 -05:00
|
|
|
public export
|
|
|
|
isCloE : CloTest Elim
|
|
|
|
isCloE (CloE {}) = True
|
|
|
|
isCloE (DCloE {}) = True
|
|
|
|
isCloE _ = False
|
2022-04-23 18:21:30 -04:00
|
|
|
|
|
|
|
mutual
|
2023-02-20 15:42:31 -05:00
|
|
|
export
|
|
|
|
PushSubsts Term Reduce.isCloT where
|
2023-01-22 18:53:34 -05:00
|
|
|
pushSubstsWith th ph (TYPE l) =
|
2023-02-20 15:42:31 -05:00
|
|
|
nclo $ TYPE l
|
2023-02-22 01:40:19 -05:00
|
|
|
pushSubstsWith th ph (Pi qty a body) =
|
|
|
|
nclo $ Pi qty (a // th // ph) (body // th // ph)
|
|
|
|
pushSubstsWith th ph (Lam body) =
|
|
|
|
nclo $ Lam (body // th // ph)
|
|
|
|
pushSubstsWith th ph (Sig a b) =
|
|
|
|
nclo $ Sig (a // th // ph) (b // th // ph)
|
2023-01-26 13:54:46 -05:00
|
|
|
pushSubstsWith th ph (Pair s t) =
|
2023-02-20 16:22:23 -05:00
|
|
|
nclo $ Pair (s // th // ph) (t // th // ph)
|
2023-02-22 01:45:10 -05:00
|
|
|
pushSubstsWith th ph (Enum tags) =
|
|
|
|
nclo $ Enum tags
|
|
|
|
pushSubstsWith th ph (Tag tag) =
|
|
|
|
nclo $ Tag tag
|
2023-02-22 01:40:19 -05:00
|
|
|
pushSubstsWith th ph (Eq ty l r) =
|
|
|
|
nclo $ Eq (ty // th // ph) (l // th // ph) (r // th // ph)
|
|
|
|
pushSubstsWith th ph (DLam body) =
|
|
|
|
nclo $ DLam (body // th // ph)
|
2023-01-22 18:53:34 -05:00
|
|
|
pushSubstsWith th ph (E e) =
|
2023-02-20 15:42:31 -05:00
|
|
|
let Element e nc = pushSubstsWith th ph e in nclo $ E e
|
2023-01-22 18:53:34 -05:00
|
|
|
pushSubstsWith th ph (CloT s ps) =
|
|
|
|
pushSubstsWith th (comp th ps ph) s
|
|
|
|
pushSubstsWith th ph (DCloT s ps) =
|
|
|
|
pushSubstsWith (ps . th) ph s
|
|
|
|
|
2023-02-20 15:42:31 -05:00
|
|
|
export
|
|
|
|
PushSubsts Elim Reduce.isCloE where
|
2023-01-22 18:53:34 -05:00
|
|
|
pushSubstsWith th ph (F x) =
|
2023-02-20 15:42:31 -05:00
|
|
|
nclo $ F x
|
2023-01-22 18:53:34 -05:00
|
|
|
pushSubstsWith th ph (B i) =
|
|
|
|
let res = ph !! i in
|
2023-02-20 15:42:31 -05:00
|
|
|
case nchoose $ isCloE res of
|
2023-01-22 18:53:34 -05:00
|
|
|
Left yes => assert_total pushSubsts res
|
|
|
|
Right no => Element res no
|
|
|
|
pushSubstsWith th ph (f :@ s) =
|
2023-02-20 16:22:23 -05:00
|
|
|
nclo $ (f // th // ph) :@ (s // th // ph)
|
2023-02-22 01:40:19 -05:00
|
|
|
pushSubstsWith th ph (CasePair pi p r b) =
|
|
|
|
nclo $ CasePair pi (p // th // ph) (r // th // ph) (b // th // ph)
|
2023-02-22 01:45:10 -05:00
|
|
|
pushSubstsWith th ph (CaseEnum pi t r arms) =
|
|
|
|
nclo $ CaseEnum pi (t // th // ph) (r // th // ph)
|
|
|
|
(map (\b => b // th // ph) arms)
|
2023-01-22 18:53:34 -05:00
|
|
|
pushSubstsWith th ph (f :% d) =
|
2023-02-20 16:22:23 -05:00
|
|
|
nclo $ (f // th // ph) :% (d // th)
|
2023-01-22 18:53:34 -05:00
|
|
|
pushSubstsWith th ph (s :# a) =
|
2023-02-20 16:22:23 -05:00
|
|
|
nclo $ (s // th // ph) :# (a // th // ph)
|
2023-01-22 18:53:34 -05:00
|
|
|
pushSubstsWith th ph (CloE e ps) =
|
|
|
|
pushSubstsWith th (comp th ps ph) e
|
|
|
|
pushSubstsWith th ph (DCloE e ps) =
|
|
|
|
pushSubstsWith (ps . th) ph e
|
2022-04-23 18:21:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-02-22 01:45:10 -05:00
|
|
|
||| errors that might happen if you pass an ill typed expression into
|
|
|
|
||| whnf. don't do that please
|
|
|
|
public export
|
|
|
|
data WhnfErr = MissingEnumArm TagVal (List TagVal)
|
|
|
|
|
|
|
|
|
2023-02-20 15:42:31 -05:00
|
|
|
public export
|
|
|
|
0 RedexTest : TermLike -> Type
|
|
|
|
RedexTest tm = forall q, d, n, g. Definitions' q g -> tm q d n -> Bool
|
|
|
|
|
|
|
|
public export
|
2023-02-22 01:45:10 -05:00
|
|
|
interface Whnf (0 tm : TermLike)
|
|
|
|
(0 isRedex : RedexTest tm)
|
|
|
|
(0 err : Type) | tm
|
|
|
|
where
|
2023-02-20 15:42:31 -05:00
|
|
|
whnf : (defs : Definitions' q g) ->
|
2023-02-22 01:45:10 -05:00
|
|
|
tm q d n -> Either err (Subset (tm q d n) (No . isRedex defs))
|
2023-02-20 15:42:31 -05:00
|
|
|
|
|
|
|
public export
|
2023-02-22 01:45:10 -05:00
|
|
|
0 IsRedex, NotRedex : {isRedex : RedexTest tm} -> Whnf tm isRedex err =>
|
2023-02-20 15:42:31 -05:00
|
|
|
Definitions' q g -> Pred (tm q d n)
|
|
|
|
IsRedex defs = So . isRedex defs
|
|
|
|
NotRedex defs = No . isRedex defs
|
|
|
|
|
|
|
|
public export
|
2023-02-22 01:45:10 -05:00
|
|
|
0 NonRedex : (tm : TermLike) -> {isRedex : RedexTest tm} ->
|
|
|
|
Whnf tm isRedex err =>
|
2023-02-20 15:42:31 -05:00
|
|
|
(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
|
2023-02-22 01:45:10 -05:00
|
|
|
nred : {0 isRedex : RedexTest tm} -> (0 _ : Whnf tm isRedex err) =>
|
2023-02-20 15:42:31 -05:00
|
|
|
(t : tm q d n) -> (0 nr : NotRedex defs t) =>
|
|
|
|
NonRedex tm q d n defs
|
|
|
|
nred t = Element t nr
|
2022-04-23 18:21:30 -04:00
|
|
|
|
|
|
|
|
2023-01-20 20:34:28 -05:00
|
|
|
public export %inline
|
2023-01-22 18:53:34 -05:00
|
|
|
isLamHead : Elim {} -> Bool
|
|
|
|
isLamHead (Lam {} :# Pi {}) = True
|
|
|
|
isLamHead _ = False
|
2022-05-25 09:59:58 -04:00
|
|
|
|
|
|
|
public export %inline
|
2023-01-22 18:53:34 -05:00
|
|
|
isDLamHead : Elim {} -> Bool
|
|
|
|
isDLamHead (DLam {} :# Eq {}) = True
|
|
|
|
isDLamHead _ = False
|
2022-05-25 09:59:58 -04:00
|
|
|
|
2023-01-26 13:54:46 -05:00
|
|
|
public export %inline
|
|
|
|
isPairHead : Elim {} -> Bool
|
|
|
|
isPairHead (Pair {} :# Sig {}) = True
|
|
|
|
isPairHead _ = False
|
|
|
|
|
2023-02-22 01:45:10 -05:00
|
|
|
public export %inline
|
|
|
|
isTagHead : Elim {} -> Bool
|
|
|
|
isTagHead (Tag t :# Enum _) = True
|
|
|
|
isTagHead _ = False
|
|
|
|
|
2022-05-25 09:59:58 -04:00
|
|
|
public export %inline
|
2023-01-22 18:53:34 -05:00
|
|
|
isE : Term {} -> Bool
|
|
|
|
isE (E _) = True
|
|
|
|
isE _ = False
|
2022-05-25 09:59:58 -04:00
|
|
|
|
|
|
|
public export %inline
|
2023-01-22 18:53:34 -05:00
|
|
|
isAnn : Elim {} -> Bool
|
|
|
|
isAnn (_ :# _) = True
|
|
|
|
isAnn _ = False
|
|
|
|
|
2023-02-20 15:42:31 -05:00
|
|
|
|
|
|
|
mutual
|
|
|
|
public export
|
|
|
|
isRedexE : RedexTest Elim
|
|
|
|
isRedexE defs (F x) {d, n} =
|
|
|
|
isJust $ lookupElim x defs {d, n}
|
|
|
|
isRedexE _ (B _) = False
|
|
|
|
isRedexE defs (f :@ _) =
|
|
|
|
isRedexE defs f || isLamHead f
|
|
|
|
isRedexE defs (CasePair {pair, _}) =
|
|
|
|
isRedexE defs pair || isPairHead pair
|
2023-02-22 01:45:10 -05:00
|
|
|
isRedexE defs (CaseEnum {tag, _}) =
|
|
|
|
isRedexE defs tag || isTagHead tag
|
2023-02-20 15:42:31 -05:00
|
|
|
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
|
|
|
|
|
2023-02-19 12:22:53 -05:00
|
|
|
public export
|
2023-02-20 15:42:31 -05:00
|
|
|
isRedexT : RedexTest Term
|
|
|
|
isRedexT _ (CloT {}) = True
|
|
|
|
isRedexT _ (DCloT {}) = True
|
|
|
|
isRedexT defs (E e) = isAnn e || isRedexE defs e
|
|
|
|
isRedexT _ _ = False
|
|
|
|
|
|
|
|
mutual
|
|
|
|
export covering
|
2023-02-22 01:45:10 -05:00
|
|
|
Whnf Elim Reduce.isRedexE WhnfErr where
|
2023-02-20 15:42:31 -05:00
|
|
|
whnf defs (F x) with (lookupElim x defs) proof eq
|
|
|
|
_ | Just y = whnf defs y
|
2023-02-22 01:45:10 -05:00
|
|
|
_ | Nothing = pure $ Element (F x) $ rewrite eq in Ah
|
2023-02-20 15:42:31 -05:00
|
|
|
|
2023-02-22 01:45:10 -05:00
|
|
|
whnf _ (B i) = pure $ nred $ B i
|
2023-02-20 15:42:31 -05:00
|
|
|
|
2023-02-22 01:45:10 -05:00
|
|
|
whnf defs (f :@ s) = do
|
|
|
|
Element f fnf <- whnf defs f
|
2023-02-20 15:42:31 -05:00
|
|
|
case nchoose $ isLamHead f of
|
|
|
|
Left _ =>
|
2023-02-22 01:40:19 -05:00
|
|
|
let Lam body :# Pi {arg, res, _} = f
|
2023-02-20 15:42:31 -05:00
|
|
|
s = s :# arg
|
|
|
|
in
|
|
|
|
whnf defs $ sub1 body s :# sub1 res s
|
2023-02-22 01:45:10 -05:00
|
|
|
Right nlh => pure $ Element (f :@ s) $ fnf `orNo` nlh
|
2023-02-20 15:42:31 -05:00
|
|
|
|
2023-02-22 01:45:10 -05:00
|
|
|
whnf defs (CasePair pi pair ret body) = do
|
|
|
|
Element pair pairnf <- whnf defs pair
|
2023-02-20 15:42:31 -05:00
|
|
|
case nchoose $ isPairHead pair of
|
|
|
|
Left _ =>
|
|
|
|
let Pair {fst, snd} :# Sig {fst = tfst, snd = tsnd, _} = pair
|
|
|
|
fst = fst :# tfst
|
|
|
|
snd = snd :# sub1 tsnd fst
|
|
|
|
in
|
|
|
|
whnf defs $ subN body [fst, snd] :# sub1 ret pair
|
|
|
|
Right np =>
|
2023-02-22 01:45:10 -05:00
|
|
|
pure $ Element (CasePair pi pair ret body)
|
|
|
|
(pairnf `orNo` np)
|
|
|
|
|
|
|
|
whnf defs (CaseEnum pi tag ret arms) = do
|
|
|
|
Element tag tagnf <- whnf defs tag
|
|
|
|
case nchoose $ isTagHead tag of
|
|
|
|
Left t =>
|
|
|
|
let Tag t :# Enum ts = tag
|
|
|
|
ty = sub1 ret tag
|
|
|
|
in
|
|
|
|
case lookup t arms of
|
|
|
|
Just arm => whnf defs $ arm :# ty
|
|
|
|
Nothing => Left $ MissingEnumArm t (keys arms)
|
|
|
|
Right nt =>
|
|
|
|
pure $ Element (CaseEnum pi tag ret arms) $ tagnf `orNo` nt
|
|
|
|
|
|
|
|
whnf defs (f :% p) = do
|
|
|
|
Element f fnf <- whnf defs f
|
2023-02-20 15:42:31 -05:00
|
|
|
case nchoose $ isDLamHead f of
|
|
|
|
Left _ =>
|
2023-02-22 01:40:19 -05:00
|
|
|
let DLam body :# Eq {ty = ty, l, r, _} = f
|
2023-02-20 15:42:31 -05:00
|
|
|
body = endsOr l r (dsub1 body p) p
|
|
|
|
in
|
|
|
|
whnf defs $ body :# dsub1 ty p
|
|
|
|
Right ndlh =>
|
2023-02-22 01:45:10 -05:00
|
|
|
pure $ Element (f :% p) $ fnf `orNo` ndlh
|
2023-02-20 15:42:31 -05:00
|
|
|
|
2023-02-22 01:45:10 -05:00
|
|
|
whnf defs (s :# a) = do
|
|
|
|
Element s snf <- whnf defs s
|
2023-02-20 15:42:31 -05:00
|
|
|
case nchoose $ isE s of
|
2023-02-22 01:45:10 -05:00
|
|
|
Left _ => let E e = s in pure $ Element e $ noOr2 snf
|
|
|
|
Right ne => do
|
|
|
|
Element a anf <- whnf defs a
|
|
|
|
pure $ Element (s :# a) $ ne `orNo` snf `orNo` anf
|
2023-02-20 15:42:31 -05:00
|
|
|
|
|
|
|
whnf defs (CloE el th) = whnf defs $ pushSubstsWith' id th el
|
|
|
|
whnf defs (DCloE el th) = whnf defs $ pushSubstsWith' th id el
|
|
|
|
|
|
|
|
export covering
|
2023-02-22 01:45:10 -05:00
|
|
|
Whnf Term Reduce.isRedexT WhnfErr where
|
|
|
|
whnf _ t@(TYPE {}) = pure $ nred t
|
|
|
|
whnf _ t@(Pi {}) = pure $ nred t
|
|
|
|
whnf _ t@(Lam {}) = pure $ nred t
|
|
|
|
whnf _ t@(Sig {}) = pure $ nred t
|
|
|
|
whnf _ t@(Pair {}) = pure $ nred t
|
|
|
|
whnf _ t@(Enum {}) = pure $ nred t
|
|
|
|
whnf _ t@(Tag {}) = pure $ nred t
|
|
|
|
whnf _ t@(Eq {}) = pure $ nred t
|
|
|
|
whnf _ t@(DLam {}) = pure $ nred t
|
|
|
|
|
|
|
|
whnf defs (E e) = do
|
|
|
|
Element e enf <- whnf defs e
|
2023-02-20 15:42:31 -05:00
|
|
|
case nchoose $ isAnn e of
|
2023-02-22 01:45:10 -05:00
|
|
|
Left _ => let tm :# _ = e in pure $ Element tm $ noOr1 $ noOr2 enf
|
|
|
|
Right na => pure $ Element (E e) $ na `orNo` enf
|
2023-02-20 15:42:31 -05:00
|
|
|
|
|
|
|
whnf defs (CloT tm th) = whnf defs $ pushSubstsWith' id th tm
|
|
|
|
whnf defs (DCloT tm th) = whnf defs $ pushSubstsWith' th id tm
|