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