@@ -12,6 +12,7 @@ import Control.Applicative ((<$>), (<*>))
12import Control.Arrow (second)
12import Control.Arrow (second)
13import Data.Binary
13import Data.Binary
14import qualified Data.Map as Map
14import qualified Data.Map as Map
15+import qualified Data.Maybe as Maybe
15import Text.PrettyPrint as P
16import Text.PrettyPrint as P
17import qualified AST.Variable as Var
18import qualified AST.Variable as Var
@@ -161,74 +162,49 @@ instance (P.Pretty t) => P.Pretty (Port t) where
163instance P.Pretty Raw' where
164instance P.Pretty Raw' where
164− pretty _ tipe =
165+ pretty needsParens tipe =
168+ P.parensIf needsParens (prettyLambda getRawLambda arg body)
165− P.text (show tipe)
171+ P.text x
178+ isTuple (A.A _ (RType name)) = Help.isTuple (Var.toString name)
181+ prettyApp needsParens isTuple func args
183+ RRecord fields ext ->
184+ prettyRecord (flattenRawRecord fields ext)
168instance P.Pretty Canonical where
187instance P.Pretty Canonical where
169 pretty needsParens tipe =
188 pretty needsParens tipe =
170 case tipe of
189 case tipe of
171− Lambda _ _ ->
190+ Lambda arg body ->
172− P.parensIf needsParens $
191+ P.parensIf needsParens (prettyLambda getCanLambda arg body)
173− P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
176− map prettyLambda (collectLambdas tipe)
188 Type var ->
196 Type var ->
189− let v = Var.toString var
197+ prettyType var
201+ isTuple (Type name) = Help.isTuple (Var.toString name)
191− P.text (if v == "_Tuple0" then "()" else v)
204+ prettyApp needsParens isTuple func args
196− | Help.isTuple (Var.toString name) ->
198− P.punctuate P.comma (map (P.pretty False) args)
204− P.parensIf needsParens $
208− (P.sep (map (P.pretty True) args))
210− Record _ _ ->
206+ Record fields ext ->
211− case flattenRecord tipe of
207+ prettyRecord (flattenCanRecord fields ext)
215− (fields, Nothing) ->
217− [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
223− (P.lbrace <+> P.text x <+> P.text "|")
226− [ P.cat (zipWith (<+>) (P.space : repeat P.comma) (map prettyField fields))
230− prettyField (field, tipe) =
231− P.text field <+> P.text ":" <+> P.pretty False tipe
233 Aliased name args _ ->
209 Aliased name args _ ->
234 P.parensIf (needsParens && not (null args)) $
210 P.parensIf (needsParens && not (null args)) $
@@ -238,32 +214,149 @@ instance P.Pretty Canonical where
238 (P.sep (map (P.pretty True . snd) args))
214 (P.sep (map (P.pretty True . snd) args))
241−collectLambdas :: Canonical -> [Canonical]
217+-- PRETTY HELPERS
242−collectLambdas tipe =
218+
219+prettyType :: (Var.ToString var) => var -> P.Doc
222+ v = Var.toString var
224+ P.text (if v == "_Tuple0" then "()" else v)
229+prettyLambda :: (P.Pretty t) => (t -> Maybe (t,t)) -> t -> t -> P.Doc
230+prettyLambda getLambda arg body =
233+ gatherLambda getLambda body
236+ P.pretty (Maybe.isJust (getLambda t)) t
240+ , P.sep (map (\t -> P.text "->" <+> prettyArg t) rest)
244+getRawLambda :: Raw -> Maybe (Raw, Raw)
245+getRawLambda (A.A _ tipe) =
243 case tipe of
246 case tipe of
244− Lambda arg body ->
247+ RLambda arg body -> Just (arg, body)
245− arg : collectLambdas body
248+ _ -> Nothing
251+getCanLambda :: Canonical -> Maybe (Canonical, Canonical)
254+ Lambda arg body -> Just (arg, body)
258+gatherLambda :: (t -> Maybe (t,t)) -> t -> [t]
259+gatherLambda get tipe =
262+ arg : gatherLambda get body
251−flattenRecord :: Canonical -> ( [(String, Canonical)], Maybe String )
268+collectLambdas :: Canonical -> [Canonical]
252−flattenRecord tipe =
269+collectLambdas tipe =
253− case tipe of
270+ gatherLambda getCanLambda tipe
275+prettyApp :: (P.Pretty t) => Bool -> (t -> Bool) -> t -> [t] -> P.Doc
276+prettyApp needsParens isTuple func args
279+ P.punctuate P.comma (map (P.pretty False) args)
282+ P.pretty needsParens func
285+ P.parensIf needsParens $
287+ (P.pretty True func)
289+ (P.sep (map (P.pretty True) args))
294+prettyRecord :: (P.Pretty t) => ([(String, t)], Maybe String) -> P.Doc
295+prettyRecord recordInfo =
297+ prettyField (field, tipe) =
299+ (P.text field <+> P.text ":")
301+ (P.pretty False tipe)
257− Record fields Nothing ->
307+ (fields, Nothing) ->
309+ [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
315+ (P.lbrace <+> P.text x <+> P.text "|")
318+ [ P.cat (zipWith (<+>) (P.space : repeat P.comma) (map prettyField fields))
327+ -> ( [(String, Raw)], Maybe String )
328+flattenRawRecord fields ext =
333+ Just (A.A _ (RVar x)) ->
336+ Just (A.A _ (RRecord fields' ext')) ->
337+ flattenRawRecord (fields' ++ fields) ext'
340+ error "Trying to flatten ill-formed record."
344+ :: [(String, Canonical)]
346+ -> ( [(String, Canonical)], Maybe String )
347+flattenCanRecord fields ext =
258 (fields, Nothing)
350 (fields, Nothing)
260− Record fields (Just ext) ->
355+ Just (Record fields' ext') ->
261− let (fields',ext') = flattenRecord ext
263− (fields' ++ fields, ext')
356+ flattenCanRecord (fields' ++ fields) ext'
265− Aliased _ args tipe' ->
358+ Just (Aliased _ args tipe) ->
266− flattenRecord (dealias args tipe')
359+ flattenCanRecord fields (Just (dealias args tipe))
269 error "Trying to flatten ill-formed record."
362 error "Trying to flatten ill-formed record."