2 ( Type(..), AliasType(..)
3 , RawType, CanonicalType
4 , PortType(..), portType
5 , fieldMap, recordOf, listOf, tupleOf
11import Control.Applicative ((<$>), (<*>))
12import Control.Arrow (second)
14import qualified Data.Map as Map
16import qualified AST.Variable as Var
18import qualified AST.Helpers as Help
19import Text.PrettyPrint as P
25 = Lambda (Type var) (Type var)
28 | App (Type var) [Type var]
29 | Record [(String, Type var)] (Maybe (Type var))
30 | Aliased Var.Canonical [(String, Type var)] (AliasType var)
50 | Signal { root :: Type var, arg :: Type var }
54portType :: PortType var -> Type var
61fieldMap :: [(String,a)] -> Map.Map String [a]
63 let add r (field,tipe) =
64 Map.insertWith (++) field [tipe] r
66 foldl add Map.empty fields
69recordOf :: [(String, Type var)] -> Type var
74listOf :: RawType -> RawType
76 App (Type (Var.Raw "List")) [tipe]
79tupleOf :: [RawType] -> RawType
81 let name = Var.Raw ("_Tuple" ++ show (length types))
88deepDealias :: Type v -> Type v
90 let go = deepDealias in
99 Record (map (second go) fields) (fmap go ext)
101 Aliased _name args tipe' ->
102 deepDealias (dealias args tipe')
108 App (go f) (map go args)
111dealias :: [(String, Type v)] -> AliasType v -> Type v
112dealias args aliasType =
115 dealiasHelp (Map.fromList args) tipe
121dealiasHelp :: Map.Map String (Type var) -> Type var -> Type var
122dealiasHelp typeTable tipe =
123 let go = dealiasHelp typeTable in
129 Map.findWithDefault tipe x typeTable
132 Record (map (second go) fields) (fmap go ext)
134 Aliased original args t' ->
135 Aliased original (map (second go) args) t'
141 App (go f) (map go args)
146instance (Pretty var, Var.ToString var) => Pretty (PortType var) where
156instance (Var.ToString var, Pretty var) => Pretty (Type var) where
160 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
162 t:ts = map prettyLambda (collectLambdas tipe)
165 Lambda _ _ -> P.parens (pretty t)
172 let v = Var.toString var
174 P.text (if v == "_Tuple0" then "()" else v)
179 | Help.isTuple (Var.toString name) ->
180 P.parens (P.sep (P.punctuate P.comma (map pretty args)))
182 _ -> P.hang (pretty f) 2 (P.sep (map prettyParens args))
185 case flattenRecord tipe of
191 [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
197 (P.lbrace <+> P.text x <+> P.text "|")
200 [ P.cat (zipWith (<+>) (P.space : repeat P.comma) (map prettyField fields))
204 prettyField (field, tipe) =
205 P.text field <+> P.text ":" <+> pretty tipe
207 Aliased name args _ ->
208 P.hang (pretty name) 2 (P.sep (map (prettyParens . snd) args))
211collectLambdas :: Type var -> [Type var]
215 arg : collectLambdas body
221prettyParens :: (Var.ToString var, Pretty var) => Type var -> Doc
223 parensIf (needed tipe) (pretty tipe)
227 Aliased _ [] _ -> False
229 Aliased _ _ _ -> True
234 | Help.isTuple (Var.toString name) ->
237 App t' [] -> needed t'
244flattenRecord :: Type var -> ( [(String, Type var)], Maybe String )
250 Record fields Nothing ->
253 Record fields (Just ext) ->
254 let (fields',ext') = flattenRecord ext
256 (fields' ++ fields, ext')
258 Aliased _ args tipe' ->
259 flattenRecord (dealias args tipe')
262 error "Trying to flatten ill-formed record."
267instance Binary var => Binary (Type var) where
271 putWord8 0 >> put t1 >> put t2
277 putWord8 2 >> put name
280 putWord8 3 >> put t1 >> put t2
283 putWord8 4 >> put fs >> put ext
285 Aliased var args t ->
286 putWord8 5 >> put var >> put args >> put t
291 0 -> Lambda <$> get <*> get
294 3 -> App <$> get <*> get
295 4 -> Record <$> get <*> get
296 5 -> Aliased <$> get <*> get <*> get
297 _ -> error "Error reading a valid type from serialized string"
300instance Binary var => Binary (AliasType var) where
304 putWord8 0 >> put tipe
307 putWord8 1 >> put tipe
314 _ -> 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