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