3import qualified Data.Char as Char
4import qualified Data.List as List
5import qualified Data.Map as Map
6import qualified Data.UnionFind.IO as UF
8import Text.PrettyPrint as P
10import Control.Applicative ((<$>),(<*>))
11import Control.Monad.State (StateT, get, put, execStateT, runStateT)
12import Control.Monad.Error (ErrorT, Error, liftIO)
13import Data.Traversable (traverse)
15import qualified AST.PrettyPrint as PP
16import qualified AST.Type as T
17import qualified AST.Variable as Var
22type Type = TermN Variable
24type Variable = UF.Point Descriptor
26type TypeConstraint = Constraint Type Variable
28type TypeScheme = Scheme Type Variable
38 | Record1 (Map.Map String [a]) a
42 = VarN (Maybe Var.Canonical) a
43 | TermN (Maybe Var.Canonical) (Term1 (TermN a))
51termN :: (Term1 (TermN a)) -> TermN a
56record :: Map.Map String [TermN a] -> TermN a -> TermN a
58 termN (Record1 fs rec)
63data Descriptor = Descriptor
64 { structure :: Maybe (Term1 Variable)
67 , name :: Maybe TypeName
68 , copy :: Maybe Variable
70 , alias :: Maybe Var.Canonical
102 Located (BasicConstraint a b)
104data BasicConstraint a b
108 | CAnd [Constraint a b]
109 | CLet [Scheme a b] (Constraint a b)
110 | CInstance SchemeName a
112type SchemeName = String
114type TypeName = Var.Canonical
116data Scheme a b = Scheme
117 { rigidQuantifiers :: [b]
118 , flexibleQuantifiers :: [b]
119 , constraint :: Constraint a b
120 , header :: Map.Map String a
128(==>) :: Type -> Type -> Type
133(<|) :: TermN a -> TermN a -> TermN a
140namedVar :: Flex -> Var.Canonical -> IO Variable
141namedVar flex name = UF.fresh $ Descriptor
142 { structure = Nothing
152variable :: Flex -> IO Variable
153variable flex = UF.fresh $ Descriptor
154 { structure = Nothing
166monoscheme :: Map.Map String a -> Scheme a b
168 Scheme [] [] (noneNoDocs CTrue) headers
173(/\) :: Constraint a b -> Constraint a b -> Constraint a b
174(/\) a@(A _ c1) b@(A _ c2) =
178 _ -> mergeOldDocs a b (CAnd [a,b])
181-- ex qs constraint == exists qs. constraint
182ex :: [Variable] -> TypeConstraint -> TypeConstraint
183ex fqs constraint@(A ann _) =
184 A ann $ CLet [Scheme [] fqs constraint Map.empty] (A ann CTrue)
187-- fl qs constraint == forall qs. constraint
188fl :: [Variable] -> TypeConstraint -> TypeConstraint
189fl rqs constraint@(A ann _) =
190 A ann $ CLet [Scheme rqs [] constraint Map.empty] (A ann CTrue)
193exists :: Error e => (Type -> ErrorT e IO TypeConstraint) -> ErrorT e IO TypeConstraint
195 do v <- liftIO $ variable Flexible
196 ex [v] <$> f (varN v)
199existsNumber :: Error e => (Type -> ErrorT e IO TypeConstraint) -> ErrorT e IO TypeConstraint
201 do v <- liftIO $ variable (Is Number)
202 ex [v] <$> f (varN v)
205-- TYPES TO PRETTY STRINGS
207instance PrettyType a => PrettyType (UF.Point a) where
209 unsafePerformIO $ fmap (pretty when) (UF.descriptor point)
212instance PrettyType t => PrettyType (Annotated a t) where
213 pretty when (A _ e) =
217instance PrettyType a => PrettyType (Term1 a) where
219 let prty = pretty Never in
222 parensIf needed (px <+> pretty App x)
231 parensIf needed (pretty Fn arg <+> P.text "->" <+> prty body)
244 Record1 fields ext ->
245 P.braces (extend <+> commaSep prettyFields)
250 | P.render prettyExt == "{}" =
253 prettyExt <+> P.text "|"
256 P.text f <+> P.text ":" <+> prty t
259 concatMap (\(f,ts) -> map (mkPretty f) ts) (Map.toList fields)
262instance PrettyType a => PrettyType (TermN a) where
266 either alias (pretty when x)
269 either alias (pretty when t1)
271 either maybeAlias doc =
273 Just alias -> PP.pretty alias
277instance PrettyType Descriptor where
279 case (alias desc, structure desc, name desc) of
287 | Var.isTuple name ->
288 P.parens . P.text $ replicate (read (drop 6 (Var.toString name)) - 1) ','
290 P.text (Var.toString name)
295instance (PrettyType a, PrettyType b) => PrettyType (BasicConstraint a b) where
296 pretty _ constraint =
297 let prty = pretty Never in
299 CTrue -> P.text "True"
300 CSaveEnv -> P.text "SaveTheEnvironment!!!"
301 CEqual a b -> prty a <+> P.text "=" <+> prty b
302 CAnd [] -> P.text "True"
305 P.parens . P.sep $ P.punctuate (P.text " and") (map (pretty Never) cs)
307 CLet [Scheme [] fqs constraint header] (A _ CTrue) | Map.null header ->
308 P.sep [ binder, pretty Never c ]
310 mergeExists vs (A _ c) =
312 CLet [Scheme [] fqs' c' _] (A _ CTrue) -> mergeExists (vs ++ fqs') c'
315 (fqs', c) = mergeExists fqs constraint
317 binder = if null fqs' then P.empty else
318 P.text "\x2203" <+> P.hsep (map (pretty Never) fqs') <> P.text "."
320 CLet schemes constraint ->
321 P.fsep [ P.hang (P.text "let") 4 (P.brackets . commaSep $ map (pretty Never) schemes)
322 , P.text "in", pretty Never constraint ]
324 CInstance name tipe ->
325 P.text name <+> P.text "<" <+> prty tipe
328instance (PrettyType a, PrettyType b) => PrettyType (Scheme a b) where
329 pretty _ (Scheme rqs fqs (A _ constraint) headers) =
330 P.sep [ forall, cs, headers' ]
334 forall = if null rqs && null fqs then P.empty else
335 P.text "\x2200" <+> frees <+> rigids
337 frees = P.hsep $ map prty fqs
338 rigids = if null rqs then P.empty else P.braces . P.hsep $ map prty rqs
340 cs = case constraint of
343 _ -> P.brackets (pretty Never constraint)
345 headers' = if Map.size headers > 0 then dict else P.empty
346 dict = P.parens . commaSep . map prettyPair $ Map.toList headers
347 prettyPair (n,t) = P.text n <+> P.text ":" <+> pretty Never t
350extraPretty :: (PrettyType t, Crawl t) => t -> IO Doc
352 pretty Never <$> addNames t
355-- ADDING NAMES TO TYPES
357addNames :: (Crawl t) => t -> IO t
359 do (rawVars, _, _, _) <- execStateT (crawl getNames value) ([], 0, 0, 0)
361 let vars = map head . List.group $ List.sort rawVars
363 let suffix s = map (++s) (map (:[]) ['a'..'z'])
365 let allVars = concatMap suffix $ ["","'","_"] ++ map show [ (0 :: Int) .. ]
367 let okayVars = filter (`notElem` vars) allVars
369 runStateT (crawl rename value) (okayVars, 0, 0, 0)
373 getNames desc state@(vars, a, b, c) =
374 let name' = name desc in
376 Just (Var.Canonical _ var) -> (name', (var:vars, a, b, c))
379 rename desc state@(vars, a, b, c) =
381 Just var -> (Just var, state)
384 Is Number -> (local $ "number" ++ replicate a '\'', (vars, a+1, b, c))
385 Is Comparable -> (local $ "comparable" ++ replicate b '\'', (vars, a, b+1, c))
386 Is Appendable -> (local $ "appendable" ++ replicate c '\'', (vars, a, b, c+1))
387 _ -> (local $ head vars, (tail vars, a, b, c))
389 local v = Just (Var.local v)
392-- CRAWLING OVER TYPES
394type CrawlState = ([String], Int, Int, Int)
397-- Code for traversing all the type data-structures and giving
398-- names to the variables embedded deep in there.
400 crawl :: (Descriptor -> CrawlState -> (Maybe TypeName, CrawlState))
402 -> StateT CrawlState IO t
405instance Crawl e => Crawl (Annotated a e) where
406 crawl nextState (A ann e) =
407 A ann <$> crawl nextState e
410instance (Crawl t, Crawl v) => Crawl (BasicConstraint t v) where
411 crawl nextState constraint =
412 let rnm = crawl nextState in
414 CTrue -> return CTrue
415 CSaveEnv -> return CSaveEnv
416 CEqual a b -> CEqual <$> rnm a <*> rnm b
417 CAnd cs -> CAnd <$> crawl nextState cs
418 CLet schemes c -> CLet <$> crawl nextState schemes <*> crawl nextState c
419 CInstance name tipe -> CInstance name <$> rnm tipe
422instance Crawl a => Crawl [a] where
423 crawl nextState list =
424 mapM (crawl nextState) list
427instance (Crawl t, Crawl v) => Crawl (Scheme t v) where
428 crawl nextState (Scheme rqs fqs c headers) =
429 let rnm = crawl nextState
434 <*> crawl nextState c
438instance Crawl t => Crawl (TermN t) where
439 crawl nextState tipe =
442 VarN a <$> crawl nextState x
445 TermN a <$> crawl nextState term
448instance Crawl t => Crawl (Term1 t) where
449 crawl nextState term =
450 let rnm = crawl nextState in
453 App1 <$> rnm a <*> rnm b
456 Fun1 <$> rnm a <*> rnm b
464 Record1 fields ext ->
466 <$> traverse (mapM rnm) fields
470instance Crawl a => Crawl (UF.Point a) where
471 crawl nextState point =
472 do desc <- liftIO $ UF.descriptor point
473 desc' <- crawl nextState desc
474 liftIO $ UF.setDescriptor point desc'
478instance Crawl Descriptor where
479 crawl nextState desc =
481 let (name', state') = nextState desc state
482 structure' <- traverse (crawl nextState) (structure desc)
484 return $ desc { name = name', structure = structure' }
487toSrcType :: Variable -> IO T.CanonicalType
492 do desc <- UF.descriptor v
493 srcType <- maybe (backupSrcType desc) termToSrcType (structure desc)
494 return $ maybe srcType (\name -> T.Aliased name srcType) (alias desc)
496 backupSrcType :: Descriptor -> IO T.CanonicalType
499 Just v@(Var.Canonical _ x@(c:_))
508 [ "Problem converting the following type "
509 , "from a type-checker type to a source-syntax type:"
510 , P.render (pretty Never var)
513 termToSrcType :: Term1 Variable -> IO T.CanonicalType
521 return $ T.App f (args ++ [b'])
523 return $ T.App a' [b']
526 T.Lambda <$> go a <*> go b
532 return $ T.Record [] Nothing
534 Record1 tfields extension ->
535 do fields' <- traverse (mapM go) tfields
536 let fields = concat [ map ((,) name) ts | (name,ts) <- Map.toList fields' ]
537 ext' <- dealias <$> go extension
540 T.Record fs ext -> T.Record (fs ++ fields) ext
541 T.Var _ -> T.Record fields (Just ext')
542 _ -> error "Used toSrcType on a type that is not well-formed"
544 dealias :: T.CanonicalType -> T.CanonicalType
557collectApps :: Variable -> IO AppStructure
562 desc <- UF.descriptor var
563 case (structure desc, vars) of
566 Just n | Var.isList n -> return (List v)
571 Just n | Var.isTuple n -> return (Tuple vs)
576 App1 a b -> go (vars ++ [b]) a
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