1-----------------------------------------------------------------------------
3-- Module : PureScript.TypeChecker.Synonyms
4-- Copyright : (c) Phil Freeman 2013
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
13-----------------------------------------------------------------------------
15{-# LANGUAGE Rank2Types #-}
17module PureScript.TypeChecker.Synonyms (
21import PureScript.Types
22import PureScript.Declarations
24import Data.Maybe (fromMaybe)
28import Control.Monad.Writer
29import Control.Monad.Error
30import qualified Data.Map as M
32replaceTypeVars :: M.Map String Type -> Type -> Type
33replaceTypeVars m = everywhere (mkT replace)
35 replace :: Type -> Type
36 replace t@(TypeVar var) = fromMaybe t (M.lookup var m)
39buildTypeSubstitution :: String -> Type -> [String] -> Type -> WriterT Any (Either String) (Maybe Type)
40buildTypeSubstitution name ty args = go (reverse args) M.empty
42 go :: [String] -> M.Map String Type -> Type -> WriterT Any (Either String) (Maybe Type)
43 go [] m (TypeConstructor ctor) | name == ctor = tell (Any True) >> return (Just $ replaceTypeVars m ty)
44 go (_:_) _ (TypeConstructor ctor) | name == ctor = lift . throwError $ "Partially applied type synonym " ++ name
45 go (arg:args) m (TypeApp f x) = go args (M.insert arg x m) f
46 go _ _ _ = return Nothing
48everywhereButM' :: (Monad m, Data d) => (forall d. (Data d) => d -> WriterT Any m d) -> d -> m d
49everywhereButM' f x = do
50 (y, halt) <- runWriterT $ f x
53 _ -> gmapM (everywhereButM' f) y
55substituteTypeSynonym :: String -> [String] -> Type -> Type -> Either String Type
56substituteTypeSynonym name args ty = everywhereButM' (mkM replace)
58 replace t = fmap (fromMaybe t) $ buildTypeSubstitution name ty args t
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list