@@ -2,9 +2,9 @@
22 PatternGuards #-}
33
44module Core.Evaluate(normalise, normaliseC, simplify, specialise, hnf,
5− Def(..),
5+ Def(..), Accessibility(..),
66 Context, initContext, ctxtAlist, uconstraints, next_tvar,
7− addToCtxt, addCtxtDef, addTyDecl, addDatatype,
7+ addToCtxt, setAccess, addCtxtDef, addTyDecl, addDatatype,
88 addCasedef, addOperator,
99 lookupTy, lookupP, lookupDef, lookupVal, lookupTyEnv,
1010 Value(..)) where
@@ -108,11 +108,11 @@ eval ctxt statics genv tm opts = ev True [] tm where
108108
109109 ev top env (P _ n ty)
110110 | Just (Let t v) <- lookup n genv = ev top env v
111− ev top env (P Ref n ty) = case lookupDef Nothing n ctxt of
112− [Function _ tm] -> ev True env tm
113− [TyDecl nt ty] -> do vty <- ev True env ty
114− return $ VP nt n vty
115− [CaseOp inl _ _ [] tree] ->
111+ ev top env (P Ref n ty) = case lookupDefAcc Nothing n ctxt of
112+ [(Function _ tm, Public)] -> trace ("Eval " ++ show n) $ ev True env tm
113+ [(TyDecl nt ty, _)] -> do vty <- ev True env ty
114+ return $ VP nt n vty
115+ [(CaseOp inl _ _ [] tree, Public)] ->
116116 if simpl && (not inl)
117117 then liftM (VP Ref n) (ev top env ty)
118118 else do c <- evCase top env [] [] tree
@@ -153,7 +153,7 @@ eval ctxt statics genv tm opts = ev True [] tm where
153153 apply False env f args
154154 | spec = return $ unload env f args
155155 apply top env (VP Ref n ty) args
156− | [CaseOp inl _ _ ns tree] <- lookupDef Nothing n ctxt
156+ | [(CaseOp inl _ _ ns tree, Public)] <- lookupDefAcc Nothing n ctxt
157157 = -- traceWhen (n == UN ["interp"]) (show (n, args)) $
158158 if simpl && (not inl) then return $ unload env (VP Ref n ty) args
159159 else do c <- evCase top env ns args tree
@@ -449,32 +449,47 @@ instance Binary (a -> b) where
449449
450450-------
451451
452+-- Frozen => doesn't reduce
453+-- Hidden => doesn't reduce and invisible to type checker
454+
455+data Accessibility = Public | Frozen | Hidden
456+ deriving (Show, Eq)
457+{-!
458+deriving instance Binary Accessibility
459+!-}
460+
452461data Context = MkContext { uconstraints :: [UConstraint],
453462 next_tvar :: Int,
454− definitions :: Ctxt Def }
463+ definitions :: Ctxt (Def, Accessibility) }
455464
456465initContext = MkContext [] 0 emptyContext
457466
458467ctxtAlist :: Context -> [(Name, Def)]
459−ctxtAlist ctxt = toAlist (definitions ctxt)
468+ctxtAlist ctxt = map (\(n, (d, a)) -> (n, d)) $ toAlist (definitions ctxt)
460469
461470veval ctxt env t = evalState (eval ctxt emptyContext env t []) ()
462471
463472addToCtxt :: Name -> Term -> Type -> Context -> Context
464473addToCtxt n tm ty uctxt
465474 = let ctxt = definitions uctxt
466− ctxt' = addDef n (Function ty tm) ctxt in
475+ ctxt' = addDef n (Function ty tm, Public) ctxt in
467476 uctxt { definitions = ctxt' }
468477
478+setAccess :: Name -> Accessibility -> Context -> Context
479+setAccess n a uctxt
480+ = let ctxt = definitions uctxt
481+ ctxt' = updateDef n (\ (d, _) -> (d, a)) ctxt in
482+ uctxt { definitions = ctxt' }
483+
469484addCtxtDef :: Name -> Def -> Context -> Context
470485addCtxtDef n d c = let ctxt = definitions c
471− ctxt' = addDef n d ctxt in
486+ ctxt' = addDef n (d, Public) ctxt in
472487 c { definitions = ctxt' }
473488
474489addTyDecl :: Name -> Type -> Context -> Context
475490addTyDecl n ty uctxt
476491 = let ctxt = definitions uctxt
477− ctxt' = addDef n (TyDecl Ref ty) ctxt in
492+ ctxt' = addDef n (TyDecl Ref ty, Public) ctxt in
478493 uctxt { definitions = ctxt' }
479494
480495addDatatype :: Datatype Name -> Context -> Context
@@ -482,14 +497,14 @@ addDatatype (Data n tag ty cons) uctxt
482497 = let ctxt = definitions uctxt
483498 ty' = normalise uctxt [] ty
484499 ctxt' = addCons 0 cons (addDef n
485− (TyDecl (TCon tag (arity ty')) ty) ctxt) in
500+ (TyDecl (TCon tag (arity ty')) ty, Public) ctxt) in
486501 uctxt { definitions = ctxt' }
487502 where
488503 addCons tag [] ctxt = ctxt
489504 addCons tag ((n, ty) : cons) ctxt
490505 = let ty' = normalise uctxt [] ty in
491506 addCons (tag+1) cons (addDef n
492− (TyDecl (DCon tag (arity ty')) ty) ctxt)
507+ (TyDecl (DCon tag (arity ty')) ty, Public) ctxt)
493508
494509addCasedef :: Name -> Bool -> Bool -> [(Term, Term)] -> Type -> Context -> Context
495510addCasedef n alwaysInline tcase ps ty uctxt
@@ -497,7 +512,8 @@ addCasedef n alwaysInline tcase ps ty uctxt
497512 ps' = ps -- simpl ps in
498513 ctxt' = case simpleCase tcase ps' of
499514 CaseDef args sc -> let inl = alwaysInline in
500− addDef n (CaseOp inl ty ps args sc) ctxt in
515+ addDef n (CaseOp inl ty ps args sc,
516+ Public) ctxt in
501517 uctxt { definitions = ctxt' }
502518 where simpl [] = []
503519 simpl ((l,r) : xs) = (l, simplify uctxt [] r) : simpl xs
@@ -505,13 +521,13 @@ addCasedef n alwaysInline tcase ps ty uctxt
505521addOperator :: Name -> Type -> Int -> ([Value] -> Maybe Value) -> Context -> Context
506522addOperator n ty a op uctxt
507523 = let ctxt = definitions uctxt
508− ctxt' = addDef n (Operator ty a op) ctxt in
524+ ctxt' = addDef n (Operator ty a op, Public) ctxt in
509525 uctxt { definitions = ctxt' }
510526
511527lookupTy :: Maybe [String] -> Name -> Context -> [Type]
512528lookupTy root n ctxt
513529 = do def <- lookupCtxt root n (definitions ctxt)
514− case def of
530+ case fst def of
515531 (Function ty _) -> return ty
516532 (TyDecl _ ty) -> return ty
517533 (Operator ty _ _) -> return ty
@@ -520,20 +536,26 @@ lookupTy root n ctxt
520536lookupP :: Maybe [String] -> Name -> Context -> [Term]
521537lookupP root n ctxt
522538 = do def <- lookupCtxt root n (definitions ctxt)
523− case def of
524− (Function ty tm) -> return (P Ref n ty)
525− (TyDecl nt ty) -> return (P nt n ty)
526− (CaseOp _ ty _ _ _) -> return (P Ref n ty)
527− (Operator ty _ _) -> return (P Ref n ty)
539+ p <- case def of
540+ (Function ty tm, a) -> return (P Ref n ty, a)
541+ (TyDecl nt ty, a) -> return (P nt n ty, a)
542+ (CaseOp _ ty _ _ _, a) -> return (P Ref n ty, a)
543+ (Operator ty _ _, a) -> return (P Ref n ty, a)
544+ case snd p of
545+ Hidden -> []
546+ _ -> return (fst p)
528547
529548lookupDef :: Maybe [String] -> Name -> Context -> [Def]
530−lookupDef root n ctxt = lookupCtxt root n (definitions ctxt)
549+lookupDef root n ctxt = map fst $ lookupCtxt root n (definitions ctxt)
550+
551+lookupDefAcc :: Maybe [String] -> Name -> Context -> [(Def, Accessibility)]
552+lookupDefAcc root n ctxt = lookupCtxt root n (definitions ctxt)
531553
532554lookupVal :: Maybe [String] -> Name -> Context -> [Value]
533555lookupVal root n ctxt
534556 = do def <- lookupCtxt root n (definitions ctxt)
535− case def of
536− (Function _ htm) -> return (veval ctxt [] htm)
557+ case fst def of
558+ (Function _ htm) -> return (veval ctxt [] htm)
537559 (TyDecl nt ty) -> return (VP nt n (veval ctxt [] ty))
538560
539561lookupTyEnv :: Name -> Env -> Maybe (Int, Type)
@@ -542,3 +564,4 @@ lookupTyEnv n env = li n 0 env where
542564 li n i ((x, b): xs)
543565 | n == x = Just (i, binderTy b)
544566 | otherwise = li n (i+1) xs
567+