1module Type.Constrain.Pattern where
3import Control.Arrow (second)
4import Control.Applicative ((<$>))
5import qualified Control.Monad as Monad
6import Control.Monad.Error
7import qualified Data.List as List
8import qualified Data.Maybe as Maybe
9import qualified Data.Map as Map
11import SourceSyntax.Pattern
12import SourceSyntax.PrettyPrint
13import Text.PrettyPrint (render)
14import qualified SourceSyntax.Location as Loc
17import Type.Environment as Env
18import qualified Type.Constrain.Literal as Literal
21constrain :: Environment -> Pattern -> Type -> ErrorT String IO Fragment
22constrain env pattern tipe =
23 let span = Loc.NoSpan (render $ pretty pattern)
24 t1 === t2 = Loc.L span (CEqual t1 t2)
25 x <? t = Loc.L span (CInstance x t)
28 PAnything -> return emptyFragment
31 c <- liftIO $ Literal.constrain env span lit tipe
32 return $ emptyFragment { typeConstraint = c }
35 v <- liftIO $ var Flexible
37 typeEnv = Map.singleton name (VarN v),
39 typeConstraint = VarN v === tipe
43 fragment <- constrain env p tipe
45 typeEnv = Map.insert name tipe (typeEnv fragment),
46 typeConstraint = name <? tipe /\ typeConstraint fragment
49 PData name patterns -> do
50 (kind, cvars, args, result) <- liftIO $ freshDataScheme env name
51 let msg = concat [ "Constructor '", name, "' expects ", show kind
52 , " argument", if kind == 1 then "" else "s"
53 , " but was given ", show (length patterns), "." ]
54 if length patterns /= kind then throwError msg else do
55 fragment <- Monad.liftM joinFragments (Monad.zipWithM (constrain env) patterns args)
57 typeConstraint = typeConstraint fragment /\ tipe === result,
58 vars = cvars ++ vars fragment
62 pairs <- liftIO $ mapM (\name -> (,) name <$> var Flexible) fields
63 let tenv = Map.fromList (map (second VarN) pairs)
64 c <- liftIO . exists $ \t -> return (tipe === record (Map.map (:[]) tenv) 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