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