1 ----------------------------------------------------------------
2 -- the Henk Abstract Syntax
3 -- Copyright 2000, Jan-Willem Roorda and Daan Leijen
4 ----------------------------------------------------------------
9 ----------------------------------------------------------------
11 ----------------------------------------------------------------
12 data Program = Program [TypeDecl] [ValueDecl]
14 data TypeDecl = Data Var [Var]
16 data ValueDecl = Let Bind
19 data Bind = Bind Var Expr
28 | Case Expr [Alt] [Expr]
33 data Alt = Alt Pat Expr
38 data Var = TVar Identifier Expr
40 data Lit = LitInt Integer
42 type Identifier = String
45 isAnonymous s = (null s || (head s == head anonymous))
48 ----------------------------------------------------------------
49 -- pretty print abstract syntax
50 ----------------------------------------------------------------
51 instance Show Program where
52 showsPrec d program = shows (pprogram program)
55 = vcat (map ($$ text "") ds)
59 pprogram (Program tdecls vdecls)
60 = vsep ((map ptdecl tdecls) ++ (map pvdecl vdecls))
63 = (text "data" <+> pbindvar v)
64 $$ indent (text "=" <+> braced (map ptvar vs))
69 Let bind -> text "let" <+> pbind bind
70 LetRec binds -> text "letrec" $$ indent (braced (map pbind binds))
73 = pbindvar v $$ indent (text "=" <+> pexpr e)
75 -- expressions (are parenthesis correct ?)
78 In _ _ -> parens (pexpr e)
79 Pi _ _ -> parens (pexpr e)
80 Lam _ _ -> parens (pexpr e)
81 Case _ _ _ -> parens (pexpr e)
82 App _ _ -> parens (pexpr e)
83 Var (TVar i t) -> case t of
85 other -> parens (pexpr e)
96 App e1 e2 -> pexpr e1 <+> parensExpr e2
97 Case e as ts-> sep $ [text "case" <+> parensExpr e <+> text "of"
98 ,nest 3 (braced (map palt as))
103 ,nest 3 (braced (map pexpr ts))
106 In v e -> sep[ pvdecl v, text "in" <+> pexpr e]
108 TVar i t | isAnonymous i -> parensExpr t <+> text "->" <+> pexpr e
109 TVar i Star -> sep[ text "\\/" <> text i <> text ".", pexpr e]
110 other -> sep[ text "|~|" <> pbindvar v <> text ".", pexpr e]
112 TVar i Star -> sep[ text "/\\" <> text i <> text ".", pexpr e]
113 other -> sep[ text "\\" <> pbindvar v <> text ".", pexpr e]
118 = ppat p <+> text "=>" <+> pexpr e
121 = case p of PatVar v -> pboundvar v
125 pboundvar v@(TVar i e)
126 = case e of Unknown -> text i
129 pbindvar v@(TVar i e)
130 = case e of Star -> text i
134 = text i <> colon <+> pexpr e
138 = case l of LitInt i -> integer i
144 = let prefix = map text $ ["{"] ++ repeat ";"
145 in cat ((zipWith (<+>) prefix ds) ++ [text "}"])