3 , Canonical(..), Aliased(..)
4 , Port(..), getPortType
11import Control.Applicative ((<$>), (<*>))
12import Control.Arrow (second)
14import qualified Data.Map as Map
15import qualified Data.Maybe as Maybe
16import Text.PrettyPrint as P
18import qualified AST.Variable as Var
19import qualified AST.Helpers as Help
20import qualified Reporting.Annotation as A
21import qualified Reporting.PrettyPrint as P
22import qualified Reporting.Region as R
36 | RRecord [(String, Raw)] (Maybe Raw)
41 = Lambda Canonical Canonical
44 | App Canonical [Canonical]
45 | Record [(String, Canonical)] (Maybe Canonical)
46 | Aliased Var.Canonical [(String, Canonical)] (Aliased Canonical)
47 deriving (Eq, Ord, Show)
53 deriving (Eq, Ord, Show)
58 | Signal { root :: t, arg :: t }
62getPortType :: Port tipe -> tipe
69fieldMap :: [(String,a)] -> Map.Map String [a]
71 let add r (field,tipe) =
72 Map.insertWith (++) field [tipe] r
74 foldl add Map.empty fields
77recordOf :: [(String, Type var)] -> Type var
82listOf :: RawType -> RawType
84 App (Type (Var.Raw "List")) [tipe]
87tuple :: R.Region -> [Raw] -> Raw
89 let name = Var.Raw ("_Tuple" ++ show (length types))
91 A.A region (RApp (A.A region (RType name)) types)
96deepDealias :: Canonical -> Canonical
98 let go = deepDealias in
107 Record (map (second go) fields) (fmap go ext)
109 Aliased _name args tipe' ->
110 deepDealias (dealias args tipe')
116 App (go f) (map go args)
119dealias :: [(String, Canonical)] -> Aliased Canonical -> Canonical
120dealias args aliasType =
123 dealiasHelp (Map.fromList args) tipe
129dealiasHelp :: Map.Map String Canonical -> Canonical -> Canonical
130dealiasHelp typeTable tipe =
131 let go = dealiasHelp typeTable in
137 Map.findWithDefault tipe x typeTable
140 Record (map (second go) fields) (fmap go ext)
142 Aliased original args t' ->
143 Aliased original (map (second go) args) t'
149 App (go f) (map go args)
154instance (P.Pretty t) => P.Pretty (Port t) where
155 pretty dealiaser needsParens portType =
156 P.pretty dealiaser needsParens (getPortType portType)
159instance P.Pretty Raw' where
160 pretty dealiaser needsParens tipe =
163 P.parensIf needsParens (prettyLambda dealiaser getRawLambda arg body)
169 prettyType dealiaser var
173 isTuple (A.A _ (RType name)) = Help.isTuple (Var.toString name)
176 prettyApp dealiaser needsParens isTuple func args
178 RRecord fields ext ->
179 prettyRecord dealiaser (flattenRawRecord fields ext)
182instance P.Pretty Canonical where
183 pretty dealiaser needsParens tipe =
186 P.parensIf needsParens (prettyLambda dealiaser getCanLambda arg body)
192 prettyType dealiaser var
196 isTuple (Type name) = Help.isTuple (Var.toString name)
199 prettyApp dealiaser needsParens isTuple func args
202 prettyRecord dealiaser (flattenCanRecord fields ext)
204 Aliased name args _ ->
205 P.parensIf (needsParens && not (null args)) $
207 (P.pretty dealiaser False name)
209 (P.sep (map (P.pretty dealiaser True . snd) args))
214prettyType :: (Var.ToString var) => P.Dealiaser -> var -> P.Doc
215prettyType dealiaser var =
220 if v == "_Tuple0" then
223 maybe v id (Map.lookup v dealiaser)
228prettyLambda :: (P.Pretty t) => P.Dealiaser -> (t -> Maybe (t,t)) -> t -> t -> P.Doc
229prettyLambda dealiaser getLambda arg body =
232 gatherLambda getLambda body
235 P.pretty dealiaser (Maybe.isJust (getLambda t)) t
239 , P.sep (map (\t -> P.text "->" <+> prettyArg t) rest)
243getRawLambda :: Raw -> Maybe (Raw, Raw)
244getRawLambda (A.A _ tipe) =
246 RLambda arg body -> Just (arg, body)
250getCanLambda :: Canonical -> Maybe (Canonical, Canonical)
253 Lambda arg body -> Just (arg, body)
257gatherLambda :: (t -> Maybe (t,t)) -> t -> [t]
258gatherLambda get tipe =
261 arg : gatherLambda get body
267collectLambdas :: Canonical -> [Canonical]
269 gatherLambda getCanLambda tipe
274prettyApp :: (P.Pretty t) => P.Dealiaser -> Bool -> (t -> Bool) -> t -> [t] -> P.Doc
275prettyApp dealiaser needsParens isTuple func args
278 P.punctuate P.comma (map (P.pretty dealiaser False) args)
281 P.pretty dealiaser needsParens func
284 P.parensIf needsParens $
286 (P.pretty dealiaser True func)
288 (P.sep (map (P.pretty dealiaser True) args))
293prettyRecord :: (P.Pretty t) => P.Dealiaser -> ([(String, t)], Maybe String) -> P.Doc
294prettyRecord dealiaser recordInfo =
296 prettyField (field, tipe) =
298 (P.text field <+> P.text ":")
300 (P.pretty dealiaser False tipe)
308 [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
314 (P.lbrace <+> P.text x <+> P.text "|")
317 [ P.cat (zipWith (<+>) (P.space : repeat P.comma) (map prettyField fields))
326 -> ( [(String, Raw)], Maybe String )
327flattenRawRecord fields ext =
332 Just (A.A _ (RVar x)) ->
335 Just (A.A _ (RRecord fields' ext')) ->
336 flattenRawRecord (fields' ++ fields) ext'
339 error "Trying to flatten ill-formed record."
343 :: [(String, Canonical)]
345 -> ( [(String, Canonical)], Maybe String )
346flattenCanRecord fields ext =
354 Just (Record fields' ext') ->
355 flattenCanRecord (fields' ++ fields) ext'
357 Just (Aliased _ args tipe) ->
358 flattenCanRecord fields (Just (dealias args tipe))
361 error "Trying to flatten ill-formed record."
366instance Binary Canonical where
370 putWord8 0 >> put t1 >> put t2
376 putWord8 2 >> put name
379 putWord8 3 >> put t1 >> put t2
382 putWord8 4 >> put fs >> put ext
384 Aliased var args t ->
385 putWord8 5 >> put var >> put args >> put t
390 0 -> Lambda <$> get <*> get
393 3 -> App <$> get <*> get
394 4 -> Record <$> get <*> get
395 5 -> Aliased <$> get <*> get <*> get
396 _ -> error "Error reading a valid type from serialized string"
399instance Binary t => Binary (Aliased t) where
403 putWord8 0 >> put tipe
406 putWord8 1 >> put tipe
413 _ -> 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