Checks.hs 3.21 KB
Newer Older
Björn Peemöller 's avatar
Björn Peemöller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
{- |
    Module      :  $Header$
    Description :  Different checks on a Curry module
    Copyright   :  (c) 2011, Björn Peemöller (bjp@informatik.uni-kiel.de)
    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

16
import Control.Monad.Trans.Either
Björn Peemöller 's avatar
Björn Peemöller committed
17
18
19
import Curry.Syntax (Module (..))

import Base.Messages
Björn Peemöller 's avatar
Björn Peemöller committed
20

21
import qualified Checks.ExportCheck as EC (exportCheck)
Björn Peemöller 's avatar
Björn Peemöller committed
22
23
24
25
26
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
27
28
29
30

import CompilerEnv
import CompilerOpts

31
32
type Check m = Options -> CompilerEnv -> Module
            -> EitherT [Message] m (CompilerEnv, Module)
Björn Peemöller 's avatar
Björn Peemöller committed
33
34

-- |Check the kinds of type definitions and signatures.
35
36
37
38
--
-- * Declarations: Nullary type constructors and type variables are
--                 disambiguated
-- * Environment:  remains unchanged
39
40
41
42
kindCheck :: Monad m => Check m
kindCheck _ env (Module m es is ds)
  | null msgs = right (env, Module m es is ds')
  | otherwise = left msgs
Björn Peemöller 's avatar
Björn Peemöller committed
43
  where (ds', msgs) = KC.kindCheck (moduleIdent env) (tyConsEnv env) ds
Björn Peemöller 's avatar
Björn Peemöller committed
44

Björn Peemöller 's avatar
Björn Peemöller committed
45
-- |Check for a correct syntax.
46
47
--
-- * Declarations: Nullary data constructors and variables are
48
--                 disambiguated, variables are renamed
49
-- * Environment:  remains unchanged
50
syntaxCheck :: Monad m => Check m
Björn Peemöller 's avatar
Björn Peemöller committed
51
syntaxCheck opts env (Module m es is ds)
52
53
  | null msgs = right (env, Module m es is ds')
  | otherwise = left msgs
54
55
  where (ds', msgs) = SC.syntaxCheck opts (moduleIdent env)
                      (valueEnv env) (tyConsEnv env) ds
Björn Peemöller 's avatar
Björn Peemöller committed
56

Björn Peemöller 's avatar
Björn Peemöller committed
57
-- |Check the precedences of infix operators.
58
59
60
61
--
-- * Declarations: Expressions are reordered according to the specified
--                 precedences
-- * Environment:  The operator precedence environment is updated
62
63
64
65
precCheck :: Monad m => Check m
precCheck _ env (Module m es is ds)
  | null msgs = right (env { opPrecEnv = pEnv' }, Module m es is ds')
  | otherwise = left msgs
Björn Peemöller 's avatar
Björn Peemöller committed
66
67
68
69
70
  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.
71
72
73
74
typeCheck :: Monad m => Check m
typeCheck _ env mdl@(Module _ _ _ ds)
  | null msgs = right (env { tyConsEnv = tcEnv', valueEnv = tyEnv' }, mdl)
  | otherwise = left msgs
75
76
  where (tcEnv', tyEnv', msgs) = TC.typeCheck (moduleIdent env)
                                 (tyConsEnv env) (valueEnv env) ds
Björn Peemöller 's avatar
Björn Peemöller committed
77

78
-- |Check the export specification
79
80
81
82
exportCheck :: Monad m => Check m
exportCheck _ env (Module m es is ds)
  | null msgs = right (env, Module m es' is ds)
  | otherwise = left msgs
83
84
  where (es', msgs) = EC.exportCheck (moduleIdent env) (aliasEnv env)
                                     (tyConsEnv env) (valueEnv env) es
85

86
-- TODO: Which kind of warnings?
Björn Peemöller 's avatar
Björn Peemöller committed
87
88

-- |Check for warnings.
Björn Peemöller 's avatar
Björn Peemöller committed
89
warnCheck :: CompilerEnv -> Module -> [Message]
90
warnCheck env mdl = WC.warnCheck (valueEnv env) (tyConsEnv env) mdl