@@ -10,7 +10,7 @@ import qualified Data.Map as Map
10import qualified Data.Set as Set
10import qualified Data.Set as Set
13−import Context
13+import Located
14import Guid
14import Guid
16import Types.Types
16import Types.Types
@@ -62,7 +62,7 @@ mergeSchemes schmss = do (ass,css,sss) <- unzip3 `liftM` mapM split kvs
62 kvs = Map.toList $ Map.unionsWith (++) (map (Map.map (:[])) schmss)
62 kvs = Map.toList $ Map.unionsWith (++) (map (Map.map (:[])) schmss)
63 split (k,vs) =
63 split (k,vs) =
64 let ps = zipWith (\s v -> (s++k,v)) (map (flip replicate '_') [0..]) vs
64 let ps = zipWith (\s v -> (s++k,v)) (map (flip replicate '_') [0..]) vs
65− eq t u = C (Just $ msg ++ k) NoSpan (VarT t :=: VarT u)
65+ eq t u = L (Just $ msg ++ k) NoSpan (VarT t :=: VarT u)
66 msg = "the definition of "
66 msg = "the definition of "
67 in do xs <- mapM (\_ -> guid) vs
67 in do xs <- mapM (\_ -> guid) vs
68 return ( Map.fromList $ zip (map fst ps) (map (:[]) xs)
68 return ( Map.fromList $ zip (map fst ps) (map (:[]) xs)
@@ -81,7 +81,7 @@ constrain typeHints (Module _ _ imports stmts) = do
81 insert as n = do v <- guid; return $ Map.insertWith' (\_ x -> x) n [v] as
81 insert as n = do v <- guid; return $ Map.insertWith' (\_ x -> x) n [v] as
82 assumptions <- foldM insert as (Map.keys schemes)
82 assumptions <- foldM insert as (Map.keys schemes)
83 findAmbiguous (map fst aliasHints) assumptions $ do
83 findAmbiguous (map fst aliasHints) assumptions $ do
84− let f k s vs = map (\v -> C (Just k) NoSpan $ v :<<: s) vs
84+ let f k s vs = map (\v -> L (Just k) NoSpan $ v :<<: s) vs
85 cs = concat . Map.elems $ Map.intersectionWithKey f allHints assumptions
85 cs = concat . Map.elems $ Map.intersectionWithKey f allHints assumptions
86 escapees = Map.keys $ Map.difference assumptions allHints
86 escapees = Map.keys $ Map.difference assumptions allHints
87 msg = "Warning! Type-checker could not find variables:\n" ++ intercalate ", " escapees
87 msg = "Warning! Type-checker could not find variables:\n" ++ intercalate ", " escapees
@@ -91,13 +91,13 @@ constrain typeHints (Module _ _ imports stmts) = do
91 --_ -> Left ("Undefined variable(s): " ++ intercalate ", " escapees)
91 --_ -> Left ("Undefined variable(s): " ++ intercalate ", " escapees)
93type TVarMap = Map.Map String [X]
93type TVarMap = Map.Map String [X]
94−type ConstraintSet = Set.Set (Context Constraint)
94+type ConstraintSet = Set.Set (Located Constraint)
96−ctx e span = C (Just $ show e) span
96+loc e span = L (Just $ show e) span
98gen :: CExpr -> GuidCounter (TVarMap, ConstraintSet, Type)
98gen :: CExpr -> GuidCounter (TVarMap, ConstraintSet, Type)
99−gen (C _ span expr) =
99+gen (L _ span expr) =
100− let ctx' = C (Just $ show expr) span in
100+ let loc' = L (Just $ show expr) span in
101 case expr of
101 case expr of
103 do b <- guid
103 do b <- guid
@@ -109,7 +109,7 @@ gen (C _ span expr) =
109 b <- beta
109 b <- beta
110 return ( unionA a1 a2
110 return ( unionA a1 a2
111 , Set.unions [c1,c2
111 , Set.unions [c1,c2
112− ,Set.singleton . ctx' $ t1 :=: (LambdaT t2 b)]
112+ ,Set.singleton . loc' $ t1 :=: (LambdaT t2 b)]
115 Lambda x e ->
115 Lambda x e ->
@@ -118,7 +118,7 @@ gen (C _ span expr) =
118 v <- guid
118 v <- guid
119 return ( Map.delete x a
119 return ( Map.delete x a
120 , Set.union c . Set.fromList .
120 , Set.union c . Set.fromList .
121− map (\x -> ctx' $ VarT x :=: b) $
121+ map (\x -> loc' $ VarT x :=: b) $
122 Map.findWithDefault [v] x a
122 Map.findWithDefault [v] x a
123 , LambdaT b t )
123 , LambdaT b t )
@@ -132,7 +132,7 @@ gen (C _ span expr) =
132 genCs name s = do
132 genCs name s = do
133 v <- guid
133 v <- guid
134 let vs = Map.findWithDefault [v] name assumptions
134 let vs = Map.findWithDefault [v] name assumptions
135− return $ map (\x -> ctx name span $ x :<<: s) vs
135+ return $ map (\x -> loc name span $ x :<<: s) vs
136 cs' <- zipWithM genCs names schemes
136 cs' <- zipWithM genCs names schemes
137 return ( foldr Map.delete assumptions names
137 return ( foldr Map.delete assumptions names
138 , Set.union (Set.fromList . concat $ cs') cs
138 , Set.union (Set.fromList . concat $ cs') cs
@@ -143,9 +143,9 @@ gen (C _ span expr) =
143 (ass,css,ts) <- liftM unzip3 $ mapM (caseGen t) cases
143 (ass,css,ts) <- liftM unzip3 $ mapM (caseGen t) cases
144 return ( unionsA $ as:ass
144 return ( unionsA $ as:ass
145 , let cases' = map snd cases
145 , let cases' = map snd cases
146− ctxs = zipWith epos cases' (tail cases')
146+ locs = zipWith epos cases' (tail cases')
147 csts = zipWith (:=:) ts (tail ts)
147 csts = zipWith (:=:) ts (tail ts)
148− cs' = Set.fromList (zipWith ($) ctxs csts)
148+ cs' = Set.fromList (zipWith ($) locs csts)
149 in Set.unions $ cs' : cs : css
149 in Set.unions $ cs' : cs : css
150 , head ts)
150 , head ts)
@@ -154,30 +154,30 @@ gen (C _ span expr) =
154 (a2,c2,t2) <- gen e2
154 (a2,c2,t2) <- gen e2
155 (a3,c3,t3) <- gen e3
155 (a3,c3,t3) <- gen e3
156 return ( unionsA [a1,a2,a3]
156 return ( unionsA [a1,a2,a3]
157− , let c4 = Set.fromList [ ctx e1 span (t1 :=: bool)
157+ , let c4 = Set.fromList [ loc e1 span (t1 :=: bool)
158− , ctx' (t2 :=: t3) ]
158+ , loc' (t2 :=: t3) ]
159 in Set.unions [c1,c2,c3,c4]
159 in Set.unions [c1,c2,c3,c4]
162 Data name es ->
162 Data name es ->
163− gen $ foldl' (\f x -> epos f x $ App f x) (ctx' $ Var name) es
163+ gen $ foldl' (\f x -> epos f x $ App f x) (loc' $ Var name) es
165 Binop op e1 e2 ->
165 Binop op e1 e2 ->
166− gen $ ctx' (App (ctx' $ App (ctx' $ Var op) e1) e2)
166+ gen $ loc' (App (loc' $ App (loc' $ Var op) e1) e2)
168 Access e label ->
168 Access e label ->
169 do (as,cs,rtype) <- gen e
169 do (as,cs,rtype) <- gen e
170 t <- beta
170 t <- beta
171 rtype' <- beta
171 rtype' <- beta
172 let fs = Map.singleton label [t]
172 let fs = Map.singleton label [t]
173− c = (ctx' (RecordT fs rtype' :=: rtype))
173+ c = (loc' (RecordT fs rtype' :=: rtype))
174 return (as, Set.insert c cs, t)
174 return (as, Set.insert c cs, t)
176 Remove e x ->
176 Remove e x ->
177 do (as,cs,rtype) <- gen e
177 do (as,cs,rtype) <- gen e
178 t <- beta
178 t <- beta
179 rtype' <- beta
179 rtype' <- beta
180− let c = (ctx' (RecordT (Map.singleton x [t]) rtype' :=: rtype))
180+ let c = (loc' (RecordT (Map.singleton x [t]) rtype' :=: rtype))
181 return (as, Set.insert c cs, rtype')
181 return (as, Set.insert c cs, rtype')
183 Insert e x v ->
183 Insert e x v ->
@@ -193,7 +193,7 @@ gen (C _ span expr) =
193 oldTs <- mapM (\_ -> beta) fields
193 oldTs <- mapM (\_ -> beta) fields
194 rtype' <- beta
194 rtype' <- beta
195 let rT ts = RecordT (recordT (zip (map fst fields) ts)) rtype'
195 let rT ts = RecordT (recordT (zip (map fst fields) ts)) rtype'
196− c = Set.singleton (ctx' (rtype :=: rT oldTs))
196+ c = Set.singleton (loc' (rtype :=: rT oldTs))
197 return ( unionsA (ras:ass), Set.unions (c : rcs : css), rT newTs )
197 return ( unionsA (ras:ass), Set.unions (c : rcs : css), rT newTs )
199 Record fields ->
199 Record fields ->
@@ -206,27 +206,27 @@ gen (C _ span expr) =
206 , Set.fromList (concat css)
206 , Set.fromList (concat css)
207 , RecordT (foldr ($) Map.empty fs) EmptyRecord )
207 , RecordT (foldr ($) Map.empty fs) EmptyRecord )
209− Range e1@(C w1 s1 _) e2@(C w2 s2 _) ->
209+ Range e1@(L w1 s1 _) e2@(L w2 s2 _) ->
210 do (a1,c1,t1) <- gen e1
210 do (a1,c1,t1) <- gen e1
211 (a2,c2,t2) <- gen e2
211 (a2,c2,t2) <- gen e2
212 return ( unionsA [a1,a2]
212 return ( unionsA [a1,a2]
213− , Set.unions [ c1, c2, Set.fromList [ C w1 s1 (t1 :=: int)
213+ , Set.unions [ c1, c2, Set.fromList [ L w1 s1 (t1 :=: int)
214− , C w1 s2 (t2 :=: int) ] ]
214+ , L w1 s2 (t2 :=: int) ] ]
215 , listOf int )
215 , listOf int )
217 MultiIf ps -> do (ass,css,t:ts) <- unzip3 `liftM` mapM genPair ps
217 MultiIf ps -> do (ass,css,t:ts) <- unzip3 `liftM` mapM genPair ps
218− let cs = Set.fromList (map (ctx' . (t :=:)) ts)
218+ let cs = Set.fromList (map (loc' . (t :=:)) ts)
219 return (unionsA ass, Set.unions (cs:css), t)
219 return (unionsA ass, Set.unions (cs:css), t)
220− where genPair (b@(C t s _),e) = do
220+ where genPair (b@(L t s _),e) = do
221 (a1,c1,t1) <- gen b
221 (a1,c1,t1) <- gen b
222 (a2,c2,t2) <- gen e
222 (a2,c2,t2) <- gen e
223 return ( unionsA [a1,a2]
223 return ( unionsA [a1,a2]
224 , Set.unions [ c1, c2
224 , Set.unions [ c1, c2
225− , Set.singleton (C t s (t1 :=: bool)) ]
225+ , Set.singleton (L t s (t1 :=: bool)) ]
228 IntNum _ -> do t <- beta
228 IntNum _ -> do t <- beta
229− return (Map.empty, Set.singleton (ctx' $ t :<: number), t)
229+ return (Map.empty, Set.singleton (loc' $ t :<: number), t)
231 FloatNum _ -> primitive float
231 FloatNum _ -> primitive float
232 Chr _ -> primitive char
232 Chr _ -> primitive char
@@ -241,41 +241,41 @@ primitive t = return (Map.empty, Set.empty, t)
241caseGen :: Type
241caseGen :: Type
242 -> (Pattern, CExpr)
242 -> (Pattern, CExpr)
243 -> GuidCounter (TVarMap, ConstraintSet, Type)
243 -> GuidCounter (TVarMap, ConstraintSet, Type)
244−caseGen tipe (p, ce@(C _ span e)) = do
244+caseGen tipe (p, ce@(L _ span e)) = do
245 (as ,cs ,t) <- gen ce
245 (as ,cs ,t) <- gen ce
246− (as',cs',_) <- patternGen (ctx p span) tipe as p
246+ (as',cs',_) <- patternGen (loc p span) tipe as p
247 return ( as', Set.union cs cs', t )
247 return ( as', Set.union cs cs', t )
249−patternGen :: (Constraint -> Context Constraint)
249+patternGen :: (Constraint -> Located Constraint)
250 -> Type -- Type of e in `case e of ...`
250 -> Type -- Type of e in `case e of ...`
251 -> TVarMap
251 -> TVarMap
252 -> Pattern
252 -> Pattern
253 -> GuidCounter (TVarMap, ConstraintSet, Type)
253 -> GuidCounter (TVarMap, ConstraintSet, Type)
254−patternGen ctxt tipe as pattern =
254+patternGen loc tipe as pattern =
255 case pattern of
255 case pattern of
256 PAnything -> do b <- beta ; return ( as, Set.empty, b )
256 PAnything -> do b <- beta ; return ( as, Set.empty, b )
257 PVar v -> do
257 PVar v -> do
258 b <- beta
258 b <- beta
259− let cs = map (ctxt . (b :=:) . VarT) (Map.findWithDefault [] v as)
259+ let cs = map (loc . (b :=:) . VarT) (Map.findWithDefault [] v as)
260− return ( Map.delete v as, Set.fromList (ctxt (b :=: tipe) : cs), b )
260+ return ( Map.delete v as, Set.fromList (loc (b :=: tipe) : cs), b )
261 PData name ps -> do
261 PData name ps -> do
262 constr <- guid
262 constr <- guid
263 output <- beta
263 output <- beta
264 let step (as,cs,tipe) p = do b <- beta
264 let step (as,cs,tipe) p = do b <- beta
265− (as',cs',t) <- patternGen ctxt b as p
265+ (as',cs',t) <- patternGen loc b as p
266 return (as', Set.union cs cs', t ==> tipe)
266 return (as', Set.union cs cs', t ==> tipe)
267 (as',cs, t) <- foldM step (as,Set.empty,tipe) (reverse ps)
267 (as',cs, t) <- foldM step (as,Set.empty,tipe) (reverse ps)
268 return ( Map.insert name [constr] as'
268 return ( Map.insert name [constr] as'
269− , Set.insert (ctxt (VarT constr :=: t)) cs
269+ , Set.insert (loc (VarT constr :=: t)) cs
270 , output )
270 , output )
271 PRecord fs ->
271 PRecord fs ->
272 do pairs <- mapM (\f -> do b <- beta; return (f,b)) fs
272 do pairs <- mapM (\f -> do b <- beta; return (f,b)) fs
273 b <- beta
273 b <- beta
274 let t = RecordT (Map.fromList $ map (second (:[])) pairs) b
274 let t = RecordT (Map.fromList $ map (second (:[])) pairs) b
275− mkCs (name,tipe) = map (ctxt . (tipe :=:) . VarT)
275+ mkCs (name,tipe) = map (loc . (tipe :=:) . VarT)
276 (Map.findWithDefault [] name as)
276 (Map.findWithDefault [] name as)
277 return ( foldr Map.delete as fs
277 return ( foldr Map.delete as fs
278− , Set.fromList (ctxt (t :=: tipe) : concatMap mkCs pairs)
278+ , Set.fromList (loc (t :=: tipe) : concatMap mkCs pairs)
@@ -295,7 +295,7 @@ defGenHelp name args e = do
295 genCs (arg,x) = do
295 genCs (arg,x) = do
296 v <- guid
296 v <- guid
297 let as' = Map.findWithDefault [v] arg as
297 let as' = Map.findWithDefault [v] arg as
298− return $ map (\y -> ctx arg NoSpan $ VarT x :=: VarT y) as'
298+ return $ map (\y -> loc arg NoSpan $ VarT x :=: VarT y) as'
299 cs' <- concat `liftM` mapM genCs argDict
299 cs' <- concat `liftM` mapM genCs argDict
300 scheme <- Subs.generalize (concat $ Map.elems as') $
300 scheme <- Subs.generalize (concat $ Map.elems as') $
301 Forall (map snd argDict) (cs' ++ Set.toList cs) tipe
301 Forall (map snd argDict) (cs' ++ Set.toList cs) tipe
@@ -316,13 +316,13 @@ stmtGen stmt =
316 ExportEvent js elm tipe ->
316 ExportEvent js elm tipe ->
317 do x <- guid
317 do x <- guid
318 return ( Map.singleton elm [x]
318 return ( Map.singleton elm [x]
319− , Set.singleton . ctx elm NoSpan $ VarT x :=: tipe
319+ , Set.singleton . loc elm NoSpan $ VarT x :=: tipe
320 , Map.empty )
320 , Map.empty )
322− ImportEvent js e@(C txt span base) elm tipe ->
322+ ImportEvent js e@(L txt span base) elm tipe ->
323 do (as,cs,t) <- gen e
323 do (as,cs,t) <- gen e
324 return ( as
324 return ( as
325− , Set.insert (C txt span (signalOf t :=: tipe)) cs
325+ , Set.insert (L txt span (signalOf t :=: tipe)) cs
326 , Map.singleton elm (Forall [] [] tipe) )
326 , Map.singleton elm (Forall [] [] tipe) )
328 TypeAnnotation name tipe ->
328 TypeAnnotation name tipe ->