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.
|
69280
|
36 |
|
|
37 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/General/basics.ML\<close>, \<^file>\<open>$ISABELLE_HOME/src/Pure/library.ML\<close>.
|
69225
|
38 |
-}
|
|
39 |
|
69240
|
40 |
module Isabelle.Library (
|
|
41 |
(|>), (|->), (#>), (#->),
|
|
42 |
|
|
43 |
the, the_default,
|
|
44 |
|
|
45 |
fold, fold_rev, single, map_index, get_index,
|
|
46 |
|
69288
|
47 |
quote, trim_line, clean_name)
|
69225
|
48 |
where
|
|
49 |
|
69234
|
50 |
import Data.Maybe
|
|
51 |
|
|
52 |
|
69225
|
53 |
{- functions -}
|
|
54 |
|
|
55 |
(|>) :: a -> (a -> b) -> b
|
|
56 |
x |> f = f x
|
|
57 |
|
|
58 |
(|->) :: (a, b) -> (a -> b -> c) -> c
|
|
59 |
(x, y) |-> f = f x y
|
|
60 |
|
|
61 |
(#>) :: (a -> b) -> (b -> c) -> a -> c
|
|
62 |
(f #> g) x = x |> f |> g
|
|
63 |
|
|
64 |
(#->) :: (a -> (c, b)) -> (c -> b -> d) -> a -> d
|
|
65 |
(f #-> g) x = x |> f |-> g
|
|
66 |
|
|
67 |
|
69234
|
68 |
{- options -}
|
|
69 |
|
69240
|
70 |
the :: Maybe a -> a
|
|
71 |
the (Just x) = x
|
|
72 |
the Nothing = error "the Nothing"
|
|
73 |
|
69234
|
74 |
the_default :: a -> Maybe a -> a
|
|
75 |
the_default x Nothing = x
|
|
76 |
the_default _ (Just y) = y
|
|
77 |
|
|
78 |
|
69225
|
79 |
{- lists -}
|
|
80 |
|
|
81 |
fold :: (a -> b -> b) -> [a] -> b -> b
|
|
82 |
fold _ [] y = y
|
|
83 |
fold f (x : xs) y = fold f xs (f x y)
|
|
84 |
|
|
85 |
fold_rev :: (a -> b -> b) -> [a] -> b -> b
|
|
86 |
fold_rev _ [] y = y
|
|
87 |
fold_rev f (x : xs) y = f x (fold_rev f xs y)
|
|
88 |
|
|
89 |
single :: a -> [a]
|
|
90 |
single x = [x]
|
|
91 |
|
69240
|
92 |
map_index :: ((Int, a) -> b) -> [a] -> [b]
|
|
93 |
map_index f = map_aux 0
|
|
94 |
where
|
|
95 |
map_aux _ [] = []
|
|
96 |
map_aux i (x : xs) = f (i, x) : map_aux (i + 1) xs
|
|
97 |
|
|
98 |
get_index :: (a -> Maybe b) -> [a] -> Maybe (Int, b)
|
|
99 |
get_index f = get_aux 0
|
|
100 |
where
|
|
101 |
get_aux _ [] = Nothing
|
|
102 |
get_aux i (x : xs) =
|
|
103 |
case f x of
|
|
104 |
Nothing -> get_aux (i + 1) xs
|
|
105 |
Just y -> Just (i, y)
|
|
106 |
|
69225
|
107 |
|
|
108 |
{- strings -}
|
|
109 |
|
|
110 |
quote :: String -> String
|
|
111 |
quote s = "\"" ++ s ++ "\""
|
|
112 |
|
|
113 |
trim_line :: String -> String
|
|
114 |
trim_line line =
|
|
115 |
case reverse line of
|
|
116 |
'\n' : '\r' : rest -> reverse rest
|
|
117 |
'\n' : rest -> reverse rest
|
|
118 |
_ -> line
|
69288
|
119 |
|
|
120 |
clean_name :: String -> String
|
|
121 |
clean_name = reverse #> dropWhile (== '_') #> reverse
|
69225
|
122 |
\<close>
|
|
123 |
|
69240
|
124 |
generate_haskell_file "Value.hs" = \<open>
|
69233
|
125 |
{- Title: Haskell/Tools/Value.hs
|
|
126 |
Author: Makarius
|
|
127 |
LICENSE: BSD 3-clause (Isabelle)
|
|
128 |
|
|
129 |
Plain values, represented as string.
|
69280
|
130 |
|
|
131 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/General/value.ML\<close>.
|
69233
|
132 |
-}
|
|
133 |
|
|
134 |
module Isabelle.Value
|
|
135 |
(print_bool, parse_bool, print_int, parse_int, print_real, parse_real)
|
|
136 |
where
|
|
137 |
|
|
138 |
import Data.Maybe
|
|
139 |
import qualified Data.List as List
|
|
140 |
import qualified Text.Read as Read
|
|
141 |
|
|
142 |
|
|
143 |
{- bool -}
|
|
144 |
|
|
145 |
print_bool :: Bool -> String
|
|
146 |
print_bool True = "true"
|
|
147 |
print_bool False = "false"
|
|
148 |
|
|
149 |
parse_bool :: String -> Maybe Bool
|
|
150 |
parse_bool "true" = Just True
|
|
151 |
parse_bool "false" = Just False
|
|
152 |
parse_bool _ = Nothing
|
|
153 |
|
|
154 |
|
|
155 |
{- int -}
|
|
156 |
|
|
157 |
print_int :: Int -> String
|
|
158 |
print_int = show
|
|
159 |
|
|
160 |
parse_int :: String -> Maybe Int
|
|
161 |
parse_int = Read.readMaybe
|
|
162 |
|
|
163 |
|
|
164 |
{- real -}
|
|
165 |
|
|
166 |
print_real :: Double -> String
|
|
167 |
print_real x =
|
|
168 |
let s = show x in
|
|
169 |
case span (/= '.') s of
|
|
170 |
(a, '.' : b) | List.all (== '0') b -> a
|
|
171 |
_ -> s
|
|
172 |
|
|
173 |
parse_real :: String -> Maybe Double
|
|
174 |
parse_real = Read.readMaybe
|
|
175 |
\<close>
|
|
176 |
|
69240
|
177 |
generate_haskell_file "Buffer.hs" = \<open>
|
69226
|
178 |
{- Title: Tools/Haskell/Buffer.hs
|
69225
|
179 |
Author: Makarius
|
|
180 |
LICENSE: BSD 3-clause (Isabelle)
|
|
181 |
|
|
182 |
Efficient text buffers.
|
69280
|
183 |
|
|
184 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/General/buffer.ML\<close>.
|
69225
|
185 |
-}
|
69222
|
186 |
|
69225
|
187 |
module Isabelle.Buffer (T, empty, add, content)
|
|
188 |
where
|
|
189 |
|
|
190 |
newtype T = Buffer [String]
|
|
191 |
|
|
192 |
empty :: T
|
|
193 |
empty = Buffer []
|
|
194 |
|
|
195 |
add :: String -> T -> T
|
|
196 |
add "" buf = buf
|
|
197 |
add x (Buffer xs) = Buffer (x : xs)
|
|
198 |
|
|
199 |
content :: T -> String
|
|
200 |
content (Buffer xs) = concat (reverse xs)
|
|
201 |
\<close>
|
|
202 |
|
69240
|
203 |
generate_haskell_file "Properties.hs" = \<open>
|
69226
|
204 |
{- Title: Tools/Haskell/Properties.hs
|
69225
|
205 |
Author: Makarius
|
|
206 |
LICENSE: BSD 3-clause (Isabelle)
|
|
207 |
|
|
208 |
Property lists.
|
69280
|
209 |
|
|
210 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/General/properties.ML\<close>.
|
69225
|
211 |
-}
|
|
212 |
|
|
213 |
module Isabelle.Properties (Entry, T, defined, get, put, remove)
|
|
214 |
where
|
|
215 |
|
|
216 |
import qualified Data.List as List
|
|
217 |
|
|
218 |
|
|
219 |
type Entry = (String, String)
|
|
220 |
type T = [Entry]
|
|
221 |
|
|
222 |
defined :: T -> String -> Bool
|
|
223 |
defined props name = any (\(a, _) -> a == name) props
|
|
224 |
|
|
225 |
get :: T -> String -> Maybe String
|
|
226 |
get props name = List.lookup name props
|
|
227 |
|
|
228 |
put :: Entry -> T -> T
|
|
229 |
put entry props = entry : remove (fst entry) props
|
|
230 |
|
|
231 |
remove :: String -> T -> T
|
|
232 |
remove name props =
|
|
233 |
if defined props name then filter (\(a, _) -> a /= name) props
|
|
234 |
else props
|
|
235 |
\<close>
|
|
236 |
|
69240
|
237 |
generate_haskell_file "Markup.hs" = \<open>
|
69226
|
238 |
{- Title: Haskell/Tools/Markup.hs
|
69225
|
239 |
Author: Makarius
|
|
240 |
LICENSE: BSD 3-clause (Isabelle)
|
|
241 |
|
|
242 |
Quasi-abstract markup elements.
|
69280
|
243 |
|
|
244 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/markup.ML\<close>.
|
69225
|
245 |
-}
|
|
246 |
|
69234
|
247 |
module Isabelle.Markup (
|
|
248 |
T, empty, is_empty, properties,
|
|
249 |
|
|
250 |
nameN, name, xnameN, xname, kindN,
|
|
251 |
|
69288
|
252 |
completionN, completion, no_completionN, no_completion,
|
|
253 |
|
69234
|
254 |
lineN, end_lineN, offsetN, end_offsetN, fileN, idN, positionN, position,
|
|
255 |
|
69248
|
256 |
markupN, consistentN, unbreakableN, indentN, widthN,
|
|
257 |
blockN, block, breakN, break, fbreakN, fbreak, itemN, item,
|
|
258 |
|
69234
|
259 |
wordsN, words, no_wordsN, no_words,
|
|
260 |
|
|
261 |
tfreeN, tfree, tvarN, tvar, freeN, free, skolemN, skolem, boundN, bound, varN, var,
|
|
262 |
numeralN, numeral, literalN, literal, delimiterN, delimiter, inner_stringN, inner_string,
|
|
263 |
inner_cartoucheN, inner_cartouche, inner_commentN, inner_comment,
|
|
264 |
token_rangeN, token_range,
|
|
265 |
sortingN, sorting, typingN, typing, class_parameterN, class_parameter,
|
|
266 |
|
|
267 |
antiquotedN, antiquoted, antiquoteN, antiquote,
|
|
268 |
|
|
269 |
paragraphN, paragraph, text_foldN, text_fold,
|
|
270 |
|
|
271 |
keyword1N, keyword1, keyword2N, keyword2, keyword3N, keyword3, quasi_keywordN, quasi_keyword,
|
|
272 |
improperN, improper, operatorN, operator, stringN, string, alt_stringN, alt_string,
|
|
273 |
verbatimN, verbatim, cartoucheN, cartouche, commentN, comment,
|
|
274 |
|
|
275 |
writelnN, writeln, stateN, state, informationN, information, tracingN, tracing,
|
|
276 |
warningN, warning, legacyN, legacy, errorN, error, reportN, report, no_reportN, no_report,
|
|
277 |
|
|
278 |
intensifyN, intensify,
|
|
279 |
Output, no_output)
|
69225
|
280 |
where
|
|
281 |
|
69248
|
282 |
import Prelude hiding (words, error, break)
|
69234
|
283 |
|
|
284 |
import Isabelle.Library
|
69225
|
285 |
import qualified Isabelle.Properties as Properties
|
69248
|
286 |
import qualified Isabelle.Value as Value
|
69225
|
287 |
|
|
288 |
|
69234
|
289 |
{- basic markup -}
|
|
290 |
|
69225
|
291 |
type T = (String, Properties.T)
|
|
292 |
|
|
293 |
empty :: T
|
|
294 |
empty = ("", [])
|
|
295 |
|
|
296 |
is_empty :: T -> Bool
|
|
297 |
is_empty ("", _) = True
|
|
298 |
is_empty _ = False
|
|
299 |
|
69234
|
300 |
properties :: Properties.T -> T -> T
|
|
301 |
properties more_props (elem, props) =
|
|
302 |
(elem, fold_rev Properties.put more_props props)
|
|
303 |
|
|
304 |
markup_elem name = (name, (name, []) :: T)
|
|
305 |
|
|
306 |
|
|
307 |
{- misc properties -}
|
|
308 |
|
|
309 |
nameN :: String
|
|
310 |
nameN = \<open>Markup.nameN\<close>
|
|
311 |
|
|
312 |
name :: String -> T -> T
|
|
313 |
name a = properties [(nameN, a)]
|
|
314 |
|
|
315 |
xnameN :: String
|
|
316 |
xnameN = \<open>Markup.xnameN\<close>
|
|
317 |
|
|
318 |
xname :: String -> T -> T
|
|
319 |
xname a = properties [(xnameN, a)]
|
|
320 |
|
|
321 |
kindN :: String
|
|
322 |
kindN = \<open>Markup.kindN\<close>
|
|
323 |
|
|
324 |
|
69288
|
325 |
{- completion -}
|
|
326 |
|
|
327 |
completionN :: String; completion :: T
|
|
328 |
(completionN, completion) = markup_elem \<open>Markup.completionN\<close>
|
|
329 |
|
|
330 |
no_completionN :: String; no_completion :: T
|
|
331 |
(no_completionN, no_completion) = markup_elem \<open>Markup.no_completionN\<close>
|
|
332 |
|
|
333 |
|
69234
|
334 |
{- position -}
|
|
335 |
|
|
336 |
lineN, end_lineN :: String
|
|
337 |
lineN = \<open>Markup.lineN\<close>
|
|
338 |
end_lineN = \<open>Markup.end_lineN\<close>
|
|
339 |
|
|
340 |
offsetN, end_offsetN :: String
|
|
341 |
offsetN = \<open>Markup.offsetN\<close>
|
|
342 |
end_offsetN = \<open>Markup.end_offsetN\<close>
|
|
343 |
|
|
344 |
fileN, idN :: String
|
|
345 |
fileN = \<open>Markup.fileN\<close>
|
|
346 |
idN = \<open>Markup.idN\<close>
|
|
347 |
|
|
348 |
positionN :: String; position :: T
|
|
349 |
(positionN, position) = markup_elem \<open>Markup.positionN\<close>
|
|
350 |
|
|
351 |
|
69248
|
352 |
{- pretty printing -}
|
|
353 |
|
|
354 |
markupN, consistentN, unbreakableN, indentN :: String
|
|
355 |
markupN = \<open>Markup.markupN\<close>;
|
|
356 |
consistentN = \<open>Markup.consistentN\<close>;
|
|
357 |
unbreakableN = \<open>Markup.unbreakableN\<close>;
|
|
358 |
indentN = \<open>Markup.indentN\<close>;
|
|
359 |
|
|
360 |
widthN :: String
|
|
361 |
widthN = \<open>Markup.widthN\<close>
|
|
362 |
|
|
363 |
blockN :: String
|
|
364 |
blockN = \<open>Markup.blockN\<close>
|
|
365 |
block :: Bool -> Int -> T
|
|
366 |
block c i =
|
|
367 |
(blockN,
|
|
368 |
(if c then [(consistentN, Value.print_bool c)] else []) ++
|
|
369 |
(if i /= 0 then [(indentN, Value.print_int i)] else []))
|
|
370 |
|
|
371 |
breakN :: String
|
|
372 |
breakN = \<open>Markup.breakN\<close>
|
|
373 |
break :: Int -> Int -> T
|
|
374 |
break w i =
|
|
375 |
(breakN,
|
|
376 |
(if w /= 0 then [(widthN, Value.print_int w)] else []) ++
|
|
377 |
(if i /= 0 then [(indentN, Value.print_int i)] else []))
|
|
378 |
|
|
379 |
fbreakN :: String; fbreak :: T
|
|
380 |
(fbreakN, fbreak) = markup_elem \<open>Markup.fbreakN\<close>
|
|
381 |
|
|
382 |
itemN :: String; item :: T
|
|
383 |
(itemN, item) = markup_elem \<open>Markup.itemN\<close>
|
|
384 |
|
|
385 |
|
69234
|
386 |
{- text properties -}
|
|
387 |
|
|
388 |
wordsN :: String; words :: T
|
|
389 |
(wordsN, words) = markup_elem \<open>Markup.wordsN\<close>
|
|
390 |
|
|
391 |
no_wordsN :: String; no_words :: T
|
|
392 |
(no_wordsN, no_words) = markup_elem \<open>Markup.no_wordsN\<close>
|
|
393 |
|
|
394 |
|
|
395 |
{- inner syntax -}
|
|
396 |
|
|
397 |
tfreeN :: String; tfree :: T
|
|
398 |
(tfreeN, tfree) = markup_elem \<open>Markup.tfreeN\<close>
|
|
399 |
|
|
400 |
tvarN :: String; tvar :: T
|
|
401 |
(tvarN, tvar) = markup_elem \<open>Markup.tvarN\<close>
|
|
402 |
|
|
403 |
freeN :: String; free :: T
|
|
404 |
(freeN, free) = markup_elem \<open>Markup.freeN\<close>
|
|
405 |
|
|
406 |
skolemN :: String; skolem :: T
|
|
407 |
(skolemN, skolem) = markup_elem \<open>Markup.skolemN\<close>
|
|
408 |
|
|
409 |
boundN :: String; bound :: T
|
|
410 |
(boundN, bound) = markup_elem \<open>Markup.boundN\<close>
|
|
411 |
|
|
412 |
varN :: String; var :: T
|
|
413 |
(varN, var) = markup_elem \<open>Markup.varN\<close>
|
|
414 |
|
|
415 |
numeralN :: String; numeral :: T
|
|
416 |
(numeralN, numeral) = markup_elem \<open>Markup.numeralN\<close>
|
|
417 |
|
|
418 |
literalN :: String; literal :: T
|
|
419 |
(literalN, literal) = markup_elem \<open>Markup.literalN\<close>
|
|
420 |
|
|
421 |
delimiterN :: String; delimiter :: T
|
|
422 |
(delimiterN, delimiter) = markup_elem \<open>Markup.delimiterN\<close>
|
|
423 |
|
|
424 |
inner_stringN :: String; inner_string :: T
|
|
425 |
(inner_stringN, inner_string) = markup_elem \<open>Markup.inner_stringN\<close>
|
|
426 |
|
|
427 |
inner_cartoucheN :: String; inner_cartouche :: T
|
|
428 |
(inner_cartoucheN, inner_cartouche) = markup_elem \<open>Markup.inner_cartoucheN\<close>
|
|
429 |
|
|
430 |
inner_commentN :: String; inner_comment :: T
|
|
431 |
(inner_commentN, inner_comment) = markup_elem \<open>Markup.inner_commentN\<close>
|
|
432 |
|
|
433 |
|
|
434 |
token_rangeN :: String; token_range :: T
|
|
435 |
(token_rangeN, token_range) = markup_elem \<open>Markup.token_rangeN\<close>
|
|
436 |
|
|
437 |
|
|
438 |
sortingN :: String; sorting :: T
|
|
439 |
(sortingN, sorting) = markup_elem \<open>Markup.sortingN\<close>
|
|
440 |
|
|
441 |
typingN :: String; typing :: T
|
|
442 |
(typingN, typing) = markup_elem \<open>Markup.typingN\<close>
|
|
443 |
|
|
444 |
class_parameterN :: String; class_parameter :: T
|
|
445 |
(class_parameterN, class_parameter) = markup_elem \<open>Markup.class_parameterN\<close>
|
|
446 |
|
|
447 |
|
|
448 |
{- antiquotations -}
|
|
449 |
|
|
450 |
antiquotedN :: String; antiquoted :: T
|
|
451 |
(antiquotedN, antiquoted) = markup_elem \<open>Markup.antiquotedN\<close>
|
|
452 |
|
|
453 |
antiquoteN :: String; antiquote :: T
|
|
454 |
(antiquoteN, antiquote) = markup_elem \<open>Markup.antiquoteN\<close>
|
|
455 |
|
|
456 |
|
|
457 |
{- text structure -}
|
|
458 |
|
|
459 |
paragraphN :: String; paragraph :: T
|
|
460 |
(paragraphN, paragraph) = markup_elem \<open>Markup.paragraphN\<close>
|
|
461 |
|
|
462 |
text_foldN :: String; text_fold :: T
|
|
463 |
(text_foldN, text_fold) = markup_elem \<open>Markup.text_foldN\<close>
|
|
464 |
|
|
465 |
|
|
466 |
{- outer syntax -}
|
|
467 |
|
|
468 |
keyword1N :: String; keyword1 :: T
|
|
469 |
(keyword1N, keyword1) = markup_elem \<open>Markup.keyword1N\<close>
|
|
470 |
|
|
471 |
keyword2N :: String; keyword2 :: T
|
|
472 |
(keyword2N, keyword2) = markup_elem \<open>Markup.keyword2N\<close>
|
|
473 |
|
|
474 |
keyword3N :: String; keyword3 :: T
|
|
475 |
(keyword3N, keyword3) = markup_elem \<open>Markup.keyword3N\<close>
|
|
476 |
|
|
477 |
quasi_keywordN :: String; quasi_keyword :: T
|
|
478 |
(quasi_keywordN, quasi_keyword) = markup_elem \<open>Markup.quasi_keywordN\<close>
|
|
479 |
|
|
480 |
improperN :: String; improper :: T
|
|
481 |
(improperN, improper) = markup_elem \<open>Markup.improperN\<close>
|
|
482 |
|
|
483 |
operatorN :: String; operator :: T
|
|
484 |
(operatorN, operator) = markup_elem \<open>Markup.operatorN\<close>
|
|
485 |
|
|
486 |
stringN :: String; string :: T
|
|
487 |
(stringN, string) = markup_elem \<open>Markup.stringN\<close>
|
|
488 |
|
|
489 |
alt_stringN :: String; alt_string :: T
|
|
490 |
(alt_stringN, alt_string) = markup_elem \<open>Markup.alt_stringN\<close>
|
|
491 |
|
|
492 |
verbatimN :: String; verbatim :: T
|
|
493 |
(verbatimN, verbatim) = markup_elem \<open>Markup.verbatimN\<close>
|
|
494 |
|
|
495 |
cartoucheN :: String; cartouche :: T
|
|
496 |
(cartoucheN, cartouche) = markup_elem \<open>Markup.cartoucheN\<close>
|
|
497 |
|
|
498 |
commentN :: String; comment :: T
|
|
499 |
(commentN, comment) = markup_elem \<open>Markup.commentN\<close>
|
|
500 |
|
|
501 |
|
|
502 |
{- messages -}
|
|
503 |
|
|
504 |
writelnN :: String; writeln :: T
|
|
505 |
(writelnN, writeln) = markup_elem \<open>Markup.writelnN\<close>
|
|
506 |
|
|
507 |
stateN :: String; state :: T
|
|
508 |
(stateN, state) = markup_elem \<open>Markup.stateN\<close>
|
|
509 |
|
|
510 |
informationN :: String; information :: T
|
|
511 |
(informationN, information) = markup_elem \<open>Markup.informationN\<close>
|
|
512 |
|
|
513 |
tracingN :: String; tracing :: T
|
|
514 |
(tracingN, tracing) = markup_elem \<open>Markup.tracingN\<close>
|
|
515 |
|
|
516 |
warningN :: String; warning :: T
|
|
517 |
(warningN, warning) = markup_elem \<open>Markup.warningN\<close>
|
|
518 |
|
|
519 |
legacyN :: String; legacy :: T
|
|
520 |
(legacyN, legacy) = markup_elem \<open>Markup.legacyN\<close>
|
|
521 |
|
|
522 |
errorN :: String; error :: T
|
|
523 |
(errorN, error) = markup_elem \<open>Markup.errorN\<close>
|
|
524 |
|
|
525 |
reportN :: String; report :: T
|
|
526 |
(reportN, report) = markup_elem \<open>Markup.reportN\<close>
|
|
527 |
|
|
528 |
no_reportN :: String; no_report :: T
|
|
529 |
(no_reportN, no_report) = markup_elem \<open>Markup.no_reportN\<close>
|
|
530 |
|
|
531 |
intensifyN :: String; intensify :: T
|
|
532 |
(intensifyN, intensify) = markup_elem \<open>Markup.intensifyN\<close>
|
|
533 |
|
|
534 |
|
|
535 |
{- output -}
|
69225
|
536 |
|
|
537 |
type Output = (String, String)
|
|
538 |
|
|
539 |
no_output :: Output
|
|
540 |
no_output = ("", "")
|
69222
|
541 |
\<close>
|
|
542 |
|
69288
|
543 |
generate_haskell_file "Completion.hs" = \<open>
|
|
544 |
{- Title: Tools/Haskell/Completion.hs
|
|
545 |
Author: Makarius
|
|
546 |
LICENSE: BSD 3-clause (Isabelle)
|
|
547 |
|
|
548 |
Completion of names.
|
|
549 |
|
|
550 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/General/completion.ML\<close>.
|
|
551 |
-}
|
|
552 |
|
69289
|
553 |
module Isabelle.Completion (
|
|
554 |
Name, T, names, none, make, markup_element, markup_report, make_report
|
|
555 |
) where
|
69288
|
556 |
|
|
557 |
import qualified Data.List as List
|
|
558 |
|
|
559 |
import Isabelle.Library
|
|
560 |
import qualified Isabelle.Properties as Properties
|
|
561 |
import qualified Isabelle.Markup as Markup
|
|
562 |
import qualified Isabelle.XML.Encode as Encode
|
|
563 |
import qualified Isabelle.XML as XML
|
|
564 |
import qualified Isabelle.YXML as YXML
|
|
565 |
|
|
566 |
|
|
567 |
type Name = (String, (String, String)) -- external name, kind, internal name
|
|
568 |
data T = Completion Properties.T Int [Name] -- position, total length, names
|
|
569 |
|
|
570 |
names :: Int -> Properties.T -> [Name] -> T
|
|
571 |
names limit props names = Completion props (length names) (take limit names)
|
|
572 |
|
|
573 |
none :: T
|
|
574 |
none = names 0 [] []
|
|
575 |
|
|
576 |
make :: Int -> (String, Properties.T) -> ((String -> Bool) -> [Name]) -> T
|
|
577 |
make limit (name, props) make_names =
|
|
578 |
if name /= "" && name /= "_"
|
|
579 |
then names limit props (make_names $ List.isPrefixOf $ clean_name name)
|
|
580 |
else none
|
|
581 |
|
69289
|
582 |
markup_element :: T -> (Markup.T, XML.Body)
|
|
583 |
markup_element (Completion props total names) =
|
|
584 |
if not (null names) then
|
|
585 |
let
|
|
586 |
markup = Markup.properties props Markup.completion
|
|
587 |
body =
|
|
588 |
Encode.pair Encode.int
|
|
589 |
(Encode.list (Encode.pair Encode.string (Encode.pair Encode.string Encode.string)))
|
|
590 |
(total, names)
|
|
591 |
in (markup, body)
|
|
592 |
else (Markup.empty, [])
|
69288
|
593 |
|
69289
|
594 |
markup_report :: [T] -> String
|
|
595 |
markup_report [] = ""
|
|
596 |
markup_report elems =
|
69290
|
597 |
YXML.string_of $ XML.Elem (Markup.report, map (XML.Elem . markup_element) elems)
|
69289
|
598 |
|
|
599 |
make_report :: Int -> (String, Properties.T) -> ((String -> Bool) -> [Name]) -> String
|
|
600 |
make_report limit name_props make_names =
|
|
601 |
markup_report [make limit name_props make_names]
|
69288
|
602 |
\<close>
|
|
603 |
|
69278
|
604 |
generate_haskell_file "File.hs" = \<open>
|
|
605 |
{- Title: Tools/Haskell/File.hs
|
|
606 |
Author: Makarius
|
|
607 |
LICENSE: BSD 3-clause (Isabelle)
|
|
608 |
|
69280
|
609 |
File-system operations.
|
|
610 |
|
|
611 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/General/file.ML\<close>.
|
69278
|
612 |
-}
|
|
613 |
|
|
614 |
module Isabelle.File (setup, read, write, append) where
|
|
615 |
|
|
616 |
import Prelude hiding (read)
|
|
617 |
import System.IO (IO)
|
|
618 |
import qualified System.IO as IO
|
|
619 |
|
|
620 |
setup :: IO.Handle -> IO ()
|
|
621 |
setup h = do
|
|
622 |
IO.hSetEncoding h IO.utf8
|
|
623 |
IO.hSetNewlineMode h IO.noNewlineTranslation
|
|
624 |
|
|
625 |
read :: IO.FilePath -> IO String
|
|
626 |
read path =
|
|
627 |
IO.withFile path IO.ReadMode (\h ->
|
|
628 |
do setup h; IO.hGetContents h >>= \s -> length s `seq` return s)
|
|
629 |
|
|
630 |
write :: IO.FilePath -> String -> IO ()
|
|
631 |
write path s =
|
|
632 |
IO.withFile path IO.WriteMode (\h -> do setup h; IO.hPutStr h s)
|
|
633 |
|
|
634 |
append :: IO.FilePath -> String -> IO ()
|
|
635 |
append path s =
|
|
636 |
IO.withFile path IO.AppendMode (\h -> do setup h; IO.hPutStr h s)
|
|
637 |
\<close>
|
|
638 |
|
69240
|
639 |
generate_haskell_file "XML.hs" = \<open>
|
69226
|
640 |
{- Title: Tools/Haskell/XML.hs
|
69225
|
641 |
Author: Makarius
|
|
642 |
LICENSE: BSD 3-clause (Isabelle)
|
|
643 |
|
|
644 |
Untyped XML trees and representation of ML values.
|
69280
|
645 |
|
|
646 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/xml.ML\<close>.
|
69225
|
647 |
-}
|
|
648 |
|
|
649 |
module Isabelle.XML (Attributes, Body, Tree(..), wrap_elem, unwrap_elem, content_of)
|
|
650 |
where
|
|
651 |
|
|
652 |
import qualified Data.List as List
|
|
653 |
|
|
654 |
import Isabelle.Library
|
|
655 |
import qualified Isabelle.Properties as Properties
|
|
656 |
import qualified Isabelle.Markup as Markup
|
|
657 |
import qualified Isabelle.Buffer as Buffer
|
|
658 |
|
|
659 |
|
|
660 |
{- types -}
|
|
661 |
|
|
662 |
type Attributes = Properties.T
|
|
663 |
type Body = [Tree]
|
69290
|
664 |
data Tree = Elem (Markup.T, Body) | Text String
|
69225
|
665 |
|
|
666 |
|
|
667 |
{- wrapped elements -}
|
|
668 |
|
69236
|
669 |
wrap_elem (((a, atts), body1), body2) =
|
69290
|
670 |
Elem ((\<open>XML.xml_elemN\<close>, (\<open>XML.xml_nameN\<close>, a) : atts), Elem ((\<open>XML.xml_bodyN\<close>, []), body1) : body2)
|
69225
|
671 |
|
69236
|
672 |
unwrap_elem
|
69290
|
673 |
(Elem ((\<open>XML.xml_elemN\<close>, (\<open>XML.xml_nameN\<close>, a) : atts), Elem ((\<open>XML.xml_bodyN\<close>, []), body1) : body2)) =
|
69236
|
674 |
Just (((a, atts), body1), body2)
|
69225
|
675 |
unwrap_elem _ = Nothing
|
|
676 |
|
|
677 |
|
|
678 |
{- text content -}
|
|
679 |
|
|
680 |
add_content tree =
|
|
681 |
case unwrap_elem tree of
|
|
682 |
Just (_, ts) -> fold add_content ts
|
|
683 |
Nothing ->
|
|
684 |
case tree of
|
69290
|
685 |
Elem (_, ts) -> fold add_content ts
|
69225
|
686 |
Text s -> Buffer.add s
|
|
687 |
|
|
688 |
content_of body = Buffer.empty |> fold add_content body |> Buffer.content
|
|
689 |
|
|
690 |
|
|
691 |
{- string representation -}
|
|
692 |
|
|
693 |
encode '<' = "<"
|
|
694 |
encode '>' = ">"
|
|
695 |
encode '&' = "&"
|
|
696 |
encode '\'' = "'"
|
|
697 |
encode '\"' = """
|
|
698 |
encode c = [c]
|
|
699 |
|
|
700 |
instance Show Tree where
|
|
701 |
show tree =
|
|
702 |
Buffer.empty |> show_tree tree |> Buffer.content
|
|
703 |
where
|
69290
|
704 |
show_tree (Elem ((name, atts), [])) =
|
69225
|
705 |
Buffer.add "<" #> Buffer.add (show_elem name atts) #> Buffer.add "/>"
|
69290
|
706 |
show_tree (Elem ((name, atts), ts)) =
|
69225
|
707 |
Buffer.add "<" #> Buffer.add (show_elem name atts) #> Buffer.add ">" #>
|
|
708 |
fold show_tree ts #>
|
|
709 |
Buffer.add "</" #> Buffer.add name #> Buffer.add ">"
|
|
710 |
show_tree (Text s) = Buffer.add (show_text s)
|
|
711 |
|
|
712 |
show_elem name atts =
|
|
713 |
unwords (name : map (\(a, x) -> a ++ "=\"" ++ show_text x ++ "\"") atts)
|
|
714 |
|
|
715 |
show_text = concatMap encode
|
|
716 |
\<close>
|
|
717 |
|
69240
|
718 |
generate_haskell_file "XML/Encode.hs" = \<open>
|
|
719 |
{- Title: Tools/Haskell/XML/Encode.hs
|
|
720 |
Author: Makarius
|
|
721 |
LICENSE: BSD 3-clause (Isabelle)
|
|
722 |
|
|
723 |
XML as data representation language.
|
69280
|
724 |
|
|
725 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/xml.ML\<close>.
|
69240
|
726 |
-}
|
|
727 |
|
|
728 |
module Isabelle.XML.Encode (
|
|
729 |
A, T, V,
|
|
730 |
|
|
731 |
int_atom, bool_atom, unit_atom,
|
|
732 |
|
69287
|
733 |
tree, properties, string, int, bool, unit, pair, triple, list, variant
|
69240
|
734 |
)
|
|
735 |
where
|
|
736 |
|
|
737 |
import Isabelle.Library
|
|
738 |
import qualified Isabelle.Value as Value
|
|
739 |
import qualified Isabelle.Properties as Properties
|
|
740 |
import qualified Isabelle.XML as XML
|
|
741 |
|
|
742 |
|
|
743 |
type A a = a -> String
|
|
744 |
type T a = a -> XML.Body
|
|
745 |
type V a = a -> Maybe ([String], XML.Body)
|
|
746 |
|
|
747 |
|
|
748 |
-- atomic values
|
|
749 |
|
|
750 |
int_atom :: A Int
|
|
751 |
int_atom = Value.print_int
|
|
752 |
|
|
753 |
bool_atom :: A Bool
|
|
754 |
bool_atom False = "0"
|
|
755 |
bool_atom True = "1"
|
|
756 |
|
|
757 |
unit_atom :: A ()
|
|
758 |
unit_atom () = ""
|
|
759 |
|
|
760 |
|
|
761 |
-- structural nodes
|
|
762 |
|
69290
|
763 |
node ts = XML.Elem ((":", []), ts)
|
69240
|
764 |
|
|
765 |
vector = map_index (\(i, x) -> (int_atom i, x))
|
|
766 |
|
69290
|
767 |
tagged (tag, (xs, ts)) = XML.Elem ((int_atom tag, vector xs), ts)
|
69240
|
768 |
|
|
769 |
|
|
770 |
-- representation of standard types
|
|
771 |
|
|
772 |
tree :: T XML.Tree
|
|
773 |
tree t = [t]
|
|
774 |
|
|
775 |
properties :: T Properties.T
|
69290
|
776 |
properties props = [XML.Elem ((":", props), [])]
|
69240
|
777 |
|
|
778 |
string :: T String
|
|
779 |
string "" = []
|
|
780 |
string s = [XML.Text s]
|
|
781 |
|
|
782 |
int :: T Int
|
|
783 |
int = string . int_atom
|
|
784 |
|
|
785 |
bool :: T Bool
|
|
786 |
bool = string . bool_atom
|
|
787 |
|
|
788 |
unit :: T ()
|
|
789 |
unit = string . unit_atom
|
|
790 |
|
|
791 |
pair :: T a -> T b -> T (a, b)
|
|
792 |
pair f g (x, y) = [node (f x), node (g y)]
|
|
793 |
|
|
794 |
triple :: T a -> T b -> T c -> T (a, b, c)
|
|
795 |
triple f g h (x, y, z) = [node (f x), node (g y), node (h z)]
|
|
796 |
|
|
797 |
list :: T a -> T [a]
|
|
798 |
list f xs = map (node . f) xs
|
|
799 |
|
|
800 |
variant :: [V a] -> T a
|
|
801 |
variant fs x = [tagged (the (get_index (\f -> f x) fs))]
|
|
802 |
\<close>
|
|
803 |
|
|
804 |
generate_haskell_file "XML/Decode.hs" = \<open>
|
|
805 |
{- Title: Tools/Haskell/XML/Decode.hs
|
|
806 |
Author: Makarius
|
|
807 |
LICENSE: BSD 3-clause (Isabelle)
|
|
808 |
|
|
809 |
XML as data representation language.
|
69280
|
810 |
|
|
811 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/xml.ML\<close>.
|
69240
|
812 |
-}
|
|
813 |
|
|
814 |
module Isabelle.XML.Decode (
|
|
815 |
A, T, V,
|
|
816 |
|
|
817 |
int_atom, bool_atom, unit_atom,
|
|
818 |
|
|
819 |
tree, properties, string, init, bool, unit, pair, triple, list, variant
|
|
820 |
)
|
|
821 |
where
|
|
822 |
|
|
823 |
import Data.List ((!!))
|
|
824 |
|
|
825 |
import Isabelle.Library
|
|
826 |
import qualified Isabelle.Value as Value
|
|
827 |
import qualified Isabelle.Properties as Properties
|
|
828 |
import qualified Isabelle.XML as XML
|
|
829 |
|
|
830 |
|
|
831 |
type A a = String -> a
|
|
832 |
type T a = XML.Body -> a
|
|
833 |
type V a = ([String], XML.Body) -> a
|
|
834 |
|
|
835 |
err_atom = error "Malformed XML atom"
|
|
836 |
err_body = error "Malformed XML body"
|
|
837 |
|
|
838 |
|
|
839 |
{- atomic values -}
|
|
840 |
|
|
841 |
int_atom :: A Int
|
|
842 |
int_atom s =
|
|
843 |
case Value.parse_int s of
|
|
844 |
Just i -> i
|
|
845 |
Nothing -> err_atom
|
|
846 |
|
|
847 |
bool_atom :: A Bool
|
|
848 |
bool_atom "0" = False
|
|
849 |
bool_atom "1" = True
|
|
850 |
bool_atom _ = err_atom
|
|
851 |
|
|
852 |
unit_atom :: A ()
|
|
853 |
unit_atom "" = ()
|
|
854 |
unit_atom _ = err_atom
|
|
855 |
|
|
856 |
|
|
857 |
{- structural nodes -}
|
|
858 |
|
69290
|
859 |
node (XML.Elem ((":", []), ts)) = ts
|
69240
|
860 |
node _ = err_body
|
|
861 |
|
|
862 |
vector atts =
|
|
863 |
map_index (\(i, (a, x)) -> if int_atom a == i then x else err_atom) atts
|
|
864 |
|
69290
|
865 |
tagged (XML.Elem ((name, atts), ts)) = (int_atom name, (vector atts, ts))
|
69240
|
866 |
tagged _ = err_body
|
|
867 |
|
|
868 |
|
|
869 |
{- representation of standard types -}
|
|
870 |
|
|
871 |
tree :: T XML.Tree
|
|
872 |
tree [t] = t
|
|
873 |
tree _ = err_body
|
|
874 |
|
|
875 |
properties :: T Properties.T
|
69290
|
876 |
properties [XML.Elem ((":", props), [])] = props
|
69240
|
877 |
properties _ = err_body
|
|
878 |
|
|
879 |
string :: T String
|
|
880 |
string [] = ""
|
|
881 |
string [XML.Text s] = s
|
|
882 |
string _ = err_body
|
|
883 |
|
|
884 |
int :: T Int
|
|
885 |
int = int_atom . string
|
|
886 |
|
|
887 |
bool :: T Bool
|
|
888 |
bool = bool_atom . string
|
|
889 |
|
|
890 |
unit :: T ()
|
|
891 |
unit = unit_atom . string
|
|
892 |
|
|
893 |
pair :: T a -> T b -> T (a, b)
|
|
894 |
pair f g [t1, t2] = (f (node t1), g (node t2))
|
|
895 |
pair _ _ _ = err_body
|
|
896 |
|
|
897 |
triple :: T a -> T b -> T c -> T (a, b, c)
|
|
898 |
triple f g h [t1, t2, t3] = (f (node t1), g (node t2), h (node t3))
|
|
899 |
triple _ _ _ _ = err_body
|
|
900 |
|
|
901 |
list :: T a -> T [a]
|
|
902 |
list f ts = map (f . node) ts
|
|
903 |
|
|
904 |
option :: T a -> T (Maybe a)
|
|
905 |
option _ [] = Nothing
|
|
906 |
option f [t] = Just (f (node t))
|
|
907 |
option _ _ = err_body
|
|
908 |
|
|
909 |
variant :: [V a] -> T a
|
|
910 |
variant fs [t] = (fs !! tag) (xs, ts)
|
|
911 |
where (tag, (xs, ts)) = tagged t
|
|
912 |
variant _ _ = err_body
|
|
913 |
\<close>
|
|
914 |
|
69288
|
915 |
generate_haskell_file "YXML.hs" = \<open>
|
|
916 |
{- Title: Tools/Haskell/YXML.hs
|
|
917 |
Author: Makarius
|
|
918 |
LICENSE: BSD 3-clause (Isabelle)
|
|
919 |
|
|
920 |
Efficient text representation of XML trees. Suitable for direct
|
|
921 |
inlining into plain text.
|
|
922 |
|
|
923 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/yxml.ML\<close>.
|
|
924 |
-}
|
|
925 |
|
|
926 |
module Isabelle.YXML (charX, charY, strX, strY, detect, output_markup,
|
|
927 |
buffer_body, buffer, string_of_body, string_of, parse_body, parse)
|
|
928 |
where
|
|
929 |
|
|
930 |
import qualified Data.Char as Char
|
|
931 |
import qualified Data.List as List
|
|
932 |
|
|
933 |
import Isabelle.Library
|
|
934 |
import qualified Isabelle.Markup as Markup
|
|
935 |
import qualified Isabelle.XML as XML
|
|
936 |
import qualified Isabelle.Buffer as Buffer
|
|
937 |
|
|
938 |
|
|
939 |
{- markers -}
|
|
940 |
|
|
941 |
charX, charY :: Char
|
|
942 |
charX = Char.chr 5
|
|
943 |
charY = Char.chr 6
|
|
944 |
|
|
945 |
strX, strY, strXY, strXYX :: String
|
|
946 |
strX = [charX]
|
|
947 |
strY = [charY]
|
|
948 |
strXY = strX ++ strY
|
|
949 |
strXYX = strXY ++ strX
|
|
950 |
|
|
951 |
detect :: String -> Bool
|
|
952 |
detect = any (\c -> c == charX || c == charY)
|
|
953 |
|
|
954 |
|
|
955 |
{- output -}
|
|
956 |
|
|
957 |
output_markup :: Markup.T -> Markup.Output
|
|
958 |
output_markup markup@(name, atts) =
|
|
959 |
if Markup.is_empty markup then Markup.no_output
|
|
960 |
else (strXY ++ name ++ concatMap (\(a, x) -> strY ++ a ++ "=" ++ x) atts ++ strX, strXYX)
|
|
961 |
|
|
962 |
buffer_attrib (a, x) =
|
|
963 |
Buffer.add strY #> Buffer.add a #> Buffer.add "=" #> Buffer.add x
|
|
964 |
|
|
965 |
buffer_body :: XML.Body -> Buffer.T -> Buffer.T
|
|
966 |
buffer_body = fold buffer
|
|
967 |
|
|
968 |
buffer :: XML.Tree -> Buffer.T -> Buffer.T
|
69290
|
969 |
buffer (XML.Elem ((name, atts), ts)) =
|
69288
|
970 |
Buffer.add strXY #> Buffer.add name #> fold buffer_attrib atts #> Buffer.add strX #>
|
|
971 |
buffer_body ts #>
|
|
972 |
Buffer.add strXYX
|
|
973 |
buffer (XML.Text s) = Buffer.add s
|
|
974 |
|
|
975 |
string_of_body :: XML.Body -> String
|
|
976 |
string_of_body body = Buffer.empty |> buffer_body body |> Buffer.content
|
|
977 |
|
|
978 |
string_of :: XML.Tree -> String
|
|
979 |
string_of = string_of_body . single
|
|
980 |
|
|
981 |
|
|
982 |
{- parse -}
|
|
983 |
|
|
984 |
-- split: fields or non-empty tokens
|
|
985 |
|
|
986 |
split :: Bool -> Char -> String -> [String]
|
|
987 |
split _ _ [] = []
|
|
988 |
split fields sep str = splitting str
|
|
989 |
where
|
|
990 |
splitting rest =
|
|
991 |
case span (/= sep) rest of
|
|
992 |
(_, []) -> cons rest []
|
|
993 |
(prfx, _ : rest') -> cons prfx (splitting rest')
|
|
994 |
cons item = if fields || not (null item) then (:) item else id
|
|
995 |
|
|
996 |
|
|
997 |
-- structural errors
|
|
998 |
|
|
999 |
err msg = error ("Malformed YXML: " ++ msg)
|
|
1000 |
err_attribute = err "bad attribute"
|
|
1001 |
err_element = err "bad element"
|
|
1002 |
err_unbalanced "" = err "unbalanced element"
|
|
1003 |
err_unbalanced name = err ("unbalanced element " ++ quote name)
|
|
1004 |
|
|
1005 |
|
|
1006 |
-- stack operations
|
|
1007 |
|
|
1008 |
add x ((elem, body) : pending) = (elem, x : body) : pending
|
|
1009 |
|
|
1010 |
push "" _ _ = err_element
|
|
1011 |
push name atts pending = ((name, atts), []) : pending
|
|
1012 |
|
|
1013 |
pop ((("", _), _) : _) = err_unbalanced ""
|
69290
|
1014 |
pop ((markup, body) : pending) = add (XML.Elem (markup, reverse body)) pending
|
69288
|
1015 |
|
|
1016 |
|
|
1017 |
-- parsing
|
|
1018 |
|
|
1019 |
parse_attrib s =
|
|
1020 |
case List.elemIndex '=' s of
|
|
1021 |
Just i | i > 0 -> (take i s, drop (i + 1) s)
|
|
1022 |
_ -> err_attribute
|
|
1023 |
|
|
1024 |
parse_chunk ["", ""] = pop
|
|
1025 |
parse_chunk ("" : name : atts) = push name (map parse_attrib atts)
|
|
1026 |
parse_chunk txts = fold (add . XML.Text) txts
|
|
1027 |
|
|
1028 |
parse_body :: String -> XML.Body
|
|
1029 |
parse_body source =
|
|
1030 |
case fold parse_chunk chunks [(("", []), [])] of
|
|
1031 |
[(("", _), result)] -> reverse result
|
|
1032 |
((name, _), _) : _ -> err_unbalanced name
|
|
1033 |
where chunks = split False charX source |> map (split True charY)
|
|
1034 |
|
|
1035 |
parse :: String -> XML.Tree
|
|
1036 |
parse source =
|
|
1037 |
case parse_body source of
|
|
1038 |
[result] -> result
|
|
1039 |
[] -> XML.Text ""
|
|
1040 |
_ -> err "multiple results"
|
|
1041 |
\<close>
|
|
1042 |
|
69248
|
1043 |
generate_haskell_file "Pretty.hs" = \<open>
|
|
1044 |
{- Title: Tools/Haskell/Pretty.hs
|
|
1045 |
Author: Makarius
|
|
1046 |
LICENSE: BSD 3-clause (Isabelle)
|
|
1047 |
|
|
1048 |
Generic pretty printing module.
|
69280
|
1049 |
|
|
1050 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/General/pretty.ML\<close>.
|
69248
|
1051 |
-}
|
|
1052 |
|
|
1053 |
module Isabelle.Pretty (
|
|
1054 |
T, symbolic, formatted, unformatted,
|
|
1055 |
|
|
1056 |
str, brk_indent, brk, fbrk, breaks, fbreaks, blk, block, strs, markup, mark, mark_str, marks_str,
|
|
1057 |
item, text_fold, keyword1, keyword2, text, paragraph, para, quote, cartouche, separate,
|
|
1058 |
commas, enclose, enum, list, str_list, big_list)
|
|
1059 |
where
|
|
1060 |
|
|
1061 |
import Isabelle.Library hiding (quote)
|
|
1062 |
import qualified Data.List as List
|
|
1063 |
import qualified Isabelle.Buffer as Buffer
|
|
1064 |
import qualified Isabelle.Markup as Markup
|
|
1065 |
import qualified Isabelle.XML as XML
|
|
1066 |
import qualified Isabelle.YXML as YXML
|
|
1067 |
|
|
1068 |
|
|
1069 |
data T =
|
|
1070 |
Block Markup.T Bool Int [T]
|
|
1071 |
| Break Int Int
|
|
1072 |
| Str String
|
|
1073 |
|
|
1074 |
|
|
1075 |
{- output -}
|
|
1076 |
|
|
1077 |
output_spaces n = replicate n ' '
|
|
1078 |
|
|
1079 |
symbolic_text "" = []
|
|
1080 |
symbolic_text s = [XML.Text s]
|
|
1081 |
|
|
1082 |
symbolic_markup markup body =
|
|
1083 |
if Markup.is_empty markup then body
|
69290
|
1084 |
else [XML.Elem (markup, body)]
|
69248
|
1085 |
|
|
1086 |
symbolic :: T -> XML.Body
|
|
1087 |
symbolic (Block markup consistent indent prts) =
|
|
1088 |
concatMap symbolic prts
|
|
1089 |
|> symbolic_markup block_markup
|
|
1090 |
|> symbolic_markup markup
|
|
1091 |
where block_markup = if null prts then Markup.empty else Markup.block consistent indent
|
69290
|
1092 |
symbolic (Break wd ind) = [XML.Elem (Markup.break wd ind, symbolic_text (output_spaces wd))]
|
69248
|
1093 |
symbolic (Str s) = symbolic_text s
|
|
1094 |
|
|
1095 |
formatted :: T -> String
|
|
1096 |
formatted = YXML.string_of_body . symbolic
|
|
1097 |
|
|
1098 |
unformatted :: T -> String
|
|
1099 |
unformatted prt = Buffer.empty |> out prt |> Buffer.content
|
|
1100 |
where
|
|
1101 |
out (Block markup _ _ prts) =
|
|
1102 |
let (bg, en) = YXML.output_markup markup
|
|
1103 |
in Buffer.add bg #> fold out prts #> Buffer.add en
|
|
1104 |
out (Break _ wd) = Buffer.add (output_spaces wd)
|
|
1105 |
out (Str s) = Buffer.add s
|
|
1106 |
|
|
1107 |
|
|
1108 |
{- derived operations to create formatting expressions -}
|
|
1109 |
|
|
1110 |
force_nat n | n < 0 = 0
|
|
1111 |
force_nat n = n
|
|
1112 |
|
|
1113 |
str :: String -> T
|
|
1114 |
str = Str
|
|
1115 |
|
|
1116 |
brk_indent :: Int -> Int -> T
|
|
1117 |
brk_indent wd ind = Break (force_nat wd) ind
|
|
1118 |
|
|
1119 |
brk :: Int -> T
|
|
1120 |
brk wd = brk_indent wd 0
|
|
1121 |
|
|
1122 |
fbrk :: T
|
|
1123 |
fbrk = str "\n"
|
|
1124 |
|
|
1125 |
breaks, fbreaks :: [T] -> [T]
|
|
1126 |
breaks = List.intersperse (brk 1)
|
|
1127 |
fbreaks = List.intersperse fbrk
|
|
1128 |
|
|
1129 |
blk :: (Int, [T]) -> T
|
|
1130 |
blk (indent, es) = Block Markup.empty False (force_nat indent) es
|
|
1131 |
|
|
1132 |
block :: [T] -> T
|
|
1133 |
block prts = blk (2, prts)
|
|
1134 |
|
|
1135 |
strs :: [String] -> T
|
|
1136 |
strs = block . breaks . map str
|
|
1137 |
|
|
1138 |
markup :: Markup.T -> [T] -> T
|
|
1139 |
markup m = Block m False 0
|
|
1140 |
|
|
1141 |
mark :: Markup.T -> T -> T
|
|
1142 |
mark m prt = if m == Markup.empty then prt else markup m [prt]
|
|
1143 |
|
|
1144 |
mark_str :: (Markup.T, String) -> T
|
|
1145 |
mark_str (m, s) = mark m (str s)
|
|
1146 |
|
|
1147 |
marks_str :: ([Markup.T], String) -> T
|
|
1148 |
marks_str (ms, s) = fold_rev mark ms (str s)
|
|
1149 |
|
|
1150 |
item :: [T] -> T
|
|
1151 |
item = markup Markup.item
|
|
1152 |
|
|
1153 |
text_fold :: [T] -> T
|
|
1154 |
text_fold = markup Markup.text_fold
|
|
1155 |
|
|
1156 |
keyword1, keyword2 :: String -> T
|
|
1157 |
keyword1 name = mark_str (Markup.keyword1, name)
|
|
1158 |
keyword2 name = mark_str (Markup.keyword2, name)
|
|
1159 |
|
|
1160 |
text :: String -> [T]
|
|
1161 |
text = breaks . map str . words
|
|
1162 |
|
|
1163 |
paragraph :: [T] -> T
|
|
1164 |
paragraph = markup Markup.paragraph
|
|
1165 |
|
|
1166 |
para :: String -> T
|
|
1167 |
para = paragraph . text
|
|
1168 |
|
|
1169 |
quote :: T -> T
|
|
1170 |
quote prt = blk (1, [str "\"", prt, str "\""])
|
|
1171 |
|
|
1172 |
cartouche :: T -> T
|
|
1173 |
cartouche prt = blk (1, [str "\92<open>", prt, str "\92<close>"])
|
|
1174 |
|
|
1175 |
separate :: String -> [T] -> [T]
|
|
1176 |
separate sep = List.intercalate [str sep, brk 1] . map single
|
|
1177 |
|
|
1178 |
commas :: [T] -> [T]
|
|
1179 |
commas = separate ","
|
|
1180 |
|
|
1181 |
enclose :: String -> String -> [T] -> T
|
|
1182 |
enclose lpar rpar prts = block (str lpar : prts ++ [str rpar])
|
|
1183 |
|
|
1184 |
enum :: String -> String -> String -> [T] -> T
|
|
1185 |
enum sep lpar rpar = enclose lpar rpar . separate sep
|
|
1186 |
|
|
1187 |
list :: String -> String -> [T] -> T
|
|
1188 |
list = enum ","
|
|
1189 |
|
|
1190 |
str_list :: String -> String -> [String] -> T
|
|
1191 |
str_list lpar rpar = list lpar rpar . map str
|
|
1192 |
|
|
1193 |
big_list :: String -> [T] -> T
|
|
1194 |
big_list name prts = block (fbreaks (str name : prts))
|
|
1195 |
\<close>
|
|
1196 |
|
69240
|
1197 |
generate_haskell_file "Term.hs" = \<open>
|
|
1198 |
{- Title: Tools/Haskell/Term.hs
|
|
1199 |
Author: Makarius
|
|
1200 |
LICENSE: BSD 3-clause (Isabelle)
|
|
1201 |
|
|
1202 |
Lambda terms, types, sorts.
|
69280
|
1203 |
|
|
1204 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/term.scala\<close>.
|
69240
|
1205 |
-}
|
|
1206 |
|
|
1207 |
module Isabelle.Term (
|
|
1208 |
Indexname,
|
|
1209 |
|
|
1210 |
Sort, dummyS,
|
|
1211 |
|
|
1212 |
Typ(..), dummyT, Term(..))
|
|
1213 |
where
|
|
1214 |
|
|
1215 |
type Indexname = (String, Int)
|
|
1216 |
|
|
1217 |
|
|
1218 |
type Sort = [String]
|
|
1219 |
|
|
1220 |
dummyS :: Sort
|
|
1221 |
dummyS = [""]
|
|
1222 |
|
|
1223 |
|
|
1224 |
data Typ =
|
|
1225 |
Type (String, [Typ])
|
|
1226 |
| TFree (String, Sort)
|
|
1227 |
| TVar (Indexname, Sort)
|
|
1228 |
deriving Show
|
|
1229 |
|
|
1230 |
dummyT :: Typ
|
|
1231 |
dummyT = Type (\<open>\<^type_name>\<open>dummy\<close>\<close>, [])
|
|
1232 |
|
|
1233 |
|
|
1234 |
data Term =
|
|
1235 |
Const (String, Typ)
|
|
1236 |
| Free (String, Typ)
|
|
1237 |
| Var (Indexname, Typ)
|
|
1238 |
| Bound Int
|
|
1239 |
| Abs (String, Typ, Term)
|
|
1240 |
| App (Term, Term)
|
|
1241 |
deriving Show
|
|
1242 |
\<close>
|
|
1243 |
|
|
1244 |
generate_haskell_file "Term_XML/Encode.hs" = \<open>
|
|
1245 |
{- Title: Tools/Haskell/Term_XML/Encode.hs
|
|
1246 |
Author: Makarius
|
|
1247 |
LICENSE: BSD 3-clause (Isabelle)
|
|
1248 |
|
|
1249 |
XML data representation of lambda terms.
|
69280
|
1250 |
|
|
1251 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/term_xml.ML\<close>.
|
69240
|
1252 |
-}
|
|
1253 |
|
|
1254 |
{-# LANGUAGE LambdaCase #-}
|
|
1255 |
|
|
1256 |
module Isabelle.Term_XML.Encode (sort, typ, term)
|
|
1257 |
where
|
|
1258 |
|
|
1259 |
import Isabelle.Library
|
|
1260 |
import qualified Isabelle.XML as XML
|
|
1261 |
import Isabelle.XML.Encode
|
|
1262 |
import Isabelle.Term
|
|
1263 |
|
|
1264 |
|
|
1265 |
sort :: T Sort
|
|
1266 |
sort = list string
|
|
1267 |
|
|
1268 |
typ :: T Typ
|
|
1269 |
typ ty =
|
|
1270 |
ty |> variant
|
|
1271 |
[\case { Type (a, b) -> Just ([a], list typ b); _ -> Nothing },
|
|
1272 |
\case { TFree (a, b) -> Just ([a], sort b); _ -> Nothing },
|
|
1273 |
\case { TVar ((a, b), c) -> Just ([a, int_atom b], sort c); _ -> Nothing }]
|
|
1274 |
|
|
1275 |
term :: T Term
|
|
1276 |
term t =
|
|
1277 |
t |> variant
|
|
1278 |
[\case { Const (a, b) -> Just ([a], typ b); _ -> Nothing },
|
|
1279 |
\case { Free (a, b) -> Just ([a], typ b); _ -> Nothing },
|
|
1280 |
\case { Var ((a, b), c) -> Just ([a, int_atom b], typ c); _ -> Nothing },
|
|
1281 |
\case { Bound a -> Just ([int_atom a], []); _ -> Nothing },
|
|
1282 |
\case { Abs (a, b, c) -> Just ([a], pair typ term (b, c)); _ -> Nothing },
|
|
1283 |
\case { App a -> Just ([], pair term term a); _ -> Nothing }]
|
|
1284 |
\<close>
|
|
1285 |
|
|
1286 |
generate_haskell_file "Term_XML/Decode.hs" = \<open>
|
|
1287 |
{- Title: Tools/Haskell/Term_XML/Decode.hs
|
|
1288 |
Author: Makarius
|
|
1289 |
LICENSE: BSD 3-clause (Isabelle)
|
|
1290 |
|
|
1291 |
XML data representation of lambda terms.
|
69280
|
1292 |
|
|
1293 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/term_xml.ML\<close>.
|
69240
|
1294 |
-}
|
|
1295 |
|
|
1296 |
module Isabelle.Term_XML.Decode (sort, typ, term)
|
|
1297 |
where
|
|
1298 |
|
|
1299 |
import Isabelle.Library
|
|
1300 |
import qualified Isabelle.XML as XML
|
|
1301 |
import Isabelle.XML.Decode
|
|
1302 |
import Isabelle.Term
|
|
1303 |
|
|
1304 |
|
|
1305 |
sort :: T Sort
|
|
1306 |
sort = list string
|
|
1307 |
|
|
1308 |
typ :: T Typ
|
|
1309 |
typ ty =
|
|
1310 |
ty |> variant
|
|
1311 |
[\([a], b) -> Type (a, list typ b),
|
|
1312 |
\([a], b) -> TFree (a, sort b),
|
|
1313 |
\([a, b], c) -> TVar ((a, int_atom b), sort c)]
|
|
1314 |
|
|
1315 |
term :: T Term
|
|
1316 |
term t =
|
|
1317 |
t |> variant
|
|
1318 |
[\([a], b) -> Const (a, typ b),
|
|
1319 |
\([a], b) -> Free (a, typ b),
|
|
1320 |
\([a, b], c) -> Var ((a, int_atom b), typ c),
|
|
1321 |
\([a], []) -> Bound (int_atom a),
|
|
1322 |
\([a], b) -> let (c, d) = pair typ term b in Abs (a, c, d),
|
|
1323 |
\([], a) -> App (pair term term a)]
|
|
1324 |
\<close>
|
|
1325 |
|
69222
|
1326 |
end
|