2module Type.Constrain.Expression where
4import qualified Data.List as List
5import qualified Data.Map as Map
6import qualified Data.Set as Set
7import Control.Applicative ((<$>))
8import qualified Control.Monad as Monad
9import Control.Monad.Error
10import qualified Text.PrettyPrint as PP
12import SourceSyntax.Location as Loc
13import SourceSyntax.Pattern (Pattern(PVar), boundVars)
14import SourceSyntax.Expression
15import qualified SourceSyntax.Type as SrcT
16import Type.Type hiding (Descriptor(..))
18import qualified Type.Environment as Env
19import qualified Type.Constrain.Literal as Literal
20import qualified Type.Constrain.Pattern as Pattern
22constrain :: Env.Environment -> LExpr -> Type -> ErrorT [PP.Doc] IO TypeConstraint
23constrain env (L span expr) tipe =
24 let list t = Env.get env Env.types "_List" <| t
27 t1 === t2 = L span (CEqual t1 t2)
28 x <? t = L span (CInstance x t)
29 clet schemes c = L span (CLet schemes c)
32 Literal lit -> liftIO $ Literal.constrain env span lit tipe
34 Var name | name == saveEnvName -> return (L span CSaveEnv)
35 | otherwise -> return (name <? tipe)
39 clo <- constrain env lo x
40 chi <- constrain env hi x
41 return $ and [clo, chi, list x === tipe]
45 cs <- mapM (\e -> constrain env e x) exprs
46 return . and $ list x === tipe : cs
51 c1 <- constrain env e1 t1
52 c2 <- constrain env e2 t2
53 return $ and [ c1, c2, op <? (t1 ==> t2 ==> tipe) ]
58 fragment <- try span $ Pattern.constrain env p t1
59 c2 <- constrain env e t2
60 let c = ex (vars fragment) (clet [monoscheme (typeEnv fragment)]
61 (typeConstraint fragment /\ c2 ))
62 return $ c /\ tipe === (t1 ==> t2)
66 c1 <- constrain env e1 (t ==> tipe)
67 c2 <- constrain env e2 t
70 MultiIf branches -> and <$> mapM constrain' branches
72 bool = Env.get env Env.types "Bool"
74 cb <- constrain env b bool
75 ce <- constrain env e tipe
80 ce <- constrain env exp t
82 fragment <- try span $ Pattern.constrain env p t
83 clet [toScheme fragment] <$> constrain env e tipe
84 and . (:) ce <$> mapM branch branches
87 do vars <- forM exprs $ \_ -> liftIO (var Flexible)
88 let pairs = zip exprs (map VarN vars)
89 (ctipe, cs) <- Monad.foldM step (tipe,true) (reverse pairs)
90 return $ ex vars (cs /\ name <? ctipe)
93 c' <- constrain env e x
94 return (x ==> t, c /\ c')
98 constrain env e (record (Map.singleton label [tipe]) t)
102 constrain env e (record (Map.singleton label [t]) tipe)
104 Insert e label value ->
107 cVal <- constrain env value tVal
108 cRec <- constrain env e tRec
109 let c = tipe === record (Map.singleton label [tVal]) tRec
110 return (and [cVal, cRec, c])
114 oldVars <- forM fields $ \_ -> liftIO (var Flexible)
115 let oldFields = SrcT.fieldMap (zip (map fst fields) (map VarN oldVars))
116 cOld <- ex oldVars <$> constrain env e (record oldFields t)
118 newVars <- forM fields $ \_ -> liftIO (var Flexible)
119 let newFields = SrcT.fieldMap (zip (map fst fields) (map VarN newVars))
120 let cNew = tipe === record newFields t
122 cs <- zipWithM (constrain env) (map snd fields) (map VarN newVars)
124 return $ cOld /\ ex newVars (and (cNew : cs))
127 do vars <- forM fields $ \_ -> liftIO (var Flexible)
128 cs <- zipWithM (constrain env) (map snd fields) (map VarN vars)
129 let fields' = SrcT.fieldMap (zip (map fst fields) (map VarN vars))
130 recordType = record fields' (TermN EmptyRecord1)
131 return . ex vars . and $ tipe === recordType : cs
134 do vars <- forM es $ \_ -> liftIO (var Flexible)
135 let tvars = map VarN vars
136 cs <- zipWithM (constrain env) es tvars
137 return . ex vars $ and ("Text.markdown" <? tipe : cs)
140 do c <- constrain env body tipe
141 (schemes, rqs, fqs, header, c2, c1) <-
142 Monad.foldM (constrainDef env)
143 ([], [], [], Map.empty, true, true)
144 (concatMap expandPattern defs)
145 return $ clet schemes
146 (clet [Scheme rqs fqs (clet [monoscheme header] c2) header ]
149 PortIn _ _ -> return true
151 PortOut _ _ signal ->
152 constrain env signal tipe
154constrainDef env info (Definition pattern expr maybeTipe) =
155 let qs = [] -- should come from the def, but I'm not sure what would live there...
156 (schemes, rigidQuantifiers, flexibleQuantifiers, headers, c2, c1) = info
158 do rigidVars <- forM qs (\_ -> liftIO $ var Rigid) -- Some mistake may be happening here.
159 -- Currently, qs is always [].
160 case (pattern, maybeTipe) of
161 (PVar name, Just tipe) -> do
162 flexiVars <- forM qs (\_ -> liftIO $ var Flexible)
163 let inserts = zipWith (\arg typ -> Map.insert arg (VarN typ)) qs flexiVars
164 env' = env { Env.value = List.foldl' (\x f -> f x) (Env.value env) inserts }
165 (vars, typ) <- Env.instantiateType env tipe Map.empty
166 let scheme = Scheme { rigidQuantifiers = [],
167 flexibleQuantifiers = flexiVars ++ vars,
168 constraint = Loc.noneNoDocs CTrue,
169 header = Map.singleton name typ }
170 c <- constrain env' expr typ
171 return ( scheme : schemes
173 , flexibleQuantifiers
176 , fl rigidVars c /\ c1 )
178 (PVar name, Nothing) -> do
179 v <- liftIO $ var Flexible
181 inserts = zipWith (\arg typ -> Map.insert arg (VarN typ)) qs rigidVars
182 env' = env { Env.value = List.foldl' (\x f -> f x) (Env.value env) inserts }
183 c <- constrain env' expr tipe
185 , rigidVars ++ rigidQuantifiers
186 , v : flexibleQuantifiers
187 , Map.insert name tipe headers
191 _ -> error (show pattern)
193expandPattern :: Def -> [Def]
194expandPattern def@(Definition pattern lexpr@(L s _) maybeType) =
197 _ -> Definition (PVar x) lexpr maybeType : map toDef vars
199 vars = Set.toList $ boundVars pattern
200 x = "$" ++ concat vars
202 toDef y = Definition (PVar y) (L s $ Case (mkVar x) [(pattern, mkVar y)]) Nothing
204try :: SrcSpan -> ErrorT (SrcSpan -> PP.Doc) IO a -> ErrorT [PP.Doc] IO a
205try span computation = do
206 result <- liftIO $ runErrorT computation
208 Left err -> throwError [err span]
209 Right value -> return value
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