quox/lib/Quox/Syntax/Term/Split.idr

208 lines
5.4 KiB
Idris
Raw Normal View History

2022-04-23 18:21:30 -04:00
module Quox.Syntax.Term.Split
import Quox.Syntax.Term.Base
import Quox.Syntax.Term.Subst
import Data.So
import Data.Vect
%default total
2023-01-08 14:44:25 -05:00
public export
2023-01-20 20:34:28 -05:00
data IsLam : Pred $ Term {} where ItIsLam : IsLam $ Lam x body
2023-01-08 14:44:25 -05:00
2022-04-23 18:21:30 -04:00
public export %inline
2023-01-20 20:34:28 -05:00
isLam : Dec1 IsLam
2023-01-08 14:44:25 -05:00
isLam (TYPE _) = No $ \case _ impossible
isLam (Pi {}) = No $ \case _ impossible
isLam (Lam {}) = Yes ItIsLam
2023-01-20 20:34:28 -05:00
isLam (Eq {}) = No $ \case _ impossible
isLam (DLam {}) = No $ \case _ impossible
2023-01-08 14:44:25 -05:00
isLam (E _) = No $ \case _ impossible
isLam (CloT {}) = No $ \case _ impossible
isLam (DCloT {}) = No $ \case _ impossible
2022-04-23 18:21:30 -04:00
2023-01-20 20:34:28 -05:00
public export
0 NotLam : Pred $ Term {}
2023-01-08 14:44:25 -05:00
NotLam = Not . IsLam
2022-04-23 18:21:30 -04:00
2023-01-08 14:44:25 -05:00
public export
2023-01-20 20:34:28 -05:00
data IsDLam : Pred $ Term {} where ItIsDLam : IsDLam $ DLam i body
public export %inline
isDLam : Dec1 IsDLam
isDLam (TYPE _) = No $ \case _ impossible
isDLam (Pi {}) = No $ \case _ impossible
isDLam (Eq {}) = No $ \case _ impossible
isDLam (Lam {}) = No $ \case _ impossible
isDLam (DLam {}) = Yes ItIsDLam
isDLam (E _) = No $ \case _ impossible
isDLam (CloT {}) = No $ \case _ impossible
isDLam (DCloT {}) = No $ \case _ impossible
public export
0 NotDLam : Pred $ Term {}
NotDLam = Not . IsDLam
public export
data IsApp : Pred $ Elim {} where ItIsApp : IsApp $ f :@ s
2023-01-08 14:44:25 -05:00
2022-04-23 18:21:30 -04:00
public export %inline
2023-01-20 20:34:28 -05:00
isApp : Dec1 IsApp
2023-01-08 14:44:25 -05:00
isApp (F _) = No $ \case _ impossible
isApp (B _) = No $ \case _ impossible
isApp (_ :@ _) = Yes ItIsApp
2023-01-20 20:34:28 -05:00
isApp (_ :% _) = No $ \case _ impossible
2023-01-08 14:44:25 -05:00
isApp (_ :# _) = No $ \case _ impossible
isApp (CloE {}) = No $ \case _ impossible
isApp (DCloE {}) = No $ \case _ impossible
2022-04-23 18:21:30 -04:00
public export
2023-01-20 20:34:28 -05:00
0 NotApp : Pred $ Elim {}
2023-01-08 14:44:25 -05:00
NotApp = Not . IsApp
2022-04-23 18:21:30 -04:00
2023-01-20 20:34:28 -05:00
public export
data IsDApp : Pred $ Elim {} where ItIsDApp : IsDApp $ f :% d
public export %inline
isDApp : Dec1 IsDApp
isDApp (F _) = No $ \case _ impossible
isDApp (B _) = No $ \case _ impossible
isDApp (_ :@ _) = No $ \case _ impossible
isDApp (_ :% _) = Yes ItIsDApp
isDApp (_ :# _) = No $ \case _ impossible
isDApp (CloE {}) = No $ \case _ impossible
isDApp (DCloE {}) = No $ \case _ impossible
public export
0 NotDApp : Pred $ Elim {}
NotDApp = Not . IsDApp
2022-04-23 18:21:30 -04:00
infixl 9 :@@
||| apply multiple arguments at once
public export %inline
2023-01-08 14:44:25 -05:00
(:@@) : Elim q d n -> List (Term q d n) -> Elim q d n
2022-04-23 18:21:30 -04:00
f :@@ ss = foldl (:@) f ss
public export
2023-01-08 14:44:25 -05:00
record GetArgs q d n where
2022-04-23 18:21:30 -04:00
constructor GotArgs
2023-01-08 14:44:25 -05:00
fun : Elim q d n
args : List (Term q d n)
2022-04-23 18:21:30 -04:00
0 notApp : NotApp fun
export
2023-01-08 14:44:25 -05:00
getArgs' : Elim q d n -> List (Term q d n) -> GetArgs q d n
getArgs' fun args with (isApp fun)
2023-01-20 20:34:28 -05:00
getArgs' (f :@ a) args | Yes _ = getArgs' f (a :: args)
getArgs' fun args | No no = GotArgs {fun, args, notApp = no}
2022-04-23 18:21:30 -04:00
||| splits an application into its head and arguments. if it's not an
||| application then the list is just empty
export %inline
2023-01-08 14:44:25 -05:00
getArgs : Elim q d n -> GetArgs q d n
2022-04-23 18:21:30 -04:00
getArgs e = getArgs' e []
2023-01-20 20:34:28 -05:00
infixl 9 :%%
||| apply multiple dimension arguments at once
public export %inline
(:%%) : Elim q d n -> List (Dim d) -> Elim q d n
f :%% ss = foldl (:%) f ss
public export
record GetDArgs q d n where
constructor GotDArgs
fun : Elim q d n
args : List (Dim d)
0 notDApp : NotDApp fun
export
getDArgs' : Elim q d n -> List (Dim d) -> GetDArgs q d n
getDArgs' fun args with (isDApp fun)
getDArgs' (f :% a) args | Yes yes = getDArgs' f (a :: args)
getDArgs' fun args | No no = GotDArgs {fun, args, notDApp = no}
||| splits a dimension application into its head and arguments. if it's not an
||| d application then the list is just empty
export %inline
getDArgs : Elim q d n -> GetDArgs q d n
getDArgs e = getDArgs' e []
2022-04-23 18:21:30 -04:00
infixr 1 :\\
public export
2023-01-08 14:44:25 -05:00
absN : Vect m Name -> Term q d (m + n) -> Term q d n
absN {m = 0} [] s = s
absN {m = S m} (x :: xs) s = Lam x $ TUsed $ absN xs $
rewrite sym $ plusSuccRightSucc m n in s
public export %inline
(:\\) : Vect m Name -> Term q d (m + n) -> Term q d n
(:\\) = absN
2022-04-23 18:21:30 -04:00
2023-01-20 20:34:28 -05:00
infixr 1 :\\%
public export
dabsN : Vect m Name -> Term q (m + d) n -> Term q d n
dabsN {m = 0} [] s = s
dabsN {m = S m} (x :: xs) s = DLam x $ DUsed $ dabsN xs $
rewrite sym $ plusSuccRightSucc m d in s
public export %inline
(:\\%) : Vect m Name -> Term q (m + d) n -> Term q d n
(:\\%) = dabsN
2022-04-23 18:21:30 -04:00
public export
2023-01-08 14:44:25 -05:00
record GetLams q d n where
2022-04-23 18:21:30 -04:00
constructor GotLams
2023-01-20 20:34:28 -05:00
{0 lams, rest : Nat}
2022-04-23 18:21:30 -04:00
names : Vect lams Name
2023-01-08 14:44:25 -05:00
body : Term q d rest
2022-04-23 18:21:30 -04:00
0 eq : lams + n = rest
0 notLam : NotLam body
2023-01-08 14:44:25 -05:00
export
getLams' : forall lams, rest.
2023-01-20 20:34:28 -05:00
Vect lams Name -> Term q d rest -> (0 eq : lams + n = rest) ->
GetLams q d n
2023-01-08 14:44:25 -05:00
getLams' xs s eq with (isLam s)
2023-01-20 20:34:28 -05:00
getLams' xs (Lam x sbody) Refl | Yes _ =
let body = assert_smaller (Lam x sbody) sbody.term in
2023-01-08 14:44:25 -05:00
getLams' (x :: xs) body Refl
2023-01-20 20:34:28 -05:00
getLams' xs s eq | No no = GotLams xs s eq no
2023-01-08 14:44:25 -05:00
2023-01-20 20:34:28 -05:00
export %inline
2023-01-08 14:44:25 -05:00
getLams : Term q d n -> GetLams q d n
getLams s = getLams' [] s Refl
2023-01-20 20:34:28 -05:00
public export
record GetDLams q d n where
constructor GotDLams
{0 lams, rest : Nat}
names : Vect lams Name
body : Term q rest n
0 eq : lams + d = rest
0 notDLam : NotDLam body
export
getDLams' : forall lams, rest.
Vect lams Name -> Term q rest n -> (0 eq : lams + d = rest) ->
GetDLams q d n
getDLams' is s eq with (isDLam s)
getDLams' is (DLam i sbody) Refl | Yes _ =
let body = assert_smaller (DLam i sbody) sbody.term in
getDLams' (i :: is) body Refl
getDLams' is s eq | No no = GotDLams is s eq no
export %inline
getDLams : Term q d n -> GetDLams q d n
getDLams s = getDLams' [] s Refl