3import qualified Data.List as List
4import qualified Data.Map as Map
5import qualified Data.UnionFind.IO as UF
6import SourceSyntax.PrettyPrint
7import Text.PrettyPrint as P
9import Control.Applicative ((<$>),(<*>))
10import Control.Monad.State
11import Data.Traversable (traverse)
18 | Record1 (Map.Map String [a]) a
23 | TermN (Term1 (TermN a))
26record fs rec = TermN (Record1 fs rec)
28type SchemeName = String
34 | CAnd [Constraint a b]
35 | CLet [Scheme a b] (Constraint a b)
36 | CInstance SchemeName a
39data Scheme a b = Scheme {
40 rigidQuantifiers :: [b],
41 flexibleQuantifiers :: [b],
42 constraint :: Constraint a b,
43 header :: Map.Map String a
46monoscheme headers = Scheme [] [] CTrue headers
48data Descriptor = Descriptor {
49 structure :: Maybe (Term1 Variable),
52 name :: Maybe TypeName,
53 copy :: Maybe Variable,
58outermostRank = 0 :: Int
63data Flex = Rigid | Flexible | Constant
66type Variable = UF.Point Descriptor
68type Type = TermN Variable
69type TypeConstraint = Constraint Type Variable
70type TypeScheme = Scheme Type Variable
74(/\) :: Constraint a b -> Constraint a b -> Constraint a b
77(===) :: Type -> Type -> TypeConstraint
80(<?) :: SchemeName -> Type -> TypeConstraint
84(==>) :: Type -> Type -> Type
85a ==> b = TermN (Fun1 a b)
87namedVar name = UF.fresh $ Descriptor {
96flexibleVar = UF.fresh $ Descriptor {
105rigidVar = UF.fresh $ Descriptor {
114-- ex qs constraint == exists qs. constraint
115ex :: [Variable] -> TypeConstraint -> TypeConstraint
116ex fqs constraint = CLet [Scheme [] fqs constraint Map.empty] CTrue
118-- fl qs constraint == forall qs. constraint
119fl :: [Variable] -> TypeConstraint -> TypeConstraint
120fl rqs constraint = CLet [Scheme rqs [] constraint Map.empty] CTrue
122exists :: (Type -> IO TypeConstraint) -> IO TypeConstraint
125 ex [v] <$> f (VarN v)
127instance Show a => Show (UF.Point a) where
128 show point = unsafePerformIO $ fmap show (UF.descriptor point)
130instance Pretty a => Pretty (UF.Point a) where
131 pretty point = unsafePerformIO $ fmap pretty (UF.descriptor point)
133instance Pretty a => Pretty (Term1 a) where
136 App1 f x -> pretty f <+> pretty x
137 Fun1 arg body -> formattedArg <+> P.text "->" <+> pretty body
139 prettyArg = pretty arg
140 formattedArg = parensIf ("->" `List.isInfixOf` P.render prettyArg) prettyArg
142 EmptyRecord1 -> P.braces P.empty
143 Record1 fields ext ->
144 P.braces (pretty ext <+> P.text "|" <+> P.sep (P.punctuate P.comma prettyFields))
146 mkPretty f t = P.text f <+> P.text ":" <+> pretty t
147 prettyFields = concatMap (\(f,ts) -> map (mkPretty f) ts) (Map.toList fields)
149instance Pretty a => Pretty (TermN a) where
153 TermN t1 -> pretty t1
155instance Pretty Descriptor where
157 case (structure desc, name desc) of
158 (Just term, _) -> pretty term
159 (_, Just name) -> P.text name
162instance (Pretty a, Pretty b) => Pretty (Constraint a b) where
165 CTrue -> P.text "True"
166 CEqual a b -> pretty a <+> P.text "=" <+> pretty b
167 CAnd [] -> P.text "True"
170 P.parens . P.sep $ P.punctuate (P.text " and") (map pretty cs)
172 CLet [Scheme [] fqs constraint header] CTrue | Map.null header ->
173 P.sep [ binder, pretty c ]
177 CLet [Scheme [] fqs' c' _] CTrue -> mergeExists (vs ++ fqs') c'
180 (fqs', c) = mergeExists fqs constraint
182 binder = if null fqs' then P.empty else
183 P.text "exists" <+> P.hsep (map pretty fqs') <> P.text "."
185 CLet schemes constraint ->
186 P.fsep [ P.hang (P.text "let") 4 (P.brackets . P.sep . P.punctuate P.comma $ map pretty schemes)
187 , P.text "in", pretty constraint ]
189 CInstance name tipe ->
190 P.text name <+> P.text "<" <+> pretty tipe
192instance (Pretty a, Pretty b) => Pretty (Scheme a b) where
193 pretty (Scheme rqs fqs constraint headers) =
194 P.sep [ forall, cs, headers' ]
196 forall = if null rqs && null fqs then P.empty else
197 P.text "forall" <+> frees <+> rigids
199 frees = P.hsep $ map pretty fqs
200 rigids = if null rqs then P.empty else P.braces . P.hsep $ map pretty rqs
202 cs = case constraint of
205 _ -> P.brackets (pretty constraint)
207 headers' = if Map.size headers > 0 then dict else P.empty
208 dict = P.parens . P.sep . P.punctuate P.comma . map prettyPair $ Map.toList headers
209 prettyPair (n,t) = P.text n <+> P.text ":" <+> pretty t
211extraPretty :: (Pretty t, Crawl t) => t -> IO Doc
212extraPretty value = do
213 (_, rawVars) <- runStateT (crawl getNames value) []
214 let vars = map head . List.group $ List.sort rawVars
215 letters = map (:[]) ['a'..'z']
217 allVars = letters ++ suffix "'" letters ++ concatMap (\n -> suffix (show n) letters) [0..]
218 okayVars = filter (`notElem` vars) allVars
219 runStateT (crawl rename value) okayVars
220 return (pretty value)
224 Just var -> (name, var:vars)
225 Nothing -> (name, vars)
229 Just var -> (name, vars)
230 Nothing -> (Just (head vars), tail vars)
233-- Code for traversing all the type data-structures and giving
234-- names to the variables embedded deep in there.
236 crawl :: (Maybe TypeName -> [String] -> (Maybe TypeName, [String]))
238 -> StateT [String] IO t
240instance (Crawl t, Crawl v) => Crawl (Constraint t v) where
241 crawl nextState constraint =
242 let rnm = crawl nextState in
244 CTrue -> return CTrue
245 CEqual a b -> CEqual <$> rnm a <*> rnm b
246 CAnd cs -> CAnd <$> crawl nextState cs
247 CLet schemes c -> CLet <$> crawl nextState schemes <*> crawl nextState c
248 CInstance name tipe -> CInstance name <$> rnm tipe
250instance Crawl a => Crawl [a] where
251 crawl nextState list = mapM (crawl nextState) list
253instance (Crawl t, Crawl v) => Crawl (Scheme t v) where
254 crawl nextState (Scheme rqs fqs c headers) =
255 let rnm = crawl nextState in
256 Scheme <$> rnm rqs <*> rnm fqs <*> crawl nextState c <*> return headers
258instance Crawl t => Crawl (TermN t) where
259 crawl nextState tipe =
261 VarN x -> VarN <$> crawl nextState x
262 TermN term -> TermN <$> crawl nextState term
264instance Crawl t => Crawl (Term1 t) where
265 crawl nextState term =
266 let rnm = crawl nextState in
268 App1 a b -> App1 <$> rnm a <*> rnm b
269 Fun1 a b -> Fun1 <$> rnm a <*> rnm b
270 Var1 a -> Var1 <$> rnm a
271 EmptyRecord1 -> return EmptyRecord1
272 Record1 fields ext ->
273 Record1 <$> traverse (mapM rnm) fields <*> rnm ext
275instance Crawl a => Crawl (UF.Point a) where
276 crawl nextState point = do
277 desc <- liftIO $ UF.descriptor point
278 desc' <- crawl nextState desc
279 liftIO $ UF.setDescriptor point desc'
282instance Crawl Descriptor where
283 crawl nextState desc = do
285 let (name', state') = nextState (name desc) state
286 structure' <- traverse (crawl nextState) (structure desc)
288 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