Printing.hs revision 521e1648b2c66064c41e9ac47bcd510356ed2355
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
{- |
Module : $Header$
Description : Dealing with transformation from/to Haskell and Maude
Copyright : (c) Adrian Riesco, Facultad de Informatica UCM 2009
License : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
Maintainer : ariesco@fdi.ucm.es
Stability : experimental
Portability : portable
Translations between Maude and Haskell
-}
{-
Ref.
...
-}
module Maude.Printing where
import Maude.AS_Maude
import qualified Data.Set as Set
import qualified Data.Map as Map
import qualified Common.Lib.Rel as Rel
import Common.Id (Token)
getSpec :: String -> String
getSpec = quitNil . quitEnd . quitBegin
quitBegin :: String -> String
quitBegin ('S' : ('p' : l)) = 'S' : ('p' : l)
quitBegin (_ : l) = quitBegin l
quitBegin [] = []
quitEnd :: String -> String
quitEnd ('@' : ('#' : ('$' : ('e' : ('n' : _))))) = []
quitEnd (h : l) = h : (quitEnd l)
quitEnd [] = []
quitNil :: String -> String
quitNil = Prelude.filter (/= '\NUL')
printSign sts sbsts ops = ss ++ sbs ++ opd
where ss = sorts2maude sts
sbs = subsorts2maude sbsts
opd = ops2maude ops
sorts2maude :: Set.Set Qid -> String
sorts2maude ss = if Set.null ss
then ""
subsorts2maude :: Rel.Rel Qid -> String
subsorts2maude ssbs = if Rel.null ssbs
then ""
else foldr (++) "" (map printPair $ Rel.toList ssbs)
printPair :: (Token,Token) -> String
printPair (a,b) = "subsort " ++ show a ++ " < " ++ show b ++ " .\n"
ops2maude om = flatten (flatten (map printOp (Map.toList om)))
flatten :: [[a]] -> [a]
flatten [] = []
flatten (a : l) = a ++ (flatten l)
printOp :: (Qid, Set.Set ([Qid], Qid, [Attr])) -> [String]
printOp (opid, s) = map (printOpAux opid) (Set.toList s)
printOpAux :: Qid -> ([Qid], Qid, [Attr]) -> String
printOpAux opid (ar, co, ats) = "op " ++ show opid ++ " : " ++ printArity ar ++
" -> " ++ show co ++ printAttrSet ats ++ " .\n"
printArity :: [Qid] -> String
printArity a = foldr (++) "" (map showSpace a)
showSpace ::Show t => t -> String
showSpace s = show s ++ " "
printAttrSet :: [Attr] -> String
printAttrSet [] = []
printAttrSet ats = " [" ++ printAttrSetAux ats ++ "] "
printAttrSetAux :: [Attr] -> String
printAttrSetAux [] = []
printAttrSetAux [a] = printAttr a
printAttrSetAux (a : ats) = printAttr a ++ " " ++ printAttrSetAux ats
printAttr :: Attr -> String
printAttr Comm = "comm"
printAttr Assoc = "assoc"
printAttr Idem = "idem"
printAttr Iter = "iter"
printAttr Memo = "memo"
printAttr Ctor = "ctor"
printAttr Msg = "msg"
printAttr Object = "object"
printAttr (Id t) = "id: " ++ printTerm t
printAttr (LeftId t) = "id-left: " ++ printTerm t
printAttr (RightId t) = "id-right: " ++ printTerm t
printAttr (Prec p) = "prec " ++ show p
printAttr (Strat ls) = "strat (" ++ printListSpaces ls ++ ")"
printAttr (Poly ls) = "poly (" ++ printListSpaces ls ++ ")"
printAttr (Frozen ls) = if null ls
then "frozen"
else "frozen (" ++ printListSpaces ls ++ ")"
printAttr (Gather ls) = "gather (" ++ printListSpaces ls ++ ")"
printAttr (Format ls) = "format (" ++ printListSpaces ls ++ ")"
printAttr _ = ""
printStmntAttrSet :: [StmntAttr] -> String
printStmntAttrSet [] = []
printStmntAttrSet ats = " [ " ++ printStmntAttrSetAux ats ++ " ] "
printStmntAttrSetAux :: [StmntAttr] -> String
printStmntAttrSetAux [] = []
printStmntAttrSetAux [a] = printAttrStmnt a
printStmntAttrSetAux (a : ats) = printAttrStmnt a ++ " " ++ printStmntAttrSetAux ats
printAttrStmnt :: StmntAttr -> String
printAttrStmnt Owise = "owise"
printAttrStmnt Nonexec = "nonexec"
printAttrStmnt (Metadata s) = "metadata \"" ++ s ++ "\""
printAttrStmnt (Label q) = "label \"" ++ show q ++ "\""
printAttrStmnt (Print _) = ""
printTerm :: Term -> String
printTerm (Const q _) = show q
printTerm (Var q _) = show q
printTerm (Apply q tl) = show q ++ "(" ++ printTermList tl ++ ")"
printTermList :: [Term] -> String
printTermList [] = []
printTermList [t] = printTerm t
printTermList (t : tl) = printTerm t ++ ", " ++ printTermList tl
printListSpaces :: Show t => [t] -> String
printListSpaces = foldr ((++) . showSpace) ""
printMb :: Membership -> String
printMb (Mb t s conds ats) = "mb " ++ printTerm t ++ " : " ++ printSort s ++
printConds conds ++ printStmntAttrSet ats ++ " .\n"
printEq :: Equation -> String
printEq (Eq t1 t2 conds ats) = "eq " ++ printTerm t1 ++ " = " ++ printTerm t2 ++
printConds conds ++ printStmntAttrSet ats ++ " .\n"
printRl :: Rule -> String
printRl (Rl t1 t2 conds ats) = "rl " ++ printTerm t1 ++ " => " ++ printTerm t2 ++
printConds conds ++ printStmntAttrSet ats ++ " .\n"
printSort :: Sort -> String
printSort (SortId q) = show q
printConds :: [Condition] -> String
printConds [] = ""
printConds cs = "if " ++ printCondsAux cs
printCondsAux :: [Condition] -> String
printCondsAux [] = ""
printCondsAux [c] = printCond c
printCondsAux (c : cs) = printCond c ++ " /\\ " ++ printCondsAux cs
printCond :: Condition -> String
printCond (EqCond t1 t2) = printTerm t1 ++ " = " ++ printTerm t2
printCond (MatchCond t1 t2) = printTerm t1 ++ " := " ++ printTerm t2
printCond (MbCond t s) = printTerm t ++ " : " ++ printSort s
printCond (RwCond t1 t2) = printTerm t1 ++ " => " ++ printTerm t2
printMorphism :: Map.Map Qid Qid -> Map.Map Qid (Map.Map ([Qid], Qid) (Qid, ([Qid], Qid))) -> Map.Map Qid Qid -> String
printMorphism sorts ops labels = if str == ""
then ""
else "\n\nMorphism:\n\n" ++ str
where str = (printQidMap "sort" sorts) ++ (printOpRenaming ops)
++ (printQidMap "label" labels)
printQidMap :: String -> Map.Map Qid Qid -> String
printQidMap str = Map.foldWithKey f ""
where f = \ x y z -> str ++ " " ++ show x ++ " to " ++ show y ++ "\n" ++ z
printOpRenaming = Map.foldWithKey f ""
where f = \ x y z -> (Map.foldWithKey (g x) "" y) ++ z
where g = \ from (ar, co) (to, _) z' ->
"op " ++ show from ++ " : " ++ printArity ar ++ " -> "
++ show co ++ " to " ++ show to ++ z'