3import Control.Applicative ((<$>), (<*>))
5import qualified Data.Map as Map
7import qualified AST.Variable as Var
9import qualified AST.Helpers as Help
10import Text.PrettyPrint as P
14 = Lambda (Type var) (Type var)
17 | App (Type var) [Type var]
18 | Record [(String, Type var)] (Maybe (Type var))
19 | Aliased Var.Canonical (Type var)
31fieldMap :: [(String,a)] -> Map.Map String [a]
33 foldl (\r (field,tipe) -> Map.insertWith (++) field [tipe] r) Map.empty fields
36recordOf :: [(String, Type var)] -> Type var
41listOf :: RawType -> RawType
43 App (Type (Var.Raw "List")) [tipe]
46tupleOf :: [RawType] -> RawType
48 let name = Var.Raw ("_Tuple" ++ show (length types))
53instance (Var.ToString var, Pretty var) => Pretty (Type var) where
57 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
59 t:ts = map prettyLambda (collectLambdas tipe)
62 Lambda _ _ -> P.parens (pretty t)
69 let v = Var.toString var
71 P.text (if v == "_Tuple0" then "()" else v)
76 | Help.isTuple (Var.toString name) ->
77 P.parens (P.sep (P.punctuate P.comma (map pretty args)))
79 _ -> P.hang (pretty f) 2 (P.sep (map prettyParens args))
82 case flattenRecord tipe of
88 [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
94 (P.lbrace <+> P.text x <+> P.text "|")
97 [ P.cat (zipWith (<+>) (P.space : repeat P.comma) (map prettyField fields))
101 prettyField (field, tipe) =
102 P.text field <+> P.text ":" <+> pretty tipe
107 if show t' `elem` ["Int", "Float", "String", "Char", "Bool"]
112collectLambdas :: Type var -> [Type var]
116 arg : collectLambdas body
122prettyParens :: (Var.ToString var, Pretty var) => Type var -> Doc
124 parensIf (needed tipe) (pretty tipe)
128 Aliased _ t' -> needed t'
133 | Help.isTuple (Var.toString name) ->
136 App t' [] -> needed t'
143flattenRecord :: Type var -> ( [(String, Type var)], Maybe String )
149 Record fields Nothing ->
152 Record fields (Just ext) ->
153 let (fields',ext') = flattenRecord ext
155 (fields' ++ fields, ext')
161 error "Trying to flatten ill-formed record."
164instance Binary var => Binary (Type var) where
167 Lambda t1 t2 -> putWord8 0 >> put t1 >> put t2
168 Var x -> putWord8 1 >> put x
169 Type name -> putWord8 2 >> put name
170 App t1 t2 -> putWord8 3 >> put t1 >> put t2
171 Record fs ext -> putWord8 4 >> put fs >> put ext
172 Aliased var t -> putWord8 5 >> put var >> put t
177 0 -> Lambda <$> get <*> get
180 3 -> App <$> get <*> get
181 4 -> Record <$> get <*> get
182 5 -> Aliased <$> get <*> get
183 _ -> 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