1{-# OPTIONS_GHC -Wall #-}
2module SourceSyntax.Type where
5import qualified Data.Map as Map
6import qualified SourceSyntax.Helpers as Help
7import Control.Applicative ((<$>), (<*>))
8import SourceSyntax.PrettyPrint
9import Text.PrettyPrint as P
11data Type = Lambda Type Type
14 | Record [(String,Type)] (Maybe String)
17fieldMap :: [(String,a)] -> Map.Map String [a]
19 foldl (\r (x,t) -> Map.insertWith (++) x [t] r) Map.empty fields
21recordOf :: [(String,Type)] -> Type
22recordOf fields = Record fields Nothing
25listOf t = Data "_List" [t]
27tupleOf :: [Type] -> Type
28tupleOf ts = Data ("_Tuple" ++ show (length ts)) ts
31instance Pretty Type where
34 Lambda _ _ -> P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
35 where t:ts = collectLambdas tipe
37 Data "_List" [t] -> P.brackets (pretty t)
39 | Help.isTuple name -> P.parens . P.sep . P.punctuate P.comma $ map pretty tipes
40 | otherwise -> P.hang (P.text name) 2 (P.sep $ map prettyParens tipes)
42 P.braces $ case ext of
43 Nothing -> prettyFields
44 Just x -> P.hang (P.text x <+> P.text "|") 4 prettyFields
46 prettyField (f,t) = P.text f <+> P.text ":" <+> pretty t
47 prettyFields = commaSep . map prettyField $ fields
49collectLambdas :: Type -> [Doc]
52 Lambda arg@(Lambda _ _) body -> P.parens (pretty arg) : collectLambdas body
53 Lambda arg body -> pretty arg : collectLambdas body
56prettyParens :: Type -> Doc
57prettyParens tipe = parensIf needed (pretty tipe)
62 Data "_List" [_] -> False
67instance Binary Type where
71 putWord8 0 >> put t1 >> put t2
75 putWord8 2 >> put ctor >> put tipes
77 putWord8 3 >> put fs >> put ext
82 0 -> Lambda <$> get <*> get
84 2 -> Data <$> get <*> get
85 3 -> Record <$> get <*> get
86 _ -> 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