quox/lib/Quox/Definition.idr

155 lines
4.1 KiB
Idris
Raw Normal View History

2022-08-22 04:17:08 -04:00
module Quox.Definition
import public Quox.No
2022-08-22 04:17:08 -04:00
import public Quox.Syntax
2023-08-28 13:59:36 -04:00
import Quox.Displace
2022-08-22 04:17:08 -04:00
import public Data.SortedMap
2023-05-01 21:06:25 -04:00
import public Quox.Loc
2023-10-15 10:12:43 -04:00
import Quox.Pretty
2023-03-31 13:23:30 -04:00
import Control.Eff
2023-10-15 10:23:38 -04:00
import Data.Singleton
2023-01-08 14:44:25 -05:00
import Decidable.Decidable
2022-08-22 04:17:08 -04:00
public export
2024-05-27 15:28:22 -04:00
data DefBody : (q : Nat) -> Type where
MonoDef : Term 0 0 0 -> DefBody 0
PolyDef : (0 nz : IsSucc q) => Term q 0 0 -> DefBody q
Postulate : DefBody q
namespace DefBody
public export
2024-05-27 15:28:22 -04:00
(.term0) : DefBody q -> Maybe (Term q 0 0)
(MonoDef t).term0 = Just t
(PolyDef t).term0 = Just t
(Postulate).term0 = Nothing
2022-08-22 04:17:08 -04:00
public export
2023-04-01 13:16:43 -04:00
record Definition where
constructor MkDef
qty : GQty
2024-05-27 15:28:22 -04:00
{qlen : Nat}
qargs : BContext qlen
type0 : Term qlen 0 0
body0 : DefBody qlen
scheme : Maybe String
isMain : Bool
loc_ : Loc
2022-08-22 04:17:08 -04:00
public export %inline
2024-05-27 15:28:22 -04:00
mkPostulate : GQty -> BContext q -> Term q 0 0 ->
Maybe String -> Bool -> Loc -> Definition
mkPostulate qty qargs type0 scheme isMain loc_ =
let Val q = lengthPrf0 qargs in
MkDef {qty, qargs, type0, body0 = Postulate, scheme, isMain, loc_}
2022-08-22 04:17:08 -04:00
2023-03-13 14:33:09 -04:00
public export %inline
2024-05-27 15:28:22 -04:00
mkDef : GQty -> BContext q -> (type0, term0 : Term q 0 0) -> Maybe String ->
Bool -> Loc -> Definition
mkDef qty qargs type0 term0 scheme isMain loc_ =
case (lengthPrf0 qargs) of
Val 0 =>
MkDef {qty, qargs, type0, body0 = MonoDef term0, scheme, isMain, loc_}
Val (S _) =>
MkDef {qty, qargs, type0, body0 = PolyDef term0, scheme, isMain, loc_}
2023-05-01 21:06:25 -04:00
export Located Definition where def.loc = def.loc_
export Relocatable Definition where setLoc loc = {loc_ := loc}
2022-08-22 04:17:08 -04:00
2024-05-27 15:28:22 -04:00
public export
record Poly (0 tm : TermLike) d n where
constructor P
qlen : Nat
type : tm qlen d n
parameters {d, n : Nat}
public export %inline
2024-05-27 15:28:22 -04:00
(.type) : Definition -> Poly Term d n
def.type = P def.qlen $ def.type0 // shift0 d // shift0 n
2023-01-20 19:41:21 -05:00
2023-08-28 13:59:36 -04:00
public export %inline
2024-05-27 15:28:22 -04:00
(.typeAt) : Definition -> Universe -> Poly Term d n
def.typeAt u = {type $= displace u} def.type
2023-08-28 13:59:36 -04:00
public export %inline
2024-05-27 15:28:22 -04:00
(.term) : Definition -> Maybe (Poly Term d n)
def.term = def.body0.term0 <&> \t => P def.qlen $ t // shift0 d // shift0 n
public export %inline
2024-05-27 15:28:22 -04:00
(.termAt) : Definition -> Universe -> Maybe (Poly Term d n)
def.termAt u = {type $= displace u} <$> def.term
2023-08-28 13:59:36 -04:00
public export %inline
2024-05-27 15:28:22 -04:00
toElim : Definition -> Universe -> Maybe (Poly Elim d n)
toElim def u = do
tm <- def.body0.term0; let ty = def.type0
pure $ P def.qlen $ Ann tm ty def.loc // shift0 d // shift0 n
2023-10-15 10:23:38 -04:00
public export
2024-05-27 15:28:22 -04:00
(.typeWith) : Definition -> Singleton d -> Singleton n -> Poly Term d n
def.typeWith (Val d) (Val n) = def.type
2023-10-15 10:23:38 -04:00
public export
2024-05-27 15:28:22 -04:00
(.typeWithAt) : Definition -> Singleton d -> Singleton n ->
Universe -> Poly Term d n
def.typeWithAt (Val d) (Val n) u = def.typeAt u
2023-10-15 10:23:38 -04:00
public export
2024-05-27 15:28:22 -04:00
(.termWith) : Definition -> Singleton d -> Singleton n -> Maybe (Poly Term d n)
2023-10-15 10:23:38 -04:00
g.termWith (Val d) (Val n) = g.term
2022-08-22 04:17:08 -04:00
public export %inline
2023-04-01 13:16:43 -04:00
isZero : Definition -> Bool
isZero g = g.qty == GZero
2023-01-08 14:44:25 -05:00
2023-04-17 17:58:24 -04:00
public export
NDefinition : Type
NDefinition = (Name, Definition)
2023-04-17 17:58:24 -04:00
2023-01-08 14:44:25 -05:00
public export
2023-04-01 13:16:43 -04:00
Definitions : Type
Definitions = SortedMap Name Definition
2023-01-08 14:44:25 -05:00
public export
data DefEnvTag = DEFS
2023-01-20 19:41:30 -05:00
public export
2023-05-01 21:06:25 -04:00
DefsReader : Type -> Type
2023-04-17 17:58:24 -04:00
DefsReader = ReaderL DEFS Definitions
2023-05-01 21:06:25 -04:00
public export
DefsState : Type -> Type
DefsState = StateL DEFS Definitions
public export %inline
2024-05-27 15:28:22 -04:00
lookupElim : {d, n : Nat} ->
Name -> Universe -> Definitions -> Maybe (Poly Elim d n)
2023-08-28 13:59:36 -04:00
lookupElim x u defs = toElim !(lookup x defs) u
2023-10-15 10:12:43 -04:00
2023-10-15 10:23:38 -04:00
public export %inline
2024-05-27 15:28:22 -04:00
lookupElim0 : Name -> Universe -> Definitions -> Maybe (Poly Elim 0 0)
2023-10-15 10:23:38 -04:00
lookupElim0 = lookupElim
2023-10-15 10:12:43 -04:00
2024-05-27 15:28:22 -04:00
export
prettyQBinders : {opts : LayoutOpts} -> BContext q -> Eff Pretty (Doc opts)
prettyQBinders [<] = pure empty
prettyQBinders qnames =
qbrackets . separateTight !commaD =<< traverse prettyQBind (toList' qnames)
2023-10-15 10:12:43 -04:00
export
prettyDef : {opts : LayoutOpts} -> Name -> Definition -> Eff Pretty (Doc opts)
prettyDef name def = withPrec Outer $ do
2024-05-27 15:28:22 -04:00
qty <- prettyQConst def.qty.qconst
2023-10-15 10:12:43 -04:00
dot <- dotD
name <- prettyFree name
2024-05-27 15:28:22 -04:00
qargs <- prettyQBinders def.qargs
2023-10-15 10:12:43 -04:00
colon <- colonD
2024-05-27 15:28:22 -04:00
type <- prettyTerm (fromQNames def.qargs) def.type0
hangDSingle (hsep [hcat [qty, dot, name, qargs], colon]) type