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