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
15 | Record [(String,Type)] Type
18fieldMap :: [(String,a)] -> Map.Map String [a]
20 foldl (\r (x,t) -> Map.insertWith (++) x [t] r) Map.empty fields
22recordOf :: [(String,Type)] -> Type
23recordOf fields = Record fields EmptyRecord
26listOf t = Data "_List" [t]
28tupleOf :: [Type] -> Type
29tupleOf ts = Data ("_Tuple" ++ show (length ts)) ts
32instance Pretty Type where
35 Lambda _ _ -> P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
36 where t:ts = collectLambdas tipe
38 Data "_List" [t] -> P.brackets (pretty t)
40 | Help.isTuple name -> P.parens . P.sep . P.punctuate P.comma $ map pretty tipes
41 | otherwise -> P.hang (P.text name) 2 (P.sep $ map prettyParens tipes)
42 EmptyRecord -> P.braces P.empty
43 Record _ _ -> P.braces $ case ext of
44 EmptyRecord -> prettyFields
45 _ -> P.hang (pretty ext <+> P.text "|") 4 prettyFields
47 (fields, ext) = collectRecords tipe
48 prettyField (f,t) = P.text f <+> P.text ":" <+> pretty t
49 prettyFields = commaSep . map prettyField $ fields
51collectLambdas :: Type -> [Doc]
54 Lambda arg@(Lambda _ _) body -> P.parens (pretty arg) : collectLambdas body
55 Lambda arg body -> pretty arg : collectLambdas body
58collectRecords :: Type -> ([(String,Type)], Type)
63 Record fs ext -> go (fs ++ fields) ext
66prettyParens :: Type -> Doc
67prettyParens tipe = parensIf needed (pretty tipe)
72 Data "_List" [_] -> False
77instance Binary Type where
81 putWord8 0 >> put t1 >> put t2
85 putWord8 2 >> put ctor >> put tipes
89 putWord8 4 >> put fs >> put ext
94 0 -> Lambda <$> get <*> get
96 2 -> Data <$> get <*> get
97 3 -> return EmptyRecord
98 4 -> Record <$> get <*> get
99 _ -> 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