1{-# LANGUAGE DeriveDataTypeable #-}
2module SourceSyntax.Type where
6import qualified Data.Map as Map
7import qualified SourceSyntax.Helpers as Help
8import Control.Applicative ((<$>), (<*>))
9import SourceSyntax.PrettyPrint
10import Text.PrettyPrint as P
12data Type = Lambda Type Type
16 | Record [(String,Type)] Type
17 deriving (Eq, Show, Data, Typeable)
19fieldMap :: [(String,a)] -> Map.Map String [a]
21 foldl (\r (x,t) -> Map.insertWith (++) x [t] r) Map.empty fields
23recordOf :: [(String,Type)] -> Type
24recordOf fields = Record fields EmptyRecord
27listOf t = Data "_List" [t]
29tupleOf :: [Type] -> Type
30tupleOf ts = Data ("_Tuple" ++ show (length ts)) ts
33instance Pretty Type where
36 Lambda t1 t2 -> P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
37 where t:ts = collectLambdas tipe
39 Data "_List" [t] -> P.brackets (pretty t)
41 | Help.isTuple name -> P.parens . P.sep . P.punctuate P.comma $ map pretty tipes
42 | otherwise -> P.hang (P.text name) 2 (P.sep $ map prettyParens tipes)
43 EmptyRecord -> P.braces P.empty
44 Record fields ext -> P.braces $ P.hang (pretty ext <+> P.text "|") 4 prettyFields
46 prettyField (f,t) = P.text f <+> P.text ":" <+> pretty t
47 prettyFields = commaSep . map prettyField $ fields
51 Lambda arg@(Lambda _ _) body -> P.parens (pretty arg) : collectLambdas body
52 Lambda arg body -> pretty arg : collectLambdas body
55prettyParens tipe = parensIf needed (pretty tipe)
64instance Binary Type where
68 putWord8 0 >> put t1 >> put t2
72 putWord8 2 >> put ctor >> put tipes
76 putWord8 4 >> put fs >> put ext
81 0 -> Lambda <$> get <*> get
83 2 -> Data <$> get <*> get
84 3 -> return EmptyRecord
85 4 -> Record <$> get <*> get
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