4import Control.Applicative ((<$>), (<*>))
6import qualified Data.Map as Map
8import qualified AST.Variable as Var
10import qualified AST.Helpers as Help
11import 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)
22type RawType = Type Var.Raw
23type CanonicalType = Type Var.Canonical
25fieldMap :: [(String,a)] -> Map.Map String [a]
27 foldl (\r (x,t) -> Map.insertWith (++) x [t] r) Map.empty fields
29recordOf :: [(String, Type var)] -> Type var
30recordOf fields = Record fields Nothing
32listOf :: RawType -> RawType
33listOf t = App (Type (Var.Raw "_List")) [t]
35tupleOf :: [RawType] -> RawType
36tupleOf ts = App (Type t) ts
38 t = Var.Raw ("_Tuple" ++ show (length ts))
40instance (Var.ToString var, Pretty var) => Pretty (Type var) where
43 Lambda _ _ -> P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
45 t:ts = map prettyLambda (collectLambdas tipe)
46 prettyLambda t = case t of
47 Lambda _ _ -> P.parens (pretty t)
53 let v = Var.toString var in
54 P.text (if v == "_Tuple0" then "()" else v)
59 | Var.toString name == "_List" -> P.brackets (pretty t)
62 | Help.isTuple (Var.toString name) ->
63 P.parens . P.sep . P.punctuate P.comma $ map pretty args
65 _ -> P.hang (pretty f) 2 (P.sep $ map prettyParens args)
68 P.braces $ case flattenRecord tipe of
69 (fields, Nothing) -> prettyFields fields
71 P.hang (P.text x <+> P.text "|") 4 (prettyFields fields)
73 prettyField (f,t) = P.text f <+> P.text ":" <+> pretty t
74 prettyFields fields = commaSep (map prettyField fields)
78 if show t' `elem` ["Int", "Float", "String", "Char", "Bool"]
82collectLambdas :: Type var -> [Type var]
85 Lambda arg body -> arg : collectLambdas body
88prettyParens :: (Var.ToString var, Pretty var) => Type var -> Doc
89prettyParens tipe = parensIf (needed tipe) (pretty tipe)
93 Aliased _ t' -> needed t'
97 App (Type name) [_] | Var.toString name == "_List" -> False
98 App (Type name) _ | Help.isTuple (Var.toString name) -> False
99 App t' [] -> needed t'
104flattenRecord :: Type var -> ( [(String, Type var)], Maybe String )
107 Var x -> ([], Just x)
109 Record fields Nothing -> (fields, Nothing)
111 Record fields (Just ext) ->
112 let (fields',ext') = flattenRecord ext
113 in (fields' ++ fields, ext')
115 Aliased _ tipe' -> flattenRecord tipe'
117 _ -> error "Trying to flatten ill-formed record."
119instance Binary var => Binary (Type var) where
122 Lambda t1 t2 -> putWord8 0 >> put t1 >> put t2
123 Var x -> putWord8 1 >> put x
124 Type name -> putWord8 2 >> put name
125 App t1 t2 -> putWord8 3 >> put t1 >> put t2
126 Record fs ext -> putWord8 4 >> put fs >> put ext
127 Aliased var t -> putWord8 5 >> put var >> put t
132 0 -> Lambda <$> get <*> get
135 3 -> App <$> get <*> get
136 4 -> Record <$> get <*> get
137 5 -> Aliased <$> get <*> get
138 _ -> 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