-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathPrinting.hs
162 lines (126 loc) · 5.13 KB
/
Printing.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
{- |
Module : ./Maude/Printing.hs
Description : Translation from Haskell to Maude
Copyright : (c) Martin Kuehl, Uni Bremen 2009
License : GPLv2 or higher, see LICENSE.txt
Maintainer : mkhl@informatik.uni-bremen.de
Stability : experimental
Portability : portable
Translations from Haskell to Maude.
The translations from Haskell datatypes to Maude source code are
implemented as instances of the typeclass 'Pretty' as defined in the
modules "Common.Doc" and "Common.DocUtils", which see.
Nothing else is exported by this module.
-}
module Maude.Printing () where
import Maude.AS_Maude
import Maude.Symbol
import Common.Doc
import Common.DocUtils (Pretty (..))
import Data.List (intersperse)
-- * Combinators
-- | Convert every item in @list@, combine with @dsep@, wrap with @wrap@.
combine :: (Pretty a) => (Doc -> Doc) -> ([Doc] -> Doc) -> [a] -> Doc
combine _ _ [] = empty
combine wrap dsep list = wrap . dsep . map pretty $ list
-- | Separate with spaces, wrap with parentheses.
parenPretties :: (Pretty a) => [a] -> Doc
parenPretties = combine parens hsep
-- | Separate with spaces, wrap with square brackets.
bracketPretties :: (Pretty a) => [a] -> Doc
bracketPretties = combine brackets hsep
-- | Separate with newlines, wrap with square brackets and newlines.
combineHooks :: (Pretty a) => [a] -> Doc
combineHooks = let bracketed doc = lbrack $+$ doc <> rbrack
in combine bracketed vcat
{- | Assemble a pretty-printing for all parts of a Sentence,
distinguishing conditional Sentence from simple ones. -}
prettySentence :: (Pretty a, Pretty b) =>
String -> String -> Doc -> a -> b -> [Condition] -> [StmntAttr] -> Doc
prettySentence s1 s2 op t1 t2 cs as = hsep $ if null cs
then [keyword s1, pretty t1, op, pretty t2, pretty as, dot]
else [keyword s2, pretty t1, op, pretty t2, pretty cs, pretty as, dot]
-- * Pretty instances
-- ** Pretty Sentences
instance Pretty Membership where
pretty (Mb t s cs as) = prettySentence "mb" "cmb" colon t s cs as
instance Pretty Equation where
pretty (Eq t1 t2 cs as) = prettySentence "eq" "ceq" equals t1 t2 cs as
instance Pretty Rule where
pretty (Rl t1 t2 cs as) = prettySentence "rl" "crl" implies t1 t2 cs as
-- ** Pretty Conditions
instance Pretty Condition where
pretty cond = let pretty' x y z = hsep [pretty x, y, pretty z]
in case cond of
MbCond t s -> pretty' t colon s
EqCond t1 t2 -> pretty' t1 equals t2
RwCond t1 t2 -> pretty' t1 implies t2
MatchCond t1 t2 -> pretty' t1 (text ":=") t2
pretties = combine (text "if" <+>) (hsep . intersperse andDoc)
-- ** Pretty Attributes
instance Pretty Attr where
pretty attr = case attr of
Assoc -> text "assoc"
Comm -> text "comm"
Idem -> text "idem"
Iter -> text "iter"
Id term -> text "id:" <+> pretty term
LeftId term -> text "id-left:" <+> pretty term
RightId term -> text "id-right:" <+> pretty term
Strat ints -> text "strat" <+> parenPretties ints
Memo -> text "memo"
Prec int -> text "prec" <+> pretty int
Gather qids -> text "gather" <+> parenPretties qids
Format qids -> text "format" <+> parenPretties qids
Ctor -> text "ctor"
Config -> text "config"
Object -> text "object"
Msg -> text "msg"
Frozen ints -> text "frozen" <+> parenPretties ints
Poly ints -> text "poly" <+> parenPretties ints
Special hooks -> text "special" <+> combineHooks hooks
pretties = bracketPretties
instance Pretty StmntAttr where
pretty attr = case attr of
Owise -> text "owise"
Nonexec -> text "nonexec"
Metadata str -> text "metadata" <+> doubleQuotes (pretty str)
Label qid -> text "label" <+> doubleQuotes (pretty qid)
Print _ -> empty
pretties = bracketPretties
-- ** Pretty Hooks
instance Pretty Hook where
pretty hook = case hook of
IdHook qid qs -> hsep
[text "id-hook", pretty qid, parenPretties qs]
OpHook qid op dom cod -> let symb = mkOpPartial op dom cod
in hsep [text "op-hook", pretty qid, parens $ pretty symb]
TermHook qid term -> hsep
[text "term-hook", pretty qid, parens $ pretty term]
pretties = combine parens vcat
-- ** Pretty Terms
instance Pretty Term where
pretty term = case term of
Const qid _ -> pretty qid
Var qid tp -> hcat [pretty qid, colon, pretty tp]
Apply qid ts _ -> pretty qid <> (parens . pretty $ ts)
pretties = combine id sepByCommas
-- ** Pretty Identifiers
instance Pretty Type where
pretty typ = case typ of
TypeSort sort -> pretty sort
TypeKind kind -> pretty kind
instance Pretty Sort where
pretty (SortId qid) = pretty qid
instance Pretty Kind where
pretty (KindId qid) = brackets $ pretty qid
instance Pretty ParamId where
pretty (ParamId qid) = pretty qid
instance Pretty ViewId where
pretty (ViewId qid) = pretty qid
instance Pretty ModId where
pretty (ModId qid) = pretty qid
instance Pretty LabelId where
pretty (LabelId qid) = pretty qid
instance Pretty OpId where
pretty (OpId qid) = pretty qid