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
97 let go = deepDealias in
106 Record (map (second go) fields) (fmap go ext)
108 Aliased _name args tipe' ->
109 deepDealias (dealias args tipe')
115 App (go f) (map go args)
118dealias :: [(String, Canonical)] -> Aliased Canonical -> Canonical
119dealias args aliasType =
122 dealiasHelp (Map.fromList args) tipe
128dealiasHelp :: Map.Map String Canonical -> Canonical -> Canonical
129dealiasHelp typeTable tipe =
130 let go = dealiasHelp typeTable in
136 Map.findWithDefault tipe x typeTable
139 Record (map (second go) fields) (fmap go ext)
141 Aliased original args t' ->
142 Aliased original (map (second go) args) t'
148 App (go f) (map go args)
153instance (P.Pretty t) => P.Pretty (Port t) where
154 pretty dealiaser needsParens portType =
155 P.pretty dealiaser needsParens (getPortType portType)
158instance P.Pretty Raw' where
159 pretty dealiaser needsParens tipe =
162 P.parensIf needsParens (prettyLambda dealiaser getRawLambda arg body)
168 prettyType dealiaser var
172 isTuple (A.A _ (RType name)) = Help.isTuple (Var.toString name)
175 prettyApp dealiaser needsParens isTuple func args
177 RRecord fields ext ->
178 prettyRecord dealiaser (flattenRawRecord fields ext)
181instance P.Pretty Canonical where
182 pretty dealiaser needsParens tipe =
185 P.parensIf needsParens (prettyLambda dealiaser getCanLambda arg body)
191 prettyType dealiaser var
195 isTuple (Type name) = Help.isTuple (Var.toString name)
198 prettyApp dealiaser needsParens isTuple func args
201 prettyRecord dealiaser (flattenCanRecord fields ext)
203 Aliased name args _ ->
204 P.parensIf (needsParens && not (null args)) $
206 (P.pretty dealiaser False name)
208 (P.sep (map (P.pretty dealiaser True . snd) args))
213prettyType :: (Var.ToString var) => P.Dealiaser -> var -> P.Doc
214prettyType dealiaser var =
219 if v == "_Tuple0" then
222 maybe v id (Map.lookup v dealiaser)
227prettyLambda :: (P.Pretty t) => P.Dealiaser -> (t -> Maybe (t,t)) -> t -> t -> P.Doc
228prettyLambda dealiaser getLambda arg body =
231 gatherLambda getLambda body
234 P.pretty dealiaser (Maybe.isJust (getLambda t)) t
238 , P.sep (map (\t -> P.text "->" <+> prettyArg t) rest)
242getRawLambda :: Raw -> Maybe (Raw, Raw)
243getRawLambda (A.A _ tipe) =
245 RLambda arg body -> Just (arg, body)
249getCanLambda :: Canonical -> Maybe (Canonical, Canonical)
252 Lambda arg body -> Just (arg, body)
256gatherLambda :: (t -> Maybe (t,t)) -> t -> [t]
257gatherLambda get tipe =
260 arg : gatherLambda get body
266collectLambdas :: Canonical -> [Canonical]
268 gatherLambda getCanLambda tipe
273prettyApp :: (P.Pretty t) => P.Dealiaser -> Bool -> (t -> Bool) -> t -> [t] -> P.Doc
274prettyApp dealiaser needsParens isTuple func args
277 P.punctuate P.comma (map (P.pretty dealiaser False) args)
280 P.pretty dealiaser needsParens func
283 P.parensIf needsParens $
285 (P.pretty dealiaser True func)
287 (P.sep (map (P.pretty dealiaser True) args))
292prettyRecord :: (P.Pretty t) => P.Dealiaser -> ([(String, t)], Maybe String) -> P.Doc
293prettyRecord dealiaser recordInfo =
295 prettyField (field, tipe) =
297 (P.text field <+> P.text ":")
299 (P.pretty dealiaser False tipe)
307 [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
313 (P.lbrace <+> P.text x <+> P.text "|")
316 [ P.sep (P.punctuate P.comma (map prettyField fields))
325 -> ( [(String, Raw)], Maybe String )
326flattenRawRecord fields ext =
331 Just (A.A _ (RVar x)) ->
334 Just (A.A _ (RRecord fields' ext')) ->
335 flattenRawRecord (fields' ++ fields) ext'
338 error "Trying to flatten ill-formed record."
342 :: [(String, Canonical)]
344 -> ( [(String, Canonical)], Maybe String )
345flattenCanRecord fields ext =
353 Just (Record fields' ext') ->
354 flattenCanRecord (fields' ++ fields) ext'
356 Just (Aliased _ args tipe) ->
357 flattenCanRecord fields (Just (dealias args tipe))
360 error "Trying to flatten ill-formed record."
365instance Binary Canonical where
369 putWord8 0 >> put t1 >> put t2
375 putWord8 2 >> put name
378 putWord8 3 >> put t1 >> put t2
381 putWord8 4 >> put fs >> put ext
383 Aliased var args t ->
384 putWord8 5 >> put var >> put args >> put t
389 0 -> Lambda <$> get <*> get
392 3 -> App <$> get <*> get
393 4 -> Record <$> get <*> get
394 5 -> Aliased <$> get <*> get <*> get
395 _ -> error "Error reading a valid type from serialized string"
398instance Binary t => Binary (Aliased t) where
402 putWord8 0 >> put tipe
405 putWord8 1 >> put tipe
412 _ -> 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