3 , Canonical(..), Aliased(..)
11import Control.Applicative ((<$>), (<*>))
12import Control.Arrow (second)
14import qualified Data.Map as Map
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 }
61portType :: 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 needsParens portType =
157 P.pretty needsParens tipe
160 P.pretty needsParens tipe
163instance P.Pretty Raw' where
168instance P.Pretty Canonical where
169 pretty needsParens tipe =
172 P.parensIf needsParens $
173 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
176 map prettyLambda (collectLambdas tipe)
189 let v = Var.toString var
191 P.text (if v == "_Tuple0" then "()" else v)
196 | Help.isTuple (Var.toString name) ->
198 P.punctuate P.comma (map (P.pretty False) args)
204 P.parensIf needsParens $
208 (P.sep (map (P.pretty True) args))
211 case flattenRecord tipe of
217 [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
223 (P.lbrace <+> P.text x <+> P.text "|")
226 [ P.cat (zipWith (<+>) (P.space : repeat P.comma) (map prettyField fields))
230 prettyField (field, tipe) =
231 P.text field <+> P.text ":" <+> P.pretty False tipe
233 Aliased name args _ ->
234 P.parensIf (needsParens && not (null args)) $
236 (P.pretty False name)
238 (P.sep (map (P.pretty True . snd) args))
241collectLambdas :: Canonical -> [Canonical]
245 arg : collectLambdas body
251flattenRecord :: Canonical -> ( [(String, Canonical)], Maybe String )
257 Record fields Nothing ->
260 Record fields (Just ext) ->
261 let (fields',ext') = flattenRecord ext
263 (fields' ++ fields, ext')
265 Aliased _ args tipe' ->
266 flattenRecord (dealias args tipe')
269 error "Trying to flatten ill-formed record."
274instance Binary Canonical where
278 putWord8 0 >> put t1 >> put t2
284 putWord8 2 >> put name
287 putWord8 3 >> put t1 >> put t2
290 putWord8 4 >> put fs >> put ext
292 Aliased var args t ->
293 putWord8 5 >> put var >> put args >> put t
298 0 -> Lambda <$> get <*> get
301 3 -> App <$> get <*> get
302 4 -> Record <$> get <*> get
303 5 -> Aliased <$> get <*> get <*> get
304 _ -> error "Error reading a valid type from serialized string"
307instance Binary t => Binary (Aliased t) where
311 putWord8 0 >> put tipe
314 putWord8 1 >> put tipe
321 _ -> 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