{-# LANGUAGE FlexibleContexts #-}
module Cryptol.ModuleSystem (
ModuleEnv(..), initialModuleEnv
, DynamicEnv(..)
, ModuleError(..), ModuleWarning(..)
, ModuleCmd, ModuleRes
, ModuleInput(..)
, findModule
, loadModuleByPath
, loadModuleByName
, checkExpr
, evalExpr
, checkDecls
, evalDecls
, noPat
, focusedEnv
, getPrimMap
, renameVar
, renameType
, Iface(..), IfaceParams(..), IfaceDecls(..), genIface
, IfaceTySyn, IfaceDecl(..)
) where
import qualified Cryptol.Eval.Concrete as Concrete
import Cryptol.ModuleSystem.Env
import Cryptol.ModuleSystem.Interface
import Cryptol.ModuleSystem.Monad
import Cryptol.ModuleSystem.Name (Name,PrimMap)
import qualified Cryptol.ModuleSystem.Renamer as R
import qualified Cryptol.ModuleSystem.Base as Base
import qualified Cryptol.Parser.AST as P
import Cryptol.Parser.Name (PName)
import Cryptol.Parser.NoPat (RemovePatterns)
import qualified Cryptol.TypeCheck.AST as T
import qualified Cryptol.Utils.Ident as M
type ModuleCmd a = ModuleInput IO -> IO (ModuleRes a)
type ModuleRes a = (Either ModuleError (a,ModuleEnv), [ModuleWarning])
getPrimMap :: ModuleCmd PrimMap
getPrimMap :: ModuleCmd PrimMap
getPrimMap ModuleInput IO
me = ModuleInput IO
-> ModuleM PrimMap
-> IO (Either ModuleError (PrimMap, ModuleEnv), [ModuleWarning])
forall a.
ModuleInput IO
-> ModuleM a
-> IO (Either ModuleError (a, ModuleEnv), [ModuleWarning])
runModuleM ModuleInput IO
me ModuleM PrimMap
Base.getPrimMap
findModule :: P.ModName -> ModuleCmd ModulePath
findModule :: ModName -> ModuleCmd ModulePath
findModule ModName
n ModuleInput IO
env = ModuleInput IO
-> ModuleM ModulePath
-> IO (Either ModuleError (ModulePath, ModuleEnv), [ModuleWarning])
forall a.
ModuleInput IO
-> ModuleM a
-> IO (Either ModuleError (a, ModuleEnv), [ModuleWarning])
runModuleM ModuleInput IO
env (ModName -> ModuleM ModulePath
Base.findModule ModName
n)
loadModuleByPath :: FilePath -> ModuleCmd (ModulePath,T.Module)
loadModuleByPath :: FilePath -> ModuleCmd (ModulePath, Module)
loadModuleByPath FilePath
path ModuleInput IO
minp =
ModuleInput IO
-> ModuleM (ModulePath, Module)
-> IO
(Either ModuleError ((ModulePath, Module), ModuleEnv),
[ModuleWarning])
forall a.
ModuleInput IO
-> ModuleM a
-> IO (Either ModuleError (a, ModuleEnv), [ModuleWarning])
runModuleM ModuleInput IO
minp{ minpModuleEnv :: ModuleEnv
minpModuleEnv = ModuleEnv -> ModuleEnv
resetModuleEnv (ModuleInput IO -> ModuleEnv
forall (m :: * -> *). ModuleInput m -> ModuleEnv
minpModuleEnv ModuleInput IO
minp) } (ModuleM (ModulePath, Module)
-> IO
(Either ModuleError ((ModulePath, Module), ModuleEnv),
[ModuleWarning]))
-> ModuleM (ModulePath, Module)
-> IO
(Either ModuleError ((ModulePath, Module), ModuleEnv),
[ModuleWarning])
forall a b. (a -> b) -> a -> b
$ do
(LoadedModule -> Bool) -> ModuleM ()
unloadModule ((FilePath -> ModulePath
InFile FilePath
path ModulePath -> ModulePath -> Bool
forall a. Eq a => a -> a -> Bool
==) (ModulePath -> Bool)
-> (LoadedModule -> ModulePath) -> LoadedModule -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LoadedModule -> ModulePath
lmFilePath)
Module
m <- FilePath -> ModuleM Module
Base.loadModuleByPath FilePath
path
ModName -> ModuleM ()
setFocusedModule (Module -> ModName
T.mName Module
m)
(ModulePath, Module) -> ModuleM (ModulePath, Module)
forall (m :: * -> *) a. Monad m => a -> m a
return (FilePath -> ModulePath
InFile FilePath
path,Module
m)
loadModuleByName :: P.ModName -> ModuleCmd (ModulePath,T.Module)
loadModuleByName :: ModName -> ModuleCmd (ModulePath, Module)
loadModuleByName ModName
n ModuleInput IO
minp =
ModuleInput IO
-> ModuleM (ModulePath, Module)
-> IO
(Either ModuleError ((ModulePath, Module), ModuleEnv),
[ModuleWarning])
forall a.
ModuleInput IO
-> ModuleM a
-> IO (Either ModuleError (a, ModuleEnv), [ModuleWarning])
runModuleM ModuleInput IO
minp{ minpModuleEnv :: ModuleEnv
minpModuleEnv = ModuleEnv -> ModuleEnv
resetModuleEnv (ModuleInput IO -> ModuleEnv
forall (m :: * -> *). ModuleInput m -> ModuleEnv
minpModuleEnv ModuleInput IO
minp) } (ModuleM (ModulePath, Module)
-> IO
(Either ModuleError ((ModulePath, Module), ModuleEnv),
[ModuleWarning]))
-> ModuleM (ModulePath, Module)
-> IO
(Either ModuleError ((ModulePath, Module), ModuleEnv),
[ModuleWarning])
forall a b. (a -> b) -> a -> b
$ do
(LoadedModule -> Bool) -> ModuleM ()
unloadModule ((ModName
n ModName -> ModName -> Bool
forall a. Eq a => a -> a -> Bool
==) (ModName -> Bool)
-> (LoadedModule -> ModName) -> LoadedModule -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LoadedModule -> ModName
lmName)
(ModulePath
path,Module
m') <- Bool -> ImportSource -> ModuleM (ModulePath, Module)
Base.loadModuleFrom Bool
False (ModName -> ImportSource
FromModule ModName
n)
ModName -> ModuleM ()
setFocusedModule (Module -> ModName
T.mName Module
m')
(ModulePath, Module) -> ModuleM (ModulePath, Module)
forall (m :: * -> *) a. Monad m => a -> m a
return (ModulePath
path,Module
m')
checkExpr :: P.Expr PName -> ModuleCmd (P.Expr Name,T.Expr,T.Schema)
checkExpr :: Expr PName -> ModuleCmd (Expr Name, Expr, Schema)
checkExpr Expr PName
e ModuleInput IO
env = ModuleInput IO
-> ModuleM (Expr Name, Expr, Schema)
-> IO
(Either ModuleError ((Expr Name, Expr, Schema), ModuleEnv),
[ModuleWarning])
forall a.
ModuleInput IO
-> ModuleM a
-> IO (Either ModuleError (a, ModuleEnv), [ModuleWarning])
runModuleM ModuleInput IO
env (ModuleM (Expr Name, Expr, Schema)
-> ModuleM (Expr Name, Expr, Schema)
forall a. ModuleM a -> ModuleM a
interactive (Expr PName -> ModuleM (Expr Name, Expr, Schema)
Base.checkExpr Expr PName
e))
evalExpr :: T.Expr -> ModuleCmd Concrete.Value
evalExpr :: Expr -> ModuleCmd Value
evalExpr Expr
e ModuleInput IO
env = ModuleInput IO
-> ModuleM Value
-> IO (Either ModuleError (Value, ModuleEnv), [ModuleWarning])
forall a.
ModuleInput IO
-> ModuleM a
-> IO (Either ModuleError (a, ModuleEnv), [ModuleWarning])
runModuleM ModuleInput IO
env (ModuleM Value -> ModuleM Value
forall a. ModuleM a -> ModuleM a
interactive (Expr -> ModuleM Value
Base.evalExpr Expr
e))
checkDecls :: [P.TopDecl PName] -> ModuleCmd (R.NamingEnv,[T.DeclGroup])
checkDecls :: [TopDecl PName] -> ModuleCmd (NamingEnv, [DeclGroup])
checkDecls [TopDecl PName]
ds ModuleInput IO
env = ModuleInput IO
-> ModuleM (NamingEnv, [DeclGroup])
-> IO
(Either ModuleError ((NamingEnv, [DeclGroup]), ModuleEnv),
[ModuleWarning])
forall a.
ModuleInput IO
-> ModuleM a
-> IO (Either ModuleError (a, ModuleEnv), [ModuleWarning])
runModuleM ModuleInput IO
env
(ModuleM (NamingEnv, [DeclGroup])
-> IO
(Either ModuleError ((NamingEnv, [DeclGroup]), ModuleEnv),
[ModuleWarning]))
-> ModuleM (NamingEnv, [DeclGroup])
-> IO
(Either ModuleError ((NamingEnv, [DeclGroup]), ModuleEnv),
[ModuleWarning])
forall a b. (a -> b) -> a -> b
$ ModuleM (NamingEnv, [DeclGroup])
-> ModuleM (NamingEnv, [DeclGroup])
forall a. ModuleM a -> ModuleM a
interactive
(ModuleM (NamingEnv, [DeclGroup])
-> ModuleM (NamingEnv, [DeclGroup]))
-> ModuleM (NamingEnv, [DeclGroup])
-> ModuleM (NamingEnv, [DeclGroup])
forall a b. (a -> b) -> a -> b
$ [TopDecl PName] -> ModuleM (NamingEnv, [DeclGroup])
Base.checkDecls [TopDecl PName]
ds
evalDecls :: [T.DeclGroup] -> ModuleCmd ()
evalDecls :: [DeclGroup] -> ModuleCmd ()
evalDecls [DeclGroup]
dgs ModuleInput IO
env = ModuleInput IO
-> ModuleM ()
-> IO (Either ModuleError ((), ModuleEnv), [ModuleWarning])
forall a.
ModuleInput IO
-> ModuleM a
-> IO (Either ModuleError (a, ModuleEnv), [ModuleWarning])
runModuleM ModuleInput IO
env (ModuleM () -> ModuleM ()
forall a. ModuleM a -> ModuleM a
interactive ([DeclGroup] -> ModuleM ()
Base.evalDecls [DeclGroup]
dgs))
noPat :: RemovePatterns a => a -> ModuleCmd a
noPat :: a -> ModuleCmd a
noPat a
a ModuleInput IO
env = ModuleInput IO
-> ModuleM a
-> IO (Either ModuleError (a, ModuleEnv), [ModuleWarning])
forall a.
ModuleInput IO
-> ModuleM a
-> IO (Either ModuleError (a, ModuleEnv), [ModuleWarning])
runModuleM ModuleInput IO
env (ModuleM a -> ModuleM a
forall a. ModuleM a -> ModuleM a
interactive (a -> ModuleM a
forall a. RemovePatterns a => a -> ModuleM a
Base.noPat a
a))
renameVar :: R.NamingEnv -> PName -> ModuleCmd Name
renameVar :: NamingEnv -> PName -> ModuleCmd Name
renameVar NamingEnv
names PName
n ModuleInput IO
env = ModuleInput IO
-> ModuleM Name
-> IO (Either ModuleError (Name, ModuleEnv), [ModuleWarning])
forall a.
ModuleInput IO
-> ModuleM a
-> IO (Either ModuleError (a, ModuleEnv), [ModuleWarning])
runModuleM ModuleInput IO
env (ModuleM Name
-> IO (Either ModuleError (Name, ModuleEnv), [ModuleWarning]))
-> ModuleM Name
-> IO (Either ModuleError (Name, ModuleEnv), [ModuleWarning])
forall a b. (a -> b) -> a -> b
$ ModuleM Name -> ModuleM Name
forall a. ModuleM a -> ModuleM a
interactive (ModuleM Name -> ModuleM Name) -> ModuleM Name -> ModuleM Name
forall a b. (a -> b) -> a -> b
$
ModName -> NamingEnv -> RenameM Name -> ModuleM Name
forall a. ModName -> NamingEnv -> RenameM a -> ModuleM a
Base.rename ModName
M.interactiveName NamingEnv
names (PName -> RenameM Name
R.renameVar PName
n)
renameType :: R.NamingEnv -> PName -> ModuleCmd Name
renameType :: NamingEnv -> PName -> ModuleCmd Name
renameType NamingEnv
names PName
n ModuleInput IO
env = ModuleInput IO
-> ModuleM Name
-> IO (Either ModuleError (Name, ModuleEnv), [ModuleWarning])
forall a.
ModuleInput IO
-> ModuleM a
-> IO (Either ModuleError (a, ModuleEnv), [ModuleWarning])
runModuleM ModuleInput IO
env (ModuleM Name
-> IO (Either ModuleError (Name, ModuleEnv), [ModuleWarning]))
-> ModuleM Name
-> IO (Either ModuleError (Name, ModuleEnv), [ModuleWarning])
forall a b. (a -> b) -> a -> b
$ ModuleM Name -> ModuleM Name
forall a. ModuleM a -> ModuleM a
interactive (ModuleM Name -> ModuleM Name) -> ModuleM Name -> ModuleM Name
forall a b. (a -> b) -> a -> b
$
ModName -> NamingEnv -> RenameM Name -> ModuleM Name
forall a. ModName -> NamingEnv -> RenameM a -> ModuleM a
Base.rename ModName
M.interactiveName NamingEnv
names (PName -> RenameM Name
R.renameType PName
n)