@@ -10,18 +10,16 @@ module AST.Type
10import Control.Arrow (second)
10import Control.Arrow (second)
11import Data.Binary
11import Data.Binary
12import qualified Data.Map as Map
12import qualified Data.Map as Map
13−import qualified Data.Maybe as Maybe
14−import Text.PrettyPrint as P
16import qualified AST.Variable as Var
14import qualified AST.Variable as Var
17−import qualified AST.Helpers as Help
18import qualified Reporting.Annotation as A
15import qualified Reporting.Annotation as A
19−import qualified Reporting.PrettyPrint as P
20import qualified Reporting.Region as R
16import qualified Reporting.Region as R
23-- DEFINITION
20-- DEFINITION
26 A.Located Raw'
24 A.Located Raw'
@@ -32,7 +30,6 @@ data Raw'
32 | RType Var.Raw
30 | RType Var.Raw
33 | RApp Raw [Raw]
31 | RApp Raw [Raw]
34 | RRecord [(String, Raw)] (Maybe Raw)
32 | RRecord [(String, Raw)] (Maybe Raw)
38data Canonical
35data Canonical
@@ -42,19 +39,19 @@ data Canonical
42 | App Canonical [Canonical]
39 | App Canonical [Canonical]
43 | Record [(String, Canonical)] (Maybe Canonical)
40 | Record [(String, Canonical)] (Maybe Canonical)
44 | Aliased Var.Canonical [(String, Canonical)] (Aliased Canonical)
41 | Aliased Var.Canonical [(String, Canonical)] (Aliased Canonical)
45− deriving (Eq, Ord, Show)
42+ deriving (Eq, Ord)
48data Aliased t
45data Aliased t
50 | Filled t
47 | Filled t
51− deriving (Eq, Ord, Show)
48+ deriving (Eq, Ord)
54data Port t
51data Port t
55 = Normal t
52 = Normal t
56 | Signal { root :: t, arg :: t }
53 | Signal { root :: t, arg :: t }
57− deriving (Eq, Show)
54+ deriving (Eq)
60getPortType :: Port tipe -> tipe
57getPortType :: Port tipe -> tipe
@@ -71,8 +68,10 @@ tuple region types =
71 A.A region (RApp (A.A region (RType name)) types)
68 A.A region (RApp (A.A region (RType name)) types)
74-- DEALIASING
72-- DEALIASING
76deepDealias :: Canonical -> Canonical
75deepDealias :: Canonical -> Canonical
77deepDealias tipe =
76deepDealias tipe =
78 case tipe of
77 case tipe of
@@ -138,95 +137,23 @@ dealiasHelp typeTable tipe =
138 App (go f) (map go args)
137 App (go f) (map go args)
143−instance (P.Pretty t) => P.Pretty (Port t) where
144− pretty dealiaser needsParens portType =
145− P.pretty dealiaser needsParens (getPortType portType)
148−instance P.Pretty Raw' where
149− pretty dealiaser needsParens tipe =
152− P.parensIf needsParens (prettyLambda dealiaser getRawLambda arg body)
158− prettyType dealiaser var
162− isTuple (A.A _ (RType name)) = Help.isTuple (Var.toString name)
165− prettyApp dealiaser needsParens isTuple func args
167− RRecord fields ext ->
168− prettyRecord dealiaser (flattenRawRecord fields ext)
171−instance P.Pretty Canonical where
172− pretty dealiaser needsParens tipe =
175− P.parensIf needsParens (prettyLambda dealiaser getCanLambda arg body)
181− prettyType dealiaser var
185− isTuple (Type name) = Help.isTuple (Var.toString name)
188− prettyApp dealiaser needsParens isTuple func args
190− Record fields ext ->
191− prettyRecord dealiaser (flattenCanRecord fields ext)
193− Aliased name args _ ->
141+-- COLLECT LAMBDAS
194− P.parensIf (needsParens && not (null args)) $
196− (P.pretty dealiaser False name)
198− (P.sep (map (P.pretty dealiaser True . snd) args))
201−-- PRETTY HELPERS
144+collectLambdas :: Canonical -> [Canonical]
202−
145+collectLambdas tipe =
203−prettyType :: (Var.ToString var) => P.Dealiaser -> var -> P.Doc
146+ gatherLambda getCanLambda tipe
204−prettyType dealiaser var =
206− v = Var.toString var
209− if v == "_Tuple0" then
212− maybe v id (Map.lookup v dealiaser)
217−prettyLambda :: (P.Pretty t) => P.Dealiaser -> (t -> Maybe (t,t)) -> t -> t -> P.Doc
149+gatherLambda :: (t -> Maybe (t,t)) -> t -> [t]
218−prettyLambda dealiaser getLambda arg body =
150+gatherLambda get tipe =
219− let
151+ case get tipe of
220− rest =
152+ Just (arg, body) ->
221− gatherLambda getLambda body
153+ arg : gatherLambda get body
223− prettyArg t =
155+ Nothing ->
224− P.pretty dealiaser (Maybe.isJust (getLambda t)) t
156+ [tipe]
228− , P.sep (map (\t -> P.text "->" <+> prettyArg t) rest)
232getRawLambda :: Raw -> Maybe (Raw, Raw)
159getRawLambda :: Raw -> Maybe (Raw, Raw)
@@ -243,115 +170,10 @@ getCanLambda tipe =
243 _ -> Nothing
170 _ -> Nothing
246−gatherLambda :: (t -> Maybe (t,t)) -> t -> [t]
247−gatherLambda get tipe =
250− arg : gatherLambda get body
256−collectLambdas :: Canonical -> [Canonical]
257−collectLambdas tipe =
258− gatherLambda getCanLambda tipe
263−prettyApp :: (P.Pretty t) => P.Dealiaser -> Bool -> (t -> Bool) -> t -> [t] -> P.Doc
264−prettyApp dealiaser needsParens isTuple func args
267− P.punctuate P.comma (map (P.pretty dealiaser False) args)
270− P.pretty dealiaser needsParens func
273− P.parensIf needsParens $
275− (P.pretty dealiaser True func)
277− (P.sep (map (P.pretty dealiaser True) args))
282−prettyRecord :: (P.Pretty t) => P.Dealiaser -> ([(String, t)], Maybe String) -> P.Doc
283−prettyRecord dealiaser recordInfo =
285− prettyField (field, tipe) =
287− (P.text field <+> P.text ":")
289− (P.pretty dealiaser False tipe)
295− (fields, Nothing) ->
297− [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
303− (P.lbrace <+> P.text x <+> P.text "|")
306− [ P.sep (P.punctuate P.comma (map prettyField fields))
315− -> ( [(String, Raw)], Maybe String )
316−flattenRawRecord fields ext =
321− Just (A.A _ (RVar x)) ->
324− Just (A.A _ (RRecord fields' ext')) ->
325− flattenRawRecord (fields' ++ fields) ext'
328− error "Trying to flatten ill-formed record."
332− :: [(String, Canonical)]
334− -> ( [(String, Canonical)], Maybe String )
335−flattenCanRecord fields ext =
343− Just (Record fields' ext') ->
344− flattenCanRecord (fields' ++ fields) ext'
346− Just (Aliased _ args tipe) ->
347− flattenCanRecord fields (Just (dealias args tipe))
350− error "Trying to flatten ill-formed record."
355instance Binary Canonical where
177instance Binary Canonical where
356 put tipe =
178 put tipe =
357 case tipe of
179 case tipe of