Checks.hs 3.55 KB
Newer Older
Björn Peemöller 's avatar
Björn Peemöller committed
1 2 3
{- |
    Module      :  $Header$
    Description :  Different checks on a Curry module
4
    Copyright   :  (c) 2011 - 2013, Björn Peemöller
Björn Peemöller 's avatar
Björn Peemöller committed
5 6 7 8 9 10 11 12 13
    License     :  OtherLicense

    Maintainer  :  bjp@informatik.uni-kiel.de
    Stability   :  experimental
    Portability :  portable

    This module subsumes the different checks to be performed on a Curry
    module during compilation, e.g. type checking.
-}
Björn Peemöller 's avatar
Björn Peemöller committed
14 15
module Checks where

Björn Peemöller 's avatar
Björn Peemöller committed
16 17 18 19 20 21 22
import qualified Checks.InterfaceCheck as IC (interfaceCheck)
import qualified Checks.ExportCheck    as EC (exportCheck)
import qualified Checks.KindCheck      as KC (kindCheck)
import qualified Checks.PrecCheck      as PC (precCheck)
import qualified Checks.SyntaxCheck    as SC (syntaxCheck)
import qualified Checks.TypeCheck      as TC (typeCheck)
import qualified Checks.WarnCheck      as WC (warnCheck)
Björn Peemöller 's avatar
Björn Peemöller committed
23

24
import Curry.Base.Monad
25
import Curry.Syntax (Module (..), Interface (..))
Björn Peemöller 's avatar
Björn Peemöller committed
26

Björn Peemöller 's avatar
Björn Peemöller committed
27
import Base.Messages
Björn Peemöller 's avatar
Björn Peemöller committed
28 29 30
import CompilerEnv
import CompilerOpts

Björn Peemöller 's avatar
Björn Peemöller committed
31
type Check m a = Options -> CompilerEnv -> a -> CYT m (CompilerEnv, a)
Björn Peemöller 's avatar
Björn Peemöller committed
32

33 34
interfaceCheck :: Monad m => Check m Interface
interfaceCheck _ env intf
35 36
  | null msgs = ok (env, intf)
  | otherwise = failMessages msgs
37
  where msgs = IC.interfaceCheck (opPrecEnv env) (tyConsEnv env)
Björn Peemöller 's avatar
Björn Peemöller committed
38 39
                                 (valueEnv env) intf

Björn Peemöller 's avatar
Björn Peemöller committed
40
-- |Check the kinds of type definitions and signatures.
41 42 43 44
--
-- * Declarations: Nullary type constructors and type variables are
--                 disambiguated
-- * Environment:  remains unchanged
Björn Peemöller 's avatar
Björn Peemöller committed
45
kindCheck :: Monad m => Check m Module
Björn Peemöller 's avatar
Björn Peemöller committed
46 47
kindCheck _ env mdl
  | null msgs = ok (env, mdl')
48
  | otherwise = failMessages msgs
Björn Peemöller 's avatar
Björn Peemöller committed
49
  where (mdl', msgs) = KC.kindCheck (tyConsEnv env) mdl
Björn Peemöller 's avatar
Björn Peemöller committed
50

Björn Peemöller 's avatar
Björn Peemöller committed
51
-- |Check for a correct syntax.
52 53
--
-- * Declarations: Nullary data constructors and variables are
54
--                 disambiguated, variables are renamed
55
-- * Environment:  remains unchanged
Björn Peemöller 's avatar
Björn Peemöller committed
56
syntaxCheck :: Monad m => Check m Module
57
syntaxCheck opts env mdl
58 59
  | null msgs = ok (env { extensions = exts }, mdl')
  | otherwise = failMessages msgs
60 61
  where ((mdl', exts), msgs) = SC.syntaxCheck opts (valueEnv env)
                                                   (tyConsEnv env) mdl
Björn Peemöller 's avatar
Björn Peemöller committed
62

Björn Peemöller 's avatar
Björn Peemöller committed
63
-- |Check the precedences of infix operators.
64 65 66 67
--
-- * Declarations: Expressions are reordered according to the specified
--                 precedences
-- * Environment:  The operator precedence environment is updated
Björn Peemöller 's avatar
Björn Peemöller committed
68
precCheck :: Monad m => Check m Module
69
precCheck _ env (Module ps m es is ds)
70 71
  | null msgs = ok (env { opPrecEnv = pEnv' }, Module ps m es is ds')
  | otherwise = failMessages msgs
Björn Peemöller 's avatar
Björn Peemöller committed
72 73 74 75 76
  where (ds', pEnv', msgs) = PC.precCheck (moduleIdent env) (opPrecEnv env) ds

-- |Apply the correct typing of the module.
-- The declarations remain unchanged; the type constructor and value
-- environments are updated.
Björn Peemöller 's avatar
Björn Peemöller committed
77
typeCheck :: Monad m => Check m Module
78
typeCheck _ env mdl@(Module _ _ _ _ ds)
79 80
  | null msgs = ok (env { tyConsEnv = tcEnv', valueEnv = tyEnv' }, mdl)
  | otherwise = failMessages msgs
81 82
  where (tcEnv', tyEnv', msgs) = TC.typeCheck (moduleIdent env)
                                 (tyConsEnv env) (valueEnv env) ds
Björn Peemöller 's avatar
Björn Peemöller committed
83

84
-- |Check the export specification
Björn Peemöller 's avatar
Björn Peemöller committed
85
exportCheck :: Monad m => Check m Module
86
exportCheck _ env (Module ps m es is ds)
87 88
  | null msgs = ok (env, Module ps m es' is ds)
  | otherwise = failMessages msgs
89 90
  where (es', msgs) = EC.exportCheck (moduleIdent env) (aliasEnv env)
                                     (tyConsEnv env) (valueEnv env) es
91

Björn Peemöller 's avatar
Björn Peemöller committed
92
-- |Check for warnings.
93
warnCheck :: Options -> CompilerEnv -> Module -> [Message]
94 95
warnCheck opts env mdl = WC.warnCheck (optWarnOpts opts) (aliasEnv env)
  (valueEnv env) (tyConsEnv env) mdl