69222
|
1 |
(* Title: Tools/Haskell/Haskell.thy
|
|
2 |
Author: Makarius
|
69225
|
3 |
|
|
4 |
Support for Isabelle tools in Haskell.
|
69222
|
5 |
*)
|
|
6 |
|
|
7 |
theory Haskell
|
|
8 |
imports Pure
|
|
9 |
keywords "generate_haskell_file" "export_haskell_file" :: thy_decl
|
|
10 |
begin
|
|
11 |
|
69225
|
12 |
ML_file "haskell.ML"
|
|
13 |
|
|
14 |
|
|
15 |
section \<open>Commands\<close>
|
|
16 |
|
69222
|
17 |
ML \<open>
|
|
18 |
Outer_Syntax.command \<^command_keyword>\<open>generate_haskell_file\<close> "generate Haskell file"
|
69225
|
19 |
(Parse.position Parse.path -- (\<^keyword>\<open>=\<close> |-- Parse.input Parse.embedded)
|
|
20 |
>> Haskell.generate_file_cmd);
|
|
21 |
|
|
22 |
Outer_Syntax.command \<^command_keyword>\<open>export_haskell_file\<close> "export Haskell file"
|
|
23 |
(Parse.name -- (\<^keyword>\<open>=\<close> |-- Parse.input Parse.embedded)
|
|
24 |
>> Haskell.export_file_cmd);
|
|
25 |
\<close>
|
|
26 |
|
|
27 |
|
|
28 |
section \<open>Source modules\<close>
|
|
29 |
|
69240
|
30 |
generate_haskell_file "Library.hs" = \<open>
|
69226
|
31 |
{- Title: Tools/Haskell/Library.hs
|
69225
|
32 |
Author: Makarius
|
|
33 |
LICENSE: BSD 3-clause (Isabelle)
|
|
34 |
|
|
35 |
Basic library of Isabelle idioms.
|
|
36 |
-}
|
|
37 |
|
69240
|
38 |
module Isabelle.Library (
|
|
39 |
(|>), (|->), (#>), (#->),
|
|
40 |
|
|
41 |
the, the_default,
|
|
42 |
|
|
43 |
fold, fold_rev, single, map_index, get_index,
|
|
44 |
|
|
45 |
quote, trim_line)
|
69225
|
46 |
where
|
|
47 |
|
69234
|
48 |
import Data.Maybe
|
|
49 |
|
|
50 |
|
69225
|
51 |
{- functions -}
|
|
52 |
|
|
53 |
(|>) :: a -> (a -> b) -> b
|
|
54 |
x |> f = f x
|
|
55 |
|
|
56 |
(|->) :: (a, b) -> (a -> b -> c) -> c
|
|
57 |
(x, y) |-> f = f x y
|
|
58 |
|
|
59 |
(#>) :: (a -> b) -> (b -> c) -> a -> c
|
|
60 |
(f #> g) x = x |> f |> g
|
|
61 |
|
|
62 |
(#->) :: (a -> (c, b)) -> (c -> b -> d) -> a -> d
|
|
63 |
(f #-> g) x = x |> f |-> g
|
|
64 |
|
|
65 |
|
69234
|
66 |
{- options -}
|
|
67 |
|
69240
|
68 |
the :: Maybe a -> a
|
|
69 |
the (Just x) = x
|
|
70 |
the Nothing = error "the Nothing"
|
|
71 |
|
69234
|
72 |
the_default :: a -> Maybe a -> a
|
|
73 |
the_default x Nothing = x
|
|
74 |
the_default _ (Just y) = y
|
|
75 |
|
|
76 |
|
69225
|
77 |
{- lists -}
|
|
78 |
|
|
79 |
fold :: (a -> b -> b) -> [a] -> b -> b
|
|
80 |
fold _ [] y = y
|
|
81 |
fold f (x : xs) y = fold f xs (f x y)
|
|
82 |
|
|
83 |
fold_rev :: (a -> b -> b) -> [a] -> b -> b
|
|
84 |
fold_rev _ [] y = y
|
|
85 |
fold_rev f (x : xs) y = f x (fold_rev f xs y)
|
|
86 |
|
|
87 |
single :: a -> [a]
|
|
88 |
single x = [x]
|
|
89 |
|
69240
|
90 |
map_index :: ((Int, a) -> b) -> [a] -> [b]
|
|
91 |
map_index f = map_aux 0
|
|
92 |
where
|
|
93 |
map_aux _ [] = []
|
|
94 |
map_aux i (x : xs) = f (i, x) : map_aux (i + 1) xs
|
|
95 |
|
|
96 |
get_index :: (a -> Maybe b) -> [a] -> Maybe (Int, b)
|
|
97 |
get_index f = get_aux 0
|
|
98 |
where
|
|
99 |
get_aux _ [] = Nothing
|
|
100 |
get_aux i (x : xs) =
|
|
101 |
case f x of
|
|
102 |
Nothing -> get_aux (i + 1) xs
|
|
103 |
Just y -> Just (i, y)
|
|
104 |
|
69225
|
105 |
|
|
106 |
{- strings -}
|
|
107 |
|
|
108 |
quote :: String -> String
|
|
109 |
quote s = "\"" ++ s ++ "\""
|
|
110 |
|
|
111 |
trim_line :: String -> String
|
|
112 |
trim_line line =
|
|
113 |
case reverse line of
|
|
114 |
'\n' : '\r' : rest -> reverse rest
|
|
115 |
'\n' : rest -> reverse rest
|
|
116 |
_ -> line
|
|
117 |
\<close>
|
|
118 |
|
69240
|
119 |
generate_haskell_file "Value.hs" = \<open>
|
69233
|
120 |
{- Title: Haskell/Tools/Value.hs
|
|
121 |
Author: Makarius
|
|
122 |
LICENSE: BSD 3-clause (Isabelle)
|
|
123 |
|
|
124 |
Plain values, represented as string.
|
|
125 |
-}
|
|
126 |
|
|
127 |
module Isabelle.Value
|
|
128 |
(print_bool, parse_bool, print_int, parse_int, print_real, parse_real)
|
|
129 |
where
|
|
130 |
|
|
131 |
import Data.Maybe
|
|
132 |
import qualified Data.List as List
|
|
133 |
import qualified Text.Read as Read
|
|
134 |
|
|
135 |
|
|
136 |
{- bool -}
|
|
137 |
|
|
138 |
print_bool :: Bool -> String
|
|
139 |
print_bool True = "true"
|
|
140 |
print_bool False = "false"
|
|
141 |
|
|
142 |
parse_bool :: String -> Maybe Bool
|
|
143 |
parse_bool "true" = Just True
|
|
144 |
parse_bool "false" = Just False
|
|
145 |
parse_bool _ = Nothing
|
|
146 |
|
|
147 |
|
|
148 |
{- int -}
|
|
149 |
|
|
150 |
print_int :: Int -> String
|
|
151 |
print_int = show
|
|
152 |
|
|
153 |
parse_int :: String -> Maybe Int
|
|
154 |
parse_int = Read.readMaybe
|
|
155 |
|
|
156 |
|
|
157 |
{- real -}
|
|
158 |
|
|
159 |
print_real :: Double -> String
|
|
160 |
print_real x =
|
|
161 |
let s = show x in
|
|
162 |
case span (/= '.') s of
|
|
163 |
(a, '.' : b) | List.all (== '0') b -> a
|
|
164 |
_ -> s
|
|
165 |
|
|
166 |
parse_real :: String -> Maybe Double
|
|
167 |
parse_real = Read.readMaybe
|
|
168 |
\<close>
|
|
169 |
|
69240
|
170 |
generate_haskell_file "Buffer.hs" = \<open>
|
69226
|
171 |
{- Title: Tools/Haskell/Buffer.hs
|
69225
|
172 |
Author: Makarius
|
|
173 |
LICENSE: BSD 3-clause (Isabelle)
|
|
174 |
|
|
175 |
Efficient text buffers.
|
|
176 |
-}
|
69222
|
177 |
|
69225
|
178 |
module Isabelle.Buffer (T, empty, add, content)
|
|
179 |
where
|
|
180 |
|
|
181 |
newtype T = Buffer [String]
|
|
182 |
|
|
183 |
empty :: T
|
|
184 |
empty = Buffer []
|
|
185 |
|
|
186 |
add :: String -> T -> T
|
|
187 |
add "" buf = buf
|
|
188 |
add x (Buffer xs) = Buffer (x : xs)
|
|
189 |
|
|
190 |
content :: T -> String
|
|
191 |
content (Buffer xs) = concat (reverse xs)
|
|
192 |
\<close>
|
|
193 |
|
69240
|
194 |
generate_haskell_file "Properties.hs" = \<open>
|
69226
|
195 |
{- Title: Tools/Haskell/Properties.hs
|
69225
|
196 |
Author: Makarius
|
|
197 |
LICENSE: BSD 3-clause (Isabelle)
|
|
198 |
|
|
199 |
Property lists.
|
|
200 |
-}
|
|
201 |
|
|
202 |
module Isabelle.Properties (Entry, T, defined, get, put, remove)
|
|
203 |
where
|
|
204 |
|
|
205 |
import qualified Data.List as List
|
|
206 |
|
|
207 |
|
|
208 |
type Entry = (String, String)
|
|
209 |
type T = [Entry]
|
|
210 |
|
|
211 |
defined :: T -> String -> Bool
|
|
212 |
defined props name = any (\(a, _) -> a == name) props
|
|
213 |
|
|
214 |
get :: T -> String -> Maybe String
|
|
215 |
get props name = List.lookup name props
|
|
216 |
|
|
217 |
put :: Entry -> T -> T
|
|
218 |
put entry props = entry : remove (fst entry) props
|
|
219 |
|
|
220 |
remove :: String -> T -> T
|
|
221 |
remove name props =
|
|
222 |
if defined props name then filter (\(a, _) -> a /= name) props
|
|
223 |
else props
|
|
224 |
\<close>
|
|
225 |
|
69240
|
226 |
generate_haskell_file "Markup.hs" = \<open>
|
69226
|
227 |
{- Title: Haskell/Tools/Markup.hs
|
69225
|
228 |
Author: Makarius
|
|
229 |
LICENSE: BSD 3-clause (Isabelle)
|
|
230 |
|
|
231 |
Quasi-abstract markup elements.
|
|
232 |
-}
|
|
233 |
|
69234
|
234 |
module Isabelle.Markup (
|
|
235 |
T, empty, is_empty, properties,
|
|
236 |
|
|
237 |
nameN, name, xnameN, xname, kindN,
|
|
238 |
|
|
239 |
lineN, end_lineN, offsetN, end_offsetN, fileN, idN, positionN, position,
|
|
240 |
|
|
241 |
wordsN, words, no_wordsN, no_words,
|
|
242 |
|
|
243 |
tfreeN, tfree, tvarN, tvar, freeN, free, skolemN, skolem, boundN, bound, varN, var,
|
|
244 |
numeralN, numeral, literalN, literal, delimiterN, delimiter, inner_stringN, inner_string,
|
|
245 |
inner_cartoucheN, inner_cartouche, inner_commentN, inner_comment,
|
|
246 |
token_rangeN, token_range,
|
|
247 |
sortingN, sorting, typingN, typing, class_parameterN, class_parameter,
|
|
248 |
|
|
249 |
antiquotedN, antiquoted, antiquoteN, antiquote,
|
|
250 |
|
|
251 |
paragraphN, paragraph, text_foldN, text_fold,
|
|
252 |
|
|
253 |
keyword1N, keyword1, keyword2N, keyword2, keyword3N, keyword3, quasi_keywordN, quasi_keyword,
|
|
254 |
improperN, improper, operatorN, operator, stringN, string, alt_stringN, alt_string,
|
|
255 |
verbatimN, verbatim, cartoucheN, cartouche, commentN, comment,
|
|
256 |
|
|
257 |
writelnN, writeln, stateN, state, informationN, information, tracingN, tracing,
|
|
258 |
warningN, warning, legacyN, legacy, errorN, error, reportN, report, no_reportN, no_report,
|
|
259 |
|
|
260 |
intensifyN, intensify,
|
|
261 |
Output, no_output)
|
69225
|
262 |
where
|
|
263 |
|
69234
|
264 |
import Prelude hiding (words, error)
|
|
265 |
|
|
266 |
import Isabelle.Library
|
69225
|
267 |
import qualified Isabelle.Properties as Properties
|
|
268 |
|
|
269 |
|
69234
|
270 |
{- basic markup -}
|
|
271 |
|
69225
|
272 |
type T = (String, Properties.T)
|
|
273 |
|
|
274 |
empty :: T
|
|
275 |
empty = ("", [])
|
|
276 |
|
|
277 |
is_empty :: T -> Bool
|
|
278 |
is_empty ("", _) = True
|
|
279 |
is_empty _ = False
|
|
280 |
|
69234
|
281 |
properties :: Properties.T -> T -> T
|
|
282 |
properties more_props (elem, props) =
|
|
283 |
(elem, fold_rev Properties.put more_props props)
|
|
284 |
|
|
285 |
markup_elem name = (name, (name, []) :: T)
|
|
286 |
|
|
287 |
|
|
288 |
{- misc properties -}
|
|
289 |
|
|
290 |
nameN :: String
|
|
291 |
nameN = \<open>Markup.nameN\<close>
|
|
292 |
|
|
293 |
name :: String -> T -> T
|
|
294 |
name a = properties [(nameN, a)]
|
|
295 |
|
|
296 |
xnameN :: String
|
|
297 |
xnameN = \<open>Markup.xnameN\<close>
|
|
298 |
|
|
299 |
xname :: String -> T -> T
|
|
300 |
xname a = properties [(xnameN, a)]
|
|
301 |
|
|
302 |
kindN :: String
|
|
303 |
kindN = \<open>Markup.kindN\<close>
|
|
304 |
|
|
305 |
|
|
306 |
{- position -}
|
|
307 |
|
|
308 |
lineN, end_lineN :: String
|
|
309 |
lineN = \<open>Markup.lineN\<close>
|
|
310 |
end_lineN = \<open>Markup.end_lineN\<close>
|
|
311 |
|
|
312 |
offsetN, end_offsetN :: String
|
|
313 |
offsetN = \<open>Markup.offsetN\<close>
|
|
314 |
end_offsetN = \<open>Markup.end_offsetN\<close>
|
|
315 |
|
|
316 |
fileN, idN :: String
|
|
317 |
fileN = \<open>Markup.fileN\<close>
|
|
318 |
idN = \<open>Markup.idN\<close>
|
|
319 |
|
|
320 |
positionN :: String; position :: T
|
|
321 |
(positionN, position) = markup_elem \<open>Markup.positionN\<close>
|
|
322 |
|
|
323 |
|
|
324 |
{- text properties -}
|
|
325 |
|
|
326 |
wordsN :: String; words :: T
|
|
327 |
(wordsN, words) = markup_elem \<open>Markup.wordsN\<close>
|
|
328 |
|
|
329 |
no_wordsN :: String; no_words :: T
|
|
330 |
(no_wordsN, no_words) = markup_elem \<open>Markup.no_wordsN\<close>
|
|
331 |
|
|
332 |
|
|
333 |
{- inner syntax -}
|
|
334 |
|
|
335 |
tfreeN :: String; tfree :: T
|
|
336 |
(tfreeN, tfree) = markup_elem \<open>Markup.tfreeN\<close>
|
|
337 |
|
|
338 |
tvarN :: String; tvar :: T
|
|
339 |
(tvarN, tvar) = markup_elem \<open>Markup.tvarN\<close>
|
|
340 |
|
|
341 |
freeN :: String; free :: T
|
|
342 |
(freeN, free) = markup_elem \<open>Markup.freeN\<close>
|
|
343 |
|
|
344 |
skolemN :: String; skolem :: T
|
|
345 |
(skolemN, skolem) = markup_elem \<open>Markup.skolemN\<close>
|
|
346 |
|
|
347 |
boundN :: String; bound :: T
|
|
348 |
(boundN, bound) = markup_elem \<open>Markup.boundN\<close>
|
|
349 |
|
|
350 |
varN :: String; var :: T
|
|
351 |
(varN, var) = markup_elem \<open>Markup.varN\<close>
|
|
352 |
|
|
353 |
numeralN :: String; numeral :: T
|
|
354 |
(numeralN, numeral) = markup_elem \<open>Markup.numeralN\<close>
|
|
355 |
|
|
356 |
literalN :: String; literal :: T
|
|
357 |
(literalN, literal) = markup_elem \<open>Markup.literalN\<close>
|
|
358 |
|
|
359 |
delimiterN :: String; delimiter :: T
|
|
360 |
(delimiterN, delimiter) = markup_elem \<open>Markup.delimiterN\<close>
|
|
361 |
|
|
362 |
inner_stringN :: String; inner_string :: T
|
|
363 |
(inner_stringN, inner_string) = markup_elem \<open>Markup.inner_stringN\<close>
|
|
364 |
|
|
365 |
inner_cartoucheN :: String; inner_cartouche :: T
|
|
366 |
(inner_cartoucheN, inner_cartouche) = markup_elem \<open>Markup.inner_cartoucheN\<close>
|
|
367 |
|
|
368 |
inner_commentN :: String; inner_comment :: T
|
|
369 |
(inner_commentN, inner_comment) = markup_elem \<open>Markup.inner_commentN\<close>
|
|
370 |
|
|
371 |
|
|
372 |
token_rangeN :: String; token_range :: T
|
|
373 |
(token_rangeN, token_range) = markup_elem \<open>Markup.token_rangeN\<close>
|
|
374 |
|
|
375 |
|
|
376 |
sortingN :: String; sorting :: T
|
|
377 |
(sortingN, sorting) = markup_elem \<open>Markup.sortingN\<close>
|
|
378 |
|
|
379 |
typingN :: String; typing :: T
|
|
380 |
(typingN, typing) = markup_elem \<open>Markup.typingN\<close>
|
|
381 |
|
|
382 |
class_parameterN :: String; class_parameter :: T
|
|
383 |
(class_parameterN, class_parameter) = markup_elem \<open>Markup.class_parameterN\<close>
|
|
384 |
|
|
385 |
|
|
386 |
{- antiquotations -}
|
|
387 |
|
|
388 |
antiquotedN :: String; antiquoted :: T
|
|
389 |
(antiquotedN, antiquoted) = markup_elem \<open>Markup.antiquotedN\<close>
|
|
390 |
|
|
391 |
antiquoteN :: String; antiquote :: T
|
|
392 |
(antiquoteN, antiquote) = markup_elem \<open>Markup.antiquoteN\<close>
|
|
393 |
|
|
394 |
|
|
395 |
{- text structure -}
|
|
396 |
|
|
397 |
paragraphN :: String; paragraph :: T
|
|
398 |
(paragraphN, paragraph) = markup_elem \<open>Markup.paragraphN\<close>
|
|
399 |
|
|
400 |
text_foldN :: String; text_fold :: T
|
|
401 |
(text_foldN, text_fold) = markup_elem \<open>Markup.text_foldN\<close>
|
|
402 |
|
|
403 |
|
|
404 |
{- outer syntax -}
|
|
405 |
|
|
406 |
keyword1N :: String; keyword1 :: T
|
|
407 |
(keyword1N, keyword1) = markup_elem \<open>Markup.keyword1N\<close>
|
|
408 |
|
|
409 |
keyword2N :: String; keyword2 :: T
|
|
410 |
(keyword2N, keyword2) = markup_elem \<open>Markup.keyword2N\<close>
|
|
411 |
|
|
412 |
keyword3N :: String; keyword3 :: T
|
|
413 |
(keyword3N, keyword3) = markup_elem \<open>Markup.keyword3N\<close>
|
|
414 |
|
|
415 |
quasi_keywordN :: String; quasi_keyword :: T
|
|
416 |
(quasi_keywordN, quasi_keyword) = markup_elem \<open>Markup.quasi_keywordN\<close>
|
|
417 |
|
|
418 |
improperN :: String; improper :: T
|
|
419 |
(improperN, improper) = markup_elem \<open>Markup.improperN\<close>
|
|
420 |
|
|
421 |
operatorN :: String; operator :: T
|
|
422 |
(operatorN, operator) = markup_elem \<open>Markup.operatorN\<close>
|
|
423 |
|
|
424 |
stringN :: String; string :: T
|
|
425 |
(stringN, string) = markup_elem \<open>Markup.stringN\<close>
|
|
426 |
|
|
427 |
alt_stringN :: String; alt_string :: T
|
|
428 |
(alt_stringN, alt_string) = markup_elem \<open>Markup.alt_stringN\<close>
|
|
429 |
|
|
430 |
verbatimN :: String; verbatim :: T
|
|
431 |
(verbatimN, verbatim) = markup_elem \<open>Markup.verbatimN\<close>
|
|
432 |
|
|
433 |
cartoucheN :: String; cartouche :: T
|
|
434 |
(cartoucheN, cartouche) = markup_elem \<open>Markup.cartoucheN\<close>
|
|
435 |
|
|
436 |
commentN :: String; comment :: T
|
|
437 |
(commentN, comment) = markup_elem \<open>Markup.commentN\<close>
|
|
438 |
|
|
439 |
|
|
440 |
{- messages -}
|
|
441 |
|
|
442 |
writelnN :: String; writeln :: T
|
|
443 |
(writelnN, writeln) = markup_elem \<open>Markup.writelnN\<close>
|
|
444 |
|
|
445 |
stateN :: String; state :: T
|
|
446 |
(stateN, state) = markup_elem \<open>Markup.stateN\<close>
|
|
447 |
|
|
448 |
informationN :: String; information :: T
|
|
449 |
(informationN, information) = markup_elem \<open>Markup.informationN\<close>
|
|
450 |
|
|
451 |
tracingN :: String; tracing :: T
|
|
452 |
(tracingN, tracing) = markup_elem \<open>Markup.tracingN\<close>
|
|
453 |
|
|
454 |
warningN :: String; warning :: T
|
|
455 |
(warningN, warning) = markup_elem \<open>Markup.warningN\<close>
|
|
456 |
|
|
457 |
legacyN :: String; legacy :: T
|
|
458 |
(legacyN, legacy) = markup_elem \<open>Markup.legacyN\<close>
|
|
459 |
|
|
460 |
errorN :: String; error :: T
|
|
461 |
(errorN, error) = markup_elem \<open>Markup.errorN\<close>
|
|
462 |
|
|
463 |
reportN :: String; report :: T
|
|
464 |
(reportN, report) = markup_elem \<open>Markup.reportN\<close>
|
|
465 |
|
|
466 |
no_reportN :: String; no_report :: T
|
|
467 |
(no_reportN, no_report) = markup_elem \<open>Markup.no_reportN\<close>
|
|
468 |
|
|
469 |
intensifyN :: String; intensify :: T
|
|
470 |
(intensifyN, intensify) = markup_elem \<open>Markup.intensifyN\<close>
|
|
471 |
|
|
472 |
|
|
473 |
{- output -}
|
69225
|
474 |
|
|
475 |
type Output = (String, String)
|
|
476 |
|
|
477 |
no_output :: Output
|
|
478 |
no_output = ("", "")
|
69222
|
479 |
\<close>
|
|
480 |
|
69240
|
481 |
generate_haskell_file "XML.hs" = \<open>
|
69226
|
482 |
{- Title: Tools/Haskell/XML.hs
|
69225
|
483 |
Author: Makarius
|
|
484 |
LICENSE: BSD 3-clause (Isabelle)
|
|
485 |
|
|
486 |
Untyped XML trees and representation of ML values.
|
|
487 |
-}
|
|
488 |
|
|
489 |
module Isabelle.XML (Attributes, Body, Tree(..), wrap_elem, unwrap_elem, content_of)
|
|
490 |
where
|
|
491 |
|
|
492 |
import qualified Data.List as List
|
|
493 |
|
|
494 |
import Isabelle.Library
|
|
495 |
import qualified Isabelle.Properties as Properties
|
|
496 |
import qualified Isabelle.Markup as Markup
|
|
497 |
import qualified Isabelle.Buffer as Buffer
|
|
498 |
|
|
499 |
|
|
500 |
{- types -}
|
|
501 |
|
|
502 |
type Attributes = Properties.T
|
|
503 |
type Body = [Tree]
|
|
504 |
data Tree = Elem Markup.T Body | Text String
|
|
505 |
|
|
506 |
|
|
507 |
{- wrapped elements -}
|
|
508 |
|
69236
|
509 |
wrap_elem (((a, atts), body1), body2) =
|
|
510 |
Elem (\<open>XML.xml_elemN\<close>, (\<open>XML.xml_nameN\<close>, a) : atts) (Elem (\<open>XML.xml_bodyN\<close>, []) body1 : body2)
|
69225
|
511 |
|
69236
|
512 |
unwrap_elem
|
|
513 |
(Elem (\<open>XML.xml_elemN\<close>, (\<open>XML.xml_nameN\<close>, a) : atts) (Elem (\<open>XML.xml_bodyN\<close>, []) body1 : body2)) =
|
|
514 |
Just (((a, atts), body1), body2)
|
69225
|
515 |
unwrap_elem _ = Nothing
|
|
516 |
|
|
517 |
|
|
518 |
{- text content -}
|
|
519 |
|
|
520 |
add_content tree =
|
|
521 |
case unwrap_elem tree of
|
|
522 |
Just (_, ts) -> fold add_content ts
|
|
523 |
Nothing ->
|
|
524 |
case tree of
|
|
525 |
Elem _ ts -> fold add_content ts
|
|
526 |
Text s -> Buffer.add s
|
|
527 |
|
|
528 |
content_of body = Buffer.empty |> fold add_content body |> Buffer.content
|
|
529 |
|
|
530 |
|
|
531 |
{- string representation -}
|
|
532 |
|
|
533 |
encode '<' = "<"
|
|
534 |
encode '>' = ">"
|
|
535 |
encode '&' = "&"
|
|
536 |
encode '\'' = "'"
|
|
537 |
encode '\"' = """
|
|
538 |
encode c = [c]
|
|
539 |
|
|
540 |
instance Show Tree where
|
|
541 |
show tree =
|
|
542 |
Buffer.empty |> show_tree tree |> Buffer.content
|
|
543 |
where
|
|
544 |
show_tree (Elem (name, atts) []) =
|
|
545 |
Buffer.add "<" #> Buffer.add (show_elem name atts) #> Buffer.add "/>"
|
|
546 |
show_tree (Elem (name, atts) ts) =
|
|
547 |
Buffer.add "<" #> Buffer.add (show_elem name atts) #> Buffer.add ">" #>
|
|
548 |
fold show_tree ts #>
|
|
549 |
Buffer.add "</" #> Buffer.add name #> Buffer.add ">"
|
|
550 |
show_tree (Text s) = Buffer.add (show_text s)
|
|
551 |
|
|
552 |
show_elem name atts =
|
|
553 |
unwords (name : map (\(a, x) -> a ++ "=\"" ++ show_text x ++ "\"") atts)
|
|
554 |
|
|
555 |
show_text = concatMap encode
|
|
556 |
\<close>
|
|
557 |
|
69240
|
558 |
generate_haskell_file "XML/Encode.hs" = \<open>
|
|
559 |
{- Title: Tools/Haskell/XML/Encode.hs
|
|
560 |
Author: Makarius
|
|
561 |
LICENSE: BSD 3-clause (Isabelle)
|
|
562 |
|
|
563 |
XML as data representation language.
|
|
564 |
-}
|
|
565 |
|
|
566 |
module Isabelle.XML.Encode (
|
|
567 |
A, T, V,
|
|
568 |
|
|
569 |
int_atom, bool_atom, unit_atom,
|
|
570 |
|
|
571 |
tree, properties, string, init, bool, unit, pair, triple, list, variant
|
|
572 |
)
|
|
573 |
where
|
|
574 |
|
|
575 |
import Isabelle.Library
|
|
576 |
import qualified Isabelle.Value as Value
|
|
577 |
import qualified Isabelle.Properties as Properties
|
|
578 |
import qualified Isabelle.XML as XML
|
|
579 |
|
|
580 |
|
|
581 |
type A a = a -> String
|
|
582 |
type T a = a -> XML.Body
|
|
583 |
type V a = a -> Maybe ([String], XML.Body)
|
|
584 |
|
|
585 |
|
|
586 |
-- atomic values
|
|
587 |
|
|
588 |
int_atom :: A Int
|
|
589 |
int_atom = Value.print_int
|
|
590 |
|
|
591 |
bool_atom :: A Bool
|
|
592 |
bool_atom False = "0"
|
|
593 |
bool_atom True = "1"
|
|
594 |
|
|
595 |
unit_atom :: A ()
|
|
596 |
unit_atom () = ""
|
|
597 |
|
|
598 |
|
|
599 |
-- structural nodes
|
|
600 |
|
|
601 |
node = XML.Elem (":", [])
|
|
602 |
|
|
603 |
vector = map_index (\(i, x) -> (int_atom i, x))
|
|
604 |
|
|
605 |
tagged (tag, (xs, ts)) = XML.Elem (int_atom tag, vector xs) ts
|
|
606 |
|
|
607 |
|
|
608 |
-- representation of standard types
|
|
609 |
|
|
610 |
tree :: T XML.Tree
|
|
611 |
tree t = [t]
|
|
612 |
|
|
613 |
properties :: T Properties.T
|
|
614 |
properties props = [XML.Elem (":", props) []]
|
|
615 |
|
|
616 |
string :: T String
|
|
617 |
string "" = []
|
|
618 |
string s = [XML.Text s]
|
|
619 |
|
|
620 |
int :: T Int
|
|
621 |
int = string . int_atom
|
|
622 |
|
|
623 |
bool :: T Bool
|
|
624 |
bool = string . bool_atom
|
|
625 |
|
|
626 |
unit :: T ()
|
|
627 |
unit = string . unit_atom
|
|
628 |
|
|
629 |
pair :: T a -> T b -> T (a, b)
|
|
630 |
pair f g (x, y) = [node (f x), node (g y)]
|
|
631 |
|
|
632 |
triple :: T a -> T b -> T c -> T (a, b, c)
|
|
633 |
triple f g h (x, y, z) = [node (f x), node (g y), node (h z)]
|
|
634 |
|
|
635 |
list :: T a -> T [a]
|
|
636 |
list f xs = map (node . f) xs
|
|
637 |
|
|
638 |
variant :: [V a] -> T a
|
|
639 |
variant fs x = [tagged (the (get_index (\f -> f x) fs))]
|
|
640 |
\<close>
|
|
641 |
|
|
642 |
generate_haskell_file "XML/Decode.hs" = \<open>
|
|
643 |
{- Title: Tools/Haskell/XML/Decode.hs
|
|
644 |
Author: Makarius
|
|
645 |
LICENSE: BSD 3-clause (Isabelle)
|
|
646 |
|
|
647 |
XML as data representation language.
|
|
648 |
-}
|
|
649 |
|
|
650 |
module Isabelle.XML.Decode (
|
|
651 |
A, T, V,
|
|
652 |
|
|
653 |
int_atom, bool_atom, unit_atom,
|
|
654 |
|
|
655 |
tree, properties, string, init, bool, unit, pair, triple, list, variant
|
|
656 |
)
|
|
657 |
where
|
|
658 |
|
|
659 |
import Data.List ((!!))
|
|
660 |
|
|
661 |
import Isabelle.Library
|
|
662 |
import qualified Isabelle.Value as Value
|
|
663 |
import qualified Isabelle.Properties as Properties
|
|
664 |
import qualified Isabelle.XML as XML
|
|
665 |
|
|
666 |
|
|
667 |
type A a = String -> a
|
|
668 |
type T a = XML.Body -> a
|
|
669 |
type V a = ([String], XML.Body) -> a
|
|
670 |
|
|
671 |
err_atom = error "Malformed XML atom"
|
|
672 |
err_body = error "Malformed XML body"
|
|
673 |
|
|
674 |
|
|
675 |
{- atomic values -}
|
|
676 |
|
|
677 |
int_atom :: A Int
|
|
678 |
int_atom s =
|
|
679 |
case Value.parse_int s of
|
|
680 |
Just i -> i
|
|
681 |
Nothing -> err_atom
|
|
682 |
|
|
683 |
bool_atom :: A Bool
|
|
684 |
bool_atom "0" = False
|
|
685 |
bool_atom "1" = True
|
|
686 |
bool_atom _ = err_atom
|
|
687 |
|
|
688 |
unit_atom :: A ()
|
|
689 |
unit_atom "" = ()
|
|
690 |
unit_atom _ = err_atom
|
|
691 |
|
|
692 |
|
|
693 |
{- structural nodes -}
|
|
694 |
|
|
695 |
node (XML.Elem (":", []) ts) = ts
|
|
696 |
node _ = err_body
|
|
697 |
|
|
698 |
vector atts =
|
|
699 |
map_index (\(i, (a, x)) -> if int_atom a == i then x else err_atom) atts
|
|
700 |
|
|
701 |
tagged (XML.Elem (name, atts) ts) = (int_atom name, (vector atts, ts))
|
|
702 |
tagged _ = err_body
|
|
703 |
|
|
704 |
|
|
705 |
{- representation of standard types -}
|
|
706 |
|
|
707 |
tree :: T XML.Tree
|
|
708 |
tree [t] = t
|
|
709 |
tree _ = err_body
|
|
710 |
|
|
711 |
properties :: T Properties.T
|
|
712 |
properties [XML.Elem (":", props) []] = props
|
|
713 |
properties _ = err_body
|
|
714 |
|
|
715 |
string :: T String
|
|
716 |
string [] = ""
|
|
717 |
string [XML.Text s] = s
|
|
718 |
string _ = err_body
|
|
719 |
|
|
720 |
int :: T Int
|
|
721 |
int = int_atom . string
|
|
722 |
|
|
723 |
bool :: T Bool
|
|
724 |
bool = bool_atom . string
|
|
725 |
|
|
726 |
unit :: T ()
|
|
727 |
unit = unit_atom . string
|
|
728 |
|
|
729 |
pair :: T a -> T b -> T (a, b)
|
|
730 |
pair f g [t1, t2] = (f (node t1), g (node t2))
|
|
731 |
pair _ _ _ = err_body
|
|
732 |
|
|
733 |
triple :: T a -> T b -> T c -> T (a, b, c)
|
|
734 |
triple f g h [t1, t2, t3] = (f (node t1), g (node t2), h (node t3))
|
|
735 |
triple _ _ _ _ = err_body
|
|
736 |
|
|
737 |
list :: T a -> T [a]
|
|
738 |
list f ts = map (f . node) ts
|
|
739 |
|
|
740 |
option :: T a -> T (Maybe a)
|
|
741 |
option _ [] = Nothing
|
|
742 |
option f [t] = Just (f (node t))
|
|
743 |
option _ _ = err_body
|
|
744 |
|
|
745 |
variant :: [V a] -> T a
|
|
746 |
variant fs [t] = (fs !! tag) (xs, ts)
|
|
747 |
where (tag, (xs, ts)) = tagged t
|
|
748 |
variant _ _ = err_body
|
|
749 |
\<close>
|
|
750 |
|
|
751 |
generate_haskell_file "YXML.hs" = \<open>
|
69226
|
752 |
{- Title: Tools/Haskell/YXML.hs
|
69225
|
753 |
Author: Makarius
|
|
754 |
LICENSE: BSD 3-clause (Isabelle)
|
|
755 |
|
|
756 |
Efficient text representation of XML trees. Suitable for direct
|
|
757 |
inlining into plain text.
|
|
758 |
-}
|
|
759 |
|
|
760 |
module Isabelle.YXML (charX, charY, strX, strY, detect,
|
|
761 |
buffer_body, buffer, string_of_body, string_of, parse_body, parse)
|
|
762 |
where
|
|
763 |
|
|
764 |
import qualified Data.Char as Char
|
|
765 |
import qualified Data.List as List
|
69222
|
766 |
|
69225
|
767 |
import Isabelle.Library
|
|
768 |
import qualified Isabelle.Markup as Markup
|
|
769 |
import qualified Isabelle.XML as XML
|
|
770 |
import qualified Isabelle.Buffer as Buffer
|
|
771 |
|
|
772 |
|
|
773 |
{- markers -}
|
|
774 |
|
|
775 |
charX, charY :: Char
|
|
776 |
charX = Char.chr 5
|
|
777 |
charY = Char.chr 6
|
|
778 |
|
|
779 |
strX, strY, strXY, strXYX :: String
|
|
780 |
strX = [charX]
|
|
781 |
strY = [charY]
|
|
782 |
strXY = strX ++ strY
|
|
783 |
strXYX = strXY ++ strX
|
|
784 |
|
|
785 |
detect :: String -> Bool
|
|
786 |
detect = any (\c -> c == charX || c == charY)
|
|
787 |
|
|
788 |
|
|
789 |
{- output -}
|
|
790 |
|
|
791 |
buffer_attrib (a, x) =
|
|
792 |
Buffer.add strY #> Buffer.add a #> Buffer.add "=" #> Buffer.add x
|
|
793 |
|
|
794 |
buffer_body :: XML.Body -> Buffer.T -> Buffer.T
|
|
795 |
buffer_body = fold buffer
|
|
796 |
|
|
797 |
buffer :: XML.Tree -> Buffer.T -> Buffer.T
|
|
798 |
buffer (XML.Elem (name, atts) ts) =
|
|
799 |
Buffer.add strXY #> Buffer.add name #> fold buffer_attrib atts #> Buffer.add strX #>
|
|
800 |
buffer_body ts #>
|
|
801 |
Buffer.add strXYX
|
|
802 |
buffer (XML.Text s) = Buffer.add s
|
|
803 |
|
|
804 |
string_of_body :: XML.Body -> String
|
|
805 |
string_of_body body = Buffer.empty |> buffer_body body |> Buffer.content
|
|
806 |
|
|
807 |
string_of :: XML.Tree -> String
|
|
808 |
string_of = string_of_body . single
|
|
809 |
|
|
810 |
|
|
811 |
{- parse -}
|
|
812 |
|
|
813 |
-- split: fields or non-empty tokens
|
|
814 |
|
|
815 |
split :: Bool -> Char -> String -> [String]
|
|
816 |
split _ _ [] = []
|
|
817 |
split fields sep str = splitting str
|
|
818 |
where
|
|
819 |
splitting rest =
|
|
820 |
case span (/= sep) rest of
|
|
821 |
(_, []) -> cons rest []
|
|
822 |
(prfx, _ : rest') -> cons prfx (splitting rest')
|
|
823 |
cons item = if fields || not (null item) then (:) item else id
|
|
824 |
|
|
825 |
|
|
826 |
-- structural errors
|
|
827 |
|
|
828 |
err msg = error ("Malformed YXML: " ++ msg)
|
|
829 |
err_attribute = err "bad attribute"
|
|
830 |
err_element = err "bad element"
|
|
831 |
err_unbalanced "" = err "unbalanced element"
|
|
832 |
err_unbalanced name = err ("unbalanced element " ++ quote name)
|
|
833 |
|
|
834 |
|
|
835 |
-- stack operations
|
|
836 |
|
|
837 |
add x ((elem, body) : pending) = (elem, x : body) : pending
|
|
838 |
|
|
839 |
push "" _ _ = err_element
|
|
840 |
push name atts pending = ((name, atts), []) : pending
|
|
841 |
|
|
842 |
pop ((("", _), _) : _) = err_unbalanced ""
|
|
843 |
pop ((markup, body) : pending) = add (XML.Elem markup (reverse body)) pending
|
|
844 |
|
|
845 |
|
|
846 |
-- parsing
|
|
847 |
|
|
848 |
parse_attrib s =
|
|
849 |
case List.elemIndex '=' s of
|
|
850 |
Just i | i > 0 -> (take i s, drop (i + 1) s)
|
|
851 |
_ -> err_attribute
|
|
852 |
|
|
853 |
parse_chunk ["", ""] = pop
|
|
854 |
parse_chunk ("" : name : atts) = push name (map parse_attrib atts)
|
|
855 |
parse_chunk txts = fold (add . XML.Text) txts
|
|
856 |
|
|
857 |
parse_body :: String -> XML.Body
|
|
858 |
parse_body source =
|
|
859 |
case fold parse_chunk chunks [(("", []), [])] of
|
|
860 |
[(("", _), result)] -> reverse result
|
|
861 |
((name, _), _) : _ -> err_unbalanced name
|
|
862 |
where chunks = split False charX source |> map (split True charY)
|
|
863 |
|
|
864 |
parse :: String -> XML.Tree
|
|
865 |
parse source =
|
|
866 |
case parse_body source of
|
|
867 |
[result] -> result
|
|
868 |
[] -> XML.Text ""
|
|
869 |
_ -> err "multiple results"
|
69222
|
870 |
\<close>
|
|
871 |
|
69240
|
872 |
generate_haskell_file "Term.hs" = \<open>
|
|
873 |
{- Title: Tools/Haskell/Term.hs
|
|
874 |
Author: Makarius
|
|
875 |
LICENSE: BSD 3-clause (Isabelle)
|
|
876 |
|
|
877 |
Lambda terms, types, sorts.
|
|
878 |
-}
|
|
879 |
|
|
880 |
module Isabelle.Term (
|
|
881 |
Indexname,
|
|
882 |
|
|
883 |
Sort, dummyS,
|
|
884 |
|
|
885 |
Typ(..), dummyT, Term(..))
|
|
886 |
where
|
|
887 |
|
|
888 |
type Indexname = (String, Int)
|
|
889 |
|
|
890 |
|
|
891 |
type Sort = [String]
|
|
892 |
|
|
893 |
dummyS :: Sort
|
|
894 |
dummyS = [""]
|
|
895 |
|
|
896 |
|
|
897 |
data Typ =
|
|
898 |
Type (String, [Typ])
|
|
899 |
| TFree (String, Sort)
|
|
900 |
| TVar (Indexname, Sort)
|
|
901 |
deriving Show
|
|
902 |
|
|
903 |
dummyT :: Typ
|
|
904 |
dummyT = Type (\<open>\<^type_name>\<open>dummy\<close>\<close>, [])
|
|
905 |
|
|
906 |
|
|
907 |
data Term =
|
|
908 |
Const (String, Typ)
|
|
909 |
| Free (String, Typ)
|
|
910 |
| Var (Indexname, Typ)
|
|
911 |
| Bound Int
|
|
912 |
| Abs (String, Typ, Term)
|
|
913 |
| App (Term, Term)
|
|
914 |
deriving Show
|
|
915 |
\<close>
|
|
916 |
|
|
917 |
generate_haskell_file "Term_XML/Encode.hs" = \<open>
|
|
918 |
{- Title: Tools/Haskell/Term_XML/Encode.hs
|
|
919 |
Author: Makarius
|
|
920 |
LICENSE: BSD 3-clause (Isabelle)
|
|
921 |
|
|
922 |
XML data representation of lambda terms.
|
|
923 |
-}
|
|
924 |
|
|
925 |
{-# LANGUAGE LambdaCase #-}
|
|
926 |
|
|
927 |
module Isabelle.Term_XML.Encode (sort, typ, term)
|
|
928 |
where
|
|
929 |
|
|
930 |
import Isabelle.Library
|
|
931 |
import qualified Isabelle.XML as XML
|
|
932 |
import Isabelle.XML.Encode
|
|
933 |
import Isabelle.Term
|
|
934 |
|
|
935 |
|
|
936 |
sort :: T Sort
|
|
937 |
sort = list string
|
|
938 |
|
|
939 |
typ :: T Typ
|
|
940 |
typ ty =
|
|
941 |
ty |> variant
|
|
942 |
[\case { Type (a, b) -> Just ([a], list typ b); _ -> Nothing },
|
|
943 |
\case { TFree (a, b) -> Just ([a], sort b); _ -> Nothing },
|
|
944 |
\case { TVar ((a, b), c) -> Just ([a, int_atom b], sort c); _ -> Nothing }]
|
|
945 |
|
|
946 |
term :: T Term
|
|
947 |
term t =
|
|
948 |
t |> variant
|
|
949 |
[\case { Const (a, b) -> Just ([a], typ b); _ -> Nothing },
|
|
950 |
\case { Free (a, b) -> Just ([a], typ b); _ -> Nothing },
|
|
951 |
\case { Var ((a, b), c) -> Just ([a, int_atom b], typ c); _ -> Nothing },
|
|
952 |
\case { Bound a -> Just ([int_atom a], []); _ -> Nothing },
|
|
953 |
\case { Abs (a, b, c) -> Just ([a], pair typ term (b, c)); _ -> Nothing },
|
|
954 |
\case { App a -> Just ([], pair term term a); _ -> Nothing }]
|
|
955 |
\<close>
|
|
956 |
|
|
957 |
generate_haskell_file "Term_XML/Decode.hs" = \<open>
|
|
958 |
{- Title: Tools/Haskell/Term_XML/Decode.hs
|
|
959 |
Author: Makarius
|
|
960 |
LICENSE: BSD 3-clause (Isabelle)
|
|
961 |
|
|
962 |
XML data representation of lambda terms.
|
|
963 |
-}
|
|
964 |
|
|
965 |
module Isabelle.Term_XML.Decode (sort, typ, term)
|
|
966 |
where
|
|
967 |
|
|
968 |
import Isabelle.Library
|
|
969 |
import qualified Isabelle.XML as XML
|
|
970 |
import Isabelle.XML.Decode
|
|
971 |
import Isabelle.Term
|
|
972 |
|
|
973 |
|
|
974 |
sort :: T Sort
|
|
975 |
sort = list string
|
|
976 |
|
|
977 |
typ :: T Typ
|
|
978 |
typ ty =
|
|
979 |
ty |> variant
|
|
980 |
[\([a], b) -> Type (a, list typ b),
|
|
981 |
\([a], b) -> TFree (a, sort b),
|
|
982 |
\([a, b], c) -> TVar ((a, int_atom b), sort c)]
|
|
983 |
|
|
984 |
term :: T Term
|
|
985 |
term t =
|
|
986 |
t |> variant
|
|
987 |
[\([a], b) -> Const (a, typ b),
|
|
988 |
\([a], b) -> Free (a, typ b),
|
|
989 |
\([a, b], c) -> Var ((a, int_atom b), typ c),
|
|
990 |
\([a], []) -> Bound (int_atom a),
|
|
991 |
\([a], b) -> let (c, d) = pair typ term b in Abs (a, c, d),
|
|
992 |
\([], a) -> App (pair term term a)]
|
|
993 |
\<close>
|
|
994 |
|
69222
|
995 |
end
|