3 , Canonical(..), Aliased(..)
4 , Port(..), getPortType
11import Control.Arrow (second)
13import qualified Data.Map as Map
14import qualified Data.Maybe as Maybe
15import Text.PrettyPrint as P
17import qualified AST.Variable as Var
18import qualified AST.Helpers as Help
19import qualified Reporting.Annotation as A
20import qualified Reporting.PrettyPrint as P
21import qualified Reporting.Region as R
35 | RRecord [(String, Raw)] (Maybe Raw)
40 = Lambda Canonical Canonical
43 | App Canonical [Canonical]
44 | Record [(String, Canonical)] (Maybe Canonical)
45 | Aliased Var.Canonical [(String, Canonical)] (Aliased Canonical)
46 deriving (Eq, Ord, Show)
52 deriving (Eq, Ord, Show)
57 | Signal { root :: t, arg :: t }
61getPortType :: Port tipe -> tipe
68fieldMap :: [(String,a)] -> Map.Map String [a]
70 let add r (field,tipe) =
71 Map.insertWith (++) field [tipe] r
73 foldl add Map.empty fields
76recordOf :: [(String, Type var)] -> Type var
81listOf :: RawType -> RawType
83 App (Type (Var.Raw "List")) [tipe]
86tuple :: R.Region -> [Raw] -> Raw
88 let name = Var.Raw ("_Tuple" ++ show (length types))
90 A.A region (RApp (A.A region (RType name)) types)
95deepDealias :: Canonical -> Canonical
99 Lambda (deepDealias a) (deepDealias b)
105 Record (map (second deepDealias) fields) (fmap deepDealias ext)
107 Aliased _name args tipe' ->
108 deepDealias (dealias args tipe')
114 App (deepDealias f) (map deepDealias args)
117dealias :: [(String, Canonical)] -> Aliased Canonical -> Canonical
118dealias args aliasType =
121 dealiasHelp (Map.fromList args) tipe
127dealiasHelp :: Map.Map String Canonical -> Canonical -> Canonical
128dealiasHelp typeTable tipe =
129 let go = dealiasHelp typeTable in
135 Map.findWithDefault tipe x typeTable
138 Record (map (second go) fields) (fmap go ext)
140 Aliased original args t' ->
141 Aliased original (map (second go) args) t'
147 App (go f) (map go args)
152instance (P.Pretty t) => P.Pretty (Port t) where
153 pretty dealiaser needsParens portType =
154 P.pretty dealiaser needsParens (getPortType portType)
157instance P.Pretty Raw' where
158 pretty dealiaser needsParens tipe =
161 P.parensIf needsParens (prettyLambda dealiaser getRawLambda arg body)
167 prettyType dealiaser var
171 isTuple (A.A _ (RType name)) = Help.isTuple (Var.toString name)
174 prettyApp dealiaser needsParens isTuple func args
176 RRecord fields ext ->
177 prettyRecord dealiaser (flattenRawRecord fields ext)
180instance P.Pretty Canonical where
181 pretty dealiaser needsParens tipe =
184 P.parensIf needsParens (prettyLambda dealiaser getCanLambda arg body)
190 prettyType dealiaser var
194 isTuple (Type name) = Help.isTuple (Var.toString name)
197 prettyApp dealiaser needsParens isTuple func args
200 prettyRecord dealiaser (flattenCanRecord fields ext)
202 Aliased name args _ ->
203 P.parensIf (needsParens && not (null args)) $
205 (P.pretty dealiaser False name)
207 (P.sep (map (P.pretty dealiaser True . snd) args))
212prettyType :: (Var.ToString var) => P.Dealiaser -> var -> P.Doc
213prettyType dealiaser var =
218 if v == "_Tuple0" then
221 maybe v id (Map.lookup v dealiaser)
226prettyLambda :: (P.Pretty t) => P.Dealiaser -> (t -> Maybe (t,t)) -> t -> t -> P.Doc
227prettyLambda dealiaser getLambda arg body =
230 gatherLambda getLambda body
233 P.pretty dealiaser (Maybe.isJust (getLambda t)) t
237 , P.sep (map (\t -> P.text "->" <+> prettyArg t) rest)
241getRawLambda :: Raw -> Maybe (Raw, Raw)
242getRawLambda (A.A _ tipe) =
244 RLambda arg body -> Just (arg, body)
248getCanLambda :: Canonical -> Maybe (Canonical, Canonical)
251 Lambda arg body -> Just (arg, body)
255gatherLambda :: (t -> Maybe (t,t)) -> t -> [t]
256gatherLambda get tipe =
259 arg : gatherLambda get body
265collectLambdas :: Canonical -> [Canonical]
267 gatherLambda getCanLambda tipe
272prettyApp :: (P.Pretty t) => P.Dealiaser -> Bool -> (t -> Bool) -> t -> [t] -> P.Doc
273prettyApp dealiaser needsParens isTuple func args
276 P.punctuate P.comma (map (P.pretty dealiaser False) args)
279 P.pretty dealiaser needsParens func
282 P.parensIf needsParens $
284 (P.pretty dealiaser True func)
286 (P.sep (map (P.pretty dealiaser True) args))
291prettyRecord :: (P.Pretty t) => P.Dealiaser -> ([(String, t)], Maybe String) -> P.Doc
292prettyRecord dealiaser recordInfo =
294 prettyField (field, tipe) =
296 (P.text field <+> P.text ":")
298 (P.pretty dealiaser False tipe)
306 [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
312 (P.lbrace <+> P.text x <+> P.text "|")
315 [ P.sep (P.punctuate P.comma (map prettyField fields))
324 -> ( [(String, Raw)], Maybe String )
325flattenRawRecord fields ext =
330 Just (A.A _ (RVar x)) ->
333 Just (A.A _ (RRecord fields' ext')) ->
334 flattenRawRecord (fields' ++ fields) ext'
337 error "Trying to flatten ill-formed record."
341 :: [(String, Canonical)]
343 -> ( [(String, Canonical)], Maybe String )
344flattenCanRecord fields ext =
352 Just (Record fields' ext') ->
353 flattenCanRecord (fields' ++ fields) ext'
355 Just (Aliased _ args tipe) ->
356 flattenCanRecord fields (Just (dealias args tipe))
359 error "Trying to flatten ill-formed record."
364instance Binary Canonical where
368 putWord8 0 >> put t1 >> put t2
374 putWord8 2 >> put name
377 putWord8 3 >> put t1 >> put t2
380 putWord8 4 >> put fs >> put ext
382 Aliased var args t ->
383 putWord8 5 >> put var >> put args >> put t
388 0 -> Lambda <$> get <*> get
391 3 -> App <$> get <*> get
392 4 -> Record <$> get <*> get
393 5 -> Aliased <$> get <*> get <*> get
394 _ -> error "Error reading a valid type from serialized string"
397instance Binary t => Binary (Aliased t) where
401 putWord8 0 >> put tipe
404 putWord8 1 >> put tipe
411 _ -> error "Error reading a valid type from serialized string"
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