1{-# OPTIONS_GHC -Wall #-}
2module Type.Constrain.Expression where
4import Control.Applicative ((<$>))
5import qualified Control.Monad as Monad
6import Control.Monad.Error
7import qualified Data.List as List
8import qualified Data.Map as Map
9import Prelude hiding (and)
10import qualified Text.PrettyPrint as PP
12import qualified AST.Literal as Lit
13import AST.Annotation as Ann
14import AST.Expression.General
15import qualified AST.Expression.Canonical as Canonical
16import qualified AST.Pattern as P
17import qualified AST.Type as ST
18import qualified AST.Variable as V
19import Type.Type hiding (Descriptor(..))
20import qualified Type.Fragment as Fragment
21import qualified Type.Environment as Env
22import qualified Type.Constrain.Literal as Literal
23import qualified Type.Constrain.Pattern as Pattern
30 -> ErrorT [PP.Doc] IO TypeConstraint
31constrain env (A region expression) tipe =
32 let list t = Env.get env Env.types "List" <| t
35 t1 === t2 = A region (CEqual t1 t2)
36 x <? t = A region (CInstance x t)
37 clet schemes c = A region (CLet schemes c)
41 liftIO $ Literal.constrain env region lit tipe
43 GLShader _uid _src gltipe ->
47 shaderTipe a u v = Env.get env Env.types "WebGL.Shader" <| a <| u <| v
48 glTipe = Env.get env Env.types . Lit.glTipeName
49 makeRec accessor baseRec =
50 let decls = accessor gltipe
51 in if Map.size decls == 0
53 else record (Map.map (\t -> [glTipe t]) decls) baseRec
54 attribute = makeRec Lit.attribute attr
55 uniform = makeRec Lit.uniform unif
56 varying = makeRec Lit.varying (termN EmptyRecord1)
58 return . A region $ CEqual tipe (shaderTipe attribute uniform varying)
61 | name == saveEnvName -> return (A region CSaveEnv)
62 | otherwise -> return (name <? tipe)
68 do clo <- constrain env lo n
69 chi <- constrain env hi n
70 return $ and [clo, chi, list n === tipe]
74 do constraints <- mapM (\e -> constrain env e x) exprs
75 return . and $ list x === tipe : constraints
80 do c1 <- constrain env e1 t1
81 c2 <- constrain env e2 t2
82 return $ and [ c1, c2, V.toString op <? (t1 ==> t2 ==> tipe) ]
87 do fragment <- try region $ Pattern.constrain env p t1
88 c2 <- constrain env e t2
89 let c = ex (Fragment.vars fragment)
90 (clet [monoscheme (Fragment.typeEnv fragment)]
91 (Fragment.typeConstraint fragment /\ c2)
93 return $ c /\ tipe === (t1 ==> t2)
97 do c1 <- constrain env e1 (t ==> tipe)
98 c2 <- constrain env e2 t
102 and <$> mapM constrain' branches
104 bool = Env.get env Env.types "Bool"
106 constrain' (cond, expr) =
107 do cb <- constrain env cond bool
108 ce <- constrain env expr tipe
111 Case expr branches ->
113 do ce <- constrain env expr t
114 and . (:) ce <$> mapM (branch t) branches
116 branch t (pattern, branchExpr) =
117 do fragment <- try region $ Pattern.constrain env pattern t
118 clet [Fragment.toScheme fragment] <$> constrain env branchExpr tipe
121 do vars <- forM exprs $ \_ -> liftIO (variable Flexible)
122 let pairs = zip exprs (map varN vars)
123 (ctipe, cs) <- Monad.foldM step (tipe,true) (reverse pairs)
124 return $ ex vars (cs /\ name <? ctipe)
127 do c' <- constrain env e x
128 return (x ==> t, c /\ c')
132 constrain env expr (record (Map.singleton label [tipe]) t)
136 constrain env expr (record (Map.singleton label [t]) tipe)
138 Insert expr label value ->
141 do cVal <- constrain env value tVal
142 cRec <- constrain env expr tRec
143 let c = tipe === record (Map.singleton label [tVal]) tRec
144 return (and [cVal, cRec, c])
146 Modify expr fields ->
148 do oldVars <- forM fields $ \_ -> liftIO (variable Flexible)
149 let oldFields = ST.fieldMap (zip (map fst fields) (map varN oldVars))
150 cOld <- ex oldVars <$> constrain env expr (record oldFields t)
152 newVars <- forM fields $ \_ -> liftIO (variable Flexible)
153 let newFields = ST.fieldMap (zip (map fst fields) (map varN newVars))
154 let cNew = tipe === record newFields t
156 cs <- zipWithM (constrain env) (map snd fields) (map varN newVars)
158 return $ cOld /\ ex newVars (and (cNew : cs))
161 do vars <- forM fields $ \_ -> liftIO (variable Flexible)
162 cs <- zipWithM (constrain env) (map snd fields) (map varN vars)
163 let fields' = ST.fieldMap (zip (map fst fields) (map varN vars))
164 let recordType = record fields' (termN EmptyRecord1)
165 return . ex vars . and $ tipe === recordType : cs
168 do c <- constrain env body tipe
170 (Info schemes rqs fqs headers c2 c1) <-
173 (Info [] [] [] Map.empty true true)
174 (concatMap expandPattern defs)
176 let letScheme = [ Scheme rqs fqs (clet [monoscheme headers] c2) headers ]
178 return $ clet schemes (clet letScheme (c1 /\ c))
184expandPattern :: Canonical.Def -> [Canonical.Def]
185expandPattern def@(Canonical.Definition pattern lexpr@(A r _) maybeType) =
190 Canonical.Definition (P.Var x) lexpr maybeType : map toDef vars
192 vars = P.boundVarList pattern
193 x = "$" ++ concat vars
194 mkVar = A r . localVar
195 toDef y = Canonical.Definition (P.Var y) (A r $ Case (mkVar x) [(pattern, mkVar y)]) Nothing
198try :: Region -> ErrorT (Region -> PP.Doc) IO a -> ErrorT [PP.Doc] IO a
199try region computation =
200 do result <- liftIO $ runErrorT computation
202 Left err -> throwError [err region]
203 Right value -> return value
206-- CONSTRAIN DEFINITIONS
209 { iSchemes :: [TypeScheme]
210 , iRigid :: [Variable]
211 , iFlex :: [Variable]
212 , iHeaders :: Map.Map String Type
213 , iC2 :: TypeConstraint
214 , iC1 :: TypeConstraint
218constrainDef :: Env.Environment -> Info -> Canonical.Def -> ErrorT [PP.Doc] IO Info
219constrainDef env info (Canonical.Definition pattern expr maybeTipe) =
220 let qs = [] -- should come from the def, but I'm not sure what would live there...
222 case (pattern, maybeTipe) of
223 (P.Var name, Just tipe) ->
224 constrainAnnotatedDef env info qs name expr tipe
226 (P.Var name, Nothing) ->
227 constrainUnannotatedDef env info qs name expr
229 _ -> error ("problem in constrainDef with " ++ show pattern)
239 -> ErrorT [PP.Doc] IO Info
240constrainAnnotatedDef env info qs name expr tipe =
241 do -- Some mistake may be happening here. Currently, qs is always [].
242 rigidVars <- forM qs (\_ -> liftIO $ variable Rigid)
244 flexiVars <- forM qs (\_ -> liftIO $ variable Flexible)
246 let inserts = zipWith (\arg typ -> Map.insert arg (varN typ)) qs flexiVars
248 let env' = env { Env.value = List.foldl' (\x f -> f x) (Env.value env) inserts }
250 (vars, typ) <- Env.instantiateType env tipe Map.empty
254 { rigidQuantifiers = []
255 , flexibleQuantifiers = flexiVars ++ vars
256 , constraint = Ann.noneNoDocs CTrue
257 , header = Map.singleton name typ
260 c <- constrain env' expr typ
263 { iSchemes = scheme : iSchemes info
264 , iC1 = fl rigidVars c /\ iC1 info
268constrainUnannotatedDef
274 -> ErrorT [PP.Doc] IO Info
275constrainUnannotatedDef env info qs name expr =
276 do -- Some mistake may be happening here. Currently, qs is always [].
277 rigidVars <- forM qs (\_ -> liftIO $ variable Rigid)
279 v <- liftIO $ variable Flexible
283 let inserts = zipWith (\arg typ -> Map.insert arg (varN typ)) qs rigidVars
285 let env' = env { Env.value = List.foldl' (\x f -> f x) (Env.value env) inserts }
287 c <- constrain env' expr tipe
290 { iRigid = rigidVars ++ iRigid info
291 , iFlex = v : iFlex info
292 , iHeaders = Map.insert name tipe (iHeaders info)
293 , iC2 = c /\ iC2 info
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