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
13 = Lambda (Type var) (Type var)
16 | App (Type var) [Type var]
17 | Record [(String, Type var)] (Maybe (Type var))
18 | Aliased Var.Canonical (Type var)
21type RawType = Type Var.Raw
22type CanonicalType = Type Var.Canonical
24fieldMap :: [(String,a)] -> Map.Map String [a]
26 foldl (\r (x,t) -> Map.insertWith (++) x [t] r) Map.empty fields
28recordOf :: [(String, Type var)] -> Type var
29recordOf fields = Record fields Nothing
31listOf :: RawType -> RawType
32listOf t = App (Type (Var.Raw "_List")) [t]
34tupleOf :: [RawType] -> RawType
35tupleOf ts = App (Type t) ts
37 t = Var.Raw ("_Tuple" ++ show (length ts))
39instance (Var.ToString var, Pretty var) => Pretty (Type var) where
42 Lambda _ _ -> P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
44 t:ts = map prettyLambda (collectLambdas tipe)
45 prettyLambda t = case t of
46 Lambda _ _ -> P.parens (pretty t)
52 let v = Var.toString var in
53 P.text (if v == "_Tuple0" then "()" else v)
58 | Var.toString name == "_List" -> P.brackets (pretty t)
61 | Help.isTuple (Var.toString name) ->
62 P.parens . P.sep . P.punctuate P.comma $ map pretty args
64 _ -> P.hang (pretty f) 2 (P.sep $ map prettyParens args)
67 P.braces $ case flattenRecord tipe of
68 (fields, Nothing) -> prettyFields fields
70 P.hang (P.text x <+> P.text "|") 4 (prettyFields fields)
72 prettyField (f,t) = P.text f <+> P.text ":" <+> pretty t
73 prettyFields fields = commaSep (map prettyField fields)
77 if show t' `elem` ["Int", "Float", "String", "Char", "Bool"]
81collectLambdas :: Type var -> [Type var]
84 Lambda arg body -> arg : collectLambdas body
87prettyParens :: (Var.ToString var, Pretty var) => Type var -> Doc
88prettyParens tipe = parensIf (needed tipe) (pretty tipe)
92 Aliased _ t' -> needed t'
96 App (Type name) [_] | Var.toString name == "_List" -> False
97 App (Type name) _ | Help.isTuple (Var.toString name) -> False
98 App t' [] -> needed t'
103flattenRecord :: Type var -> ( [(String, Type var)], Maybe String )
106 Var x -> ([], Just x)
108 Record fields Nothing -> (fields, Nothing)
110 Record fields (Just ext) ->
111 let (fields',ext') = flattenRecord ext
112 in (fields' ++ fields, ext')
114 Aliased _ tipe' -> flattenRecord tipe'
116 _ -> error "Trying to flatten ill-formed record."
118instance Binary var => Binary (Type var) where
121 Lambda t1 t2 -> putWord8 0 >> put t1 >> put t2
122 Var x -> putWord8 1 >> put x
123 Type name -> putWord8 2 >> put name
124 App t1 t2 -> putWord8 3 >> put t1 >> put t2
125 Record fs ext -> putWord8 4 >> put fs >> put ext
126 Aliased var t -> putWord8 5 >> put var >> put t
131 0 -> Lambda <$> get <*> get
134 3 -> App <$> get <*> get
135 4 -> Record <$> get <*> get
136 5 -> Aliased <$> get <*> get
137 _ -> 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