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