Skip to content

Commit fe55e94

Browse files
committed
Merge remote-tracking branch 'hdgarrood/update-deps'
2 parents 0d6711c + bcea1c0 commit fe55e94

File tree

24 files changed

+217
-52
lines changed

24 files changed

+217
-52
lines changed

psc-make/Main.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
module Main where
1818

1919
import Control.Applicative
20-
import Control.Monad.Error
20+
import Control.Monad.Except
2121
import Control.Monad.Reader
2222

2323
import Data.Version (showVersion)
@@ -52,14 +52,14 @@ readInput InputOptions{..} = do
5252
content <- forM ioInputFiles $ \inFile -> (Right inFile, ) <$> readFile inFile
5353
return (if ioNoPrelude then content else (Left P.RebuildNever, P.prelude) : content)
5454

55-
newtype Make a = Make { unMake :: ReaderT (P.Options P.Make) (ErrorT String IO) a }
55+
newtype Make a = Make { unMake :: ReaderT (P.Options P.Make) (ExceptT String IO) a }
5656
deriving (Functor, Applicative, Monad, MonadIO, MonadError String, MonadReader (P.Options P.Make))
5757

5858
runMake :: P.Options P.Make -> Make a -> IO (Either String a)
59-
runMake opts = runErrorT . flip runReaderT opts . unMake
59+
runMake opts = runExceptT . flip runReaderT opts . unMake
6060

6161
makeIO :: IO a -> Make a
62-
makeIO = Make . lift . ErrorT . fmap (either (Left . show) Right) . tryIOError
62+
makeIO = Make . lift . ExceptT . fmap (either (Left . show) Right) . tryIOError
6363

6464
instance P.MonadMake Make where
6565
getTimestamp path = makeIO $ do

psc/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
module Main where
1818

1919
import Control.Applicative
20-
import Control.Monad.Error
20+
import Control.Monad.Except
2121
import Control.Monad.Reader
2222

2323
import Data.Maybe (fromMaybe)

psci/Main.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import qualified Data.Map as M
3131

3232
import Control.Applicative
3333
import Control.Monad
34-
import Control.Monad.Error (ErrorT(..), MonadError)
34+
import Control.Monad.Except (ExceptT(..), MonadError, runExceptT)
3535
import Control.Monad.Reader (MonadReader, ReaderT, runReaderT)
3636
import Control.Monad.Trans.Class
3737
import Control.Monad.Trans.Maybe (MaybeT(..), runMaybeT)
@@ -325,14 +325,14 @@ newtype PSCI a = PSCI { runPSCI :: InputT (StateT PSCiState IO) a } deriving (Fu
325325
psciIO :: IO a -> PSCI a
326326
psciIO io = PSCI . lift $ lift io
327327

328-
newtype Make a = Make { unMake :: ReaderT (P.Options P.Make) (ErrorT String IO) a }
328+
newtype Make a = Make { unMake :: ReaderT (P.Options P.Make) (ExceptT String IO) a }
329329
deriving (Functor, Applicative, Monad, MonadError String, MonadReader (P.Options P.Make))
330330

331331
runMake :: Make a -> IO (Either String a)
332-
runMake = runErrorT . flip runReaderT options . unMake
332+
runMake = runExceptT . flip runReaderT options . unMake
333333

334334
makeIO :: IO a -> Make a
335-
makeIO = Make . lift . ErrorT . fmap (either (Left . show) Right) . tryIOError
335+
makeIO = Make . lift . ExceptT . fmap (either (Left . show) Right) . tryIOError
336336

337337
instance P.MonadMake Make where
338338
getTimestamp path = makeIO $ do

purescript.cabal

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ library
3131
filepath -any,
3232
mtl >= 2.1.0 && < 2.3.0,
3333
parsec -any,
34-
transformers >= 0.3 && < 0.5,
34+
transformers >= 0.4.0 && < 0.5,
3535
utf8-string >= 1 && < 2,
3636
pattern-arrows >= 0.0.2 && < 0.1,
37-
monad-unify >= 0.2.2 && < 0.3,
3837
file-embed >= 0.0.7 && < 0.0.8,
3938
time -any
4039
exposed-modules: Language.PureScript
@@ -111,11 +110,13 @@ library
111110
Language.PureScript.TypeChecker.Unify
112111
Language.PureScript.TypeClassDictionaries
113112
Language.PureScript.Types
113+
114+
Control.Monad.Unify
114115
exposed: True
115116
buildable: True
116117
hs-source-dirs: src
117118
other-modules: Paths_purescript
118-
ghc-options: -Wall -fno-warn-warnings-deprecations -O2
119+
ghc-options: -Wall -O2
119120

120121
executable psc
121122
build-depends: base >=4 && <5, containers -any, directory -any, filepath -any,
@@ -125,7 +126,7 @@ executable psc
125126
buildable: True
126127
hs-source-dirs: psc
127128
other-modules:
128-
ghc-options: -Wall -fno-warn-warnings-deprecations -O2 -fno-warn-unused-do-bind
129+
ghc-options: -Wall -O2 -fno-warn-unused-do-bind
129130

130131
executable psc-make
131132
build-depends: base >=4 && <5, containers -any, directory -any, filepath -any,
@@ -135,7 +136,7 @@ executable psc-make
135136
buildable: True
136137
hs-source-dirs: psc-make
137138
other-modules:
138-
ghc-options: -Wall -fno-warn-warnings-deprecations -O2 -fno-warn-unused-do-bind
139+
ghc-options: -Wall -O2 -fno-warn-unused-do-bind
139140

140141
executable psci
141142
build-depends: base >=4 && <5, containers -any, directory -any, filepath -any,
@@ -148,7 +149,7 @@ executable psci
148149
hs-source-dirs: psci
149150
other-modules: Commands
150151
Parser
151-
ghc-options: -Wall -fno-warn-warnings-deprecations -O2
152+
ghc-options: -Wall -O2
152153

153154
executable psc-docs
154155
build-depends: base >=4 && <5, purescript -any,
@@ -157,7 +158,7 @@ executable psc-docs
157158
buildable: True
158159
hs-source-dirs: psc-docs
159160
other-modules:
160-
ghc-options: -Wall -fno-warn-warnings-deprecations -O2
161+
ghc-options: -Wall -O2
161162

162163
executable psc-hierarchy
163164
build-depends: base >=4 && <5, purescript -any, optparse-applicative >= 0.10.0,
@@ -166,7 +167,7 @@ executable psc-hierarchy
166167
buildable: True
167168
hs-source-dirs: hierarchy
168169
other-modules:
169-
ghc-options: -Wall -fno-warn-warnings-deprecations -O2
170+
ghc-options: -Wall -O2
170171

171172
test-suite tests
172173
build-depends: base >=4 && <5, containers -any, directory -any,

src/Control/Monad/Unify.hs

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
-----------------------------------------------------------------------------
2+
--
3+
-- Module : Control.Monad.Unify
4+
-- Copyright : (c) Phil Freeman 2013
5+
-- License : MIT
6+
--
7+
-- Maintainer : Phil Freeman <paf31@cantab.net>
8+
-- Stability : experimental
9+
-- Portability :
10+
--
11+
-- |
12+
--
13+
--
14+
-----------------------------------------------------------------------------
15+
16+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
17+
{-# LANGUAGE FlexibleInstances #-}
18+
{-# LANGUAGE FlexibleContexts #-}
19+
{-# LANGUAGE DeriveDataTypeable #-}
20+
{-# LANGUAGE MultiParamTypeClasses #-}
21+
{-# LANGUAGE FunctionalDependencies #-}
22+
{-# LANGUAGE UndecidableInstances #-}
23+
{-# LANGUAGE OverloadedStrings #-}
24+
25+
module Control.Monad.Unify where
26+
27+
import Data.String (IsString)
28+
import Data.Monoid
29+
30+
import Control.Applicative
31+
import Control.Monad.State
32+
import Control.Monad.Error.Class
33+
34+
import Data.HashMap.Strict as M
35+
36+
-- |
37+
-- Untyped unification variables
38+
--
39+
type Unknown = Int
40+
41+
-- |
42+
-- A type which can contain unification variables
43+
--
44+
class Partial t where
45+
unknown :: Unknown -> t
46+
isUnknown :: t -> Maybe Unknown
47+
unknowns :: t -> [Unknown]
48+
($?) :: Substitution t -> t -> t
49+
50+
-- |
51+
-- Identifies types which support unification
52+
--
53+
class (Partial t) => Unifiable m t | t -> m where
54+
(=?=) :: t -> t -> UnifyT t m ()
55+
56+
-- |
57+
-- A substitution maintains a mapping from unification variables to their values
58+
--
59+
data Substitution t = Substitution { runSubstitution :: M.HashMap Int t }
60+
61+
instance (Partial t) => Monoid (Substitution t) where
62+
mempty = Substitution M.empty
63+
s1 `mappend` s2 = Substitution $
64+
M.map (s2 $?) (runSubstitution s1) `M.union`
65+
M.map (s1 $?) (runSubstitution s2)
66+
67+
-- |
68+
-- State required for type checking
69+
--
70+
data UnifyState t = UnifyState {
71+
-- |
72+
-- The next fresh unification variable
73+
--
74+
unifyNextVar :: Int
75+
-- |
76+
-- The current substitution
77+
--
78+
, unifyCurrentSubstitution :: Substitution t
79+
}
80+
81+
-- |
82+
-- An empty @UnifyState@
83+
--
84+
defaultUnifyState :: (Partial t) => UnifyState t
85+
defaultUnifyState = UnifyState 0 mempty
86+
87+
-- |
88+
-- The type checking monad, which provides the state of the type checker, and error reporting capabilities
89+
--
90+
newtype UnifyT t m a = UnifyT { unUnify :: (StateT (UnifyState t) m) a }
91+
deriving (Functor, Monad, Applicative, Alternative, MonadPlus)
92+
93+
instance (MonadState s m) => MonadState s (UnifyT t m) where
94+
get = UnifyT . lift $ get
95+
put = UnifyT . lift . put
96+
97+
instance (MonadError e m) => MonadError e (UnifyT t m) where
98+
throwError = UnifyT . throwError
99+
catchError e f = UnifyT $ catchError (unUnify e) (unUnify . f)
100+
101+
-- |
102+
-- Run a computation in the Unify monad, failing with an error, or succeeding with a return value and the new next unification variable
103+
--
104+
runUnify :: UnifyState t -> UnifyT t m a -> m (a, UnifyState t)
105+
runUnify s = flip runStateT s . unUnify
106+
107+
-- |
108+
-- Substitute a single unification variable
109+
--
110+
substituteOne :: (Partial t) => Unknown -> t -> Substitution t
111+
substituteOne u t = Substitution $ M.singleton u t
112+
113+
-- |
114+
-- Replace a unification variable with the specified value in the current substitution
115+
--
116+
(=:=) :: (IsString e, Monad m, MonadError e m, Unifiable m t) => Unknown -> t -> UnifyT t m ()
117+
(=:=) u t' = do
118+
st <- UnifyT get
119+
let sub = unifyCurrentSubstitution st
120+
let t = sub $? t'
121+
occursCheck u t
122+
let current = sub $? unknown u
123+
case isUnknown current of
124+
Just u1 | u1 == u -> return ()
125+
_ -> current =?= t
126+
UnifyT $ modify $ \s -> s { unifyCurrentSubstitution = substituteOne u t <> unifyCurrentSubstitution s }
127+
128+
-- |
129+
-- Perform the occurs check, to make sure a unification variable does not occur inside a value
130+
--
131+
occursCheck :: (IsString e, Monad m, MonadError e m, Partial t) => Unknown -> t -> UnifyT t m ()
132+
occursCheck u t =
133+
case isUnknown t of
134+
Nothing -> when (u `elem` unknowns t) $ UnifyT . lift . throwError $ "Occurs check fails"
135+
_ -> return ()
136+
137+
-- |
138+
-- Generate a fresh untyped unification variable
139+
--
140+
fresh' :: (Monad m) => UnifyT t m Unknown
141+
fresh' = do
142+
st <- UnifyT get
143+
UnifyT $ modify $ \s -> s { unifyNextVar = succ (unifyNextVar s) }
144+
return $ unifyNextVar st
145+
146+
-- |
147+
-- Generate a fresh unification variable at a specific type
148+
--
149+
fresh :: (Monad m, Partial t) => UnifyT t m t
150+
fresh = do
151+
u <- fresh'
152+
return $ unknown u
153+
154+
155+

src/Language/PureScript.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import qualified Data.Set as S
2929

3030
import Control.Applicative
3131
import Control.Arrow ((&&&))
32-
import Control.Monad.Error
32+
import Control.Monad.Except
3333
import Control.Monad.Reader
3434

3535
import System.FilePath ((</>))

src/Language/PureScript/Errors.hs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
module Language.PureScript.Errors where
1818

1919
import Data.Either (lefts, rights)
20+
import Data.String (IsString(..))
2021
import Data.List (intersperse, intercalate)
2122
import Data.Monoid
2223

23-
import Control.Monad.Error
24+
import Control.Monad.Except
2425
import Control.Applicative ((<$>))
2526

2627
import Language.PureScript.AST
@@ -73,9 +74,14 @@ instance Monoid ErrorStack where
7374
mappend (MultipleErrors es) x = MultipleErrors [ e <> x | e <- es ]
7475
mappend x (MultipleErrors es) = MultipleErrors [ x <> e | e <- es ]
7576

76-
instance Error ErrorStack where
77-
strMsg s = ErrorStack [CompileError s Nothing Nothing]
78-
noMsg = ErrorStack []
77+
-- TODO: Remove strMsg, the IsString instance, and unnecessary
78+
-- OverloadedStrings pragmas. See #745
79+
-- | Create an ErrorStack from a string
80+
strMsg :: String -> ErrorStack
81+
strMsg s = ErrorStack [CompileError s Nothing Nothing]
82+
83+
instance IsString ErrorStack where
84+
fromString = strMsg
7985

8086
prettyPrintErrorStack :: Bool -> ErrorStack -> String
8187
prettyPrintErrorStack printFullStack (ErrorStack es) =

src/Language/PureScript/ModuleDependencies.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module Language.PureScript.ModuleDependencies (
1919
ModuleGraph
2020
) where
2121

22-
import Control.Monad.Error.Class
22+
import Control.Monad.Except
2323

2424
import Data.Graph
2525
import Data.List (nub)

src/Language/PureScript/Sugar/CaseDeclarations.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import Data.List (nub, groupBy)
2424

2525
import Control.Applicative
2626
import Control.Monad ((<=<), forM, join, unless, replicateM)
27-
import Control.Monad.Error.Class
27+
import Control.Monad.Except (throwError)
2828

2929
import Language.PureScript.Names
3030
import Language.PureScript.AST

src/Language/PureScript/Sugar/Names.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Data.Maybe (fromMaybe, isJust, mapMaybe)
2121
import Data.Monoid ((<>))
2222

2323
import Control.Applicative (Applicative(..), (<$>), (<*>))
24-
import Control.Monad.Error
24+
import Control.Monad.Except
2525

2626
import qualified Data.Map as M
2727

0 commit comments

Comments
 (0)