1module Reporting.Render.Type
10import qualified Data.Map as Map
11import Text.PrettyPrint.ANSI.Leijen
13import qualified AST.Helpers as Help
39commaCat :: [Doc] -> Doc
41 cat (punctuate comma docs)
44commaSep :: [Doc] -> Doc
46 sep (punctuate comma docs)
49parensIf :: Bool -> Doc -> Doc
51 if bool then parens doc else doc
54variable :: String -> Doc
56 if Help.isOp x then parens (text x) else text x
62instance (P.Pretty t) => P.Pretty (Port t) where
63 pretty dealiaser needsParens portType =
64 P.pretty dealiaser needsParens (getPortType portType)
67instance P.Pretty Raw' where
68 pretty dealiaser needsParens tipe =
71 P.parensIf needsParens (prettyLambda dealiaser getRawLambda arg body)
77 prettyType dealiaser var
81 isTuple (A.A _ (RType name)) = Help.isTuple (Var.toString name)
84 prettyApp dealiaser needsParens isTuple func args
87 prettyRecord dealiaser (flattenRawRecord fields ext)
90instance P.Pretty Canonical where
91 pretty dealiaser needsParens tipe =
94 P.parensIf needsParens (prettyLambda dealiaser getCanLambda arg body)
100 prettyType dealiaser var
104 isTuple (Type name) = Help.isTuple (Var.toString name)
107 prettyApp dealiaser needsParens isTuple func args
110 prettyRecord dealiaser (flattenCanRecord fields ext)
112 Aliased name args _ ->
113 P.parensIf (needsParens && not (null args)) $
115 (P.pretty dealiaser False name)
117 (P.sep (map (P.pretty dealiaser True . snd) args))
122prettyType :: (Var.ToString var) => P.Dealiaser -> var -> P.Doc
123prettyType dealiaser var =
128 if v == "_Tuple0" then
131 maybe v id (Map.lookup v dealiaser)
136prettyLambda :: (P.Pretty t) => P.Dealiaser -> (t -> Maybe (t,t)) -> t -> t -> P.Doc
137prettyLambda dealiaser getLambda arg body =
140 gatherLambda getLambda body
143 P.pretty dealiaser (Maybe.isJust (getLambda t)) t
147 , P.sep (map (\t -> P.text "->" <+> prettyArg t) rest)
151getRawLambda :: Raw -> Maybe (Raw, Raw)
152getRawLambda (A.A _ tipe) =
154 RLambda arg body -> Just (arg, body)
158getCanLambda :: Canonical -> Maybe (Canonical, Canonical)
161 Lambda arg body -> Just (arg, body)
165gatherLambda :: (t -> Maybe (t,t)) -> t -> [t]
166gatherLambda get tipe =
169 arg : gatherLambda get body
175collectLambdas :: Canonical -> [Canonical]
177 gatherLambda getCanLambda tipe
182prettyApp :: (P.Pretty t) => P.Dealiaser -> Bool -> (t -> Bool) -> t -> [t] -> P.Doc
183prettyApp dealiaser needsParens isTuple func args
186 P.punctuate P.comma (map (P.pretty dealiaser False) args)
189 P.pretty dealiaser needsParens func
192 P.parensIf needsParens $
194 (P.pretty dealiaser True func)
196 (P.sep (map (P.pretty dealiaser True) args))
201prettyRecord :: (P.Pretty t) => P.Dealiaser -> ([(String, t)], Maybe String) -> P.Doc
202prettyRecord dealiaser recordInfo =
204 prettyField (field, tipe) =
206 (P.text field <+> P.text ":")
208 (P.pretty dealiaser False tipe)
216 [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
222 (P.lbrace <+> P.text x <+> P.text "|")
225 [ P.sep (P.punctuate P.comma (map prettyField fields))
234 -> ( [(String, Raw)], Maybe String )
235flattenRawRecord fields ext =
240 Just (A.A _ (RVar x)) ->
243 Just (A.A _ (RRecord fields' ext')) ->
244 flattenRawRecord (fields' ++ fields) ext'
247 error "Trying to flatten ill-formed record."
251 :: [(String, Canonical)]
253 -> ( [(String, Canonical)], Maybe String )
254flattenCanRecord fields ext =
262 Just (Record fields' ext') ->
263 flattenCanRecord (fields' ++ fields) ext'
265 Just (Aliased _ args tipe) ->
266 flattenCanRecord fields (Just (dealias args tipe))
269 error "Trying to flatten ill-formed record."
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