3import qualified Data.List as List
4import qualified Data.Map as Map
5import qualified Data.UnionFind.IO as UF
7import Text.PrettyPrint as P
9import Control.Applicative ((<$>),(<*>))
10import Control.Monad.State
11import Data.Traversable (traverse)
12import SourceSyntax.Helpers (isTuple)
13import qualified SourceSyntax.Type as Src
20 | Record1 (Map.Map String [a]) a
25 | TermN (Term1 (TermN a))
28record fs rec = TermN (Record1 fs rec)
30type Type = TermN Variable
31type Variable = UF.Point Descriptor
33type SchemeName = String
40 | CAnd [Constraint a b]
41 | CLet [Scheme a b] (Constraint a b)
42 | CInstance SchemeName a
45data Scheme a b = Scheme {
46 rigidQuantifiers :: [b],
47 flexibleQuantifiers :: [b],
48 constraint :: Constraint a b,
49 header :: Map.Map String a
52type TypeConstraint = Constraint Type Variable
53type TypeScheme = Scheme Type Variable
55monoscheme headers = Scheme [] [] CTrue headers
57data Descriptor = Descriptor {
58 structure :: Maybe (Term1 Variable),
61 name :: Maybe TypeName,
62 copy :: Maybe Variable,
67outermostRank = 0 :: Int
72data Flex = Rigid | Flexible | Constant
77(/\) :: Constraint a b -> Constraint a b -> Constraint a b
82(===) :: Type -> Type -> TypeConstraint
85(<?) :: SchemeName -> Type -> TypeConstraint
89(==>) :: Type -> Type -> Type
90a ==> b = TermN (Fun1 a b)
92f <| a = TermN (App1 f a)
95namedVar flex name = UF.fresh $ Descriptor {
104flexibleVar = UF.fresh $ Descriptor {
113rigidVar = UF.fresh $ Descriptor {
122-- ex qs constraint == exists qs. constraint
123ex :: [Variable] -> TypeConstraint -> TypeConstraint
124ex fqs constraint = CLet [Scheme [] fqs constraint Map.empty] CTrue
126-- fl qs constraint == forall qs. constraint
127fl :: [Variable] -> TypeConstraint -> TypeConstraint
128fl rqs constraint = CLet [Scheme rqs [] constraint Map.empty] CTrue
130exists :: (Type -> IO TypeConstraint) -> IO TypeConstraint
133 ex [v] <$> f (VarN v)
136instance Show a => Show (UF.Point a) where
137 show point = unsafePerformIO $ fmap show (UF.descriptor point)
140instance PrettyType a => PrettyType (UF.Point a) where
141 pretty when point = unsafePerformIO $ fmap (pretty when) (UF.descriptor point)
144instance PrettyType a => PrettyType (Term1 a) where
146 let prty = pretty Never in
148 App1 f x | P.render px == "_List" -> P.brackets (pretty Never x)
149 | otherwise -> parensIf needed (px <+> pretty App x)
152 needed = case when of
157 parensIf needed (pretty Fn arg <+> P.text "->" <+> prty body)
159 needed = case when of
165 EmptyRecord1 -> P.braces P.empty
167 Record1 fields ext ->
168 P.braces (prty ext <+> P.text "|" <+> commaSep prettyFields)
170 mkPretty f t = P.text f <+> P.text ":" <+> prty t
171 prettyFields = concatMap (\(f,ts) -> map (mkPretty f) ts) (Map.toList fields)
174instance PrettyType a => PrettyType (TermN a) where
177 VarN x -> pretty when x
178 TermN t1 -> pretty when t1
181instance PrettyType Descriptor where
183 case (structure desc, name desc) of
184 (Just term, _) -> pretty when term
185 (_, Just name) -> if not (isTuple name) then P.text name else
186 P.parens . P.text $ replicate (read (drop 6 name) - 1) ','
190instance (PrettyType a, PrettyType b) => PrettyType (Constraint a b) where
191 pretty _ constraint =
192 let prty = pretty Never in
194 CTrue -> P.text "True"
195 CSaveEnv -> P.text "SaveTheEnvironment!!!"
196 CEqual a b -> prty a <+> P.text "=" <+> prty b
197 CAnd [] -> P.text "True"
200 P.parens . P.sep $ P.punctuate (P.text " and") (map (pretty Never) cs)
202 CLet [Scheme [] fqs constraint header] CTrue | Map.null header ->
203 P.sep [ binder, pretty Never c ]
207 CLet [Scheme [] fqs' c' _] CTrue -> mergeExists (vs ++ fqs') c'
210 (fqs', c) = mergeExists fqs constraint
212 binder = if null fqs' then P.empty else
213 P.text "\x2203" <+> P.hsep (map (pretty Never) fqs') <> P.text "."
215 CLet schemes constraint ->
216 P.fsep [ P.hang (P.text "let") 4 (P.brackets . commaSep $ map (pretty Never) schemes)
217 , P.text "in", pretty Never constraint ]
219 CInstance name tipe ->
220 P.text name <+> P.text "<" <+> prty tipe
222instance (PrettyType a, PrettyType b) => PrettyType (Scheme a b) where
223 pretty _ (Scheme rqs fqs constraint headers) =
224 P.sep [ forall, cs, headers' ]
228 forall = if null rqs && null fqs then P.empty else
229 P.text "\x2200" <+> frees <+> rigids
231 frees = P.hsep $ map prty fqs
232 rigids = if null rqs then P.empty else P.braces . P.hsep $ map prty rqs
234 cs = case constraint of
237 _ -> P.brackets (pretty Never constraint)
239 headers' = if Map.size headers > 0 then dict else P.empty
240 dict = P.parens . commaSep . map prettyPair $ Map.toList headers
241 prettyPair (n,t) = P.text n <+> P.text ":" <+> pretty Never t
244extraPretty :: (PrettyType t, Crawl t) => t -> IO Doc
245extraPretty t = pretty Never <$> addNames t
247addNames :: (Crawl t) => t -> IO t
249 (_, rawVars) <- runStateT (crawl getNames value) []
250 let vars = map head . List.group $ List.sort rawVars
251 suffix s = map (++s) (map (:[]) ['a'..'z'])
252 allVars = concatMap suffix $ ["","'","_"] ++ map show [0..]
253 okayVars = filter (`notElem` vars) allVars
254 runStateT (crawl rename value) okayVars
259 Just var -> (name, var:vars)
260 Nothing -> (name, vars)
264 Just var -> (name, vars)
265 Nothing -> (Just (head vars), tail vars)
268-- Code for traversing all the type data-structures and giving
269-- names to the variables embedded deep in there.
271 crawl :: (Maybe TypeName -> [String] -> (Maybe TypeName, [String]))
273 -> StateT [String] IO t
275instance (Crawl t, Crawl v) => Crawl (Constraint t v) where
276 crawl nextState constraint =
277 let rnm = crawl nextState in
279 CTrue -> return CTrue
280 CSaveEnv -> return CSaveEnv
281 CEqual a b -> CEqual <$> rnm a <*> rnm b
282 CAnd cs -> CAnd <$> crawl nextState cs
283 CLet schemes c -> CLet <$> crawl nextState schemes <*> crawl nextState c
284 CInstance name tipe -> CInstance name <$> rnm tipe
286instance Crawl a => Crawl [a] where
287 crawl nextState list = mapM (crawl nextState) list
289instance (Crawl t, Crawl v) => Crawl (Scheme t v) where
290 crawl nextState (Scheme rqs fqs c headers) =
291 let rnm = crawl nextState in
292 Scheme <$> rnm rqs <*> rnm fqs <*> crawl nextState c <*> return headers
294instance Crawl t => Crawl (TermN t) where
295 crawl nextState tipe =
297 VarN x -> VarN <$> crawl nextState x
298 TermN term -> TermN <$> crawl nextState term
300instance Crawl t => Crawl (Term1 t) where
301 crawl nextState term =
302 let rnm = crawl nextState in
304 App1 a b -> App1 <$> rnm a <*> rnm b
305 Fun1 a b -> Fun1 <$> rnm a <*> rnm b
306 Var1 a -> Var1 <$> rnm a
307 EmptyRecord1 -> return EmptyRecord1
308 Record1 fields ext ->
309 Record1 <$> traverse (mapM rnm) fields <*> rnm ext
311instance Crawl a => Crawl (UF.Point a) where
312 crawl nextState point = do
313 desc <- liftIO $ UF.descriptor point
314 desc' <- crawl nextState desc
315 liftIO $ UF.setDescriptor point desc'
318instance Crawl Descriptor where
319 crawl nextState desc = do
321 let (name', state') = nextState (name desc) state
322 structure' <- traverse (crawl nextState) (structure desc)
324 return $ desc { name = name', structure = structure' }
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