3import Control.Applicative ((<$>), (<*>))
4import Control.Arrow (second)
6import qualified Data.Map as Map
8import qualified AST.Variable as Var
10import qualified AST.Helpers as Help
11import Text.PrettyPrint as P
15 = Lambda (Type var) (Type var)
18 | App (Type var) [Type var]
19 | Record [(String, Type var)] (Maybe (Type var))
20 | Aliased Var.Canonical (Type var)
32fieldMap :: [(String,a)] -> Map.Map String [a]
34 let add r (field,tipe) =
35 Map.insertWith (++) field [tipe] r
37 foldl add Map.empty fields
40recordOf :: [(String, Type var)] -> Type var
45listOf :: RawType -> RawType
47 App (Type (Var.Raw "List")) [tipe]
50tupleOf :: [RawType] -> RawType
52 let name = Var.Raw ("_Tuple" ++ show (length types))
57dealias :: Type v -> Type v
61 Lambda (dealias arg) (dealias body)
70 App (dealias f) (map dealias args)
73 Record (map (second dealias) fields) (fmap dealias ext)
79instance (Var.ToString var, Pretty var) => Pretty (Type var) where
83 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
85 t:ts = map prettyLambda (collectLambdas tipe)
88 Lambda _ _ -> P.parens (pretty t)
95 let v = Var.toString var
97 P.text (if v == "_Tuple0" then "()" else v)
102 | Help.isTuple (Var.toString name) ->
103 P.parens (P.sep (P.punctuate P.comma (map pretty args)))
105 _ -> P.hang (pretty f) 2 (P.sep (map prettyParens args))
108 case flattenRecord tipe of
114 [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
120 (P.lbrace <+> P.text x <+> P.text "|")
123 [ P.cat (zipWith (<+>) (P.space : repeat P.comma) (map prettyField fields))
127 prettyField (field, tipe) =
128 P.text field <+> P.text ":" <+> pretty tipe
133 if show t' `elem` ["Int", "Float", "String", "Char", "Bool"]
138collectLambdas :: Type var -> [Type var]
142 arg : collectLambdas body
148prettyParens :: (Var.ToString var, Pretty var) => Type var -> Doc
150 parensIf (needed tipe) (pretty tipe)
154 Aliased _ t' -> needed t'
159 | Help.isTuple (Var.toString name) ->
162 App t' [] -> needed t'
169flattenRecord :: Type var -> ( [(String, Type var)], Maybe String )
175 Record fields Nothing ->
178 Record fields (Just ext) ->
179 let (fields',ext') = flattenRecord ext
181 (fields' ++ fields, ext')
187 error "Trying to flatten ill-formed record."
190instance Binary var => Binary (Type var) where
193 Lambda t1 t2 -> putWord8 0 >> put t1 >> put t2
194 Var x -> putWord8 1 >> put x
195 Type name -> putWord8 2 >> put name
196 App t1 t2 -> putWord8 3 >> put t1 >> put t2
197 Record fs ext -> putWord8 4 >> put fs >> put ext
198 Aliased var t -> putWord8 5 >> put var >> put t
203 0 -> Lambda <$> get <*> get
206 3 -> App <$> get <*> get
207 4 -> Record <$> get <*> get
208 5 -> Aliased <$> get <*> get
209 _ -> 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