@@ -9,32 +9,64 @@ import qualified SourceSyntax.Pattern as Pattern
9import qualified SourceSyntax.Type as Type
9import qualified SourceSyntax.Type as Type
10import qualified SourceSyntax.Literal as Literal
10import qualified SourceSyntax.Literal as Literal
12+{-| This is a located expression. -}
12−type LExpr tipe var = Location.Located (Expr tipe var)
13+type LExpr' def = Location.Located (Expr' def)
15+{-| This is a fully general expression. The compilation process will enrich
16+this data structure with additional information after the initial parse.
17+The type holes let us reflect these structural changes in the types.
19+ def: Parsing allows two kinds of definitions (type annotations or definitions),
20+ but later checks will see that they are well formed and combine them.
22+ t: for adding type information, currently unused
24+ v: for enriching variables with information like provenonce, currently unused
26+Please don't get on my case to take the unused ones out. I have considered it.
27+It's a huge pain to add and remove, it will get used eventually, and it's not
28+hurting anything as it is.
14 = Literal Literal.Literal
31 = Literal Literal.Literal
15 | Var String
32 | Var String
16− | Range (LExpr t v) (LExpr t v)
33+ | Range (LExpr' def) (LExpr' def)
17− | ExplicitList [LExpr t v]
34+ | ExplicitList [LExpr' def]
18− | Binop String (LExpr t v) (LExpr t v)
35+ | Binop String (LExpr' def) (LExpr' def)
19− | Lambda Pattern.Pattern (LExpr t v)
36+ | Lambda Pattern.Pattern (LExpr' def)
20− | App (LExpr t v) (LExpr t v)
37+ | App (LExpr' def) (LExpr' def)
21− | MultiIf [(LExpr t v,LExpr t v)]
38+ | MultiIf [(LExpr' def,LExpr' def)]
22− | Let [Def t v] (LExpr t v)
39+ | Let [def] (LExpr' def)
23− | Case (LExpr t v) [(Pattern.Pattern, LExpr t v)]
40+ | Case (LExpr' def) [(Pattern.Pattern, LExpr' def)]
24− | Data String [LExpr t v]
41+ | Data String [LExpr' def]
25− | Access (LExpr t v) String
42+ | Access (LExpr' def) String
26− | Remove (LExpr t v) String
43+ | Remove (LExpr' def) String
27− | Insert (LExpr t v) String (LExpr t v)
44+ | Insert (LExpr' def) String (LExpr' def)
28− | Modify (LExpr t v) [(String, LExpr t v)]
45+ | Modify (LExpr' def) [(String, LExpr' def)]
29− | Record [(String, LExpr t v)]
46+ | Record [(String, LExpr' def)]
30− | Markdown String String [LExpr t v]
47+ | Markdown String String [LExpr' def]
31 deriving (Eq)
48 deriving (Eq)
33−data Def tipe var
50+type ParseExpr = Expr' ParseDef
51+type LParseExpr = LExpr' ParseDef
52+type LExpr = LExpr' Def
55+{-| Style of definitions as they are parsed. Users specify them
56+separately, and other checks will make sure they can be combined.
34− = Def Pattern.Pattern (LExpr tipe var)
59+ = Def Pattern.Pattern LParseExpr
35 | TypeAnnotation String Type.Type
60 | TypeAnnotation String Type.Type
36 deriving (Eq, Show)
61 deriving (Eq, Show)
63+{-| After checking that type annotations and definitions are all
64+valid, they can be combined.
67+ = Definition Pattern.Pattern (LExpr' Def) (Maybe Type.Type)
38tuple es = Data ("_Tuple" ++ show (length es)) es
70tuple es = Data ("_Tuple" ++ show (length es)) es
40delist (Location.L _ (Data "::" [h,t])) = h : delist t
72delist (Location.L _ (Data "::" [h,t])) = h : delist t
@@ -45,10 +77,10 @@ saveEnvName = "_save_the_environment!!!"
45dummyLet defs =
77dummyLet defs =
46 Location.none $ Let defs (Location.none $ Var saveEnvName)
78 Location.none $ Let defs (Location.none $ Var saveEnvName)
48−instance Show (Expr t v) where
80+instance Pretty def => Show (Expr' def) where
49 show = render . pretty
81 show = render . pretty
51−instance Pretty (Expr t v) where
83+instance Pretty def => Pretty (Expr' def) where
52 pretty expr =
84 pretty expr =
53 case expr of
85 case expr of
54 Literal lit -> pretty lit
86 Literal lit -> pretty lit
@@ -80,9 +112,9 @@ instance Pretty (Expr t v) where
80 Access e x -> prettyParens e <> P.text "." <> variable x
112 Access e x -> prettyParens e <> P.text "." <> variable x
81 Remove e x -> P.braces (pretty e <+> P.text "-" <+> variable x)
113 Remove e x -> P.braces (pretty e <+> P.text "-" <+> variable x)
82 Insert (Location.L _ (Remove e y)) x v ->
114 Insert (Location.L _ (Remove e y)) x v ->
83− P.braces (pretty e <+> P.text "-" <+> variable y <+> P.text "|" <+> variable x <+> P.text "=" <+> pretty v)
115+ P.braces (pretty e <+> P.text "-" <+> variable y <+> P.text "|" <+> variable x <+> P.equals <+> pretty v)
84 Insert e x v ->
116 Insert e x v ->
85− P.braces (pretty e <+> P.text "|" <+> variable x <+> P.text "=" <+> pretty v)
117+ P.braces (pretty e <+> P.text "|" <+> variable x <+> P.equals <+> pretty v)
87 Modify e fs ->
119 Modify e fs ->
88 P.braces $ P.hang (pretty e <+> P.text "|")
120 P.braces $ P.hang (pretty e <+> P.text "|")
@@ -94,17 +126,26 @@ instance Pretty (Expr t v) where
94 Record fs ->
126 Record fs ->
95 P.braces $ P.nest 2 (commaSep $ map field fs)
127 P.braces $ P.nest 2 (commaSep $ map field fs)
97− field (x,e) = variable x <+> P.text "=" <+> pretty e
129+ field (x,e) = variable x <+> P.equals <+> pretty e
99 Markdown _ _ _ -> P.text "[markdown| ... |]"
131 Markdown _ _ _ -> P.text "[markdown| ... |]"
101−instance Pretty (Def t v) where
133+instance Pretty ParseDef where
102 pretty def =
134 pretty def =
103 case def of
135 case def of
104 TypeAnnotation name tipe ->
136 TypeAnnotation name tipe ->
105− variable name <+> P.text ":" <+> pretty tipe
137+ variable name <+> P.colon <+> pretty tipe
106 Def pattern expr ->
138 Def pattern expr ->
107− pretty pattern <+> P.text "=" <+> pretty expr
139+ pretty pattern <+> P.equals <+> pretty expr
141+instance Pretty Def where
142+ pretty (Definition pattern expr maybeTipe) =
143+ P.vcat [ annotation, definition ]
145+ definition = pretty pattern <+> P.equals <+> pretty expr
146+ annotation = case maybeTipe of
148+ Just tipe -> pretty pattern <+> P.colon <+> pretty tipe
109collectApps lexpr@(Location.L _ expr) =
150collectApps lexpr@(Location.L _ expr) =
110 case expr of
151 case expr of