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