@@ -2,7 +2,7 @@
22 PatternGuards #-}
33
44module Core.Evaluate(normalise, normaliseC, simplify, specialise, hnf,
5− Fun(..), Def(..),
5+ Def(..),
66 Context, initContext, ctxtAlist, uconstraints, next_tvar,
77 addToCtxt, addTyDecl, addDatatype, addCasedef, addOperator,
88 lookupTy, lookupP, lookupDef, lookupVal, lookupTyEnv,
@@ -10,6 +10,8 @@ module Core.Evaluate(normalise, normaliseC, simplify, specialise, hnf,
1010
1111import Debug.Trace
1212import Control.Monad.State
13+import qualified Data.Binary as B
14+import Data.Binary hiding (get, put)
1315
1416import Core.TT
1517import Core.CaseTree
@@ -106,8 +108,9 @@ eval ctxt statics genv tm opts = ev True [] tm where
106108 ev top env (P _ n ty)
107109 | Just (Let t v) <- lookup n genv = ev top env v
108110 ev top env (P Ref n ty) = case lookupDef Nothing n ctxt of
109− [Function (Fun _ _ _ v)] -> return v
110− [TyDecl nt ty hty] -> return $ VP nt n hty
111+ [Function _ tm] -> ev True env tm
112+ [TyDecl nt ty] -> do vty <- ev True env ty
113+ return $ VP nt n vty
111114 [CaseOp inl _ _ [] tree] ->
112115 if simpl && (not inl)
113116 then liftM (VP Ref n) (ev top env ty)
@@ -293,8 +296,8 @@ eval_hnf ctxt statics genv tm = ev [] tm where
293296 ev env (P _ n ty)
294297 | Just (Let t v) <- lookup n genv = ev env v
295298 ev env (P Ref n ty) = case lookupDef Nothing n ctxt of
296− [Function (Fun _ _ t _)] -> ev env t
297− [TyDecl nt ty hty] -> return $ HP nt n ty
299+ [Function _ t] -> ev env t
300+ [TyDecl nt ty] -> return $ HP nt n ty
298301 [CaseOp inl _ _ [] tree] ->
299302 do c <- evCase env [] [] tree
300303 case c of
@@ -416,23 +419,20 @@ spec ctxt statics genv tm = undefined
416419
417420-- CONTEXTS -----------------------------------------------------------------
418421
419−data Fun = Fun Type Value Term Value
420− deriving Show
421−
422422{- A definition is either a simple function (just an expression with a type),
423423 a constant, which could be a data or type constructor, an axiom or as an
424424 yet undefined function, or an Operator.
425425 An Operator is a function which explains how to reduce.
426426 A CaseOp is a function defined by a simple case tree -}
427427
428−data Def = Function Fun
429− | TyDecl NameType Type Value
428+data Def = Function Type Term
429+ | TyDecl NameType Type
430430 | Operator Type Int ([Value] -> Maybe Value)
431431 | CaseOp Bool Type [(Term, Term)] [Name] SC -- Bool for inlineable
432432
433433instance Show Def where
434− show (Function f) = "Function: " ++ show f
435− show (TyDecl nt ty val) = "TyDecl: " ++ show nt ++ " " ++ show ty
434+ show (Function ty tm) = "Function: " ++ show (ty, tm)
435+ show (TyDecl nt ty) = "TyDecl: " ++ show nt ++ " " ++ show ty
436436 show (Operator ty _ _) = "Operator: " ++ show ty
437437 show (CaseOp _ ty ps ns sc) = "Case: " ++ show ps ++ "\n" ++
438438 show ns ++ " " ++ show sc
@@ -453,14 +453,13 @@ veval ctxt env t = evalState (eval ctxt emptyContext env t []) ()
453453addToCtxt :: Name -> Term -> Type -> Context -> Context
454454addToCtxt n tm ty uctxt
455455 = let ctxt = definitions uctxt
456− ctxt' = addDef n (Function (Fun ty (veval uctxt [] ty)
457− tm (veval uctxt [] tm))) ctxt in
456+ ctxt' = addDef n (Function ty tm) ctxt in
458457 uctxt { definitions = ctxt' }
459458
460459addTyDecl :: Name -> Type -> Context -> Context
461460addTyDecl n ty uctxt
462461 = let ctxt = definitions uctxt
463− ctxt' = addDef n (TyDecl Ref ty (veval uctxt [] ty)) ctxt in
462+ ctxt' = addDef n (TyDecl Ref ty) ctxt in
464463 uctxt { definitions = ctxt' }
465464
466465addDatatype :: Datatype Name -> Context -> Context
@@ -468,14 +467,14 @@ addDatatype (Data n tag ty cons) uctxt
468467 = let ctxt = definitions uctxt
469468 ty' = normalise uctxt [] ty
470469 ctxt' = addCons 0 cons (addDef n
471− (TyDecl (TCon tag (arity ty')) ty (veval uctxt [] ty)) ctxt) in
470+ (TyDecl (TCon tag (arity ty')) ty) ctxt) in
472471 uctxt { definitions = ctxt' }
473472 where
474473 addCons tag [] ctxt = ctxt
475474 addCons tag ((n, ty) : cons) ctxt
476475 = let ty' = normalise uctxt [] ty in
477476 addCons (tag+1) cons (addDef n
478− (TyDecl (DCon tag (arity ty')) ty (veval uctxt [] ty)) ctxt)
477+ (TyDecl (DCon tag (arity ty')) ty) ctxt)
479478
480479addCasedef :: Name -> Bool -> Bool -> [(Term, Term)] -> Type -> Context -> Context
481480addCasedef n alwaysInline tcase ps ty uctxt
@@ -498,8 +497,8 @@ lookupTy :: Maybe [String] -> Name -> Context -> [Type]
498497lookupTy root n ctxt
499498 = do def <- lookupCtxt root n (definitions ctxt)
500499 case def of
501− (Function (Fun ty _ _ _)) -> return ty
502− (TyDecl _ ty _) -> return ty
500+ (Function ty _) -> return ty
501+ (TyDecl _ ty) -> return ty
503502 (Operator ty _ _) -> return ty
504503 (CaseOp _ ty _ _ _) -> return ty
505504
@@ -507,8 +506,8 @@ lookupP :: Maybe [String] -> Name -> Context -> [Term]
507506lookupP root n ctxt
508507 = do def <- lookupCtxt root n (definitions ctxt)
509508 case def of
510− (Function (Fun ty _ tm _)) -> return (P Ref n ty)
511− (TyDecl nt ty hty) -> return (P nt n ty)
509+ (Function ty tm) -> return (P Ref n ty)
510+ (TyDecl nt ty) -> return (P nt n ty)
512511 (CaseOp _ ty _ _ _) -> return (P Ref n ty)
513512 (Operator ty _ _) -> return (P Ref n ty)
514513
@@ -519,8 +518,8 @@ lookupVal :: Maybe [String] -> Name -> Context -> [Value]
519518lookupVal root n ctxt
520519 = do def <- lookupCtxt root n (definitions ctxt)
521520 case def of
522− (Function (Fun _ _ _ htm)) -> return htm
523− (TyDecl nt ty hty) -> return (VP nt n hty)
521+ (Function _ htm) -> return (veval ctxt [] htm)
522+ (TyDecl nt ty) -> return (VP nt n (veval ctxt [] ty))
524523
525524lookupTyEnv :: Name -> Env -> Maybe (Int, Type)
526525lookupTyEnv n env = li n 0 env where
@@ -528,4 +527,3 @@ lookupTyEnv n env = li n 0 env where
528527 li n i ((x, b): xs)
529528 | n == x = Just (i, binderTy b)
530529 | otherwise = li n (i+1) xs
531−