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 _ _ -> P.braces $ case ext of
45 EmptyRecord -> prettyFields
46 _ -> P.hang (pretty ext <+> P.text "|") 4 prettyFields
48 (fields, ext) = collectRecords tipe
49 prettyField (f,t) = P.text f <+> P.text ":" <+> pretty t
50 prettyFields = commaSep . map prettyField $ fields
54 Lambda arg@(Lambda _ _) body -> P.parens (pretty arg) : collectLambdas body
55 Lambda arg body -> pretty arg : collectLambdas body
62 Record fs ext -> go (fs ++ fields) ext
65prettyParens tipe = parensIf needed (pretty tipe)
70 Data "_List" [_] -> False
75instance Binary Type where
79 putWord8 0 >> put t1 >> put t2
83 putWord8 2 >> put ctor >> put tipes
87 putWord8 4 >> put fs >> put ext
92 0 -> Lambda <$> get <*> get
94 2 -> Data <$> get <*> get
95 3 -> return EmptyRecord
96 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