@@ -2,7 +2,7 @@
2 PatternGuards #-}
2 PatternGuards #-}
4module Core.Evaluate(normalise, normaliseC, simplify, specialise, hnf,
4module Core.Evaluate(normalise, normaliseC, simplify, specialise, hnf,
5− Fun(..), Def(..),
5+ Def(..),
6 Context, initContext, ctxtAlist, uconstraints, next_tvar,
6 Context, initContext, ctxtAlist, uconstraints, next_tvar,
7 addToCtxt, addTyDecl, addDatatype, addCasedef, addOperator,
7 addToCtxt, addTyDecl, addDatatype, addCasedef, addOperator,
8 lookupTy, lookupP, lookupDef, lookupVal, lookupTyEnv,
8 lookupTy, lookupP, lookupDef, lookupVal, lookupTyEnv,
@@ -10,6 +10,8 @@ module Core.Evaluate(normalise, normaliseC, simplify, specialise, hnf,
11import Debug.Trace
11import Debug.Trace
12import Control.Monad.State
12import Control.Monad.State
13+import qualified Data.Binary as B
14+import Data.Binary hiding (get, put)
14import Core.TT
16import Core.TT
15import Core.CaseTree
17import Core.CaseTree
@@ -106,8 +108,9 @@ eval ctxt statics genv tm opts = ev True [] tm where
106 ev top env (P _ n ty)
108 ev top env (P _ n ty)
107 | Just (Let t v) <- lookup n genv = ev top env v
109 | Just (Let t v) <- lookup n genv = ev top env v
108 ev top env (P Ref n ty) = case lookupDef Nothing n ctxt of
110 ev top env (P Ref n ty) = case lookupDef Nothing n ctxt of
109− [Function (Fun _ _ _ v)] -> return v
111+ [Function _ tm] -> ev True env tm
112+ [TyDecl nt ty] -> do vty <- ev True env ty
110− [TyDecl nt ty hty] -> return $ VP nt n hty
113+ return $ VP nt n vty
111 [CaseOp inl _ _ [] tree] ->
114 [CaseOp inl _ _ [] tree] ->
112 if simpl && (not inl)
115 if simpl && (not inl)
113 then liftM (VP Ref n) (ev top env ty)
116 then liftM (VP Ref n) (ev top env ty)
@@ -293,8 +296,8 @@ eval_hnf ctxt statics genv tm = ev [] tm where
293 ev env (P _ n ty)
296 ev env (P _ n ty)
294 | Just (Let t v) <- lookup n genv = ev env v
297 | Just (Let t v) <- lookup n genv = ev env v
295 ev env (P Ref n ty) = case lookupDef Nothing n ctxt of
298 ev env (P Ref n ty) = case lookupDef Nothing n ctxt of
296− [Function (Fun _ _ t _)] -> ev env t
299+ [Function _ t] -> ev env t
297− [TyDecl nt ty hty] -> return $ HP nt n ty
300+ [TyDecl nt ty] -> return $ HP nt n ty
298 [CaseOp inl _ _ [] tree] ->
301 [CaseOp inl _ _ [] tree] ->
299 do c <- evCase env [] [] tree
302 do c <- evCase env [] [] tree
300 case c of
303 case c of
@@ -416,23 +419,20 @@ spec ctxt statics genv tm = undefined
417-- CONTEXTS -----------------------------------------------------------------
420-- CONTEXTS -----------------------------------------------------------------
419−data Fun = Fun Type Value Term Value
422{- A definition is either a simple function (just an expression with a type),
422{- A definition is either a simple function (just an expression with a type),
423 a constant, which could be a data or type constructor, an axiom or as an
423 a constant, which could be a data or type constructor, an axiom or as an
424 yet undefined function, or an Operator.
424 yet undefined function, or an Operator.
425 An Operator is a function which explains how to reduce.
425 An Operator is a function which explains how to reduce.
426 A CaseOp is a function defined by a simple case tree -}
426 A CaseOp is a function defined by a simple case tree -}
428−data Def = Function Fun
428+data Def = Function Type Term
429− | TyDecl NameType Type Value
429+ | TyDecl NameType Type
430 | Operator Type Int ([Value] -> Maybe Value)
430 | Operator Type Int ([Value] -> Maybe Value)
431 | CaseOp Bool Type [(Term, Term)] [Name] SC -- Bool for inlineable
431 | CaseOp Bool Type [(Term, Term)] [Name] SC -- Bool for inlineable
433instance Show Def where
433instance Show Def where
434− show (Function f) = "Function: " ++ show f
434+ show (Function ty tm) = "Function: " ++ show (ty, tm)
435− show (TyDecl nt ty val) = "TyDecl: " ++ show nt ++ " " ++ show ty
435+ show (TyDecl nt ty) = "TyDecl: " ++ show nt ++ " " ++ show ty
436 show (Operator ty _ _) = "Operator: " ++ show ty
436 show (Operator ty _ _) = "Operator: " ++ show ty
437 show (CaseOp _ ty ps ns sc) = "Case: " ++ show ps ++ "\n" ++
437 show (CaseOp _ ty ps ns sc) = "Case: " ++ show ps ++ "\n" ++
438 show ns ++ " " ++ show sc
438 show ns ++ " " ++ show sc
@@ -453,14 +453,13 @@ veval ctxt env t = evalState (eval ctxt emptyContext env t []) ()
453addToCtxt :: Name -> Term -> Type -> Context -> Context
453addToCtxt :: Name -> Term -> Type -> Context -> Context
454addToCtxt n tm ty uctxt
454addToCtxt n tm ty uctxt
455 = let ctxt = definitions uctxt
455 = let ctxt = definitions uctxt
456− ctxt' = addDef n (Function (Fun ty (veval uctxt [] ty)
456+ ctxt' = addDef n (Function ty tm) ctxt in
457− tm (veval uctxt [] tm))) ctxt in
458 uctxt { definitions = ctxt' }
457 uctxt { definitions = ctxt' }
460addTyDecl :: Name -> Type -> Context -> Context
459addTyDecl :: Name -> Type -> Context -> Context
461addTyDecl n ty uctxt
460addTyDecl n ty uctxt
462 = let ctxt = definitions uctxt
461 = 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
464 uctxt { definitions = ctxt' }
463 uctxt { definitions = ctxt' }
466addDatatype :: Datatype Name -> Context -> Context
465addDatatype :: Datatype Name -> Context -> Context
@@ -468,14 +467,14 @@ addDatatype (Data n tag ty cons) uctxt
468 = let ctxt = definitions uctxt
467 = let ctxt = definitions uctxt
469 ty' = normalise uctxt [] ty
468 ty' = normalise uctxt [] ty
470 ctxt' = addCons 0 cons (addDef n
469 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
472 uctxt { definitions = ctxt' }
471 uctxt { definitions = ctxt' }
474 addCons tag [] ctxt = ctxt
473 addCons tag [] ctxt = ctxt
475 addCons tag ((n, ty) : cons) ctxt
474 addCons tag ((n, ty) : cons) ctxt
476 = let ty' = normalise uctxt [] ty in
475 = let ty' = normalise uctxt [] ty in
477 addCons (tag+1) cons (addDef n
476 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)
480addCasedef :: Name -> Bool -> Bool -> [(Term, Term)] -> Type -> Context -> Context
479addCasedef :: Name -> Bool -> Bool -> [(Term, Term)] -> Type -> Context -> Context
481addCasedef n alwaysInline tcase ps ty uctxt
480addCasedef n alwaysInline tcase ps ty uctxt
@@ -498,8 +497,8 @@ lookupTy :: Maybe [String] -> Name -> Context -> [Type]
498lookupTy root n ctxt
497lookupTy root n ctxt
499 = do def <- lookupCtxt root n (definitions ctxt)
498 = do def <- lookupCtxt root n (definitions ctxt)
500 case def of
499 case def of
501− (Function (Fun ty _ _ _)) -> return ty
500+ (Function ty _) -> return ty
502− (TyDecl _ ty _) -> return ty
501+ (TyDecl _ ty) -> return ty
503 (Operator ty _ _) -> return ty
502 (Operator ty _ _) -> return ty
504 (CaseOp _ ty _ _ _) -> return ty
503 (CaseOp _ ty _ _ _) -> return ty
@@ -507,8 +506,8 @@ lookupP :: Maybe [String] -> Name -> Context -> [Term]
507lookupP root n ctxt
506lookupP root n ctxt
508 = do def <- lookupCtxt root n (definitions ctxt)
507 = do def <- lookupCtxt root n (definitions ctxt)
509 case def of
508 case def of
510− (Function (Fun ty _ tm _)) -> return (P Ref n ty)
509+ (Function ty tm) -> return (P Ref n ty)
511− (TyDecl nt ty hty) -> return (P nt n ty)
510+ (TyDecl nt ty) -> return (P nt n ty)
512 (CaseOp _ ty _ _ _) -> return (P Ref n ty)
511 (CaseOp _ ty _ _ _) -> return (P Ref n ty)
513 (Operator ty _ _) -> return (P Ref n ty)
512 (Operator ty _ _) -> return (P Ref n ty)
@@ -519,8 +518,8 @@ lookupVal :: Maybe [String] -> Name -> Context -> [Value]
519lookupVal root n ctxt
518lookupVal root n ctxt
520 = do def <- lookupCtxt root n (definitions ctxt)
519 = do def <- lookupCtxt root n (definitions ctxt)
521 case def of
520 case def of
522− (Function (Fun _ _ _ htm)) -> return htm
521+ (Function _ htm) -> return (veval ctxt [] htm)
523− (TyDecl nt ty hty) -> return (VP nt n hty)
522+ (TyDecl nt ty) -> return (VP nt n (veval ctxt [] ty))
525lookupTyEnv :: Name -> Env -> Maybe (Int, Type)
524lookupTyEnv :: Name -> Env -> Maybe (Int, Type)
526lookupTyEnv n env = li n 0 env where
525lookupTyEnv n env = li n 0 env where
@@ -528,4 +527,3 @@ lookupTyEnv n env = li n 0 env where
528 li n i ((x, b): xs)
527 li n i ((x, b): xs)
529 | n == x = Just (i, binderTy b)
528 | n == x = Just (i, binderTy b)
530 | otherwise = li n (i+1) xs
529 | otherwise = li n (i+1) xs