-- |
-- Module      :  Cryptol.ModuleSystem
-- Copyright   :  (c) 2013-2016 Galois, Inc.
-- License     :  BSD3
-- Maintainer  :  cryptol@galois.com
-- Stability   :  provisional
-- Portability :  portable

{-# LANGUAGE FlexibleContexts #-}

module Cryptol.ModuleSystem (
    -- * Module System
    ModuleEnv(..), initialModuleEnv
  , DynamicEnv(..)
  , ModuleError(..), ModuleWarning(..)
  , ModuleCmd, ModuleRes
  , ModuleInput(..)
  , findModule
  , loadModuleByPath
  , loadModuleByName
  , checkExpr
  , evalExpr
  , checkDecls
  , evalDecls
  , noPat
  , focusedEnv
  , getPrimMap
  , renameVar
  , renameType

    -- * Interfaces
  , Iface, IfaceG(..), IfaceParams(..), IfaceDecls(..), T.genIface
  , IfaceTySyn, IfaceDecl(..)
  ) where

import Data.Map (Map)

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.TypeCheck.Interface as T
import qualified Cryptol.Utils.Ident as M

-- Public Interface ------------------------------------------------------------

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

-- | Find the file associated with a module name in the module search path.
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)

-- | Load the module contained in the given file.
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
forall mname. ModuleG mname -> mname
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)

-- | Load the given parsed module.
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
forall mname. ModuleG mname -> mname
T.mName Module
m')
    (ModulePath, Module) -> ModuleM (ModulePath, Module)
forall (m :: * -> *) a. Monad m => a -> m a
return (ModulePath
path,Module
m')

-- Extended Environments -------------------------------------------------------

-- These functions are particularly useful for interactive modes, as
-- they allow for expressions to be evaluated in an environment that
-- can extend dynamically outside of the context of a module.

-- | Check the type of an expression.  Give back the renamed expression, the
-- core expression, and its type schema.
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))

-- | Evaluate an expression.
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))

-- | Typecheck top-level declarations.
checkDecls :: [P.TopDecl PName] -> ModuleCmd (R.NamingEnv,[T.DeclGroup], Map Name T.TySyn)
checkDecls :: [TopDecl PName]
-> ModuleCmd (NamingEnv, [DeclGroup], Map Name TySyn)
checkDecls [TopDecl PName]
ds ModuleInput IO
env = ModuleInput IO
-> ModuleM (NamingEnv, [DeclGroup], Map Name TySyn)
-> IO
     (Either
        ModuleError ((NamingEnv, [DeclGroup], Map Name TySyn), ModuleEnv),
      [ModuleWarning])
forall a.
ModuleInput IO
-> ModuleM a
-> IO (Either ModuleError (a, ModuleEnv), [ModuleWarning])
runModuleM ModuleInput IO
env
                  (ModuleM (NamingEnv, [DeclGroup], Map Name TySyn)
 -> IO
      (Either
         ModuleError ((NamingEnv, [DeclGroup], Map Name TySyn), ModuleEnv),
       [ModuleWarning]))
-> ModuleM (NamingEnv, [DeclGroup], Map Name TySyn)
-> IO
     (Either
        ModuleError ((NamingEnv, [DeclGroup], Map Name TySyn), ModuleEnv),
      [ModuleWarning])
forall a b. (a -> b) -> a -> b
$ ModuleM (NamingEnv, [DeclGroup], Map Name TySyn)
-> ModuleM (NamingEnv, [DeclGroup], Map Name TySyn)
forall a. ModuleM a -> ModuleM a
interactive
                  (ModuleM (NamingEnv, [DeclGroup], Map Name TySyn)
 -> ModuleM (NamingEnv, [DeclGroup], Map Name TySyn))
-> ModuleM (NamingEnv, [DeclGroup], Map Name TySyn)
-> ModuleM (NamingEnv, [DeclGroup], Map Name TySyn)
forall a b. (a -> b) -> a -> b
$ [TopDecl PName] -> ModuleM (NamingEnv, [DeclGroup], Map Name TySyn)
Base.checkDecls [TopDecl PName]
ds

-- | Evaluate declarations and add them to the extended environment.
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))

-- | Rename a *use* of a value name. The distinction between uses and
-- binding is used to keep track of dependencies.
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 (NameType -> PName -> RenameM Name
R.renameVar NameType
R.NameUse PName
n)

-- | Rename a *use* of a type name. The distinction between uses and
-- binding is used to keep track of dependencies.
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 (NameType -> PName -> RenameM Name
R.renameType NameType
R.NameUse PName
n)