erasure improvements

This commit is contained in:
2025-02-02 16:33:37 -08:00
parent c83632881d
commit 9586ca96bb
7 changed files with 31 additions and 26 deletions

View File

@@ -7,6 +7,7 @@ import Lib.Types
import Lib.Prettier
import Lib.CompileExp
import Lib.TopContext
import Lib.Erasure
import Data.String
import Data.Int
import Data.SortedMap
@@ -282,7 +283,8 @@ maybeWrap stmt = Apply (JLam Nil stmt) Nil
defToDoc : QName Def M Doc
defToDoc name (Fn tm) = do
debug $ \ _ => "compileFun \{render 90 $ pprint Nil tm}"
ct <- compileFun tm
tm' <- erase Nil tm Nil
ct <- compileFun tm'
let exp = maybeWrap $ termToJS emptyJSEnv ct JReturn
pure $ text "const" <+> jsIdent (show name) <+> text "=" <+/> expToDoc exp ++ text ";"
defToDoc name Axiom = pure $ text ""

View File

@@ -73,11 +73,12 @@ compileTerm : Tm -> M CExp
apply : CExp -> List CExp -> SnocList CExp -> Nat -> Tm -> M CExp
-- out of args, make one up (fix that last arg)
apply t Nil acc (S k) ty = pure $ CApp t (acc <>> Nil) (1 + cast k)
-- FIXME - this should be handled by Erasure.newt (wdiff of esbuild output says this is still used)
-- FIXME - this should be handled by Erasure.newt
-- We somehow hit the error below, with a Pi?
apply t (x :: xs) acc (S k) (Pi y str icit Zero a b) = apply t xs (acc :< CErased) k b
apply t (x :: xs) acc (S k) (Pi y str icit Many a b) = apply t xs (acc :< x) k b
-- see if there is anything we have to handle here
apply t (x :: xs) acc (S k) ty = error (getFC ty) "Expected pi \{showTm ty}. Overapplied function that escaped type checking?"
apply t (x :: xs) acc (S k) ty = error (getFC ty) "Expected pi, got \{showTm ty}. Overapplied function that escaped type checking?"
-- once we hit zero, we fold the rest
apply t ts acc Z ty = go (CApp t (acc <>> Nil) 0) ts
where
@@ -95,7 +96,7 @@ compileTerm t@(Ref fc nm) = do
| Nothing => error fc "Undefined name \{show nm}"
arity <- arityForName fc nm
case arity of
-- we don't need tu curry functions that take one argument
-- we don't need to curry functions that take one argument
(S Z) => pure $ CRef (show nm)
_ => apply (CRef (show nm)) Nil Lin arity type
@@ -103,8 +104,9 @@ compileTerm (Meta _ k) = pure $ CRef "meta$\{show k}" -- FIXME
compileTerm (Lam _ nm _ _ t) = CLam nm <$> compileTerm t
compileTerm tm@(App _ _ _) = case funArgs tm of
(Meta _ k, args) => do
info (getFC tm) "Compiling an unsolved meta \{show tm}"
pure $ CApp (CRef "Meta\{show k}") Nil 0
error (getFC tm) "Compiling an unsolved meta \{show tm}"
-- info (getFC tm) "Compiling an unsolved meta \{show tm}"
-- pure $ CApp (CRef "Meta\{show k}") Nil 0
(t@(Ref fc nm), args) => do
args' <- traverse compileTerm args
arity <- arityForName fc nm

View File

@@ -36,7 +36,9 @@ eraseSpine env t ((fc, arg) :: args) (Just (Pi fc1 str icit Many a b)) = do
-- TODO this seems wrong, we need to subst u into b to get the type
eraseSpine env (App fc t u) args (Just b)
-- eraseSpine env t ((fc, arg) :: args) (Just ty) = do
-- error fc "ceci n'est pas une ∏ \{showTm ty}" -- e.g. Bnd 1
-- -- Prelude Either and IO instance of <*> have Bnd here, possibly pattern matching
-- -- unifying in the wrong direction? we should have something like a -> b
-- error fc "ceci n'est pas une ∏ \{showTm ty} arg \{show arg}" -- e.g. Bnd 1
eraseSpine env t ((fc, arg) :: args) _ = do
u <- erase env arg Nil
eraseSpine env (App fc t u) args Nothing

View File

@@ -162,12 +162,9 @@ processDecl ns (Def fc nm clauses) = do
-- tm' <- nf Nil tm
tm' <- zonk top 0 Nil tm
debug $ \ _ => "NF\n\{render 80 $ pprint Nil tm'}"
-- TODO we want to keep both versions, but this is checking in addition to erasing
-- currently CompileExp is also doing erasure.
-- TODO we need erasure info on the lambdas or to fake up an appropriate environment
-- and erase inside. Currently the checking is imprecise
tm'' <- erase Nil tm' Nil
debug $ \ _ => "ERASED\n\{render 80 $ pprint Nil tm'}"
-- This is done in Compile.newt now, we can't store the result because we need the real thing at compile time
-- tm'' <- erase Nil tm' Nil
-- debug $ \ _ => "ERASED\n\{render 80 $ pprint Nil tm''}"
debug $ \ _ => "Add def \{nm} \{render 90 $ pprint Nil tm'} : \{render 90 $ pprint Nil ty}"
updateDef (QN ns nm) fc ty (Fn tm')

View File

@@ -84,7 +84,7 @@ data Tm : U where
-- need type?
Let : FC -> Name -> Tm -> Tm -> Tm
-- for desugaring where
LetRec : FC -> Name -> Tm -> Tm -> Tm -> Tm
LetRec : FC -> Name -> (ty : Tm) -> (t : Tm) -> (u : Tm) -> Tm
Lit : FC -> Literal -> Tm
Erased : FC -> Tm