[project @ 2002-05-31 12:22:33 by panne]
[ghc-base.git] / Text / ParserCombinators / Parsec / examples / Mondrian / Utils.hs
1 {-
2 Copyright(C) 1999 Erik Meijer and Arjan van Yzendoorn
3
4 Determines wether an express/declaration is "simple".
5 The pretty-printing strategy is to print a "complex" expression
6 on a new line.
7 -}
8
9 module Utils where
10
11 import Mondrian
12  
13 isSimpleExpr :: Expr -> Bool
14 isSimpleExpr = \e ->
15   case e of
16     { Lit l -> True
17     ; Var n -> True
18     ; Case e as -> and [ isSimpleArms as, isSimpleExpr e ]
19     ; Let ds e -> and [ isSimpleDecls ds, isSimpleExpr e ]
20     ; Lambda n e -> isSimpleExpr e
21     ; New n ds -> all isSimpleDecl ds
22     ; App f a -> and [ isSimpleExpr f, isSimpleExpr a]
23     ; Chain e oes -> and [ isSimpleExpr e, all isSimpleExpr [ e | (o,e) <- oes ] ]
24     }
25
26 isSimpleArms = \as ->
27   and [ length as == 1, all isSimpleExpr [ e | (p,e) <- as ], all isSimplePattern [ p | (p,e) <- as ] ]
28
29 isSimplePattern = \ p->
30   case p of
31     { Pattern n ds -> isSimpleDecls ds
32     ; Default -> True
33     }
34
35 isSimpleDecls = \ds ->
36   and [ all isSimpleDecl ds ]
37   
38 isSimpleDecl = \d ->
39   case d of
40     { ClassDecl n ns ds -> False
41     ; ImportDecl n -> True
42     ; VarDecl n e -> isSimpleExpr e
43     ; SigDecl n e -> True
44     }
45
46 groupLambdas :: Expr -> Expr
47 groupLambdas = \e ->
48   case e of
49     { Lambda ns (Lambda ms e) -> groupLambdas (Lambda (ns++ms) e)
50     ; otherwise -> e
51     }
52
53 isTopLevel :: [Name] -> Name -> Bool
54 isTopLevel = \topLevel -> \n ->
55   n `elem` topLevel
56   
57 topLevel :: CompilationUnit -> [Name]
58 topLevel = \p ->
59   case p of 
60     { Package n ds -> [ n | VarDecl n e <- ds ]
61     }