@@ -10,18 +10,16 @@ module AST.Type
1010import Control.Arrow (second)
1111import Data.Binary
1212import qualified Data.Map as Map
13−import qualified Data.Maybe as Maybe
14−import Text.PrettyPrint as P
1513
1614import qualified AST.Variable as Var
17−import qualified AST.Helpers as Help
1815import qualified Reporting.Annotation as A
19−import qualified Reporting.PrettyPrint as P
2016import qualified Reporting.Region as R
2117
2218
19+
2320-- DEFINITION
2421
22+
2523type Raw =
2624 A.Located Raw'
2725
@@ -32,7 +30,6 @@ data Raw'
3230 | RType Var.Raw
3331 | RApp Raw [Raw]
3432 | RRecord [(String, Raw)] (Maybe Raw)
35− deriving (Show)
3633
3734
3835data Canonical
@@ -42,19 +39,19 @@ data Canonical
4239 | App Canonical [Canonical]
4340 | Record [(String, Canonical)] (Maybe Canonical)
4441 | Aliased Var.Canonical [(String, Canonical)] (Aliased Canonical)
45− deriving (Eq, Ord, Show)
42+ deriving (Eq, Ord)
4643
4744
4845data Aliased t
4946 = Holey t
5047 | Filled t
51− deriving (Eq, Ord, Show)
48+ deriving (Eq, Ord)
5249
5350
5451data Port t
5552 = Normal t
5653 | Signal { root :: t, arg :: t }
57− deriving (Eq, Show)
54+ deriving (Eq)
5855
5956
6057getPortType :: Port tipe -> tipe
@@ -71,8 +68,10 @@ tuple region types =
7168 A.A region (RApp (A.A region (RType name)) types)
7269
7370
71+
7472-- DEALIASING
7573
74+
7675deepDealias :: Canonical -> Canonical
7776deepDealias tipe =
7877 case tipe of
@@ -138,95 +137,23 @@ dealiasHelp typeTable tipe =
138137 App (go f) (map go args)
139138
140139
141−-- PRETTY PRINTING
142−
143−instance (P.Pretty t) => P.Pretty (Port t) where
144− pretty dealiaser needsParens portType =
145− P.pretty dealiaser needsParens (getPortType portType)
146−
147−
148−instance P.Pretty Raw' where
149− pretty dealiaser needsParens tipe =
150− case tipe of
151− RLambda arg body ->
152− P.parensIf needsParens (prettyLambda dealiaser getRawLambda arg body)
153−
154− RVar x ->
155− P.text x
156−
157− RType var ->
158− prettyType dealiaser var
159−
160− RApp func args ->
161− let
162− isTuple (A.A _ (RType name)) = Help.isTuple (Var.toString name)
163− isTuple _ = False
164− in
165− prettyApp dealiaser needsParens isTuple func args
166−
167− RRecord fields ext ->
168− prettyRecord dealiaser (flattenRawRecord fields ext)
169−
170−
171−instance P.Pretty Canonical where
172− pretty dealiaser needsParens tipe =
173− case tipe of
174− Lambda arg body ->
175− P.parensIf needsParens (prettyLambda dealiaser getCanLambda arg body)
176−
177− Var x ->
178− P.text x
179−
180− Type var ->
181− prettyType dealiaser var
182−
183− App func args ->
184− let
185− isTuple (Type name) = Help.isTuple (Var.toString name)
186− isTuple _ = False
187− in
188− prettyApp dealiaser needsParens isTuple func args
189−
190− Record fields ext ->
191− prettyRecord dealiaser (flattenCanRecord fields ext)
192140
193− Aliased name args _ ->
194− P.parensIf (needsParens && not (null args)) $
195− P.hang
196− (P.pretty dealiaser False name)
197− 2
198− (P.sep (map (P.pretty dealiaser True . snd) args))
141+-- COLLECT LAMBDAS
199142
200143
201−-- PRETTY HELPERS
202−
203−prettyType :: (Var.ToString var) => P.Dealiaser -> var -> P.Doc
204−prettyType dealiaser var =
205− let
206− v = Var.toString var
207− in
208− P.text $
209− if v == "_Tuple0" then
210− "()"
211− else
212− maybe v id (Map.lookup v dealiaser)
213−
144+collectLambdas :: Canonical -> [Canonical]
145+collectLambdas tipe =
146+ gatherLambda getCanLambda tipe
214147
215−-- PRETTY LAMBDAS
216148
217−prettyLambda :: (P.Pretty t) => P.Dealiaser -> (t -> Maybe (t,t)) -> t -> t -> P.Doc
218−prettyLambda dealiaser getLambda arg body =
219− let
220− rest =
221− gatherLambda getLambda body
149+gatherLambda :: (t -> Maybe (t,t)) -> t -> [t]
150+gatherLambda get tipe =
151+ case get tipe of
152+ Just (arg, body) ->
153+ arg : gatherLambda get body
222154
223− prettyArg t =
224− P.pretty dealiaser (Maybe.isJust (getLambda t)) t
225− in
226− P.sep
227− [ prettyArg arg
228− , P.sep (map (\t -> P.text "->" <+> prettyArg t) rest)
229− ]
155+ Nothing ->
156+ [tipe]
230157
231158
232159getRawLambda :: Raw -> Maybe (Raw, Raw)
@@ -243,115 +170,10 @@ getCanLambda tipe =
243170 _ -> Nothing
244171
245172
246−gatherLambda :: (t -> Maybe (t,t)) -> t -> [t]
247−gatherLambda get tipe =
248− case get tipe of
249− Just (arg, body) ->
250− arg : gatherLambda get body
251−
252− Nothing ->
253− [tipe]
254−
255−
256−collectLambdas :: Canonical -> [Canonical]
257−collectLambdas tipe =
258− gatherLambda getCanLambda tipe
259−
260−
261−-- PRETTY APP
262−
263−prettyApp :: (P.Pretty t) => P.Dealiaser -> Bool -> (t -> Bool) -> t -> [t] -> P.Doc
264−prettyApp dealiaser needsParens isTuple func args
265− | isTuple func =
266− P.parens $ P.sep $
267− P.punctuate P.comma (map (P.pretty dealiaser False) args)
268−
269− | null args =
270− P.pretty dealiaser needsParens func
271−
272− | otherwise =
273− P.parensIf needsParens $
274− P.hang
275− (P.pretty dealiaser True func)
276− 2
277− (P.sep (map (P.pretty dealiaser True) args))
278−
279−
280−-- PRETTY RECORD
281−
282−prettyRecord :: (P.Pretty t) => P.Dealiaser -> ([(String, t)], Maybe String) -> P.Doc
283−prettyRecord dealiaser recordInfo =
284− let
285− prettyField (field, tipe) =
286− P.hang
287− (P.text field <+> P.text ":")
288− 4
289− (P.pretty dealiaser False tipe)
290− in
291− case recordInfo of
292− ([], Nothing) ->
293− P.text "{}"
294−
295− (fields, Nothing) ->
296− P.sep
297− [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
298− , P.rbrace
299− ]
300−
301− (fields, Just x) ->
302− P.hang
303− (P.lbrace <+> P.text x <+> P.text "|")
304− 4
305− (P.sep
306− [ P.sep (P.punctuate P.comma (map prettyField fields))
307− , P.rbrace
308− ]
309− )
310−
311−
312−flattenRawRecord
313− :: [(String, Raw)]
314− -> Maybe Raw
315− -> ( [(String, Raw)], Maybe String )
316−flattenRawRecord fields ext =
317− case ext of
318− Nothing ->
319− (fields, Nothing)
320−
321− Just (A.A _ (RVar x)) ->
322− (fields, Just x)
323−
324− Just (A.A _ (RRecord fields' ext')) ->
325− flattenRawRecord (fields' ++ fields) ext'
326−
327− _ ->
328− error "Trying to flatten ill-formed record."
329−
330−
331−flattenCanRecord
332− :: [(String, Canonical)]
333− -> Maybe Canonical
334− -> ( [(String, Canonical)], Maybe String )
335−flattenCanRecord fields ext =
336− case ext of
337− Nothing ->
338− (fields, Nothing)
339−
340− Just (Var x) ->
341− (fields, Just x)
342−
343− Just (Record fields' ext') ->
344− flattenCanRecord (fields' ++ fields) ext'
345−
346− Just (Aliased _ args tipe) ->
347− flattenCanRecord fields (Just (dealias args tipe))
348−
349− _ ->
350− error "Trying to flatten ill-formed record."
351−
352173
353174-- BINARY
354175
176+
355177instance Binary Canonical where
356178 put tipe =
357179 case tipe of