Checks.hs 3.61 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

Björn Peemöller 's avatar
Björn Peemöller committed
16 17 18
import Curry.Syntax (Module (..))

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

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

import CompilerEnv
import CompilerOpts

30 31 32 33 34 35 36 37 38 39 40 41 42
data CheckResult a
  = CheckSuccess a
  | CheckFailed [Message]

instance Monad CheckResult where
  return = CheckSuccess
  (>>=)  = thenCheck

thenCheck :: CheckResult a -> (a -> CheckResult b) -> CheckResult b
thenCheck chk cont = case chk of
  CheckSuccess   a -> cont a
  CheckFailed errs -> CheckFailed errs

Björn Peemöller 's avatar
Björn Peemöller committed
43 44 45
-- TODO: More documentation

-- |Check the kinds of type definitions and signatures.
46 47 48 49
--
-- * Declarations: Nullary type constructors and type variables are
--                 disambiguated
-- * Environment:  remains unchanged
50
kindCheck :: CompilerEnv -> Module -> CheckResult (CompilerEnv, Module)
Björn Peemöller 's avatar
Björn Peemöller committed
51
kindCheck env (Module m es is ds)
52 53
  | null msgs = CheckSuccess (env, Module m es is ds')
  | otherwise = CheckFailed msgs
Björn Peemöller 's avatar
Björn Peemöller committed
54
  where (ds', msgs) = KC.kindCheck (moduleIdent env) (tyConsEnv env) ds
Björn Peemöller 's avatar
Björn Peemöller committed
55

Björn Peemöller 's avatar
Björn Peemöller committed
56
-- |Check for a correct syntax.
57 58 59 60
--
-- * Declarations: Nullary data constructors and variables are
--                 disambiguated
-- * Environment:  remains unchanged
61
syntaxCheck :: Options -> CompilerEnv -> Module -> CheckResult (CompilerEnv, Module)
Björn Peemöller 's avatar
Björn Peemöller committed
62
syntaxCheck opts env (Module m es is ds)
63 64
  | null msgs = CheckSuccess (env, Module m es is ds')
  | otherwise = CheckFailed msgs
65 66
  where (ds', msgs) = SC.syntaxCheck opts (moduleIdent env)
                      (valueEnv env) (tyConsEnv env) ds
Björn Peemöller 's avatar
Björn Peemöller committed
67

Björn Peemöller 's avatar
Björn Peemöller committed
68 69 70
-- |Check the precedences of infix operators.
-- In addition, the abstract syntax tree is rearranged to reflect the
-- relative precedences; the operator precedence environment is updated.
71
precCheck :: CompilerEnv -> Module -> CheckResult (CompilerEnv, Module)
Björn Peemöller 's avatar
Björn Peemöller committed
72
precCheck env (Module m es is ds)
73 74
  | null msgs = CheckSuccess (env { opPrecEnv = pEnv' }, Module m es is ds')
  | otherwise = CheckFailed msgs
Björn Peemöller 's avatar
Björn Peemöller committed
75 76 77 78 79
  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.
80 81 82 83 84 85
typeCheck :: CompilerEnv -> Module -> CheckResult (CompilerEnv, Module)
typeCheck env mdl@(Module _ _ _ ds)
  | null msgs = CheckSuccess (env { tyConsEnv = tcEnv', valueEnv = tyEnv' }, mdl)
  | otherwise = CheckFailed msgs
  where (tcEnv', tyEnv', msgs) = TC.typeCheck (moduleIdent env)
                                 (tyConsEnv env) (valueEnv env) ds
Björn Peemöller 's avatar
Björn Peemöller committed
86

87
-- |Check the export specification
88
exportCheck :: CompilerEnv -> Module -> CheckResult (CompilerEnv, Module)
89
exportCheck env (Module m es is ds)
90 91
  | null msgs = CheckSuccess (env, Module m es' is ds)
  | otherwise = CheckFailed msgs
92 93
  where (es', msgs) = EC.exportCheck (moduleIdent env) (aliasEnv env)
                                     (tyConsEnv env) (valueEnv env) es
94

95
-- TODO: Which kind of warnings?
Björn Peemöller 's avatar
Björn Peemöller committed
96 97

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