3 , Canonical(..), Aliased(..)
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 }
62portType :: 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 needsParens portType =
158 P.pretty needsParens tipe
161 P.pretty needsParens tipe
164instance P.Pretty Raw' where
165 pretty needsParens tipe =
168 P.parensIf needsParens (prettyLambda getRawLambda arg body)
178 isTuple (A.A _ (RType name)) = Help.isTuple (Var.toString name)
181 prettyApp needsParens isTuple func args
183 RRecord fields ext ->
184 prettyRecord (flattenRawRecord fields ext)
187instance P.Pretty Canonical where
188 pretty needsParens tipe =
191 P.parensIf needsParens (prettyLambda getCanLambda arg body)
201 isTuple (Type name) = Help.isTuple (Var.toString name)
204 prettyApp needsParens isTuple func args
207 prettyRecord (flattenCanRecord fields ext)
209 Aliased name args _ ->
210 P.parensIf (needsParens && not (null args)) $
212 (P.pretty False name)
214 (P.sep (map (P.pretty True . snd) args))
219prettyType :: (Var.ToString var) => var -> P.Doc
224 P.text (if v == "_Tuple0" then "()" else v)
229prettyLambda :: (P.Pretty t) => (t -> Maybe (t,t)) -> t -> t -> P.Doc
230prettyLambda getLambda arg body =
233 gatherLambda getLambda body
236 P.pretty (Maybe.isJust (getLambda t)) t
240 , P.sep (map (\t -> P.text "->" <+> prettyArg t) rest)
244getRawLambda :: Raw -> Maybe (Raw, Raw)
245getRawLambda (A.A _ tipe) =
247 RLambda arg body -> Just (arg, body)
251getCanLambda :: Canonical -> Maybe (Canonical, Canonical)
254 Lambda arg body -> Just (arg, body)
258gatherLambda :: (t -> Maybe (t,t)) -> t -> [t]
259gatherLambda get tipe =
262 arg : gatherLambda get body
268collectLambdas :: Canonical -> [Canonical]
270 gatherLambda getCanLambda tipe
275prettyApp :: (P.Pretty t) => Bool -> (t -> Bool) -> t -> [t] -> P.Doc
276prettyApp needsParens isTuple func args
279 P.punctuate P.comma (map (P.pretty False) args)
282 P.pretty needsParens func
285 P.parensIf needsParens $
289 (P.sep (map (P.pretty True) args))
294prettyRecord :: (P.Pretty t) => ([(String, t)], Maybe String) -> P.Doc
295prettyRecord recordInfo =
297 prettyField (field, tipe) =
299 (P.text field <+> P.text ":")
301 (P.pretty False tipe)
309 [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
315 (P.lbrace <+> P.text x <+> P.text "|")
318 [ P.cat (zipWith (<+>) (P.space : repeat P.comma) (map prettyField fields))
327 -> ( [(String, Raw)], Maybe String )
328flattenRawRecord fields ext =
333 Just (A.A _ (RVar x)) ->
336 Just (A.A _ (RRecord fields' ext')) ->
337 flattenRawRecord (fields' ++ fields) ext'
340 error "Trying to flatten ill-formed record."
344 :: [(String, Canonical)]
346 -> ( [(String, Canonical)], Maybe String )
347flattenCanRecord fields ext =
355 Just (Record fields' ext') ->
356 flattenCanRecord (fields' ++ fields) ext'
358 Just (Aliased _ args tipe) ->
359 flattenCanRecord fields (Just (dealias args tipe))
362 error "Trying to flatten ill-formed record."
367instance Binary Canonical where
371 putWord8 0 >> put t1 >> put t2
377 putWord8 2 >> put name
380 putWord8 3 >> put t1 >> put t2
383 putWord8 4 >> put fs >> put ext
385 Aliased var args t ->
386 putWord8 5 >> put var >> put args >> put t
391 0 -> Lambda <$> get <*> get
394 3 -> App <$> get <*> get
395 4 -> Record <$> get <*> get
396 5 -> Aliased <$> get <*> get <*> get
397 _ -> error "Error reading a valid type from serialized string"
400instance Binary t => Binary (Aliased t) where
404 putWord8 0 >> put tipe
407 putWord8 1 >> put tipe
414 _ -> 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