2023-03-12 13:28:37 -04:00
|
|
|
||| take freshly-parsed input, translate it to core, and check it
|
|
|
|
module Quox.Parser.FromParser
|
|
|
|
|
|
|
|
import Quox.Parser.Syntax
|
2023-03-13 14:33:09 -04:00
|
|
|
import Quox.Parser.Parser
|
2023-03-12 13:28:37 -04:00
|
|
|
import Quox.Typechecker
|
|
|
|
|
2023-03-16 13:18:49 -04:00
|
|
|
import Data.List
|
|
|
|
import Data.SnocVect
|
2023-03-31 13:23:30 -04:00
|
|
|
import Quox.EffExtra
|
2023-03-12 13:28:37 -04:00
|
|
|
|
2023-03-13 14:33:09 -04:00
|
|
|
import System.File
|
|
|
|
import System.Path
|
2023-03-31 13:23:30 -04:00
|
|
|
import Data.IORef
|
2023-03-13 14:33:09 -04:00
|
|
|
|
2023-03-31 13:23:30 -04:00
|
|
|
|
|
|
|
%default total
|
|
|
|
|
|
|
|
%hide Typing.Error
|
|
|
|
%hide Lexer.Error
|
|
|
|
%hide Parser.Error
|
|
|
|
|
|
|
|
|
|
|
|
public export
|
|
|
|
0 Def : Type
|
|
|
|
Def = Definition Three
|
|
|
|
|
|
|
|
public export
|
|
|
|
0 NDef : Type
|
|
|
|
NDef = (Name, Def)
|
2023-03-13 14:33:09 -04:00
|
|
|
|
|
|
|
public export
|
|
|
|
0 Defs : Type
|
|
|
|
Defs = Definitions Three
|
2023-03-12 13:28:37 -04:00
|
|
|
|
|
|
|
public export
|
|
|
|
data FromParserError =
|
|
|
|
AnnotationNeeded PTerm
|
|
|
|
| DuplicatesInEnum (List TagVal)
|
|
|
|
| DimNotInScope PBaseName
|
|
|
|
| QtyNotGlobal PQty
|
|
|
|
| DimNameInTerm PBaseName
|
2023-03-13 14:33:09 -04:00
|
|
|
| TypeError (Typing.Error Three)
|
|
|
|
| LoadError String FileError
|
|
|
|
| ParseError Parser.Error
|
2023-03-12 13:28:37 -04:00
|
|
|
|
|
|
|
public export
|
2023-03-31 13:23:30 -04:00
|
|
|
0 IncludePath : Type
|
|
|
|
IncludePath = List String
|
2023-03-13 14:33:09 -04:00
|
|
|
|
2023-03-12 13:28:37 -04:00
|
|
|
public export
|
2023-03-31 13:23:30 -04:00
|
|
|
0 SeenFiles : Type
|
|
|
|
SeenFiles = SortedSet String
|
2023-03-12 13:28:37 -04:00
|
|
|
|
2023-03-13 14:33:09 -04:00
|
|
|
|
2023-03-12 13:28:37 -04:00
|
|
|
public export
|
2023-03-31 13:23:30 -04:00
|
|
|
data StateTag = DEFS | NS | SEEN
|
2023-03-12 13:28:37 -04:00
|
|
|
|
|
|
|
public export
|
2023-03-31 13:23:30 -04:00
|
|
|
0 FromParserEff : List (Type -> Type)
|
|
|
|
FromParserEff =
|
|
|
|
[Except Error, StateL DEFS Defs, StateL NS Mods,
|
|
|
|
Reader IncludePath, StateL SEEN SeenFiles, IO]
|
2023-03-12 13:28:37 -04:00
|
|
|
|
2023-03-31 13:23:30 -04:00
|
|
|
public export
|
|
|
|
0 FromParser : Type -> Type
|
|
|
|
FromParser = Eff FromParserEff
|
2023-03-12 13:28:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
parameters {auto _ : Functor m} (b : Var n -> m a) (f : PName -> m a)
|
|
|
|
(xs : Context' BName n)
|
|
|
|
private
|
|
|
|
fromBaseName : PBaseName -> m a
|
|
|
|
fromBaseName x = maybe (f $ MakePName [<] x) b $ Context.find (== Just x) xs
|
|
|
|
|
|
|
|
private
|
|
|
|
fromName : PName -> m a
|
|
|
|
fromName x = if null x.mods then fromBaseName x.base else f x
|
|
|
|
|
|
|
|
export
|
2023-03-31 13:23:30 -04:00
|
|
|
fromPDimWith : Has (Except Error) fs =>
|
|
|
|
Context' BName d -> PDim -> Eff fs (Dim d)
|
2023-03-12 13:28:37 -04:00
|
|
|
fromPDimWith ds (K e) = pure $ K e
|
|
|
|
fromPDimWith ds (V i) =
|
2023-03-31 13:23:30 -04:00
|
|
|
fromBaseName (pure . B) (const $ throw $ DimNotInScope i) ds i
|
2023-03-12 13:28:37 -04:00
|
|
|
|
|
|
|
private
|
2023-03-31 13:23:30 -04:00
|
|
|
avoidDim : Has (Except Error) fs =>
|
|
|
|
Context' BName d -> PName -> Eff fs (Term q d n)
|
2023-03-12 13:28:37 -04:00
|
|
|
avoidDim ds x =
|
2023-03-31 13:23:30 -04:00
|
|
|
fromName (const $ throw $ DimNameInTerm x.base) (pure . FT . fromPName) ds x
|
2023-03-12 13:28:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
mutual
|
|
|
|
export
|
2023-03-31 13:23:30 -04:00
|
|
|
fromPTermWith : Has (Except Error) fs =>
|
2023-03-12 13:28:37 -04:00
|
|
|
Context' BName d -> Context' BName n ->
|
2023-03-31 13:23:30 -04:00
|
|
|
PTerm -> Eff fs (Term Three d n)
|
2023-03-12 13:28:37 -04:00
|
|
|
fromPTermWith ds ns t0 = case t0 of
|
|
|
|
TYPE k =>
|
|
|
|
pure $ TYPE $ k
|
|
|
|
|
|
|
|
Pi pi x s t =>
|
|
|
|
Pi pi <$> fromPTermWith ds ns s
|
2023-03-16 13:18:49 -04:00
|
|
|
<*> fromPTermTScope ds ns [< x] t
|
2023-03-12 13:28:37 -04:00
|
|
|
|
|
|
|
Lam x s =>
|
2023-03-16 13:18:49 -04:00
|
|
|
Lam <$> fromPTermTScope ds ns [< x] s
|
2023-03-12 13:28:37 -04:00
|
|
|
|
|
|
|
s :@ t =>
|
|
|
|
map E $ (:@) <$> fromPTermElim ds ns s <*> fromPTermWith ds ns t
|
|
|
|
|
|
|
|
Sig x s t =>
|
2023-03-16 13:18:49 -04:00
|
|
|
Sig <$> fromPTermWith ds ns s <*> fromPTermTScope ds ns [< x] t
|
2023-03-12 13:28:37 -04:00
|
|
|
|
|
|
|
Pair s t =>
|
|
|
|
Pair <$> fromPTermWith ds ns s <*> fromPTermWith ds ns t
|
|
|
|
|
|
|
|
Case pi pair (r, ret) (CasePair (x, y) body) =>
|
|
|
|
map E $ Base.CasePair pi
|
|
|
|
<$> fromPTermElim ds ns pair
|
2023-03-16 13:18:49 -04:00
|
|
|
<*> fromPTermTScope ds ns [< r] ret
|
|
|
|
<*> fromPTermTScope ds ns [< x, y] body
|
2023-03-12 13:28:37 -04:00
|
|
|
|
|
|
|
Case pi tag (r, ret) (CaseEnum arms) =>
|
|
|
|
map E $ Base.CaseEnum pi
|
|
|
|
<$> fromPTermElim ds ns tag
|
2023-03-16 13:18:49 -04:00
|
|
|
<*> fromPTermTScope ds ns [< r] ret
|
2023-03-12 13:28:37 -04:00
|
|
|
<*> assert_total fromPTermEnumArms ds ns arms
|
|
|
|
|
2023-03-26 08:40:54 -04:00
|
|
|
Nat => pure Nat
|
|
|
|
Zero => pure Zero
|
|
|
|
Succ n => [|Succ $ fromPTermWith ds ns n|]
|
|
|
|
|
|
|
|
Case pi nat (r, ret) (CaseNat zer (s, pi', ih, suc)) =>
|
|
|
|
Prelude.map E $ Base.CaseNat pi pi'
|
|
|
|
<$> fromPTermElim ds ns nat
|
|
|
|
<*> fromPTermTScope ds ns [< r] ret
|
|
|
|
<*> fromPTermWith ds ns zer
|
|
|
|
<*> fromPTermTScope ds ns [< s, ih] suc
|
|
|
|
|
2023-03-12 13:28:37 -04:00
|
|
|
Enum strs =>
|
|
|
|
let set = SortedSet.fromList strs in
|
|
|
|
if length strs == length (SortedSet.toList set) then
|
|
|
|
pure $ Enum set
|
|
|
|
else
|
2023-03-31 13:23:30 -04:00
|
|
|
throw $ DuplicatesInEnum strs
|
2023-03-12 13:28:37 -04:00
|
|
|
|
|
|
|
Tag str => pure $ Tag str
|
|
|
|
|
|
|
|
Eq (i, ty) s t =>
|
2023-03-16 13:18:49 -04:00
|
|
|
Eq <$> fromPTermDScope ds ns [< i] ty
|
2023-03-12 13:28:37 -04:00
|
|
|
<*> fromPTermWith ds ns s
|
|
|
|
<*> fromPTermWith ds ns t
|
|
|
|
|
|
|
|
DLam i s =>
|
2023-03-16 13:18:49 -04:00
|
|
|
DLam <$> fromPTermDScope ds ns [< i] s
|
2023-03-12 13:28:37 -04:00
|
|
|
|
2023-03-31 13:11:35 -04:00
|
|
|
BOX q ty => BOX q <$> fromPTermWith ds ns ty
|
|
|
|
|
|
|
|
Box val => Box <$> fromPTermWith ds ns val
|
|
|
|
|
|
|
|
Case pi box (r, ret) (CaseBox b body) =>
|
|
|
|
Prelude.map E $ CaseBox pi
|
|
|
|
<$> fromPTermElim ds ns box
|
|
|
|
<*> fromPTermTScope ds ns [< r] ret
|
|
|
|
<*> fromPTermTScope ds ns [< b] body
|
|
|
|
|
2023-03-12 13:28:37 -04:00
|
|
|
s :% p =>
|
|
|
|
map E $ (:%) <$> fromPTermElim ds ns s <*> fromPDimWith ds p
|
|
|
|
|
|
|
|
V x =>
|
|
|
|
fromName (pure . E . B) (avoidDim ds) ns x
|
|
|
|
|
|
|
|
s :# a =>
|
|
|
|
map E $ (:#) <$> fromPTermWith ds ns s <*> fromPTermWith ds ns a
|
|
|
|
|
|
|
|
private
|
2023-03-31 13:23:30 -04:00
|
|
|
fromPTermEnumArms : Has (Except Error) fs =>
|
2023-03-12 13:28:37 -04:00
|
|
|
Context' BName d -> Context' BName n ->
|
2023-03-31 13:23:30 -04:00
|
|
|
List (TagVal, PTerm) -> Eff fs (CaseEnumArms Three d n)
|
2023-03-12 13:28:37 -04:00
|
|
|
fromPTermEnumArms ds ns =
|
|
|
|
map SortedMap.fromList . traverse (traverse $ fromPTermWith ds ns)
|
|
|
|
|
|
|
|
private
|
2023-03-31 13:23:30 -04:00
|
|
|
fromPTermElim : Has (Except Error) fs =>
|
2023-03-12 13:28:37 -04:00
|
|
|
Context' BName d -> Context' BName n ->
|
2023-03-31 13:23:30 -04:00
|
|
|
PTerm -> Eff fs (Elim Three d n)
|
2023-03-12 13:28:37 -04:00
|
|
|
fromPTermElim ds ns e =
|
|
|
|
case !(fromPTermWith ds ns e) of
|
|
|
|
E e => pure e
|
2023-03-31 13:23:30 -04:00
|
|
|
_ => throw $ AnnotationNeeded e
|
2023-03-12 13:28:37 -04:00
|
|
|
|
|
|
|
-- [todo] use SN if the var is named but still unused
|
|
|
|
private
|
2023-03-31 13:23:30 -04:00
|
|
|
fromPTermTScope : {s : Nat} -> Has (Except Error) fs =>
|
2023-03-12 13:28:37 -04:00
|
|
|
Context' BName d -> Context' BName n ->
|
2023-03-16 13:18:49 -04:00
|
|
|
SnocVect s BName ->
|
2023-03-31 13:23:30 -04:00
|
|
|
PTerm -> Eff fs (ScopeTermN s Three d n)
|
2023-03-12 13:28:37 -04:00
|
|
|
fromPTermTScope ds ns xs t =
|
|
|
|
if all isNothing xs then
|
|
|
|
SN <$> fromPTermWith ds ns t
|
|
|
|
else
|
2023-03-16 13:18:49 -04:00
|
|
|
SY (fromSnocVect $ map (maybe Unused UN) xs)
|
|
|
|
<$> fromPTermWith ds (ns ++ xs) t
|
2023-03-12 13:28:37 -04:00
|
|
|
|
|
|
|
private
|
2023-03-31 13:23:30 -04:00
|
|
|
fromPTermDScope : {s : Nat} -> Has (Except Error) fs =>
|
2023-03-12 13:28:37 -04:00
|
|
|
Context' BName d -> Context' BName n ->
|
2023-03-16 13:18:49 -04:00
|
|
|
SnocVect s BName ->
|
2023-03-31 13:23:30 -04:00
|
|
|
PTerm -> Eff fs (DScopeTermN s Three d n)
|
2023-03-12 13:28:37 -04:00
|
|
|
fromPTermDScope ds ns xs t =
|
|
|
|
if all isNothing xs then
|
|
|
|
SN <$> fromPTermWith ds ns t
|
|
|
|
else
|
2023-03-16 13:18:49 -04:00
|
|
|
SY (fromSnocVect $ map (maybe Unused UN) xs)
|
|
|
|
<$> fromPTermWith (ds ++ xs) ns t
|
2023-03-12 13:28:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
export %inline
|
2023-03-31 13:23:30 -04:00
|
|
|
fromPTerm : Has (Except Error) fs => PTerm -> Eff fs (Term Three 0 0)
|
2023-03-12 13:28:37 -04:00
|
|
|
fromPTerm = fromPTermWith [<] [<]
|
|
|
|
|
|
|
|
|
|
|
|
export
|
2023-03-31 13:23:30 -04:00
|
|
|
globalPQty : Has (Except Error) fs =>
|
|
|
|
(q : PQty) -> Eff fs (IsGlobal q)
|
2023-03-12 13:28:37 -04:00
|
|
|
globalPQty pi = case isGlobal pi of
|
|
|
|
Yes y => pure y
|
2023-03-31 13:23:30 -04:00
|
|
|
No n => throw $ QtyNotGlobal pi
|
2023-03-12 13:28:37 -04:00
|
|
|
|
|
|
|
|
2023-03-13 14:33:09 -04:00
|
|
|
export
|
2023-03-31 13:23:30 -04:00
|
|
|
fromPNameNS : Has (StateL NS Mods) fs => PName -> Eff fs Name
|
|
|
|
fromPNameNS name = pure $ addMods !(getAt NS) $ fromPName name
|
2023-03-12 13:28:37 -04:00
|
|
|
|
2023-03-13 14:33:09 -04:00
|
|
|
private
|
2023-03-31 13:23:30 -04:00
|
|
|
injTC : (Has (StateL DEFS Defs) fs, Has (Except Error) fs) =>
|
|
|
|
TC Three a -> Eff fs a
|
|
|
|
injTC act = rethrow $ mapFst TypeError $ runTC !(getAt DEFS) act
|
|
|
|
|
|
|
|
export covering
|
|
|
|
fromPDef : (Has (StateL DEFS Defs) fs,
|
|
|
|
Has (StateL NS Mods) fs,
|
|
|
|
Has (Except Error) fs) =>
|
|
|
|
PDefinition -> Eff fs NDef
|
2023-03-13 14:33:09 -04:00
|
|
|
fromPDef (MkPDef qty pname ptype pterm) = do
|
|
|
|
name <- fromPNameNS pname
|
|
|
|
qtyGlobal <- globalPQty qty
|
|
|
|
let sqty = globalToSubj $ Element qty qtyGlobal
|
|
|
|
type <- traverse fromPTerm ptype
|
|
|
|
term <- fromPTerm pterm
|
|
|
|
case type of
|
|
|
|
Just type => do
|
|
|
|
injTC $ checkTypeC empty type Nothing
|
|
|
|
injTC $ ignore $ checkC empty sqty term type
|
2023-03-31 13:23:30 -04:00
|
|
|
let def = mkDef qty type term
|
|
|
|
modifyAt DEFS $ insert name def
|
|
|
|
pure (name, def)
|
2023-03-13 14:33:09 -04:00
|
|
|
Nothing => do
|
2023-03-31 13:23:30 -04:00
|
|
|
let E elim = term | _ => throw $ AnnotationNeeded pterm
|
2023-03-13 14:33:09 -04:00
|
|
|
res <- injTC $ inferC empty sqty elim
|
2023-03-31 13:23:30 -04:00
|
|
|
let def = mkDef qty res.type term
|
|
|
|
modifyAt DEFS $ insert name def
|
|
|
|
pure (name, def)
|
|
|
|
|
|
|
|
export covering
|
|
|
|
fromPDecl : (Has (StateL DEFS Defs) fs,
|
|
|
|
Has (StateL NS Mods) fs,
|
|
|
|
Has (Except Error) fs) =>
|
|
|
|
PDecl -> Eff fs (List NDef)
|
|
|
|
fromPDecl (PDef def) = singleton <$> fromPDef def
|
2023-03-13 14:33:09 -04:00
|
|
|
fromPDecl (PNs ns) =
|
2023-03-31 13:23:30 -04:00
|
|
|
localAt NS (<+> ns.name) $ concat <$> traverse fromPDecl ns.decls
|
2023-03-13 14:33:09 -04:00
|
|
|
|
|
|
|
|
2023-03-31 13:23:30 -04:00
|
|
|
export covering
|
|
|
|
loadFile : (Has IO fs,
|
|
|
|
Has (StateL SEEN SeenFiles) fs,
|
|
|
|
Has (Reader IncludePath) fs,
|
|
|
|
Has (Except Error) fs) =>
|
|
|
|
String -> Eff fs (Maybe String)
|
2023-03-13 14:33:09 -04:00
|
|
|
loadFile file =
|
2023-03-31 13:23:30 -04:00
|
|
|
if contains file !(getAt SEEN) then
|
2023-03-13 14:33:09 -04:00
|
|
|
pure Nothing
|
|
|
|
else do
|
|
|
|
Just file <- firstExists (map (</> file) !ask)
|
2023-03-31 13:23:30 -04:00
|
|
|
| Nothing => throw $ LoadError file FileNotFound
|
2023-03-13 14:33:09 -04:00
|
|
|
case !(readFile file) of
|
2023-03-31 13:23:30 -04:00
|
|
|
Right res => modifyAt SEEN (insert file) $> Just res
|
|
|
|
Left err => throw $ LoadError file err
|
|
|
|
|
|
|
|
parameters {auto _ : (Has IO fs,
|
|
|
|
Has (StateL SEEN SeenFiles) fs,
|
|
|
|
Has (Reader IncludePath) fs,
|
|
|
|
Has (Except Error) fs,
|
|
|
|
Has (StateL DEFS Defs) fs,
|
|
|
|
Has (StateL NS Mods) fs)}
|
|
|
|
mutual
|
|
|
|
export covering
|
|
|
|
loadProcessFile : String -> Eff fs (List NDef)
|
|
|
|
loadProcessFile file =
|
|
|
|
case !(loadFile file) of
|
|
|
|
Just inp => do
|
|
|
|
tl <- either (throw . ParseError) pure $ lexParseInput inp
|
|
|
|
concat <$> traverse fromPTopLevel tl
|
|
|
|
Nothing => pure []
|
|
|
|
|
|
|
|
||| populates the `defs` field of the state
|
|
|
|
export covering
|
|
|
|
fromPTopLevel : PTopLevel -> Eff fs (List NDef)
|
|
|
|
fromPTopLevel (PD decl) = fromPDecl decl
|
|
|
|
fromPTopLevel (PLoad file) = loadProcessFile file
|
2023-03-13 14:33:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
export
|
2023-03-31 13:23:30 -04:00
|
|
|
fromParserIO : (MonadRec io, HasIO io) =>
|
|
|
|
IncludePath -> IORef SeenFiles -> IORef Defs ->
|
|
|
|
FromParser a -> io (Either Error a)
|
|
|
|
fromParserIO inc seen defs act =
|
|
|
|
runIO $
|
|
|
|
runExcept $
|
|
|
|
evalStateAt NS [<] $
|
|
|
|
runStateIORefAt SEEN seen $
|
|
|
|
runStateIORefAt DEFS defs $
|
|
|
|
runReader inc act
|