investigating issue
This commit is contained in:
55
newt/Zoo1.newt
Normal file
55
newt/Zoo1.newt
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
module Zoo1
|
||||||
|
|
||||||
|
-- I'm starting to translate ezoo 01-eval-closures-debruijn as a test cases.
|
||||||
|
|
||||||
|
ptype Int
|
||||||
|
ptype String
|
||||||
|
|
||||||
|
------- Prelude stuff
|
||||||
|
|
||||||
|
data Nat : U where
|
||||||
|
Z : Nat
|
||||||
|
S : Nat -> Nat
|
||||||
|
|
||||||
|
data Unit : U where
|
||||||
|
MkUnit : Unit
|
||||||
|
|
||||||
|
data List : U -> U where
|
||||||
|
Nil : {a : U} -> List a
|
||||||
|
Cons : {a : U} -> a -> List a -> List a
|
||||||
|
|
||||||
|
data Maybe : U -> U where
|
||||||
|
Just : {a : U} -> a -> Maybe a
|
||||||
|
Nothing : {a : U} -> Maybe a
|
||||||
|
|
||||||
|
Val : U
|
||||||
|
|
||||||
|
data Tm : U where
|
||||||
|
Var : Nat -> Tm
|
||||||
|
Lam : Tm -> Tm -- lam (x.t)
|
||||||
|
App : Tm -> Tm -> Tm
|
||||||
|
Let : Tm -> Tm -> Tm -- let t (x.u)
|
||||||
|
|
||||||
|
data Env : U where
|
||||||
|
ENil : Env
|
||||||
|
Define : Env -> Val -> Env
|
||||||
|
|
||||||
|
data Closure : U where
|
||||||
|
MkClosure : Env -> Tm -> Closure
|
||||||
|
|
||||||
|
data Val : U where
|
||||||
|
VVar : Nat -> Val
|
||||||
|
VApp : Val -> Val -> Val
|
||||||
|
VLam : Closure -> Val
|
||||||
|
|
||||||
|
length : Env -> Nat
|
||||||
|
length ENil = Z
|
||||||
|
length (Define env _) = S (length env)
|
||||||
|
|
||||||
|
lookup : Env -> Nat -> Maybe Val
|
||||||
|
lookup (Define env v) Z = Just v
|
||||||
|
lookup (Define env _) (S k) = Just (lookup env k)
|
||||||
|
-- bug in unify? are the meta args backwards? It seems to quote back right..
|
||||||
|
-- we're getting `Maybe Val` as meta3, and comparing:
|
||||||
|
-- Maybe ?m3 =?= Maybe Val
|
||||||
|
lookup (ENil) x = Nothing
|
||||||
@@ -63,6 +63,6 @@ nand x y = not (case x of
|
|||||||
-- for stuff like this, we should add Agda () and check for no constructors
|
-- for stuff like this, we should add Agda () and check for no constructors
|
||||||
data Void : U where
|
data Void : U where
|
||||||
|
|
||||||
|
SnocList : U -> U
|
||||||
|
SnocList a = List a
|
||||||
|
|
||||||
|
|||||||
@@ -128,14 +128,6 @@ getConstructors ctx tm = error (getValFC tm) "Not a type constructor \{show tm}"
|
|||||||
-- Extend environment with fresh variables from a pi-type
|
-- Extend environment with fresh variables from a pi-type
|
||||||
-- return context, remaining type, and list of names
|
-- return context, remaining type, and list of names
|
||||||
extendPi : Context -> Val -> SnocList Bind -> M (Context, Val, List Bind)
|
extendPi : Context -> Val -> SnocList Bind -> M (Context, Val, List Bind)
|
||||||
-- NEXT This doesn't work, unsound.
|
|
||||||
-- We need all of these vars with icity _and_ to insert implicits in the pattern
|
|
||||||
-- extendPi ctx (VPi x str Implicit a b) nms = do
|
|
||||||
-- let nm = fresh "pat"
|
|
||||||
-- let ctx' = extend ctx nm a
|
|
||||||
-- let v = VVar emptyFC (length ctx.env) [<]
|
|
||||||
-- tyb <- b $$ v
|
|
||||||
-- extendPi ctx' tyb nms
|
|
||||||
extendPi ctx (VPi x str icit a b) nms = do
|
extendPi ctx (VPi x str icit a b) nms = do
|
||||||
let nm = fresh "pat"
|
let nm = fresh "pat"
|
||||||
let ctx' = extend ctx nm a
|
let ctx' = extend ctx nm a
|
||||||
@@ -157,9 +149,11 @@ buildCase ctx prob scnm (dcName, _, ty) = do
|
|||||||
vty <- eval [] CBN ty
|
vty <- eval [] CBN ty
|
||||||
(ctx', ty', vars) <- extendPi ctx (vty) [<]
|
(ctx', ty', vars) <- extendPi ctx (vty) [<]
|
||||||
|
|
||||||
debug "clauses were \{show prob.clauses} (dcon \{show dcName}) (vars \{show vars})"
|
debug "(dcon \{show dcName}) (vars \{show vars}) clauses were"
|
||||||
|
for_ prob.clauses $ (\x => debug " \{show x}")
|
||||||
let clauses = mapMaybe (rewriteClause vars) prob.clauses
|
let clauses = mapMaybe (rewriteClause vars) prob.clauses
|
||||||
debug " and now \{show clauses}"
|
debug "and now:"
|
||||||
|
for_ clauses $ (\x => debug " \{show x}")
|
||||||
-- So ideally we'd know which position we're splitting and the surrounding context
|
-- So ideally we'd know which position we're splitting and the surrounding context
|
||||||
-- That might be a lot to carry forward (maybe a continuation?) but we could carry
|
-- That might be a lot to carry forward (maybe a continuation?) but we could carry
|
||||||
-- backwards as a List Missing that we augment as we go up.
|
-- backwards as a List Missing that we augment as we go up.
|
||||||
@@ -205,7 +199,7 @@ buildCase ctx prob scnm (dcName, _, ty) = do
|
|||||||
PatVar _ s => Just $ c :: (xs ++ acc)
|
PatVar _ s => Just $ c :: (xs ++ acc)
|
||||||
PatWild _ => Just $ c :: (xs ++ acc)
|
PatWild _ => Just $ c :: (xs ++ acc)
|
||||||
PatCon _ str ys => if str == dcName
|
PatCon _ str ys => if str == dcName
|
||||||
then Just $ (makeConst vars ys) ++ acc
|
then Just $ (makeConst vars ys) ++ xs ++ acc
|
||||||
else Nothing
|
else Nothing
|
||||||
else rewriteCons vars xs (c :: acc)
|
else rewriteCons vars xs (c :: acc)
|
||||||
|
|
||||||
|
|||||||
@@ -112,6 +112,9 @@ parameters (ctx: Context)
|
|||||||
debug " =?= \{show u}"
|
debug " =?= \{show u}"
|
||||||
t' <- forceMeta t
|
t' <- forceMeta t
|
||||||
u' <- forceMeta u
|
u' <- forceMeta u
|
||||||
|
debug "forced \{show t'}"
|
||||||
|
debug " =?= \{show u'}"
|
||||||
|
debug "env \{show ctx.env}"
|
||||||
case (t',u') of
|
case (t',u') of
|
||||||
(VLam _ _ t, VLam _ _ t') => unify (l + 1) !(t $$ VVar emptyFC l [<]) !(t' $$ VVar emptyFC l [<])
|
(VLam _ _ t, VLam _ _ t') => unify (l + 1) !(t $$ VVar emptyFC l [<]) !(t' $$ VVar emptyFC l [<])
|
||||||
(t, VLam fc' _ t') => unify (l + 1) !(t `vapp` VVar emptyFC l [<]) !(t' $$ VVar emptyFC l [<])
|
(t, VLam fc' _ t') => unify (l + 1) !(t `vapp` VVar emptyFC l [<]) !(t' $$ VVar emptyFC l [<])
|
||||||
@@ -121,9 +124,11 @@ parameters (ctx: Context)
|
|||||||
if k == k' then unifySpine l (k == k') sp sp'
|
if k == k' then unifySpine l (k == k') sp sp'
|
||||||
else error emptyFC "vvar mismatch \{show k} \{show k'}"
|
else error emptyFC "vvar mismatch \{show k} \{show k'}"
|
||||||
(VRef fc k def sp, VRef fc' k' def' sp' ) =>
|
(VRef fc k def sp, VRef fc' k' def' sp' ) =>
|
||||||
if k == k' then unifySpine l (k == k') sp sp'
|
if k == k' then do
|
||||||
|
debug "unify \{show l} spine at \{k} \{show sp} \{show sp'}"
|
||||||
|
unifySpine l (k == k') sp sp'
|
||||||
-- REVIEW - consider forcing?
|
-- REVIEW - consider forcing?
|
||||||
else error emptyFC "vref mismatch \{show k} \{show k'}"
|
else error emptyFC "vref mismatch \{show k} \{show k'} -- \{show sp} \{show sp'}"
|
||||||
(VMeta fc k sp, VMeta fc' k' sp' ) =>
|
(VMeta fc k sp, VMeta fc' k' sp' ) =>
|
||||||
if k == k' then unifySpine l (k == k') sp sp'
|
if k == k' then unifySpine l (k == k') sp sp'
|
||||||
else solve l k sp (VMeta fc' k' sp')
|
else solve l k sp (VMeta fc' k' sp')
|
||||||
@@ -136,13 +141,14 @@ parameters (ctx: Context)
|
|||||||
debug "expand \{show t} =?= %ref \{k'}"
|
debug "expand \{show t} =?= %ref \{k'}"
|
||||||
case lookup k' !(get) of
|
case lookup k' !(get) of
|
||||||
Just (MkEntry name ty (Fn tm)) => unify l t !(vappSpine !(eval [] CBN tm) sp')
|
Just (MkEntry name ty (Fn tm)) => unify l t !(vappSpine !(eval [] CBN tm) sp')
|
||||||
_ => error emptyFC "unify failed \{show t'} =?= \{show u'} [no Fn]\n env is \{show ctx.env} \{show $ map fst ctx.types}"
|
_ => error ctx.fc "unify failed \{show t'} =?= \{show u'} [no Fn]\n env is \{show ctx.env} \{show $ map fst ctx.types}"
|
||||||
-- REVIEW I'd like to quote this back, but we have l that aren't in the environment.
|
-- REVIEW I'd like to quote this back, but we have l that aren't in the environment.
|
||||||
_ => error emptyFC "unify failed \{show t'} =?= \{show u'} \n env is \{show ctx.env} \{show $ map fst ctx.types}"
|
_ => error ctx.fc "unify failed \{show t'} =?= \{show u'} \n env is \{show ctx.env} \{show $ map fst ctx.types}"
|
||||||
|
|
||||||
unifyCatch : FC -> Context -> Val -> Val -> M ()
|
unifyCatch : FC -> Context -> Val -> Val -> M ()
|
||||||
unifyCatch fc ctx ty' ty = catchError (unify ctx ctx.lvl ty' ty) $ \(E x str) => do
|
unifyCatch fc ctx ty' ty = catchError (unify ctx ctx.lvl ty' ty) $ \(E x str) => do
|
||||||
let names = toList $ map fst ctx.types
|
let names = toList $ map fst ctx.types
|
||||||
|
debug "fail \{show ty'} \{show ty}"
|
||||||
a <- quote ctx.lvl ty'
|
a <- quote ctx.lvl ty'
|
||||||
b <- quote ctx.lvl ty
|
b <- quote ctx.lvl ty
|
||||||
let msg = "\n failed to unify \{pprint names a}\n with \{pprint names b}\n " <+> str
|
let msg = "\n failed to unify \{pprint names a}\n with \{pprint names b}\n " <+> str
|
||||||
|
|||||||
@@ -140,9 +140,9 @@ letExpr = do
|
|||||||
pure (name,fc,t)
|
pure (name,fc,t)
|
||||||
|
|
||||||
pLetArg : Parser (Icit, String, Maybe Raw)
|
pLetArg : Parser (Icit, String, Maybe Raw)
|
||||||
pLetArg = (Implicit,,) <$> braces ident <*> optional (sym ":" >> typeExpr)
|
pLetArg = (Implicit,,) <$> braces (ident <|> uident) <*> optional (sym ":" >> typeExpr)
|
||||||
<|> (Explicit,,) <$> parens ident <*> optional (sym ":" >> typeExpr)
|
<|> (Explicit,,) <$> parens (ident <|> uident) <*> optional (sym ":" >> typeExpr)
|
||||||
<|> (Explicit,,Nothing) <$> ident
|
<|> (Explicit,,Nothing) <$> (ident <|> uident)
|
||||||
<|> (Explicit,"_",Nothing) <$ keyword "_"
|
<|> (Explicit,"_",Nothing) <$ keyword "_"
|
||||||
|
|
||||||
-- lam: λ {A} {b : A} (c : Blah) d e f. something
|
-- lam: λ {A} {b : A} (c : Blah) d e f. something
|
||||||
@@ -203,7 +203,7 @@ term = caseExpr
|
|||||||
ebind : Parser (List (String, Icit, Raw))
|
ebind : Parser (List (String, Icit, Raw))
|
||||||
ebind = do
|
ebind = do
|
||||||
sym "("
|
sym "("
|
||||||
names <- some ident
|
names <- some (ident <|> uident)
|
||||||
sym ":"
|
sym ":"
|
||||||
ty <- typeExpr
|
ty <- typeExpr
|
||||||
sym ")"
|
sym ")"
|
||||||
@@ -213,7 +213,7 @@ ibind : Parser (List (String, Icit, Raw))
|
|||||||
ibind = do
|
ibind = do
|
||||||
sym "{"
|
sym "{"
|
||||||
mustWork $ do
|
mustWork $ do
|
||||||
names <- some ident
|
names <- some (ident <|> uident)
|
||||||
ty <- optional (sym ":" >> typeExpr)
|
ty <- optional (sym ":" >> typeExpr)
|
||||||
pos <- getPos
|
pos <- getPos
|
||||||
sym "}"
|
sym "}"
|
||||||
@@ -262,7 +262,7 @@ export
|
|||||||
parseDef : Parser Decl
|
parseDef : Parser Decl
|
||||||
parseDef = do
|
parseDef = do
|
||||||
fc <- getPos
|
fc <- getPos
|
||||||
nm <- ident
|
nm <- ident <|> uident
|
||||||
pats <- many pPattern
|
pats <- many pPattern
|
||||||
keyword "="
|
keyword "="
|
||||||
body <- mustWork typeExpr
|
body <- mustWork typeExpr
|
||||||
@@ -289,6 +289,8 @@ parseData : Parser Decl
|
|||||||
parseData = do
|
parseData = do
|
||||||
fc <- getPos
|
fc <- getPos
|
||||||
keyword "data"
|
keyword "data"
|
||||||
|
-- FIXME - switch from mustWork / commit to checking if we've consumed tokens
|
||||||
|
mustWork $ do
|
||||||
name <- uident
|
name <- uident
|
||||||
keyword ":"
|
keyword ":"
|
||||||
ty <- typeExpr
|
ty <- typeExpr
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ processDecl (TypeSig fc nm tm) = do
|
|||||||
| _ => error fc "\{show nm} is already defined"
|
| _ => error fc "\{show nm} is already defined"
|
||||||
putStrLn "-----"
|
putStrLn "-----"
|
||||||
putStrLn "TypeSig \{nm} \{show tm}"
|
putStrLn "TypeSig \{nm} \{show tm}"
|
||||||
ty <- check (mkCtx top.metas) tm (VU fc)
|
ty <- check (mkCtx top.metas fc) tm (VU fc)
|
||||||
ty' <- nf [] ty
|
ty' <- nf [] ty
|
||||||
putStrLn "got \{pprint [] ty'}"
|
putStrLn "got \{pprint [] ty'}"
|
||||||
modify $ setDef nm ty' Axiom
|
modify $ setDef nm ty' Axiom
|
||||||
@@ -47,7 +47,7 @@ processDecl (PType fc nm) = do
|
|||||||
modify $ setDef nm (U fc) PrimTCon
|
modify $ setDef nm (U fc) PrimTCon
|
||||||
processDecl (PFunc fc nm ty src) = do
|
processDecl (PFunc fc nm ty src) = do
|
||||||
top <- get
|
top <- get
|
||||||
ty <- check (mkCtx top.metas) ty (VU fc)
|
ty <- check (mkCtx top.metas fc) ty (VU fc)
|
||||||
ty' <- nf [] ty
|
ty' <- nf [] ty
|
||||||
putStrLn "pfunc \{nm} : \{pprint [] ty'} := \{show src}"
|
putStrLn "pfunc \{nm} : \{pprint [] ty'} := \{show src}"
|
||||||
modify $ setDef nm ty' (PrimFn src)
|
modify $ setDef nm ty' (PrimFn src)
|
||||||
@@ -69,8 +69,7 @@ processDecl (Def fc nm clauses) = do
|
|||||||
vty <- eval empty CBN ty
|
vty <- eval empty CBN ty
|
||||||
putStrLn "vty is \{show vty}"
|
putStrLn "vty is \{show vty}"
|
||||||
|
|
||||||
tm <- buildTree ({ fc := fc} $ mkCtx ctx.metas) (MkProb clauses vty)
|
tm <- buildTree (mkCtx ctx.metas fc) (MkProb clauses vty)
|
||||||
-- tm <- check (mkCtx ctx.metas) body vty
|
|
||||||
putStrLn "Ok \{pprint [] tm}"
|
putStrLn "Ok \{pprint [] tm}"
|
||||||
|
|
||||||
mc <- readIORef ctx.metas
|
mc <- readIORef ctx.metas
|
||||||
@@ -86,10 +85,10 @@ processDecl (Def fc nm clauses) = do
|
|||||||
processDecl (DCheck fc tm ty) = do
|
processDecl (DCheck fc tm ty) = do
|
||||||
top <- get
|
top <- get
|
||||||
putStrLn "check \{show tm} at \{show ty}"
|
putStrLn "check \{show tm} at \{show ty}"
|
||||||
ty' <- check (mkCtx top.metas) tm (VU fc)
|
ty' <- check (mkCtx top.metas fc) tm (VU fc)
|
||||||
putStrLn "got type \{pprint [] ty'}"
|
putStrLn "got type \{pprint [] ty'}"
|
||||||
vty <- eval [] CBN ty'
|
vty <- eval [] CBN ty'
|
||||||
res <- check (mkCtx top.metas) ty vty
|
res <- check (mkCtx top.metas fc) ty vty
|
||||||
putStrLn "got \{pprint [] res}"
|
putStrLn "got \{pprint [] res}"
|
||||||
norm <- nf [] res
|
norm <- nf [] res
|
||||||
putStrLn "norm \{pprint [] norm}"
|
putStrLn "norm \{pprint [] norm}"
|
||||||
@@ -104,7 +103,7 @@ processDecl (DImport fc str) = throwError $ E fc "import not implemented"
|
|||||||
processDecl (Data fc nm ty cons) = do
|
processDecl (Data fc nm ty cons) = do
|
||||||
-- It seems like the FC for the errors are not here?
|
-- It seems like the FC for the errors are not here?
|
||||||
ctx <- get
|
ctx <- get
|
||||||
tyty <- check (mkCtx ctx.metas) ty (VU fc)
|
tyty <- check (mkCtx ctx.metas fc) ty (VU fc)
|
||||||
-- FIXME we need this in scope, but need to update
|
-- FIXME we need this in scope, but need to update
|
||||||
modify $ setDef nm tyty Axiom
|
modify $ setDef nm tyty Axiom
|
||||||
ctx <- get
|
ctx <- get
|
||||||
@@ -112,7 +111,7 @@ processDecl (Data fc nm ty cons) = do
|
|||||||
-- expecting tm to be a Pi type
|
-- expecting tm to be a Pi type
|
||||||
(TypeSig fc nm' tm) => do
|
(TypeSig fc nm' tm) => do
|
||||||
ctx <- get
|
ctx <- get
|
||||||
dty <- check (mkCtx ctx.metas) tm (VU fc)
|
dty <- check (mkCtx ctx.metas fc) tm (VU fc)
|
||||||
debug "dty \{nm'} is \{pprint [] dty}"
|
debug "dty \{nm'} is \{pprint [] dty}"
|
||||||
|
|
||||||
-- We only check that codomain uses the right type constructor
|
-- We only check that codomain uses the right type constructor
|
||||||
|
|||||||
@@ -48,7 +48,6 @@ record Clause where
|
|||||||
-- it has names from Pats, which will need types in the env
|
-- it has names from Pats, which will need types in the env
|
||||||
expr : Raw
|
expr : Raw
|
||||||
|
|
||||||
|
|
||||||
-- could be a pair, but I suspect stuff will be added?
|
-- could be a pair, but I suspect stuff will be added?
|
||||||
public export
|
public export
|
||||||
data RCaseAlt = MkAlt Raw Raw
|
data RCaseAlt = MkAlt Raw Raw
|
||||||
|
|||||||
@@ -378,8 +378,8 @@ M = (StateT TopContext (EitherT Impl.Error IO))
|
|||||||
-- we need more of topcontext later - Maybe switch it up so we're not passing
|
-- we need more of topcontext later - Maybe switch it up so we're not passing
|
||||||
-- around top
|
-- around top
|
||||||
export
|
export
|
||||||
mkCtx : IORef MetaContext -> Context
|
mkCtx : IORef MetaContext -> FC -> Context
|
||||||
mkCtx metas = MkCtx 0 [] [] [] metas emptyFC
|
mkCtx metas fc = MkCtx 0 [] [] [] metas fc
|
||||||
|
|
||||||
||| Force argument and print if verbose is true
|
||| Force argument and print if verbose is true
|
||||||
export
|
export
|
||||||
|
|||||||
Reference in New Issue
Block a user