author | wenzelm |
Sun, 01 Aug 2021 23:18:13 +0200 | |
changeset 74100 | fb9c119e5b49 |
parent 74099 | 0bda15b1b937 |
child 74105 | d3d6e01a6b00 |
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 |
||
74084 | 11 |
generate_file "Isabelle/Bytes.hs" = \<open> |
12 |
{- Title: Isabelle/Bytes.hs |
|
13 |
Author: Makarius |
|
14 |
LICENSE: BSD 3-clause (Isabelle) |
|
15 |
||
16 |
Compact byte strings. |
|
17 |
||
18 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/General/bytes.ML\<close> |
|
19 |
and \<^file>\<open>$ISABELLE_HOME/src/Pure/General/bytes.scala\<close>. |
|
20 |
-} |
|
21 |
||
22 |
module Isabelle.Bytes ( |
|
23 |
Bytes, |
|
24 |
make, unmake, pack, unpack, |
|
25 |
empty, null, length, index, all, any, |
|
74099 | 26 |
head, last, take, drop, isPrefixOf, isSuffixOf, |
27 |
concat, space, spaces, singleton, char, byte, |
|
74095 | 28 |
trim_line, space_explode, split_lines |
74084 | 29 |
) |
30 |
where |
|
31 |
||
32 |
import Prelude hiding (null, length, all, any, head, last, take, drop, concat) |
|
33 |
||
34 |
import qualified Data.ByteString.Short as ShortByteString |
|
35 |
import Data.ByteString.Short (ShortByteString) |
|
74090 | 36 |
import qualified Data.ByteString as ByteString |
74084 | 37 |
import Data.ByteString (ByteString) |
38 |
import qualified Data.List as List |
|
39 |
import Data.Word (Word8) |
|
74090 | 40 |
import Data.Array (Array, array, (!)) |
74084 | 41 |
|
42 |
||
43 |
type Bytes = ShortByteString |
|
44 |
||
45 |
make :: ByteString -> Bytes |
|
46 |
make = ShortByteString.toShort |
|
47 |
||
48 |
unmake :: Bytes -> ByteString |
|
49 |
unmake = ShortByteString.fromShort |
|
50 |
||
51 |
pack :: [Word8] -> Bytes |
|
52 |
pack = ShortByteString.pack |
|
53 |
||
54 |
unpack :: Bytes -> [Word8] |
|
55 |
unpack = ShortByteString.unpack |
|
56 |
||
57 |
empty :: Bytes |
|
58 |
empty = ShortByteString.empty |
|
59 |
||
60 |
null :: Bytes -> Bool |
|
61 |
null = ShortByteString.null |
|
62 |
||
63 |
length :: Bytes -> Int |
|
64 |
length = ShortByteString.length |
|
65 |
||
66 |
index :: Bytes -> Int -> Word8 |
|
67 |
index = ShortByteString.index |
|
68 |
||
69 |
all :: (Word8 -> Bool) -> Bytes -> Bool |
|
70 |
all p = List.all p . unpack |
|
71 |
||
72 |
any :: (Word8 -> Bool) -> Bytes -> Bool |
|
73 |
any p = List.any p . unpack |
|
74 |
||
75 |
head :: Bytes -> Word8 |
|
76 |
head bytes = index bytes 0 |
|
77 |
||
78 |
last :: Bytes -> Word8 |
|
79 |
last bytes = index bytes (length bytes - 1) |
|
80 |
||
81 |
take :: Int -> Bytes -> Bytes |
|
82 |
take n = pack . List.take n . unpack |
|
83 |
||
84 |
drop :: Int -> Bytes -> Bytes |
|
85 |
drop n = pack . List.drop n . unpack |
|
86 |
||
74099 | 87 |
isPrefixOf :: Bytes -> Bytes -> Bool |
88 |
isPrefixOf bs1 bs2 = |
|
89 |
n1 <= n2 && List.all (\i -> index bs1 i == index bs2 i) [0 .. n1 - 1] |
|
90 |
where n1 = length bs1; n2 = length bs2 |
|
91 |
||
92 |
isSuffixOf :: Bytes -> Bytes -> Bool |
|
93 |
isSuffixOf bs1 bs2 = |
|
94 |
n1 <= n2 && List.all (\i -> index bs1 i == index bs2 (i + k)) [0 .. n1 - 1] |
|
95 |
where n1 = length bs1; n2 = length bs2; k = n2 - n1 |
|
96 |
||
74084 | 97 |
concat :: [Bytes] -> Bytes |
98 |
concat = mconcat |
|
74088 | 99 |
|
74095 | 100 |
space :: Word8 |
101 |
space = 32 |
|
102 |
||
103 |
small_spaces :: Array Int Bytes |
|
104 |
small_spaces = array (0, 64) [(i, pack (replicate i space)) | i <- [0 .. 64]] |
|
105 |
||
106 |
spaces :: Int -> Bytes |
|
107 |
spaces n = |
|
108 |
if n < 64 then small_spaces ! n |
|
109 |
else concat ((small_spaces ! (n `mod` 64)) : replicate (n `div` 64) (small_spaces ! 64)) |
|
110 |
||
74090 | 111 |
singletons :: Array Word8 Bytes |
112 |
singletons = |
|
74095 | 113 |
array (minBound, maxBound) |
114 |
[(i, make (ByteString.singleton i)) | i <- [minBound .. maxBound]] |
|
74090 | 115 |
|
116 |
singleton :: Word8 -> Bytes |
|
117 |
singleton b = singletons ! b |
|
118 |
||
74095 | 119 |
char :: Word8 -> Char |
120 |
char = toEnum . fromEnum |
|
121 |
||
122 |
byte :: Char -> Word8 |
|
123 |
byte = toEnum . fromEnum |
|
124 |
||
74088 | 125 |
trim_line :: Bytes -> Bytes |
126 |
trim_line s = |
|
74089 | 127 |
if n >= 2 && at (n - 2) == '\r' && at (n - 1) == '\n' then take (n - 2) s |
128 |
else if n >= 1 && (at (n - 1) == '\r' || at (n - 1) == '\n') then take (n - 1) s |
|
74088 | 129 |
else s |
130 |
where |
|
131 |
n = length s |
|
74095 | 132 |
at = char . index s |
133 |
||
134 |
space_explode :: Word8 -> Bytes -> [Bytes] |
|
135 |
space_explode sep str = |
|
136 |
if null str then [] |
|
137 |
else if all (/= sep) str then [str] |
|
138 |
else explode (unpack str) |
|
139 |
where |
|
140 |
explode rest = |
|
141 |
case span (/= sep) rest of |
|
142 |
(_, []) -> [pack rest] |
|
143 |
(prfx, _ : rest') -> pack prfx : explode rest' |
|
144 |
||
145 |
split_lines :: Bytes -> [Bytes] |
|
146 |
split_lines = space_explode (byte '\n') |
|
74084 | 147 |
\<close> |
148 |
||
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
149 |
generate_file "Isabelle/UTF8.hs" = \<open> |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
150 |
{- Title: Isabelle/UTF8.hs |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
151 |
Author: Makarius |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
152 |
LICENSE: BSD 3-clause (Isabelle) |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
153 |
|
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
154 |
Variations on UTF-8. |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
155 |
|
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
156 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/General/utf8.ML\<close> |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
157 |
and \<^file>\<open>$ISABELLE_HOME/src/Pure/General/utf8.scala\<close>. |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
158 |
-} |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
159 |
|
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
160 |
{-# LANGUAGE MultiParamTypeClasses #-} |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
161 |
{-# LANGUAGE TypeSynonymInstances #-} |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
162 |
{-# LANGUAGE FlexibleInstances #-} |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
163 |
{-# LANGUAGE InstanceSigs #-} |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
164 |
|
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
165 |
module Isabelle.UTF8 ( |
74098 | 166 |
setup, setup3, |
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
167 |
Recode (..) |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
168 |
) |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
169 |
where |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
170 |
|
74098 | 171 |
import qualified System.IO as IO |
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
172 |
import Data.Text (Text) |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
173 |
import qualified Data.Text as Text |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
174 |
import qualified Data.Text.Encoding as Encoding |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
175 |
import qualified Data.Text.Encoding.Error as Error |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
176 |
|
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
177 |
import Data.ByteString (ByteString) |
74084 | 178 |
|
179 |
import qualified Isabelle.Bytes as Bytes |
|
180 |
import Isabelle.Bytes (Bytes) |
|
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
181 |
|
74098 | 182 |
setup :: IO.Handle -> IO () |
183 |
setup h = do |
|
184 |
IO.hSetEncoding h IO.utf8 |
|
185 |
IO.hSetNewlineMode h IO.noNewlineTranslation |
|
186 |
||
187 |
setup3 :: IO.Handle -> IO.Handle -> IO.Handle -> IO () |
|
188 |
setup3 h1 h2 h3 = do |
|
189 |
setup h1 |
|
190 |
setup h2 |
|
191 |
setup h3 |
|
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
192 |
|
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
193 |
class Recode a b where |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
194 |
encode :: a -> b |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
195 |
decode :: b -> a |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
196 |
|
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
197 |
instance Recode Text ByteString where |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
198 |
encode :: Text -> ByteString |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
199 |
encode = Encoding.encodeUtf8 |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
200 |
decode :: ByteString -> Text |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
201 |
decode = Encoding.decodeUtf8With Error.lenientDecode |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
202 |
|
74084 | 203 |
instance Recode Text Bytes where |
204 |
encode :: Text -> Bytes |
|
205 |
encode = Bytes.make . encode |
|
206 |
decode :: Bytes -> Text |
|
207 |
decode = decode . Bytes.unmake |
|
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
208 |
|
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
209 |
instance Recode String ByteString where |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
210 |
encode :: String -> ByteString |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
211 |
encode = encode . Text.pack |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
212 |
decode :: ByteString -> String |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
213 |
decode = Text.unpack . decode |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
214 |
|
74084 | 215 |
instance Recode String Bytes where |
216 |
encode :: String -> Bytes |
|
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
217 |
encode = encode . Text.pack |
74084 | 218 |
decode :: Bytes -> String |
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
219 |
decode = Text.unpack . decode |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
220 |
\<close> |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
221 |
|
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
222 |
generate_file "Isabelle/Library.hs" = \<open> |
69445 | 223 |
{- Title: Isabelle/Library.hs |
69225 | 224 |
Author: Makarius |
225 |
LICENSE: BSD 3-clause (Isabelle) |
|
226 |
||
227 |
Basic library of Isabelle idioms. |
|
69280 | 228 |
|
229 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/General/basics.ML\<close>, \<^file>\<open>$ISABELLE_HOME/src/Pure/library.ML\<close>. |
|
69225 | 230 |
-} |
231 |
||
74086 | 232 |
{-# LANGUAGE OverloadedStrings #-} |
74093 | 233 |
{-# LANGUAGE TypeSynonymInstances #-} |
234 |
{-# LANGUAGE FlexibleInstances #-} |
|
74086 | 235 |
|
69240 | 236 |
module Isabelle.Library ( |
237 |
(|>), (|->), (#>), (#->), |
|
238 |
||
239 |
the, the_default, |
|
240 |
||
74087 | 241 |
fold, fold_rev, single, map_index, get_index, separate, |
69240 | 242 |
|
74093 | 243 |
StringLike, STRING (..), TEXT (..), BYTES (..), |
244 |
||
69477 | 245 |
proper_string, quote, space_implode, commas, commas_quote, cat_lines, |
74099 | 246 |
space_explode, split_lines, trim_line) |
69225 | 247 |
where |
248 |
||
74093 | 249 |
import qualified Data.Text as Text |
250 |
import Data.Text (Text) |
|
74096 | 251 |
import qualified Data.Text.Lazy as Lazy |
74086 | 252 |
import Data.String (IsString) |
69453 | 253 |
import qualified Data.List.Split as Split |
69491 | 254 |
import qualified Isabelle.Symbol as Symbol |
74093 | 255 |
import Isabelle.Bytes (Bytes) |
256 |
import qualified Isabelle.UTF8 as UTF8 |
|
69234 | 257 |
|
258 |
||
69225 | 259 |
{- functions -} |
260 |
||
261 |
(|>) :: a -> (a -> b) -> b |
|
262 |
x |> f = f x |
|
263 |
||
264 |
(|->) :: (a, b) -> (a -> b -> c) -> c |
|
265 |
(x, y) |-> f = f x y |
|
266 |
||
267 |
(#>) :: (a -> b) -> (b -> c) -> a -> c |
|
268 |
(f #> g) x = x |> f |> g |
|
269 |
||
270 |
(#->) :: (a -> (c, b)) -> (c -> b -> d) -> a -> d |
|
271 |
(f #-> g) x = x |> f |-> g |
|
272 |
||
273 |
||
69234 | 274 |
{- options -} |
275 |
||
69240 | 276 |
the :: Maybe a -> a |
277 |
the (Just x) = x |
|
278 |
the Nothing = error "the Nothing" |
|
279 |
||
69234 | 280 |
the_default :: a -> Maybe a -> a |
281 |
the_default x Nothing = x |
|
282 |
the_default _ (Just y) = y |
|
283 |
||
284 |
||
69225 | 285 |
{- lists -} |
286 |
||
287 |
fold :: (a -> b -> b) -> [a] -> b -> b |
|
288 |
fold _ [] y = y |
|
289 |
fold f (x : xs) y = fold f xs (f x y) |
|
290 |
||
291 |
fold_rev :: (a -> b -> b) -> [a] -> b -> b |
|
292 |
fold_rev _ [] y = y |
|
293 |
fold_rev f (x : xs) y = f x (fold_rev f xs y) |
|
294 |
||
295 |
single :: a -> [a] |
|
296 |
single x = [x] |
|
297 |
||
69240 | 298 |
map_index :: ((Int, a) -> b) -> [a] -> [b] |
299 |
map_index f = map_aux 0 |
|
300 |
where |
|
301 |
map_aux _ [] = [] |
|
302 |
map_aux i (x : xs) = f (i, x) : map_aux (i + 1) xs |
|
303 |
||
304 |
get_index :: (a -> Maybe b) -> [a] -> Maybe (Int, b) |
|
305 |
get_index f = get_aux 0 |
|
306 |
where |
|
307 |
get_aux _ [] = Nothing |
|
308 |
get_aux i (x : xs) = |
|
309 |
case f x of |
|
310 |
Nothing -> get_aux (i + 1) xs |
|
311 |
Just y -> Just (i, y) |
|
312 |
||
74086 | 313 |
separate :: a -> [a] -> [a] |
74093 | 314 |
separate s (x : (xs @ (_ : _))) = x : s : separate s xs |
74086 | 315 |
separate _ xs = xs; |
316 |
||
69225 | 317 |
|
74093 | 318 |
{- string-like interfaces -} |
319 |
||
320 |
class (IsString a, Monoid a, Eq a, Ord a) => StringLike a |
|
321 |
instance StringLike String |
|
322 |
instance StringLike Text |
|
74096 | 323 |
instance StringLike Lazy.Text |
74093 | 324 |
instance StringLike Bytes |
325 |
||
326 |
class StringLike a => STRING a where make_string :: a -> String |
|
327 |
instance STRING String where make_string = id |
|
328 |
instance STRING Text where make_string = Text.unpack |
|
74096 | 329 |
instance STRING Lazy.Text where make_string = Lazy.unpack |
74093 | 330 |
instance STRING Bytes where make_string = UTF8.decode |
331 |
||
332 |
class StringLike a => TEXT a where make_text :: a -> Text |
|
333 |
instance TEXT String where make_text = Text.pack |
|
334 |
instance TEXT Text where make_text = id |
|
74096 | 335 |
instance TEXT Lazy.Text where make_text = Lazy.toStrict |
74093 | 336 |
instance TEXT Bytes where make_text = UTF8.decode |
337 |
||
338 |
class StringLike a => BYTES a where make_bytes :: a -> Bytes |
|
339 |
instance BYTES String where make_bytes = UTF8.encode |
|
340 |
instance BYTES Text where make_bytes = UTF8.encode |
|
74096 | 341 |
instance BYTES Lazy.Text where make_bytes = UTF8.encode . Lazy.toStrict |
74093 | 342 |
instance BYTES Bytes where make_bytes = id |
343 |
||
344 |
||
69225 | 345 |
{- strings -} |
346 |
||
74093 | 347 |
proper_string :: StringLike a => a -> Maybe a |
74086 | 348 |
proper_string s = if s == "" then Nothing else Just s |
349 |
||
74093 | 350 |
quote :: StringLike a => a -> a |
74081 | 351 |
quote s = "\"" <> s <> "\"" |
69225 | 352 |
|
74093 | 353 |
space_implode :: StringLike a => a -> [a] -> a |
74086 | 354 |
space_implode s = mconcat . separate s |
355 |
||
74093 | 356 |
commas, commas_quote :: StringLike a => [a] -> a |
69453 | 357 |
commas = space_implode ", " |
358 |
commas_quote = commas . map quote |
|
359 |
||
74093 | 360 |
cat_lines :: StringLike a => [a] -> a |
69453 | 361 |
cat_lines = space_implode "\n" |
362 |
||
363 |
||
364 |
space_explode :: Char -> String -> [String] |
|
365 |
space_explode c = Split.split (Split.dropDelims (Split.whenElt (== c))) |
|
366 |
||
367 |
split_lines :: String -> [String] |
|
368 |
split_lines = space_explode '\n' |
|
369 |
||
69225 | 370 |
trim_line :: String -> String |
371 |
trim_line line = |
|
69491 | 372 |
if not (null line) && Symbol.is_ascii_line_terminator (last line) then |
69486 | 373 |
case reverse line of |
374 |
'\n' : '\r' : rest -> reverse rest |
|
375 |
'\r' : rest -> reverse rest |
|
376 |
'\n' : rest -> reverse rest |
|
377 |
_ -> line |
|
378 |
else line |
|
69225 | 379 |
\<close> |
380 |
||
74091 | 381 |
|
382 |
generate_file "Isabelle/Symbol.hs" = \<open> |
|
383 |
{- Title: Isabelle/Symbols.hs |
|
384 |
Author: Makarius |
|
385 |
LICENSE: BSD 3-clause (Isabelle) |
|
386 |
||
387 |
Isabelle text symbols. |
|
388 |
-} |
|
389 |
||
390 |
module Isabelle.Symbol where |
|
391 |
||
392 |
{- ASCII characters -} |
|
393 |
||
394 |
is_ascii_letter :: Char -> Bool |
|
395 |
is_ascii_letter c = 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' |
|
396 |
||
397 |
is_ascii_digit :: Char -> Bool |
|
398 |
is_ascii_digit c = '0' <= c && c <= '9' |
|
399 |
||
400 |
is_ascii_hex :: Char -> Bool |
|
401 |
is_ascii_hex c = '0' <= c && c <= '9' || 'A' <= c && c <= 'F' || 'a' <= c && c <= 'f' |
|
402 |
||
403 |
is_ascii_quasi :: Char -> Bool |
|
404 |
is_ascii_quasi c = c == '_' || c == '\'' |
|
405 |
||
406 |
is_ascii_blank :: Char -> Bool |
|
407 |
is_ascii_blank c = c `elem` " \t\n\11\f\r" |
|
408 |
||
409 |
is_ascii_line_terminator :: Char -> Bool |
|
410 |
is_ascii_line_terminator c = c == '\r' || c == '\n' |
|
411 |
||
412 |
is_ascii_letdig :: Char -> Bool |
|
413 |
is_ascii_letdig c = is_ascii_letter c || is_ascii_digit c || is_ascii_quasi c |
|
414 |
||
415 |
is_ascii_identifier :: String -> Bool |
|
416 |
is_ascii_identifier s = |
|
417 |
not (null s) && is_ascii_letter (head s) && all is_ascii_letdig s |
|
418 |
\<close> |
|
419 |
||
74095 | 420 |
generate_file "Isabelle/Buffer.hs" = \<open> |
421 |
{- Title: Isabelle/Buffer.hs |
|
422 |
Author: Makarius |
|
423 |
LICENSE: BSD 3-clause (Isabelle) |
|
424 |
||
425 |
Efficient buffer of byte strings. |
|
426 |
||
427 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/General/buffer.ML\<close>. |
|
428 |
-} |
|
429 |
||
430 |
module Isabelle.Buffer (T, empty, add, content) |
|
431 |
where |
|
432 |
||
433 |
import qualified Isabelle.Bytes as Bytes |
|
434 |
import Isabelle.Bytes (Bytes) |
|
435 |
||
436 |
||
437 |
newtype T = Buffer [Bytes] |
|
438 |
||
439 |
empty :: T |
|
440 |
empty = Buffer [] |
|
441 |
||
442 |
add :: Bytes -> T -> T |
|
443 |
add b (Buffer bs) = Buffer (if Bytes.null b then bs else b : bs) |
|
444 |
||
445 |
content :: T -> Bytes |
|
446 |
content (Buffer bs) = Bytes.concat (reverse bs) |
|
447 |
\<close> |
|
448 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
449 |
generate_file "Isabelle/Value.hs" = \<open> |
73246 | 450 |
{- Title: Isabelle/Value.hs |
69233 | 451 |
Author: Makarius |
452 |
LICENSE: BSD 3-clause (Isabelle) |
|
453 |
||
454 |
Plain values, represented as string. |
|
69280 | 455 |
|
456 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/General/value.ML\<close>. |
|
69233 | 457 |
-} |
458 |
||
74095 | 459 |
{-# LANGUAGE OverloadedStrings #-} |
460 |
||
69233 | 461 |
module Isabelle.Value |
69452 | 462 |
(print_bool, parse_bool, parse_nat, print_int, parse_int, print_real, parse_real) |
69233 | 463 |
where |
464 |
||
465 |
import qualified Data.List as List |
|
466 |
import qualified Text.Read as Read |
|
74095 | 467 |
import Isabelle.Bytes (Bytes) |
468 |
import Isabelle.Library |
|
69233 | 469 |
|
470 |
||
471 |
{- bool -} |
|
472 |
||
74095 | 473 |
print_bool :: Bool -> Bytes |
69233 | 474 |
print_bool True = "true" |
475 |
print_bool False = "false" |
|
476 |
||
74095 | 477 |
parse_bool :: Bytes -> Maybe Bool |
69233 | 478 |
parse_bool "true" = Just True |
479 |
parse_bool "false" = Just False |
|
480 |
parse_bool _ = Nothing |
|
481 |
||
482 |
||
69452 | 483 |
{- nat -} |
484 |
||
74095 | 485 |
parse_nat :: Bytes -> Maybe Int |
69452 | 486 |
parse_nat s = |
74095 | 487 |
case Read.readMaybe (make_string s) of |
69452 | 488 |
Just n | n >= 0 -> Just n |
489 |
_ -> Nothing |
|
490 |
||
491 |
||
69233 | 492 |
{- int -} |
493 |
||
74095 | 494 |
print_int :: Int -> Bytes |
495 |
print_int = make_bytes . show |
|
496 |
||
497 |
parse_int :: Bytes -> Maybe Int |
|
498 |
parse_int = Read.readMaybe . make_string |
|
69233 | 499 |
|
500 |
||
501 |
{- real -} |
|
502 |
||
74095 | 503 |
print_real :: Double -> Bytes |
69233 | 504 |
print_real x = |
505 |
let s = show x in |
|
506 |
case span (/= '.') s of |
|
74095 | 507 |
(a, '.' : b) | List.all (== '0') b -> make_bytes a |
508 |
_ -> make_bytes s |
|
509 |
||
510 |
parse_real :: Bytes -> Maybe Double |
|
511 |
parse_real = Read.readMaybe . make_string |
|
69225 | 512 |
\<close> |
513 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
514 |
generate_file "Isabelle/Properties.hs" = \<open> |
69445 | 515 |
{- Title: Isabelle/Properties.hs |
69225 | 516 |
Author: Makarius |
517 |
LICENSE: BSD 3-clause (Isabelle) |
|
518 |
||
519 |
Property lists. |
|
69280 | 520 |
|
521 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/General/properties.ML\<close>. |
|
69225 | 522 |
-} |
523 |
||
69477 | 524 |
module Isabelle.Properties (Entry, T, defined, get, get_value, put, remove) |
69225 | 525 |
where |
526 |
||
527 |
import qualified Data.List as List |
|
74095 | 528 |
import Isabelle.Bytes (Bytes) |
529 |
||
530 |
||
531 |
type Entry = (Bytes, Bytes) |
|
69225 | 532 |
type T = [Entry] |
533 |
||
74095 | 534 |
defined :: T -> Bytes -> Bool |
69225 | 535 |
defined props name = any (\(a, _) -> a == name) props |
536 |
||
74095 | 537 |
get :: T -> Bytes -> Maybe Bytes |
69225 | 538 |
get props name = List.lookup name props |
539 |
||
74095 | 540 |
get_value :: (Bytes -> Maybe a) -> T -> Bytes -> Maybe a |
69477 | 541 |
get_value parse props name = |
542 |
case get props name of |
|
543 |
Nothing -> Nothing |
|
544 |
Just s -> parse s |
|
545 |
||
69225 | 546 |
put :: Entry -> T -> T |
547 |
put entry props = entry : remove (fst entry) props |
|
548 |
||
74095 | 549 |
remove :: Bytes -> T -> T |
69225 | 550 |
remove name props = |
551 |
if defined props name then filter (\(a, _) -> a /= name) props |
|
552 |
else props |
|
553 |
\<close> |
|
554 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
555 |
generate_file "Isabelle/Markup.hs" = \<open> |
73246 | 556 |
{- Title: Isabelle/Markup.hs |
69225 | 557 |
Author: Makarius |
558 |
LICENSE: BSD 3-clause (Isabelle) |
|
559 |
||
560 |
Quasi-abstract markup elements. |
|
69280 | 561 |
|
562 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/markup.ML\<close>. |
|
69225 | 563 |
-} |
564 |
||
74095 | 565 |
{-# LANGUAGE OverloadedStrings #-} |
73177 | 566 |
{-# OPTIONS_GHC -fno-warn-missing-signatures #-} |
567 |
||
69234 | 568 |
module Isabelle.Markup ( |
569 |
T, empty, is_empty, properties, |
|
570 |
||
571 |
nameN, name, xnameN, xname, kindN, |
|
572 |
||
69315 | 573 |
bindingN, binding, entityN, entity, defN, refN, |
574 |
||
69288 | 575 |
completionN, completion, no_completionN, no_completion, |
576 |
||
69234 | 577 |
lineN, end_lineN, offsetN, end_offsetN, fileN, idN, positionN, position, |
578 |
||
69291 | 579 |
expressionN, expression, |
580 |
||
581 |
citationN, citation, |
|
582 |
||
71489 | 583 |
pathN, path, urlN, url, docN, doc, |
69291 | 584 |
|
69248 | 585 |
markupN, consistentN, unbreakableN, indentN, widthN, |
586 |
blockN, block, breakN, break, fbreakN, fbreak, itemN, item, |
|
587 |
||
69968
1a400b14fd3a
clarified spell-checking (see also 30233285270a);
wenzelm
parents:
69794
diff
changeset
|
588 |
wordsN, words, |
69234 | 589 |
|
590 |
tfreeN, tfree, tvarN, tvar, freeN, free, skolemN, skolem, boundN, bound, varN, var, |
|
591 |
numeralN, numeral, literalN, literal, delimiterN, delimiter, inner_stringN, inner_string, |
|
69320 | 592 |
inner_cartoucheN, inner_cartouche, |
69234 | 593 |
token_rangeN, token_range, |
594 |
sortingN, sorting, typingN, typing, class_parameterN, class_parameter, |
|
595 |
||
596 |
antiquotedN, antiquoted, antiquoteN, antiquote, |
|
597 |
||
598 |
paragraphN, paragraph, text_foldN, text_fold, |
|
599 |
||
600 |
keyword1N, keyword1, keyword2N, keyword2, keyword3N, keyword3, quasi_keywordN, quasi_keyword, |
|
601 |
improperN, improper, operatorN, operator, stringN, string, alt_stringN, alt_string, |
|
69320 | 602 |
verbatimN, verbatim, cartoucheN, cartouche, commentN, comment, comment1N, comment1, |
603 |
comment2N, comment2, comment3N, comment3, |
|
69234 | 604 |
|
70667 | 605 |
forkedN, forked, joinedN, joined, runningN, running, finishedN, finished, |
69794 | 606 |
failedN, failed, canceledN, canceled, initializedN, initialized, finalizedN, finalized, |
607 |
consolidatedN, consolidated, |
|
608 |
||
69234 | 609 |
writelnN, writeln, stateN, state, informationN, information, tracingN, tracing, |
610 |
warningN, warning, legacyN, legacy, errorN, error, reportN, report, no_reportN, no_report, |
|
611 |
||
612 |
intensifyN, intensify, |
|
613 |
Output, no_output) |
|
69225 | 614 |
where |
615 |
||
69248 | 616 |
import Prelude hiding (words, error, break) |
69234 | 617 |
|
618 |
import Isabelle.Library |
|
69225 | 619 |
import qualified Isabelle.Properties as Properties |
69248 | 620 |
import qualified Isabelle.Value as Value |
74095 | 621 |
import qualified Isabelle.Bytes as Bytes |
622 |
import Isabelle.Bytes (Bytes) |
|
69225 | 623 |
|
624 |
||
69234 | 625 |
{- basic markup -} |
626 |
||
74095 | 627 |
type T = (Bytes, Properties.T) |
69225 | 628 |
|
629 |
empty :: T |
|
630 |
empty = ("", []) |
|
631 |
||
632 |
is_empty :: T -> Bool |
|
633 |
is_empty ("", _) = True |
|
634 |
is_empty _ = False |
|
635 |
||
69234 | 636 |
properties :: Properties.T -> T -> T |
637 |
properties more_props (elem, props) = |
|
638 |
(elem, fold_rev Properties.put more_props props) |
|
639 |
||
74095 | 640 |
markup_elem :: Bytes -> T |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
641 |
markup_elem name = (name, []) |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
642 |
|
74095 | 643 |
markup_string :: Bytes -> Bytes -> Bytes -> T |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
644 |
markup_string name prop = \s -> (name, [(prop, s)]) |
69234 | 645 |
|
646 |
||
647 |
{- misc properties -} |
|
648 |
||
74095 | 649 |
nameN :: Bytes |
69234 | 650 |
nameN = \<open>Markup.nameN\<close> |
651 |
||
74095 | 652 |
name :: Bytes -> T -> T |
69234 | 653 |
name a = properties [(nameN, a)] |
654 |
||
74095 | 655 |
xnameN :: Bytes |
69234 | 656 |
xnameN = \<open>Markup.xnameN\<close> |
657 |
||
74095 | 658 |
xname :: Bytes -> T -> T |
69234 | 659 |
xname a = properties [(xnameN, a)] |
660 |
||
74095 | 661 |
kindN :: Bytes |
69234 | 662 |
kindN = \<open>Markup.kindN\<close> |
663 |
||
664 |
||
69315 | 665 |
{- formal entities -} |
666 |
||
74095 | 667 |
bindingN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
668 |
bindingN = \<open>Markup.bindingN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
669 |
binding :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
670 |
binding = markup_elem bindingN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
671 |
|
74095 | 672 |
entityN :: Bytes |
69315 | 673 |
entityN = \<open>Markup.entityN\<close> |
74095 | 674 |
entity :: Bytes -> Bytes -> T |
69315 | 675 |
entity kind name = |
676 |
(entityN, |
|
74095 | 677 |
(if Bytes.null name then [] else [(nameN, name)]) <> |
678 |
(if Bytes.null kind then [] else [(kindN, kind)])) |
|
679 |
||
680 |
defN :: Bytes |
|
69315 | 681 |
defN = \<open>Markup.defN\<close> |
682 |
||
74095 | 683 |
refN :: Bytes |
69315 | 684 |
refN = \<open>Markup.refN\<close> |
685 |
||
686 |
||
69288 | 687 |
{- completion -} |
688 |
||
74095 | 689 |
completionN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
690 |
completionN = \<open>Markup.completionN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
691 |
completion :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
692 |
completion = markup_elem completionN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
693 |
|
74095 | 694 |
no_completionN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
695 |
no_completionN = \<open>Markup.no_completionN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
696 |
no_completion :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
697 |
no_completion = markup_elem no_completionN |
69288 | 698 |
|
699 |
||
69234 | 700 |
{- position -} |
701 |
||
74095 | 702 |
lineN, end_lineN :: Bytes |
69234 | 703 |
lineN = \<open>Markup.lineN\<close> |
704 |
end_lineN = \<open>Markup.end_lineN\<close> |
|
705 |
||
74095 | 706 |
offsetN, end_offsetN :: Bytes |
69234 | 707 |
offsetN = \<open>Markup.offsetN\<close> |
708 |
end_offsetN = \<open>Markup.end_offsetN\<close> |
|
709 |
||
74095 | 710 |
fileN, idN :: Bytes |
69234 | 711 |
fileN = \<open>Markup.fileN\<close> |
712 |
idN = \<open>Markup.idN\<close> |
|
713 |
||
74095 | 714 |
positionN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
715 |
positionN = \<open>Markup.positionN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
716 |
position :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
717 |
position = markup_elem positionN |
69234 | 718 |
|
719 |
||
69291 | 720 |
{- expression -} |
721 |
||
74095 | 722 |
expressionN :: Bytes |
69291 | 723 |
expressionN = \<open>Markup.expressionN\<close> |
724 |
||
74095 | 725 |
expression :: Bytes -> T |
69291 | 726 |
expression kind = (expressionN, if kind == "" then [] else [(kindN, kind)]) |
727 |
||
728 |
||
729 |
{- citation -} |
|
730 |
||
74095 | 731 |
citationN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
732 |
citationN = \<open>Markup.citationN\<close> |
74095 | 733 |
citation :: Bytes -> T |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
734 |
citation = markup_string nameN citationN |
69291 | 735 |
|
736 |
||
737 |
{- external resources -} |
|
738 |
||
74095 | 739 |
pathN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
740 |
pathN = \<open>Markup.pathN\<close> |
74095 | 741 |
path :: Bytes -> T |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
742 |
path = markup_string pathN nameN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
743 |
|
74095 | 744 |
urlN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
745 |
urlN = \<open>Markup.urlN\<close> |
74095 | 746 |
url :: Bytes -> T |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
747 |
url = markup_string urlN nameN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
748 |
|
74095 | 749 |
docN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
750 |
docN = \<open>Markup.docN\<close> |
74095 | 751 |
doc :: Bytes -> T |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
752 |
doc = markup_string docN nameN |
69291 | 753 |
|
754 |
||
69248 | 755 |
{- pretty printing -} |
756 |
||
74095 | 757 |
markupN, consistentN, unbreakableN, indentN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
758 |
markupN = \<open>Markup.markupN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
759 |
consistentN = \<open>Markup.consistentN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
760 |
unbreakableN = \<open>Markup.unbreakableN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
761 |
indentN = \<open>Markup.indentN\<close> |
69248 | 762 |
|
74095 | 763 |
widthN :: Bytes |
69248 | 764 |
widthN = \<open>Markup.widthN\<close> |
765 |
||
74095 | 766 |
blockN :: Bytes |
69248 | 767 |
blockN = \<open>Markup.blockN\<close> |
768 |
block :: Bool -> Int -> T |
|
769 |
block c i = |
|
770 |
(blockN, |
|
74081 | 771 |
(if c then [(consistentN, Value.print_bool c)] else []) <> |
69248 | 772 |
(if i /= 0 then [(indentN, Value.print_int i)] else [])) |
773 |
||
74095 | 774 |
breakN :: Bytes |
69248 | 775 |
breakN = \<open>Markup.breakN\<close> |
776 |
break :: Int -> Int -> T |
|
777 |
break w i = |
|
778 |
(breakN, |
|
74081 | 779 |
(if w /= 0 then [(widthN, Value.print_int w)] else []) <> |
69248 | 780 |
(if i /= 0 then [(indentN, Value.print_int i)] else [])) |
781 |
||
74095 | 782 |
fbreakN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
783 |
fbreakN = \<open>Markup.fbreakN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
784 |
fbreak :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
785 |
fbreak = markup_elem fbreakN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
786 |
|
74095 | 787 |
itemN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
788 |
itemN = \<open>Markup.itemN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
789 |
item :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
790 |
item = markup_elem itemN |
69248 | 791 |
|
792 |
||
69234 | 793 |
{- text properties -} |
794 |
||
74095 | 795 |
wordsN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
796 |
wordsN = \<open>Markup.wordsN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
797 |
words :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
798 |
words = markup_elem wordsN |
69234 | 799 |
|
800 |
||
801 |
{- inner syntax -} |
|
802 |
||
74095 | 803 |
tfreeN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
804 |
tfreeN = \<open>Markup.tfreeN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
805 |
tfree :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
806 |
tfree = markup_elem tfreeN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
807 |
|
74095 | 808 |
tvarN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
809 |
tvarN = \<open>Markup.tvarN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
810 |
tvar :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
811 |
tvar = markup_elem tvarN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
812 |
|
74095 | 813 |
freeN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
814 |
freeN = \<open>Markup.freeN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
815 |
free :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
816 |
free = markup_elem freeN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
817 |
|
74095 | 818 |
skolemN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
819 |
skolemN = \<open>Markup.skolemN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
820 |
skolem :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
821 |
skolem = markup_elem skolemN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
822 |
|
74095 | 823 |
boundN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
824 |
boundN = \<open>Markup.boundN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
825 |
bound :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
826 |
bound = markup_elem boundN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
827 |
|
74095 | 828 |
varN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
829 |
varN = \<open>Markup.varN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
830 |
var :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
831 |
var = markup_elem varN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
832 |
|
74095 | 833 |
numeralN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
834 |
numeralN = \<open>Markup.numeralN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
835 |
numeral :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
836 |
numeral = markup_elem numeralN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
837 |
|
74095 | 838 |
literalN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
839 |
literalN = \<open>Markup.literalN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
840 |
literal :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
841 |
literal = markup_elem literalN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
842 |
|
74095 | 843 |
delimiterN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
844 |
delimiterN = \<open>Markup.delimiterN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
845 |
delimiter :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
846 |
delimiter = markup_elem delimiterN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
847 |
|
74095 | 848 |
inner_stringN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
849 |
inner_stringN = \<open>Markup.inner_stringN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
850 |
inner_string :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
851 |
inner_string = markup_elem inner_stringN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
852 |
|
74095 | 853 |
inner_cartoucheN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
854 |
inner_cartoucheN = \<open>Markup.inner_cartoucheN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
855 |
inner_cartouche :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
856 |
inner_cartouche = markup_elem inner_cartoucheN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
857 |
|
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
858 |
|
74095 | 859 |
token_rangeN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
860 |
token_rangeN = \<open>Markup.token_rangeN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
861 |
token_range :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
862 |
token_range = markup_elem token_rangeN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
863 |
|
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
864 |
|
74095 | 865 |
sortingN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
866 |
sortingN = \<open>Markup.sortingN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
867 |
sorting :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
868 |
sorting = markup_elem sortingN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
869 |
|
74095 | 870 |
typingN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
871 |
typingN = \<open>Markup.typingN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
872 |
typing :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
873 |
typing = markup_elem typingN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
874 |
|
74095 | 875 |
class_parameterN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
876 |
class_parameterN = \<open>Markup.class_parameterN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
877 |
class_parameter :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
878 |
class_parameter = markup_elem class_parameterN |
69234 | 879 |
|
880 |
||
881 |
{- antiquotations -} |
|
882 |
||
74095 | 883 |
antiquotedN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
884 |
antiquotedN = \<open>Markup.antiquotedN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
885 |
antiquoted :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
886 |
antiquoted = markup_elem antiquotedN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
887 |
|
74095 | 888 |
antiquoteN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
889 |
antiquoteN = \<open>Markup.antiquoteN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
890 |
antiquote :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
891 |
antiquote = markup_elem antiquoteN |
69234 | 892 |
|
893 |
||
894 |
{- text structure -} |
|
895 |
||
74095 | 896 |
paragraphN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
897 |
paragraphN = \<open>Markup.paragraphN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
898 |
paragraph :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
899 |
paragraph = markup_elem paragraphN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
900 |
|
74095 | 901 |
text_foldN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
902 |
text_foldN = \<open>Markup.text_foldN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
903 |
text_fold :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
904 |
text_fold = markup_elem text_foldN |
69234 | 905 |
|
906 |
||
907 |
{- outer syntax -} |
|
908 |
||
74095 | 909 |
keyword1N :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
910 |
keyword1N = \<open>Markup.keyword1N\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
911 |
keyword1 :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
912 |
keyword1 = markup_elem keyword1N |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
913 |
|
74095 | 914 |
keyword2N :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
915 |
keyword2N = \<open>Markup.keyword2N\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
916 |
keyword2 :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
917 |
keyword2 = markup_elem keyword2N |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
918 |
|
74095 | 919 |
keyword3N :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
920 |
keyword3N = \<open>Markup.keyword3N\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
921 |
keyword3 :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
922 |
keyword3 = markup_elem keyword3N |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
923 |
|
74095 | 924 |
quasi_keywordN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
925 |
quasi_keywordN = \<open>Markup.quasi_keywordN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
926 |
quasi_keyword :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
927 |
quasi_keyword = markup_elem quasi_keywordN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
928 |
|
74095 | 929 |
improperN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
930 |
improperN = \<open>Markup.improperN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
931 |
improper :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
932 |
improper = markup_elem improperN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
933 |
|
74095 | 934 |
operatorN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
935 |
operatorN = \<open>Markup.operatorN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
936 |
operator :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
937 |
operator = markup_elem operatorN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
938 |
|
74095 | 939 |
stringN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
940 |
stringN = \<open>Markup.stringN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
941 |
string :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
942 |
string = markup_elem stringN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
943 |
|
74095 | 944 |
alt_stringN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
945 |
alt_stringN = \<open>Markup.alt_stringN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
946 |
alt_string :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
947 |
alt_string = markup_elem alt_stringN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
948 |
|
74095 | 949 |
verbatimN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
950 |
verbatimN = \<open>Markup.verbatimN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
951 |
verbatim :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
952 |
verbatim = markup_elem verbatimN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
953 |
|
74095 | 954 |
cartoucheN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
955 |
cartoucheN = \<open>Markup.cartoucheN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
956 |
cartouche :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
957 |
cartouche = markup_elem cartoucheN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
958 |
|
74095 | 959 |
commentN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
960 |
commentN = \<open>Markup.commentN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
961 |
comment :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
962 |
comment = markup_elem commentN |
69234 | 963 |
|
964 |
||
69320 | 965 |
{- comments -} |
966 |
||
74095 | 967 |
comment1N :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
968 |
comment1N = \<open>Markup.comment1N\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
969 |
comment1 :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
970 |
comment1 = markup_elem comment1N |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
971 |
|
74095 | 972 |
comment2N :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
973 |
comment2N = \<open>Markup.comment2N\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
974 |
comment2 :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
975 |
comment2 = markup_elem comment2N |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
976 |
|
74095 | 977 |
comment3N :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
978 |
comment3N = \<open>Markup.comment3N\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
979 |
comment3 :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
980 |
comment3 = markup_elem comment3N |
69320 | 981 |
|
982 |
||
69793 | 983 |
{- command status -} |
984 |
||
70667 | 985 |
forkedN, joinedN, runningN, finishedN, failedN, canceledN, |
74095 | 986 |
initializedN, finalizedN, consolidatedN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
987 |
forkedN = \<open>Markup.forkedN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
988 |
joinedN = \<open>Markup.joinedN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
989 |
runningN = \<open>Markup.runningN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
990 |
finishedN = \<open>Markup.finishedN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
991 |
failedN = \<open>Markup.failedN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
992 |
canceledN = \<open>Markup.canceledN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
993 |
initializedN = \<open>Markup.initializedN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
994 |
finalizedN = \<open>Markup.finalizedN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
995 |
consolidatedN = \<open>Markup.consolidatedN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
996 |
|
70667 | 997 |
forked, joined, running, finished, failed, canceled, |
69793 | 998 |
initialized, finalized, consolidated :: T |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
999 |
forked = markup_elem forkedN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1000 |
joined = markup_elem joinedN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1001 |
running = markup_elem runningN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1002 |
finished = markup_elem finishedN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1003 |
failed = markup_elem failedN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1004 |
canceled = markup_elem canceledN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1005 |
initialized = markup_elem initializedN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1006 |
finalized = markup_elem finalizedN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1007 |
consolidated = markup_elem consolidatedN |
69793 | 1008 |
|
1009 |
||
69234 | 1010 |
{- messages -} |
1011 |
||
74095 | 1012 |
writelnN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1013 |
writelnN = \<open>Markup.writelnN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1014 |
writeln :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1015 |
writeln = markup_elem writelnN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1016 |
|
74095 | 1017 |
stateN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1018 |
stateN = \<open>Markup.stateN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1019 |
state :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1020 |
state = markup_elem stateN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1021 |
|
74095 | 1022 |
informationN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1023 |
informationN = \<open>Markup.informationN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1024 |
information :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1025 |
information = markup_elem informationN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1026 |
|
74095 | 1027 |
tracingN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1028 |
tracingN = \<open>Markup.tracingN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1029 |
tracing :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1030 |
tracing = markup_elem tracingN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1031 |
|
74095 | 1032 |
warningN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1033 |
warningN = \<open>Markup.warningN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1034 |
warning :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1035 |
warning = markup_elem warningN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1036 |
|
74095 | 1037 |
legacyN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1038 |
legacyN = \<open>Markup.legacyN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1039 |
legacy :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1040 |
legacy = markup_elem legacyN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1041 |
|
74095 | 1042 |
errorN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1043 |
errorN = \<open>Markup.errorN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1044 |
error :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1045 |
error = markup_elem errorN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1046 |
|
74095 | 1047 |
reportN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1048 |
reportN = \<open>Markup.reportN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1049 |
report :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1050 |
report = markup_elem reportN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1051 |
|
74095 | 1052 |
no_reportN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1053 |
no_reportN = \<open>Markup.no_reportN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1054 |
no_report :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1055 |
no_report = markup_elem no_reportN |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1056 |
|
74095 | 1057 |
intensifyN :: Bytes |
73199
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1058 |
intensifyN = \<open>Markup.intensifyN\<close> |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1059 |
intensify :: T |
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1060 |
intensify = markup_elem intensifyN |
69234 | 1061 |
|
1062 |
||
1063 |
{- output -} |
|
69225 | 1064 |
|
74095 | 1065 |
type Output = (Bytes, Bytes) |
69225 | 1066 |
|
1067 |
no_output :: Output |
|
1068 |
no_output = ("", "") |
|
69222 | 1069 |
\<close> |
1070 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
1071 |
generate_file "Isabelle/XML.hs" = \<open> |
69445 | 1072 |
{- Title: Isabelle/XML.hs |
69225 | 1073 |
Author: Makarius |
1074 |
LICENSE: BSD 3-clause (Isabelle) |
|
1075 |
||
1076 |
Untyped XML trees and representation of ML values. |
|
69280 | 1077 |
|
1078 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/xml.ML\<close>. |
|
69225 | 1079 |
-} |
1080 |
||
74095 | 1081 |
{-# LANGUAGE OverloadedStrings #-} |
73177 | 1082 |
{-# OPTIONS_GHC -fno-warn-missing-signatures #-} |
1083 |
||
69225 | 1084 |
module Isabelle.XML (Attributes, Body, Tree(..), wrap_elem, unwrap_elem, content_of) |
1085 |
where |
|
1086 |
||
1087 |
import Isabelle.Library |
|
1088 |
import qualified Isabelle.Properties as Properties |
|
1089 |
import qualified Isabelle.Markup as Markup |
|
1090 |
import qualified Isabelle.Buffer as Buffer |
|
74095 | 1091 |
import qualified Isabelle.Bytes as Bytes |
1092 |
import Isabelle.Bytes (Bytes) |
|
69225 | 1093 |
|
1094 |
||
1095 |
{- types -} |
|
1096 |
||
1097 |
type Attributes = Properties.T |
|
1098 |
type Body = [Tree] |
|
74095 | 1099 |
data Tree = Elem (Markup.T, Body) | Text Bytes |
69225 | 1100 |
|
1101 |
||
1102 |
{- wrapped elements -} |
|
1103 |
||
69482 | 1104 |
wrap_elem :: ((Markup.T, Body), [Tree]) -> Tree |
69236 | 1105 |
wrap_elem (((a, atts), body1), body2) = |
69290 | 1106 |
Elem ((\<open>XML.xml_elemN\<close>, (\<open>XML.xml_nameN\<close>, a) : atts), Elem ((\<open>XML.xml_bodyN\<close>, []), body1) : body2) |
69225 | 1107 |
|
69482 | 1108 |
unwrap_elem :: Tree -> Maybe ((Markup.T, Body), [Tree]) |
69236 | 1109 |
unwrap_elem |
69290 | 1110 |
(Elem ((\<open>XML.xml_elemN\<close>, (\<open>XML.xml_nameN\<close>, a) : atts), Elem ((\<open>XML.xml_bodyN\<close>, []), body1) : body2)) = |
69236 | 1111 |
Just (((a, atts), body1), body2) |
69225 | 1112 |
unwrap_elem _ = Nothing |
1113 |
||
1114 |
||
1115 |
{- text content -} |
|
1116 |
||
69482 | 1117 |
add_content :: Tree -> Buffer.T -> Buffer.T |
69225 | 1118 |
add_content tree = |
1119 |
case unwrap_elem tree of |
|
1120 |
Just (_, ts) -> fold add_content ts |
|
1121 |
Nothing -> |
|
1122 |
case tree of |
|
69290 | 1123 |
Elem (_, ts) -> fold add_content ts |
69225 | 1124 |
Text s -> Buffer.add s |
1125 |
||
74095 | 1126 |
content_of :: Body -> Bytes |
69225 | 1127 |
content_of body = Buffer.empty |> fold add_content body |> Buffer.content |
1128 |
||
1129 |
||
1130 |
{- string representation -} |
|
1131 |
||
74095 | 1132 |
encode_char :: Char -> String |
1133 |
encode_char '<' = "<" |
|
1134 |
encode_char '>' = ">" |
|
1135 |
encode_char '&' = "&" |
|
1136 |
encode_char '\'' = "'" |
|
1137 |
encode_char '\"' = """ |
|
1138 |
encode_char c = [c] |
|
1139 |
||
1140 |
encode_text :: Bytes -> Bytes |
|
1141 |
encode_text = make_bytes . concatMap (encode_char . Bytes.char) . Bytes.unpack |
|
69225 | 1142 |
|
1143 |
instance Show Tree where |
|
1144 |
show tree = |
|
74095 | 1145 |
Buffer.empty |> show_tree tree |> Buffer.content |> make_string |
69225 | 1146 |
where |
69290 | 1147 |
show_tree (Elem ((name, atts), [])) = |
69225 | 1148 |
Buffer.add "<" #> Buffer.add (show_elem name atts) #> Buffer.add "/>" |
69290 | 1149 |
show_tree (Elem ((name, atts), ts)) = |
69225 | 1150 |
Buffer.add "<" #> Buffer.add (show_elem name atts) #> Buffer.add ">" #> |
1151 |
fold show_tree ts #> |
|
1152 |
Buffer.add "</" #> Buffer.add name #> Buffer.add ">" |
|
74095 | 1153 |
show_tree (Text s) = Buffer.add (encode_text s) |
69225 | 1154 |
|
1155 |
show_elem name atts = |
|
74095 | 1156 |
space_implode " " (name : map (\(a, x) -> a <> "=\"" <> encode_text x <> "\"") atts) |
69225 | 1157 |
\<close> |
1158 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
1159 |
generate_file "Isabelle/XML/Encode.hs" = \<open> |
69445 | 1160 |
{- Title: Isabelle/XML/Encode.hs |
69240 | 1161 |
Author: Makarius |
1162 |
LICENSE: BSD 3-clause (Isabelle) |
|
1163 |
||
1164 |
XML as data representation language. |
|
69280 | 1165 |
|
1166 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/xml.ML\<close>. |
|
69240 | 1167 |
-} |
1168 |
||
74095 | 1169 |
{-# LANGUAGE OverloadedStrings #-} |
73177 | 1170 |
{-# OPTIONS_GHC -fno-warn-missing-signatures #-} |
1171 |
||
69240 | 1172 |
module Isabelle.XML.Encode ( |
70845 | 1173 |
A, T, V, P, |
69240 | 1174 |
|
1175 |
int_atom, bool_atom, unit_atom, |
|
1176 |
||
71490 | 1177 |
tree, properties, string, int, bool, unit, pair, triple, list, option, variant |
69240 | 1178 |
) |
1179 |
where |
|
1180 |
||
1181 |
import Isabelle.Library |
|
74095 | 1182 |
import Isabelle.Bytes (Bytes) |
69240 | 1183 |
import qualified Isabelle.Value as Value |
1184 |
import qualified Isabelle.Properties as Properties |
|
1185 |
import qualified Isabelle.XML as XML |
|
1186 |
||
1187 |
||
74095 | 1188 |
type A a = a -> Bytes |
69240 | 1189 |
type T a = a -> XML.Body |
74095 | 1190 |
type V a = a -> Maybe ([Bytes], XML.Body) |
1191 |
type P a = a -> [Bytes] |
|
69240 | 1192 |
|
1193 |
||
1194 |
-- atomic values |
|
1195 |
||
1196 |
int_atom :: A Int |
|
1197 |
int_atom = Value.print_int |
|
1198 |
||
1199 |
bool_atom :: A Bool |
|
1200 |
bool_atom False = "0" |
|
1201 |
bool_atom True = "1" |
|
1202 |
||
1203 |
unit_atom :: A () |
|
1204 |
unit_atom () = "" |
|
1205 |
||
1206 |
||
1207 |
-- structural nodes |
|
1208 |
||
69290 | 1209 |
node ts = XML.Elem ((":", []), ts) |
69240 | 1210 |
|
1211 |
vector = map_index (\(i, x) -> (int_atom i, x)) |
|
1212 |
||
69290 | 1213 |
tagged (tag, (xs, ts)) = XML.Elem ((int_atom tag, vector xs), ts) |
69240 | 1214 |
|
1215 |
||
1216 |
-- representation of standard types |
|
1217 |
||
1218 |
tree :: T XML.Tree |
|
1219 |
tree t = [t] |
|
1220 |
||
1221 |
properties :: T Properties.T |
|
69290 | 1222 |
properties props = [XML.Elem ((":", props), [])] |
69240 | 1223 |
|
74095 | 1224 |
string :: T Bytes |
69240 | 1225 |
string "" = [] |
1226 |
string s = [XML.Text s] |
|
1227 |
||
1228 |
int :: T Int |
|
1229 |
int = string . int_atom |
|
1230 |
||
1231 |
bool :: T Bool |
|
1232 |
bool = string . bool_atom |
|
1233 |
||
1234 |
unit :: T () |
|
1235 |
unit = string . unit_atom |
|
1236 |
||
1237 |
pair :: T a -> T b -> T (a, b) |
|
1238 |
pair f g (x, y) = [node (f x), node (g y)] |
|
1239 |
||
1240 |
triple :: T a -> T b -> T c -> T (a, b, c) |
|
1241 |
triple f g h (x, y, z) = [node (f x), node (g y), node (h z)] |
|
1242 |
||
1243 |
list :: T a -> T [a] |
|
1244 |
list f xs = map (node . f) xs |
|
1245 |
||
71490 | 1246 |
option :: T a -> T (Maybe a) |
1247 |
option _ Nothing = [] |
|
1248 |
option f (Just x) = [node (f x)] |
|
1249 |
||
69240 | 1250 |
variant :: [V a] -> T a |
1251 |
variant fs x = [tagged (the (get_index (\f -> f x) fs))] |
|
1252 |
\<close> |
|
1253 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
1254 |
generate_file "Isabelle/XML/Decode.hs" = \<open> |
69445 | 1255 |
{- Title: Isabelle/XML/Decode.hs |
69240 | 1256 |
Author: Makarius |
1257 |
LICENSE: BSD 3-clause (Isabelle) |
|
1258 |
||
1259 |
XML as data representation language. |
|
69280 | 1260 |
|
1261 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/xml.ML\<close>. |
|
69240 | 1262 |
-} |
1263 |
||
74095 | 1264 |
{-# LANGUAGE OverloadedStrings #-} |
73177 | 1265 |
{-# OPTIONS_GHC -fno-warn-missing-signatures #-} |
1266 |
||
69240 | 1267 |
module Isabelle.XML.Decode ( |
70845 | 1268 |
A, T, V, P, |
69240 | 1269 |
|
1270 |
int_atom, bool_atom, unit_atom, |
|
1271 |
||
71490 | 1272 |
tree, properties, string, int, bool, unit, pair, triple, list, option, variant |
69240 | 1273 |
) |
1274 |
where |
|
1275 |
||
1276 |
import Isabelle.Library |
|
74095 | 1277 |
import Isabelle.Bytes (Bytes) |
69240 | 1278 |
import qualified Isabelle.Value as Value |
1279 |
import qualified Isabelle.Properties as Properties |
|
1280 |
import qualified Isabelle.XML as XML |
|
1281 |
||
1282 |
||
74095 | 1283 |
type A a = Bytes -> a |
69240 | 1284 |
type T a = XML.Body -> a |
74095 | 1285 |
type V a = ([Bytes], XML.Body) -> a |
1286 |
type P a = [Bytes] -> a |
|
69240 | 1287 |
|
1288 |
err_atom = error "Malformed XML atom" |
|
1289 |
err_body = error "Malformed XML body" |
|
1290 |
||
1291 |
||
1292 |
{- atomic values -} |
|
1293 |
||
1294 |
int_atom :: A Int |
|
1295 |
int_atom s = |
|
1296 |
case Value.parse_int s of |
|
1297 |
Just i -> i |
|
1298 |
Nothing -> err_atom |
|
1299 |
||
1300 |
bool_atom :: A Bool |
|
1301 |
bool_atom "0" = False |
|
1302 |
bool_atom "1" = True |
|
1303 |
bool_atom _ = err_atom |
|
1304 |
||
1305 |
unit_atom :: A () |
|
1306 |
unit_atom "" = () |
|
1307 |
unit_atom _ = err_atom |
|
1308 |
||
1309 |
||
1310 |
{- structural nodes -} |
|
1311 |
||
69290 | 1312 |
node (XML.Elem ((":", []), ts)) = ts |
69240 | 1313 |
node _ = err_body |
1314 |
||
1315 |
vector atts = |
|
1316 |
map_index (\(i, (a, x)) -> if int_atom a == i then x else err_atom) atts |
|
1317 |
||
69290 | 1318 |
tagged (XML.Elem ((name, atts), ts)) = (int_atom name, (vector atts, ts)) |
69240 | 1319 |
tagged _ = err_body |
1320 |
||
1321 |
||
1322 |
{- representation of standard types -} |
|
1323 |
||
1324 |
tree :: T XML.Tree |
|
1325 |
tree [t] = t |
|
1326 |
tree _ = err_body |
|
1327 |
||
1328 |
properties :: T Properties.T |
|
69290 | 1329 |
properties [XML.Elem ((":", props), [])] = props |
69240 | 1330 |
properties _ = err_body |
1331 |
||
74095 | 1332 |
string :: T Bytes |
69240 | 1333 |
string [] = "" |
1334 |
string [XML.Text s] = s |
|
1335 |
string _ = err_body |
|
1336 |
||
1337 |
int :: T Int |
|
1338 |
int = int_atom . string |
|
1339 |
||
1340 |
bool :: T Bool |
|
1341 |
bool = bool_atom . string |
|
1342 |
||
1343 |
unit :: T () |
|
1344 |
unit = unit_atom . string |
|
1345 |
||
1346 |
pair :: T a -> T b -> T (a, b) |
|
1347 |
pair f g [t1, t2] = (f (node t1), g (node t2)) |
|
1348 |
pair _ _ _ = err_body |
|
1349 |
||
1350 |
triple :: T a -> T b -> T c -> T (a, b, c) |
|
1351 |
triple f g h [t1, t2, t3] = (f (node t1), g (node t2), h (node t3)) |
|
1352 |
triple _ _ _ _ = err_body |
|
1353 |
||
1354 |
list :: T a -> T [a] |
|
1355 |
list f ts = map (f . node) ts |
|
1356 |
||
1357 |
option :: T a -> T (Maybe a) |
|
1358 |
option _ [] = Nothing |
|
1359 |
option f [t] = Just (f (node t)) |
|
1360 |
option _ _ = err_body |
|
1361 |
||
1362 |
variant :: [V a] -> T a |
|
1363 |
variant fs [t] = (fs !! tag) (xs, ts) |
|
1364 |
where (tag, (xs, ts)) = tagged t |
|
1365 |
variant _ _ = err_body |
|
1366 |
\<close> |
|
1367 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
1368 |
generate_file "Isabelle/YXML.hs" = \<open> |
69445 | 1369 |
{- Title: Isabelle/YXML.hs |
69288 | 1370 |
Author: Makarius |
1371 |
LICENSE: BSD 3-clause (Isabelle) |
|
1372 |
||
1373 |
Efficient text representation of XML trees. Suitable for direct |
|
1374 |
inlining into plain text. |
|
1375 |
||
1376 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/yxml.ML\<close>. |
|
1377 |
-} |
|
1378 |
||
74095 | 1379 |
{-# LANGUAGE OverloadedStrings #-} |
73177 | 1380 |
{-# OPTIONS_GHC -fno-warn-missing-signatures -fno-warn-incomplete-patterns #-} |
1381 |
||
69288 | 1382 |
module Isabelle.YXML (charX, charY, strX, strY, detect, output_markup, |
1383 |
buffer_body, buffer, string_of_body, string_of, parse_body, parse) |
|
1384 |
where |
|
1385 |
||
1386 |
import qualified Data.List as List |
|
74095 | 1387 |
import Data.Word (Word8) |
69288 | 1388 |
|
1389 |
import Isabelle.Library |
|
74095 | 1390 |
import qualified Isabelle.Bytes as Bytes |
1391 |
import Isabelle.Bytes (Bytes) |
|
69288 | 1392 |
import qualified Isabelle.Markup as Markup |
1393 |
import qualified Isabelle.XML as XML |
|
1394 |
import qualified Isabelle.Buffer as Buffer |
|
1395 |
||
1396 |
||
1397 |
{- markers -} |
|
1398 |
||
74095 | 1399 |
charX, charY :: Word8 |
1400 |
charX = 5 |
|
1401 |
charY = 6 |
|
1402 |
||
1403 |
strX, strY, strXY, strXYX :: Bytes |
|
1404 |
strX = Bytes.singleton charX |
|
1405 |
strY = Bytes.singleton charY |
|
74081 | 1406 |
strXY = strX <> strY |
1407 |
strXYX = strXY <> strX |
|
69288 | 1408 |
|
74095 | 1409 |
detect :: Bytes -> Bool |
1410 |
detect = Bytes.any (\c -> c == charX || c == charY) |
|
69288 | 1411 |
|
1412 |
||
1413 |
{- output -} |
|
1414 |
||
1415 |
output_markup :: Markup.T -> Markup.Output |
|
1416 |
output_markup markup@(name, atts) = |
|
1417 |
if Markup.is_empty markup then Markup.no_output |
|
74095 | 1418 |
else (strXY <> name <> Bytes.concat (map (\(a, x) -> strY <> a <> "=" <> x) atts) <> strX, strXYX) |
69288 | 1419 |
|
1420 |
buffer_attrib (a, x) = |
|
1421 |
Buffer.add strY #> Buffer.add a #> Buffer.add "=" #> Buffer.add x |
|
1422 |
||
1423 |
buffer_body :: XML.Body -> Buffer.T -> Buffer.T |
|
1424 |
buffer_body = fold buffer |
|
1425 |
||
1426 |
buffer :: XML.Tree -> Buffer.T -> Buffer.T |
|
69290 | 1427 |
buffer (XML.Elem ((name, atts), ts)) = |
69288 | 1428 |
Buffer.add strXY #> Buffer.add name #> fold buffer_attrib atts #> Buffer.add strX #> |
1429 |
buffer_body ts #> |
|
1430 |
Buffer.add strXYX |
|
1431 |
buffer (XML.Text s) = Buffer.add s |
|
1432 |
||
74095 | 1433 |
string_of_body :: XML.Body -> Bytes |
69288 | 1434 |
string_of_body body = Buffer.empty |> buffer_body body |> Buffer.content |
1435 |
||
74095 | 1436 |
string_of :: XML.Tree -> Bytes |
69288 | 1437 |
string_of = string_of_body . single |
1438 |
||
1439 |
||
1440 |
{- parse -} |
|
1441 |
||
1442 |
-- split: fields or non-empty tokens |
|
1443 |
||
74095 | 1444 |
split :: Bool -> Word8 -> [Word8] -> [[Word8]] |
69288 | 1445 |
split _ _ [] = [] |
1446 |
split fields sep str = splitting str |
|
1447 |
where |
|
1448 |
splitting rest = |
|
1449 |
case span (/= sep) rest of |
|
1450 |
(_, []) -> cons rest [] |
|
1451 |
(prfx, _ : rest') -> cons prfx (splitting rest') |
|
1452 |
cons item = if fields || not (null item) then (:) item else id |
|
1453 |
||
1454 |
||
1455 |
-- structural errors |
|
1456 |
||
74095 | 1457 |
err :: Bytes -> a |
1458 |
err msg = error (make_string ("Malformed YXML: " <> msg)) |
|
1459 |
||
69288 | 1460 |
err_attribute = err "bad attribute" |
1461 |
err_element = err "bad element" |
|
74095 | 1462 |
|
1463 |
err_unbalanced :: Bytes -> a |
|
1464 |
err_unbalanced name = |
|
1465 |
if Bytes.null name then err "unbalanced element" |
|
1466 |
else err ("unbalanced element " <> quote name) |
|
69288 | 1467 |
|
1468 |
||
1469 |
-- stack operations |
|
1470 |
||
1471 |
add x ((elem, body) : pending) = (elem, x : body) : pending |
|
1472 |
||
74095 | 1473 |
push name atts pending = |
1474 |
if Bytes.null name then err_element |
|
1475 |
else ((name, atts), []) : pending |
|
1476 |
||
1477 |
pop (((name, atts), body) : pending) = |
|
1478 |
if Bytes.null name then err_unbalanced name |
|
1479 |
else add (XML.Elem ((name, atts), reverse body)) pending |
|
69288 | 1480 |
|
1481 |
||
1482 |
-- parsing |
|
1483 |
||
1484 |
parse_attrib s = |
|
74095 | 1485 |
case List.elemIndex (Bytes.byte '=') s of |
1486 |
Just i | i > 0 -> (Bytes.pack $ take i s, Bytes.pack $ drop (i + 1) s) |
|
69288 | 1487 |
_ -> err_attribute |
1488 |
||
74095 | 1489 |
parse_chunk [[], []] = pop |
1490 |
parse_chunk ([] : name : atts) = push (Bytes.pack name) (map parse_attrib atts) |
|
1491 |
parse_chunk txts = fold (add . XML.Text . Bytes.pack) txts |
|
1492 |
||
1493 |
parse_body :: Bytes -> XML.Body |
|
69288 | 1494 |
parse_body source = |
74095 | 1495 |
case fold parse_chunk chunks [((Bytes.empty, []), [])] of |
1496 |
[((name, _), result)] | Bytes.null name -> reverse result |
|
69288 | 1497 |
((name, _), _) : _ -> err_unbalanced name |
74095 | 1498 |
where chunks = source |> Bytes.unpack |> split False charX |> map (split True charY) |
1499 |
||
1500 |
parse :: Bytes -> XML.Tree |
|
69288 | 1501 |
parse source = |
1502 |
case parse_body source of |
|
1503 |
[result] -> result |
|
1504 |
[] -> XML.Text "" |
|
1505 |
_ -> err "multiple results" |
|
1506 |
\<close> |
|
1507 |
||
74091 | 1508 |
generate_file "Isabelle/Completion.hs" = \<open> |
1509 |
{- Title: Isabelle/Completion.hs |
|
1510 |
Author: Makarius |
|
1511 |
LICENSE: BSD 3-clause (Isabelle) |
|
1512 |
||
1513 |
Completion of names. |
|
1514 |
||
1515 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/General/completion.ML\<close>. |
|
1516 |
-} |
|
1517 |
||
74095 | 1518 |
{-# LANGUAGE OverloadedStrings #-} |
1519 |
||
74091 | 1520 |
module Isabelle.Completion ( |
1521 |
Name, T, names, none, make, markup_element, markup_report, make_report |
|
1522 |
) where |
|
1523 |
||
1524 |
import Isabelle.Library |
|
74095 | 1525 |
import qualified Isabelle.Bytes as Bytes |
1526 |
import Isabelle.Bytes (Bytes) |
|
74091 | 1527 |
import qualified Isabelle.Properties as Properties |
1528 |
import qualified Isabelle.Markup as Markup |
|
1529 |
import qualified Isabelle.XML.Encode as Encode |
|
1530 |
import qualified Isabelle.XML as XML |
|
1531 |
import qualified Isabelle.YXML as YXML |
|
1532 |
||
1533 |
||
74095 | 1534 |
type Name = (Bytes, (Bytes, Bytes)) -- external name, kind, internal name |
74091 | 1535 |
data T = Completion Properties.T Int [Name] -- position, total length, names |
1536 |
||
1537 |
names :: Int -> Properties.T -> [Name] -> T |
|
1538 |
names limit props names = Completion props (length names) (take limit names) |
|
1539 |
||
1540 |
none :: T |
|
1541 |
none = names 0 [] [] |
|
1542 |
||
74099 | 1543 |
clean_name :: Bytes -> Bytes |
1544 |
clean_name bs = |
|
1545 |
if not (Bytes.null bs) && Bytes.last bs == u then |
|
1546 |
Bytes.unpack bs |> reverse |> dropWhile (== u) |> reverse |> Bytes.pack |
|
1547 |
else bs |
|
1548 |
where u = Bytes.byte '_' |
|
1549 |
||
74095 | 1550 |
make :: Int -> (Bytes, Properties.T) -> ((Bytes -> Bool) -> [Name]) -> T |
74091 | 1551 |
make limit (name, props) make_names = |
74095 | 1552 |
if name /= "" && name /= "_" then |
74099 | 1553 |
names limit props (make_names (Bytes.isPrefixOf (clean_name name))) |
74091 | 1554 |
else none |
1555 |
||
1556 |
markup_element :: T -> (Markup.T, XML.Body) |
|
1557 |
markup_element (Completion props total names) = |
|
1558 |
if not (null names) then |
|
1559 |
let |
|
1560 |
markup = Markup.properties props Markup.completion |
|
1561 |
body = |
|
1562 |
Encode.pair Encode.int |
|
1563 |
(Encode.list (Encode.pair Encode.string (Encode.pair Encode.string Encode.string))) |
|
1564 |
(total, names) |
|
1565 |
in (markup, body) |
|
1566 |
else (Markup.empty, []) |
|
1567 |
||
74095 | 1568 |
markup_report :: [T] -> Bytes |
1569 |
markup_report [] = Bytes.empty |
|
74091 | 1570 |
markup_report elems = |
1571 |
YXML.string_of $ XML.Elem (Markup.report, map (XML.Elem . markup_element) elems) |
|
1572 |
||
74095 | 1573 |
make_report :: Int -> (Bytes, Properties.T) -> ((Bytes -> Bool) -> [Name]) -> Bytes |
74091 | 1574 |
make_report limit name_props make_names = |
1575 |
markup_report [make limit name_props make_names] |
|
1576 |
\<close> |
|
1577 |
||
1578 |
generate_file "Isabelle/File.hs" = \<open> |
|
1579 |
{- Title: Isabelle/File.hs |
|
1580 |
Author: Makarius |
|
1581 |
LICENSE: BSD 3-clause (Isabelle) |
|
1582 |
||
1583 |
File-system operations. |
|
1584 |
||
1585 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/General/file.ML\<close>. |
|
1586 |
-} |
|
1587 |
||
74098 | 1588 |
module Isabelle.File (read, write, append) where |
74091 | 1589 |
|
1590 |
import Prelude hiding (read) |
|
1591 |
import qualified System.IO as IO |
|
74098 | 1592 |
import qualified Data.ByteString as ByteString |
1593 |
import qualified Isabelle.Bytes as Bytes |
|
1594 |
import Isabelle.Bytes (Bytes) |
|
1595 |
||
1596 |
read :: IO.FilePath -> IO Bytes |
|
1597 |
read path = Bytes.make <$> IO.withFile path IO.ReadMode ByteString.hGetContents |
|
1598 |
||
1599 |
write :: IO.FilePath -> Bytes -> IO () |
|
1600 |
write path bs = IO.withFile path IO.WriteMode (\h -> ByteString.hPut h (Bytes.unmake bs)) |
|
1601 |
||
1602 |
append :: IO.FilePath -> Bytes -> IO () |
|
1603 |
append path bs = IO.withFile path IO.AppendMode (\h -> ByteString.hPut h (Bytes.unmake bs)) |
|
74091 | 1604 |
\<close> |
1605 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
1606 |
generate_file "Isabelle/Pretty.hs" = \<open> |
69445 | 1607 |
{- Title: Isabelle/Pretty.hs |
69248 | 1608 |
Author: Makarius |
1609 |
LICENSE: BSD 3-clause (Isabelle) |
|
1610 |
||
1611 |
Generic pretty printing module. |
|
69280 | 1612 |
|
1613 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/General/pretty.ML\<close>. |
|
69248 | 1614 |
-} |
1615 |
||
74095 | 1616 |
{-# LANGUAGE OverloadedStrings #-} |
73177 | 1617 |
{-# OPTIONS_GHC -fno-warn-missing-signatures #-} |
1618 |
||
69248 | 1619 |
module Isabelle.Pretty ( |
1620 |
T, symbolic, formatted, unformatted, |
|
1621 |
||
1622 |
str, brk_indent, brk, fbrk, breaks, fbreaks, blk, block, strs, markup, mark, mark_str, marks_str, |
|
1623 |
item, text_fold, keyword1, keyword2, text, paragraph, para, quote, cartouche, separate, |
|
1624 |
commas, enclose, enum, list, str_list, big_list) |
|
1625 |
where |
|
1626 |
||
74095 | 1627 |
import qualified Data.List as List |
1628 |
||
1629 |
import qualified Isabelle.Bytes as Bytes |
|
1630 |
import Isabelle.Bytes (Bytes) |
|
74087 | 1631 |
import Isabelle.Library hiding (quote, separate, commas) |
69248 | 1632 |
import qualified Isabelle.Buffer as Buffer |
1633 |
import qualified Isabelle.Markup as Markup |
|
1634 |
import qualified Isabelle.XML as XML |
|
1635 |
import qualified Isabelle.YXML as YXML |
|
1636 |
||
1637 |
||
1638 |
data T = |
|
1639 |
Block Markup.T Bool Int [T] |
|
1640 |
| Break Int Int |
|
74095 | 1641 |
| Str Bytes |
69248 | 1642 |
|
1643 |
||
1644 |
{- output -} |
|
1645 |
||
74095 | 1646 |
symbolic_text s = if Bytes.null s then [] else [XML.Text s] |
69248 | 1647 |
|
1648 |
symbolic_markup markup body = |
|
1649 |
if Markup.is_empty markup then body |
|
69290 | 1650 |
else [XML.Elem (markup, body)] |
69248 | 1651 |
|
1652 |
symbolic :: T -> XML.Body |
|
1653 |
symbolic (Block markup consistent indent prts) = |
|
1654 |
concatMap symbolic prts |
|
1655 |
|> symbolic_markup block_markup |
|
1656 |
|> symbolic_markup markup |
|
1657 |
where block_markup = if null prts then Markup.empty else Markup.block consistent indent |
|
74095 | 1658 |
symbolic (Break wd ind) = [XML.Elem (Markup.break wd ind, symbolic_text (Bytes.spaces wd))] |
69248 | 1659 |
symbolic (Str s) = symbolic_text s |
1660 |
||
74095 | 1661 |
formatted :: T -> Bytes |
69248 | 1662 |
formatted = YXML.string_of_body . symbolic |
1663 |
||
74095 | 1664 |
unformatted :: T -> Bytes |
69248 | 1665 |
unformatted prt = Buffer.empty |> out prt |> Buffer.content |
1666 |
where |
|
1667 |
out (Block markup _ _ prts) = |
|
1668 |
let (bg, en) = YXML.output_markup markup |
|
1669 |
in Buffer.add bg #> fold out prts #> Buffer.add en |
|
74095 | 1670 |
out (Break _ wd) = Buffer.add (Bytes.spaces wd) |
69248 | 1671 |
out (Str s) = Buffer.add s |
1672 |
||
1673 |
||
1674 |
{- derived operations to create formatting expressions -} |
|
1675 |
||
1676 |
force_nat n | n < 0 = 0 |
|
1677 |
force_nat n = n |
|
1678 |
||
74095 | 1679 |
str :: BYTES a => a -> T |
1680 |
str = Str . make_bytes |
|
69248 | 1681 |
|
1682 |
brk_indent :: Int -> Int -> T |
|
1683 |
brk_indent wd ind = Break (force_nat wd) ind |
|
1684 |
||
1685 |
brk :: Int -> T |
|
1686 |
brk wd = brk_indent wd 0 |
|
1687 |
||
1688 |
fbrk :: T |
|
74095 | 1689 |
fbrk = Str "\n" |
69248 | 1690 |
|
1691 |
breaks, fbreaks :: [T] -> [T] |
|
1692 |
breaks = List.intersperse (brk 1) |
|
1693 |
fbreaks = List.intersperse fbrk |
|
1694 |
||
1695 |
blk :: (Int, [T]) -> T |
|
1696 |
blk (indent, es) = Block Markup.empty False (force_nat indent) es |
|
1697 |
||
1698 |
block :: [T] -> T |
|
1699 |
block prts = blk (2, prts) |
|
1700 |
||
74095 | 1701 |
strs :: BYTES a => [a] -> T |
69248 | 1702 |
strs = block . breaks . map str |
1703 |
||
1704 |
markup :: Markup.T -> [T] -> T |
|
1705 |
markup m = Block m False 0 |
|
1706 |
||
1707 |
mark :: Markup.T -> T -> T |
|
1708 |
mark m prt = if m == Markup.empty then prt else markup m [prt] |
|
1709 |
||
74095 | 1710 |
mark_str :: BYTES a => (Markup.T, a) -> T |
69248 | 1711 |
mark_str (m, s) = mark m (str s) |
1712 |
||
74095 | 1713 |
marks_str :: BYTES a => ([Markup.T], a) -> T |
69248 | 1714 |
marks_str (ms, s) = fold_rev mark ms (str s) |
1715 |
||
1716 |
item :: [T] -> T |
|
1717 |
item = markup Markup.item |
|
1718 |
||
1719 |
text_fold :: [T] -> T |
|
1720 |
text_fold = markup Markup.text_fold |
|
1721 |
||
74095 | 1722 |
keyword1, keyword2 :: BYTES a => a -> T |
69248 | 1723 |
keyword1 name = mark_str (Markup.keyword1, name) |
1724 |
keyword2 name = mark_str (Markup.keyword2, name) |
|
1725 |
||
74095 | 1726 |
text :: BYTES a => a -> [T] |
1727 |
text = breaks . map str . filter (not . Bytes.null) . Bytes.space_explode Bytes.space . make_bytes |
|
69248 | 1728 |
|
1729 |
paragraph :: [T] -> T |
|
1730 |
paragraph = markup Markup.paragraph |
|
1731 |
||
74095 | 1732 |
para :: BYTES a => a -> T |
69248 | 1733 |
para = paragraph . text |
1734 |
||
1735 |
quote :: T -> T |
|
74095 | 1736 |
quote prt = blk (1, [Str "\"", prt, Str "\""]) |
69248 | 1737 |
|
1738 |
cartouche :: T -> T |
|
74095 | 1739 |
cartouche prt = blk (1, [Str "\92<open>", prt, Str "\92<close>"]) |
1740 |
||
1741 |
separate :: BYTES a => a -> [T] -> [T] |
|
69248 | 1742 |
separate sep = List.intercalate [str sep, brk 1] . map single |
1743 |
||
1744 |
commas :: [T] -> [T] |
|
74095 | 1745 |
commas = separate ("," :: Bytes) |
1746 |
||
1747 |
enclose :: BYTES a => a -> a -> [T] -> T |
|
74081 | 1748 |
enclose lpar rpar prts = block (str lpar : prts <> [str rpar]) |
69248 | 1749 |
|
74095 | 1750 |
enum :: BYTES a => a -> a -> a -> [T] -> T |
69248 | 1751 |
enum sep lpar rpar = enclose lpar rpar . separate sep |
1752 |
||
74095 | 1753 |
list :: BYTES a => a -> a -> [T] -> T |
69248 | 1754 |
list = enum "," |
1755 |
||
74095 | 1756 |
str_list :: BYTES a => a -> a -> [a] -> T |
69248 | 1757 |
str_list lpar rpar = list lpar rpar . map str |
1758 |
||
74095 | 1759 |
big_list :: BYTES a => a -> [T] -> T |
69248 | 1760 |
big_list name prts = block (fbreaks (str name : prts)) |
1761 |
\<close> |
|
1762 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
1763 |
generate_file "Isabelle/Term.hs" = \<open> |
69445 | 1764 |
{- Title: Isabelle/Term.hs |
69240 | 1765 |
Author: Makarius |
1766 |
LICENSE: BSD 3-clause (Isabelle) |
|
1767 |
||
1768 |
Lambda terms, types, sorts. |
|
69280 | 1769 |
|
1770 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/term.scala\<close>. |
|
69240 | 1771 |
-} |
1772 |
||
74095 | 1773 |
{-# LANGUAGE OverloadedStrings #-} |
1774 |
||
69240 | 1775 |
module Isabelle.Term ( |
1776 |
Indexname, |
|
1777 |
||
1778 |
Sort, dummyS, |
|
1779 |
||
74100 | 1780 |
Typ(..), dummyT, is_dummyT, Term(..), |
1781 |
aconv, list_comb, strip_comb, head_of, lambda) |
|
69240 | 1782 |
where |
1783 |
||
74095 | 1784 |
import Isabelle.Bytes (Bytes) |
1785 |
||
1786 |
||
1787 |
type Indexname = (Bytes, Int) |
|
1788 |
||
1789 |
||
1790 |
type Sort = [Bytes] |
|
69240 | 1791 |
|
1792 |
dummyS :: Sort |
|
1793 |
dummyS = [""] |
|
1794 |
||
1795 |
||
1796 |
data Typ = |
|
74095 | 1797 |
Type (Bytes, [Typ]) |
1798 |
| TFree (Bytes, Sort) |
|
69240 | 1799 |
| TVar (Indexname, Sort) |
74100 | 1800 |
deriving (Show, Eq, Ord) |
69240 | 1801 |
|
1802 |
dummyT :: Typ |
|
1803 |
dummyT = Type (\<open>\<^type_name>\<open>dummy\<close>\<close>, []) |
|
1804 |
||
70845 | 1805 |
is_dummyT :: Typ -> Bool |
1806 |
is_dummyT (Type (\<open>\<^type_name>\<open>dummy\<close>\<close>, [])) = True |
|
1807 |
is_dummyT _ = False |
|
1808 |
||
69240 | 1809 |
|
1810 |
data Term = |
|
74095 | 1811 |
Const (Bytes, [Typ]) |
1812 |
| Free (Bytes, Typ) |
|
69240 | 1813 |
| Var (Indexname, Typ) |
1814 |
| Bound Int |
|
74095 | 1815 |
| Abs (Bytes, Typ, Term) |
69240 | 1816 |
| App (Term, Term) |
74100 | 1817 |
deriving (Show, Eq, Ord) |
1818 |
||
1819 |
aconv :: Term -> Term -> Bool |
|
1820 |
aconv (App (t1, u1)) (App (t2, u2)) = aconv t1 t2 && aconv u1 u2 |
|
1821 |
aconv (Abs (_, ty1, t1)) (Abs (_, ty2, t2)) = aconv t1 t2 && ty1 == ty2 |
|
1822 |
aconv a1 a2 = a1 == a2 |
|
1823 |
||
1824 |
list_comb :: Term -> [Term] -> Term |
|
1825 |
list_comb f [] = f |
|
1826 |
list_comb f (t : ts) = list_comb (App (f, t)) ts |
|
1827 |
||
1828 |
strip_comb :: Term -> (Term, [Term]) |
|
1829 |
strip_comb tm = strip (tm, []) |
|
1830 |
where |
|
1831 |
strip (App (f, t), ts) = strip (f, t : ts) |
|
1832 |
strip x = x |
|
1833 |
||
1834 |
head_of :: Term -> Term |
|
1835 |
head_of (App (f, _)) = head_of f |
|
1836 |
head_of u = u |
|
1837 |
||
1838 |
lambda :: (Bytes, Typ) -> Term -> Term |
|
1839 |
lambda (name, typ) body = Abs (name, typ, abstract 0 body) |
|
1840 |
where |
|
1841 |
abstract lev (Free (x, ty)) | name == x && typ == ty = Bound lev |
|
1842 |
abstract lev (Abs (a, ty, t)) = Abs (a, ty, abstract (lev + 1) t) |
|
1843 |
abstract lev (App (t, u)) = App (abstract lev t, abstract lev u) |
|
1844 |
abstract _ t = t |
|
69240 | 1845 |
\<close> |
1846 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
1847 |
generate_file "Isabelle/Term_XML/Encode.hs" = \<open> |
69445 | 1848 |
{- Title: Isabelle/Term_XML/Encode.hs |
69240 | 1849 |
Author: Makarius |
1850 |
LICENSE: BSD 3-clause (Isabelle) |
|
1851 |
||
1852 |
XML data representation of lambda terms. |
|
69280 | 1853 |
|
1854 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/term_xml.ML\<close>. |
|
69240 | 1855 |
-} |
1856 |
||
1857 |
{-# LANGUAGE LambdaCase #-} |
|
1858 |
||
70845 | 1859 |
module Isabelle.Term_XML.Encode (indexname, sort, typ, typ_body, term) |
69240 | 1860 |
where |
1861 |
||
1862 |
import Isabelle.Library |
|
1863 |
import Isabelle.XML.Encode |
|
1864 |
import Isabelle.Term |
|
1865 |
||
70845 | 1866 |
indexname :: P Indexname |
1867 |
indexname (a, b) = if b == 0 then [a] else [a, int_atom b] |
|
69240 | 1868 |
|
1869 |
sort :: T Sort |
|
1870 |
sort = list string |
|
1871 |
||
1872 |
typ :: T Typ |
|
1873 |
typ ty = |
|
1874 |
ty |> variant |
|
1875 |
[\case { Type (a, b) -> Just ([a], list typ b); _ -> Nothing }, |
|
1876 |
\case { TFree (a, b) -> Just ([a], sort b); _ -> Nothing }, |
|
70845 | 1877 |
\case { TVar (a, b) -> Just (indexname a, sort b); _ -> Nothing }] |
1878 |
||
1879 |
typ_body :: T Typ |
|
1880 |
typ_body ty = if is_dummyT ty then [] else typ ty |
|
69240 | 1881 |
|
1882 |
term :: T Term |
|
1883 |
term t = |
|
1884 |
t |> variant |
|
70784
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents:
70667
diff
changeset
|
1885 |
[\case { Const (a, b) -> Just ([a], list typ b); _ -> Nothing }, |
70845 | 1886 |
\case { Free (a, b) -> Just ([a], typ_body b); _ -> Nothing }, |
1887 |
\case { Var (a, b) -> Just (indexname a, typ_body b); _ -> Nothing }, |
|
1888 |
\case { Bound a -> Just ([], int a); _ -> Nothing }, |
|
69240 | 1889 |
\case { Abs (a, b, c) -> Just ([a], pair typ term (b, c)); _ -> Nothing }, |
1890 |
\case { App a -> Just ([], pair term term a); _ -> Nothing }] |
|
1891 |
\<close> |
|
1892 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
1893 |
generate_file "Isabelle/Term_XML/Decode.hs" = \<open> |
69445 | 1894 |
{- Title: Isabelle/Term_XML/Decode.hs |
69240 | 1895 |
Author: Makarius |
1896 |
LICENSE: BSD 3-clause (Isabelle) |
|
1897 |
||
1898 |
XML data representation of lambda terms. |
|
69280 | 1899 |
|
1900 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/term_xml.ML\<close>. |
|
69240 | 1901 |
-} |
1902 |
||
73177 | 1903 |
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-} |
1904 |
||
70845 | 1905 |
module Isabelle.Term_XML.Decode (indexname, sort, typ, typ_body, term) |
69240 | 1906 |
where |
1907 |
||
1908 |
import Isabelle.Library |
|
1909 |
import Isabelle.XML.Decode |
|
1910 |
import Isabelle.Term |
|
1911 |
||
1912 |
||
70845 | 1913 |
indexname :: P Indexname |
1914 |
indexname [a] = (a, 0) |
|
1915 |
indexname [a, b] = (a, int_atom b) |
|
1916 |
||
69240 | 1917 |
sort :: T Sort |
1918 |
sort = list string |
|
1919 |
||
1920 |
typ :: T Typ |
|
1921 |
typ ty = |
|
1922 |
ty |> variant |
|
1923 |
[\([a], b) -> Type (a, list typ b), |
|
1924 |
\([a], b) -> TFree (a, sort b), |
|
70845 | 1925 |
\(a, b) -> TVar (indexname a, sort b)] |
1926 |
||
1927 |
typ_body :: T Typ |
|
1928 |
typ_body [] = dummyT |
|
1929 |
typ_body body = typ body |
|
69240 | 1930 |
|
1931 |
term :: T Term |
|
1932 |
term t = |
|
1933 |
t |> variant |
|
70784
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents:
70667
diff
changeset
|
1934 |
[\([a], b) -> Const (a, list typ b), |
70845 | 1935 |
\([a], b) -> Free (a, typ_body b), |
1936 |
\(a, b) -> Var (indexname a, typ_body b), |
|
1937 |
\([], a) -> Bound (int a), |
|
69240 | 1938 |
\([a], b) -> let (c, d) = pair typ term b in Abs (a, c, d), |
1939 |
\([], a) -> App (pair term term a)] |
|
1940 |
\<close> |
|
1941 |
||
69459 | 1942 |
generate_file "Isabelle/UUID.hs" = \<open> |
1943 |
{- Title: Isabelle/UUID.hs |
|
1944 |
Author: Makarius |
|
1945 |
LICENSE: BSD 3-clause (Isabelle) |
|
1946 |
||
1947 |
Universally unique identifiers. |
|
1948 |
||
1949 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/General/uuid.scala\<close>. |
|
1950 |
-} |
|
1951 |
||
74095 | 1952 |
module Isabelle.UUID (T, random, print, parse) |
69459 | 1953 |
where |
1954 |
||
74095 | 1955 |
import Prelude hiding (print) |
1956 |
||
69459 | 1957 |
import Data.UUID (UUID) |
1958 |
import qualified Data.UUID as UUID |
|
1959 |
import Data.UUID.V4 (nextRandom) |
|
1960 |
||
74084 | 1961 |
import qualified Isabelle.Bytes as Bytes |
1962 |
import Isabelle.Bytes (Bytes) |
|
1963 |
||
69459 | 1964 |
|
69462 | 1965 |
type T = UUID |
1966 |
||
1967 |
random :: IO T |
|
69459 | 1968 |
random = nextRandom |
1969 |
||
74095 | 1970 |
print :: T -> Bytes |
1971 |
print = Bytes.make . UUID.toASCIIBytes |
|
1972 |
||
1973 |
parse :: Bytes -> Maybe T |
|
1974 |
parse = UUID.fromASCIIBytes . Bytes.unmake |
|
69459 | 1975 |
\<close> |
1976 |
||
69448 | 1977 |
generate_file "Isabelle/Byte_Message.hs" = \<open> |
1978 |
{- Title: Isabelle/Byte_Message.hs |
|
69446 | 1979 |
Author: Makarius |
1980 |
LICENSE: BSD 3-clause (Isabelle) |
|
1981 |
||
69448 | 1982 |
Byte-oriented messages. |
1983 |
||
1984 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/byte_message.ML\<close> |
|
1985 |
and \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/byte_message.scala\<close>. |
|
69446 | 1986 |
-} |
1987 |
||
74083 | 1988 |
{-# LANGUAGE OverloadedStrings #-} |
73177 | 1989 |
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-} |
1990 |
||
69452 | 1991 |
module Isabelle.Byte_Message ( |
69467 | 1992 |
write, write_line, |
74088 | 1993 |
read, read_block, read_line, |
69476 | 1994 |
make_message, write_message, read_message, |
73178 | 1995 |
make_line_message, write_line_message, read_line_message, |
1996 |
read_yxml, write_yxml |
|
69452 | 1997 |
) |
69446 | 1998 |
where |
1999 |
||
69449 | 2000 |
import Prelude hiding (read) |
69454 | 2001 |
import Data.Maybe |
69446 | 2002 |
import qualified Data.ByteString as ByteString |
74084 | 2003 |
import qualified Isabelle.Bytes as Bytes |
2004 |
import Isabelle.Bytes (Bytes) |
|
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
2005 |
import qualified Isabelle.UTF8 as UTF8 |
73178 | 2006 |
import qualified Isabelle.XML as XML |
2007 |
import qualified Isabelle.YXML as YXML |
|
73177 | 2008 |
|
69446 | 2009 |
import Network.Socket (Socket) |
74082 | 2010 |
import qualified Network.Socket.ByteString as Socket |
69446 | 2011 |
|
74088 | 2012 |
import Isabelle.Library |
69446 | 2013 |
import qualified Isabelle.Value as Value |
2014 |
||
2015 |
||
69452 | 2016 |
{- output operations -} |
2017 |
||
74084 | 2018 |
write :: Socket -> [Bytes] -> IO () |
2019 |
write socket = Socket.sendMany socket . map Bytes.unmake |
|
2020 |
||
2021 |
write_line :: Socket -> Bytes -> IO () |
|
74083 | 2022 |
write_line socket s = write socket [s, "\n"] |
69467 | 2023 |
|
69452 | 2024 |
|
2025 |
{- input operations -} |
|
2026 |
||
74084 | 2027 |
read :: Socket -> Int -> IO Bytes |
69452 | 2028 |
read socket n = read_body 0 [] |
69449 | 2029 |
where |
74084 | 2030 |
result = Bytes.concat . reverse |
69452 | 2031 |
read_body len ss = |
69449 | 2032 |
if len >= n then return (result ss) |
2033 |
else |
|
2034 |
(do |
|
74082 | 2035 |
s <- Socket.recv socket (min (n - len) 8192) |
69449 | 2036 |
case ByteString.length s of |
2037 |
0 -> return (result ss) |
|
74084 | 2038 |
m -> read_body (len + m) (Bytes.make s : ss)) |
2039 |
||
2040 |
read_block :: Socket -> Int -> IO (Maybe Bytes, Int) |
|
69452 | 2041 |
read_block socket n = do |
2042 |
msg <- read socket n |
|
74084 | 2043 |
let len = Bytes.length msg |
69452 | 2044 |
return (if len == n then Just msg else Nothing, len) |
69449 | 2045 |
|
74084 | 2046 |
read_line :: Socket -> IO (Maybe Bytes) |
69452 | 2047 |
read_line socket = read_body [] |
69446 | 2048 |
where |
74088 | 2049 |
result = Bytes.trim_line . Bytes.pack . reverse |
69452 | 2050 |
read_body bs = do |
74082 | 2051 |
s <- Socket.recv socket 1 |
69446 | 2052 |
case ByteString.length s of |
2053 |
0 -> return (if null bs then Nothing else Just (result bs)) |
|
2054 |
1 -> |
|
2055 |
case ByteString.head s of |
|
2056 |
10 -> return (Just (result bs)) |
|
69452 | 2057 |
b -> read_body (b : bs) |
69446 | 2058 |
|
69448 | 2059 |
|
69454 | 2060 |
{- messages with multiple chunks (arbitrary content) -} |
2061 |
||
74084 | 2062 |
make_header :: [Int] -> [Bytes] |
74095 | 2063 |
make_header ns = [space_implode "," (map Value.print_int ns), "\n"] |
69454 | 2064 |
|
74084 | 2065 |
make_message :: [Bytes] -> [Bytes] |
2066 |
make_message chunks = make_header (map Bytes.length chunks) <> chunks |
|
2067 |
||
2068 |
write_message :: Socket -> [Bytes] -> IO () |
|
69476 | 2069 |
write_message socket = write socket . make_message |
69454 | 2070 |
|
74084 | 2071 |
parse_header :: Bytes -> [Int] |
69454 | 2072 |
parse_header line = |
2073 |
let |
|
74095 | 2074 |
res = map Value.parse_nat (Bytes.space_explode (Bytes.byte ',') line) |
69454 | 2075 |
in |
2076 |
if all isJust res then map fromJust res |
|
74081 | 2077 |
else error ("Malformed message header: " <> quote (UTF8.decode line)) |
69454 | 2078 |
|
74084 | 2079 |
read_chunk :: Socket -> Int -> IO Bytes |
69454 | 2080 |
read_chunk socket n = do |
2081 |
res <- read_block socket n |
|
2082 |
return $ |
|
2083 |
case res of |
|
2084 |
(Just chunk, _) -> chunk |
|
2085 |
(Nothing, len) -> |
|
74081 | 2086 |
error ("Malformed message chunk: unexpected EOF after " <> |
2087 |
show len <> " of " <> show n <> " bytes") |
|
69454 | 2088 |
|
74084 | 2089 |
read_message :: Socket -> IO (Maybe [Bytes]) |
69454 | 2090 |
read_message socket = do |
2091 |
res <- read_line socket |
|
2092 |
case res of |
|
2093 |
Just line -> Just <$> mapM (read_chunk socket) (parse_header line) |
|
2094 |
Nothing -> return Nothing |
|
2095 |
||
2096 |
||
69448 | 2097 |
-- hybrid messages: line or length+block (with content restriction) |
2098 |
||
74084 | 2099 |
is_length :: Bytes -> Bool |
69452 | 2100 |
is_length msg = |
74084 | 2101 |
not (Bytes.null msg) && Bytes.all (\b -> 48 <= b && b <= 57) msg |
2102 |
||
2103 |
is_terminated :: Bytes -> Bool |
|
69452 | 2104 |
is_terminated msg = |
74084 | 2105 |
not (Bytes.null msg) && (Bytes.last msg == 13 || Bytes.last msg == 10) |
2106 |
||
2107 |
make_line_message :: Bytes -> [Bytes] |
|
69476 | 2108 |
make_line_message msg = |
74084 | 2109 |
let n = Bytes.length msg in |
69476 | 2110 |
if is_length msg || is_terminated msg then |
74081 | 2111 |
error ("Bad content for line message:\n" <> take 100 (UTF8.decode msg)) |
69476 | 2112 |
else |
74084 | 2113 |
(if n > 100 || Bytes.any (== 10) msg then make_header [n + 1] else []) <> [msg, "\n"] |
2114 |
||
2115 |
write_line_message :: Socket -> Bytes -> IO () |
|
69476 | 2116 |
write_line_message socket = write socket . make_line_message |
69448 | 2117 |
|
74084 | 2118 |
read_line_message :: Socket -> IO (Maybe Bytes) |
69448 | 2119 |
read_line_message socket = do |
69446 | 2120 |
opt_line <- read_line socket |
2121 |
case opt_line of |
|
2122 |
Nothing -> return Nothing |
|
2123 |
Just line -> |
|
74095 | 2124 |
case Value.parse_nat line of |
69446 | 2125 |
Nothing -> return $ Just line |
74088 | 2126 |
Just n -> fmap Bytes.trim_line . fst <$> read_block socket n |
73178 | 2127 |
|
2128 |
||
2129 |
read_yxml :: Socket -> IO (Maybe XML.Body) |
|
2130 |
read_yxml socket = do |
|
2131 |
res <- read_line_message socket |
|
74095 | 2132 |
return (YXML.parse_body <$> res) |
73178 | 2133 |
|
2134 |
write_yxml :: Socket -> XML.Body -> IO () |
|
2135 |
write_yxml socket body = |
|
74095 | 2136 |
write_line_message socket (YXML.string_of_body body) |
69446 | 2137 |
\<close> |
2138 |
||
71692 | 2139 |
generate_file "Isabelle/Isabelle_Thread.hs" = \<open> |
2140 |
{- Title: Isabelle/Isabelle_Thread.hs |
|
69473 | 2141 |
Author: Makarius |
2142 |
LICENSE: BSD 3-clause (Isabelle) |
|
2143 |
||
71692 | 2144 |
Isabelle-specific thread management. |
2145 |
||
2146 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/Concurrent/isabelle_thread.ML\<close> |
|
2147 |
and \<^file>\<open>$ISABELLE_HOME/src/Pure/Concurrent/isabelle_thread.scala\<close>. |
|
69473 | 2148 |
-} |
2149 |
||
2150 |
{-# LANGUAGE NamedFieldPuns #-} |
|
2151 |
||
71692 | 2152 |
module Isabelle.Isabelle_Thread ( |
69473 | 2153 |
ThreadId, Result, |
69494 | 2154 |
find_id, |
69473 | 2155 |
properties, change_properties, |
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2156 |
add_resource, del_resource, bracket_resource, |
69497 | 2157 |
is_stopped, expose_stopped, stop, |
69496 | 2158 |
my_uuid, stop_uuid, |
69494 | 2159 |
Fork, fork_finally, fork) |
69473 | 2160 |
where |
2161 |
||
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2162 |
import Data.Unique |
69473 | 2163 |
import Data.IORef |
2164 |
import System.IO.Unsafe |
|
2165 |
||
69494 | 2166 |
import qualified Data.List as List |
69497 | 2167 |
import Control.Monad (when, forM_) |
69473 | 2168 |
import Data.Map.Strict (Map) |
2169 |
import qualified Data.Map.Strict as Map |
|
2170 |
import Control.Exception as Exception |
|
2171 |
import Control.Concurrent (ThreadId) |
|
2172 |
import qualified Control.Concurrent as Concurrent |
|
2173 |
import Control.Concurrent.Thread (Result) |
|
2174 |
import qualified Control.Concurrent.Thread as Thread |
|
69494 | 2175 |
import qualified Isabelle.UUID as UUID |
69473 | 2176 |
import qualified Isabelle.Properties as Properties |
2177 |
||
2178 |
||
2179 |
{- thread info -} |
|
2180 |
||
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2181 |
type Resources = Map Unique (IO ()) |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2182 |
data Info = Info {uuid :: UUID.T, props :: Properties.T, stopped :: Bool, resources :: Resources} |
69473 | 2183 |
type Infos = Map ThreadId Info |
2184 |
||
2185 |
lookup_info :: Infos -> ThreadId -> Maybe Info |
|
69494 | 2186 |
lookup_info infos id = Map.lookup id infos |
2187 |
||
2188 |
init_info :: ThreadId -> UUID.T -> Infos -> (Infos, ()) |
|
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2189 |
init_info id uuid infos = (Map.insert id (Info uuid [] False Map.empty) infos, ()) |
69473 | 2190 |
|
2191 |
||
2192 |
{- global state -} |
|
2193 |
||
2194 |
{-# NOINLINE global_state #-} |
|
2195 |
global_state :: IORef Infos |
|
2196 |
global_state = unsafePerformIO (newIORef Map.empty) |
|
2197 |
||
69494 | 2198 |
find_id :: UUID.T -> IO (Maybe ThreadId) |
2199 |
find_id uuid = do |
|
2200 |
state <- readIORef global_state |
|
2201 |
return $ fst <$> List.find (\(_, Info{uuid = uuid'}) -> uuid == uuid') (Map.assocs state) |
|
2202 |
||
69473 | 2203 |
get_info :: ThreadId -> IO (Maybe Info) |
2204 |
get_info id = do |
|
2205 |
state <- readIORef global_state |
|
2206 |
return $ lookup_info state id |
|
2207 |
||
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2208 |
map_info :: ThreadId -> (Info -> Info) -> IO (Maybe Info) |
69473 | 2209 |
map_info id f = |
2210 |
atomicModifyIORef' global_state |
|
2211 |
(\infos -> |
|
2212 |
case lookup_info infos id of |
|
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2213 |
Nothing -> (infos, Nothing) |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2214 |
Just info -> |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2215 |
let info' = f info |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2216 |
in (Map.insert id info' infos, Just info')) |
69473 | 2217 |
|
69495 | 2218 |
delete_info :: ThreadId -> IO () |
2219 |
delete_info id = |
|
2220 |
atomicModifyIORef' global_state (\infos -> (Map.delete id infos, ())) |
|
2221 |
||
69473 | 2222 |
|
2223 |
{- thread properties -} |
|
2224 |
||
69498
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
2225 |
my_info :: IO (Maybe Info) |
69473 | 2226 |
my_info = do |
2227 |
id <- Concurrent.myThreadId |
|
69498
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
2228 |
get_info id |
69473 | 2229 |
|
2230 |
properties :: IO Properties.T |
|
69498
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
2231 |
properties = maybe [] props <$> my_info |
69473 | 2232 |
|
2233 |
change_properties :: (Properties.T -> Properties.T) -> IO () |
|
2234 |
change_properties f = do |
|
2235 |
id <- Concurrent.myThreadId |
|
2236 |
map_info id (\info -> info {props = f (props info)}) |
|
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2237 |
return () |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2238 |
|
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2239 |
|
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2240 |
{- managed resources -} |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2241 |
|
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2242 |
add_resource :: IO () -> IO Unique |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2243 |
add_resource resource = do |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2244 |
id <- Concurrent.myThreadId |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2245 |
u <- newUnique |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2246 |
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
|
2247 |
return u |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2248 |
|
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2249 |
del_resource :: Unique -> IO () |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2250 |
del_resource u = do |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2251 |
id <- Concurrent.myThreadId |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2252 |
map_info id (\info -> info {resources = Map.delete u (resources info)}) |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2253 |
return () |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2254 |
|
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2255 |
bracket_resource :: IO () -> IO a -> IO a |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2256 |
bracket_resource resource body = |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2257 |
Exception.bracket (add_resource resource) del_resource (const body) |
69473 | 2258 |
|
2259 |
||
2260 |
{- stop -} |
|
2261 |
||
2262 |
is_stopped :: IO Bool |
|
69498
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
2263 |
is_stopped = maybe False stopped <$> my_info |
69473 | 2264 |
|
69497 | 2265 |
expose_stopped :: IO () |
2266 |
expose_stopped = do |
|
2267 |
stopped <- is_stopped |
|
2268 |
when stopped $ throw ThreadKilled |
|
2269 |
||
69473 | 2270 |
stop :: ThreadId -> IO () |
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2271 |
stop id = do |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2272 |
info <- map_info id (\info -> info {stopped = True}) |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2273 |
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
|
2274 |
sequence_ ops |
69473 | 2275 |
|
2276 |
||
69496 | 2277 |
{- UUID -} |
2278 |
||
69498
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
2279 |
my_uuid :: IO (Maybe UUID.T) |
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
2280 |
my_uuid = fmap uuid <$> my_info |
69496 | 2281 |
|
2282 |
stop_uuid :: UUID.T -> IO () |
|
2283 |
stop_uuid uuid = do |
|
2284 |
id <- find_id uuid |
|
2285 |
forM_ id stop |
|
2286 |
||
2287 |
||
69473 | 2288 |
{- fork -} |
2289 |
||
69494 | 2290 |
type Fork a = (ThreadId, UUID.T, IO (Result a)) |
2291 |
||
2292 |
fork_finally :: IO a -> (Either SomeException a -> IO b) -> IO (Fork b) |
|
69473 | 2293 |
fork_finally body finally = do |
69494 | 2294 |
uuid <- UUID.random |
69473 | 2295 |
(id, result) <- |
2296 |
Exception.mask (\restore -> |
|
2297 |
Thread.forkIO |
|
2298 |
(Exception.try |
|
2299 |
(do |
|
2300 |
id <- Concurrent.myThreadId |
|
69494 | 2301 |
atomicModifyIORef' global_state (init_info id uuid) |
69495 | 2302 |
restore body) |
2303 |
>>= (\res -> do id <- Concurrent.myThreadId; delete_info id; finally res))) |
|
69494 | 2304 |
return (id, uuid, result) |
2305 |
||
2306 |
fork :: IO a -> IO (Fork a) |
|
69473 | 2307 |
fork body = fork_finally body Thread.result |
2308 |
\<close> |
|
2309 |
||
69455 | 2310 |
generate_file "Isabelle/Server.hs" = \<open> |
2311 |
{- Title: Isabelle/Server.hs |
|
2312 |
Author: Makarius |
|
2313 |
LICENSE: BSD 3-clause (Isabelle) |
|
2314 |
||
2315 |
TCP server on localhost. |
|
2316 |
-} |
|
2317 |
||
69465 | 2318 |
module Isabelle.Server ( |
2319 |
localhost_name, localhost, publish_text, publish_stdout, |
|
73178 | 2320 |
server, connection |
69465 | 2321 |
) |
2322 |
where |
|
69455 | 2323 |
|
69465 | 2324 |
import Control.Monad (forever, when) |
69455 | 2325 |
import qualified Control.Exception as Exception |
2326 |
import Network.Socket (Socket) |
|
2327 |
import qualified Network.Socket as Socket |
|
69472 | 2328 |
import qualified System.IO as IO |
69455 | 2329 |
|
69462 | 2330 |
import Isabelle.Library |
74084 | 2331 |
import Isabelle.Bytes (Bytes) |
69462 | 2332 |
import qualified Isabelle.UUID as UUID |
69465 | 2333 |
import qualified Isabelle.Byte_Message as Byte_Message |
71692 | 2334 |
import qualified Isabelle.Isabelle_Thread as Isabelle_Thread |
69462 | 2335 |
|
2336 |
||
2337 |
{- server address -} |
|
69455 | 2338 |
|
69463 | 2339 |
localhost_name :: String |
2340 |
localhost_name = "127.0.0.1" |
|
2341 |
||
69455 | 2342 |
localhost :: Socket.HostAddress |
2343 |
localhost = Socket.tupleToHostAddress (127, 0, 0, 1) |
|
2344 |
||
69462 | 2345 |
publish_text :: String -> String -> UUID.T -> String |
2346 |
publish_text name address password = |
|
74081 | 2347 |
"server " <> quote name <> " = " <> address <> " (password " <> quote (show password) <> ")" |
69462 | 2348 |
|
2349 |
publish_stdout :: String -> String -> UUID.T -> IO () |
|
74095 | 2350 |
publish_stdout name address password = |
2351 |
putStrLn (publish_text name address password) |
|
69462 | 2352 |
|
2353 |
||
2354 |
{- server -} |
|
2355 |
||
2356 |
server :: (String -> UUID.T -> IO ()) -> (Socket -> IO ()) -> IO () |
|
69455 | 2357 |
server publish handle = |
69462 | 2358 |
Socket.withSocketsDo $ Exception.bracket open (Socket.close . fst) (uncurry loop) |
69455 | 2359 |
where |
74084 | 2360 |
open :: IO (Socket, Bytes) |
69455 | 2361 |
open = do |
69466 | 2362 |
server_socket <- Socket.socket Socket.AF_INET Socket.Stream Socket.defaultProtocol |
2363 |
Socket.bind server_socket (Socket.SockAddrInet 0 localhost) |
|
2364 |
Socket.listen server_socket 50 |
|
69462 | 2365 |
|
69466 | 2366 |
port <- Socket.socketPort server_socket |
74081 | 2367 |
let address = localhost_name <> ":" <> show port |
69462 | 2368 |
password <- UUID.random |
2369 |
publish address password |
|
69455 | 2370 |
|
74095 | 2371 |
return (server_socket, UUID.print password) |
69462 | 2372 |
|
74084 | 2373 |
loop :: Socket -> Bytes -> IO () |
69466 | 2374 |
loop server_socket password = forever $ do |
69480 | 2375 |
(connection, _) <- Socket.accept server_socket |
71692 | 2376 |
Isabelle_Thread.fork_finally |
69465 | 2377 |
(do |
2378 |
line <- Byte_Message.read_line connection |
|
2379 |
when (line == Just password) $ handle connection) |
|
69473 | 2380 |
(\finally -> do |
69472 | 2381 |
Socket.close connection |
69473 | 2382 |
case finally of |
69472 | 2383 |
Left exn -> IO.hPutStrLn IO.stderr $ Exception.displayException exn |
2384 |
Right () -> return ()) |
|
69455 | 2385 |
return () |
73178 | 2386 |
|
2387 |
||
2388 |
{- client connection -} |
|
2389 |
||
74095 | 2390 |
connection :: String -> Bytes -> (Socket -> IO a) -> IO a |
73178 | 2391 |
connection port password client = |
2392 |
Socket.withSocketsDo $ do |
|
2393 |
addr <- resolve |
|
2394 |
Exception.bracket (open addr) Socket.close body |
|
2395 |
where |
|
2396 |
resolve = do |
|
2397 |
let hints = |
|
2398 |
Socket.defaultHints { |
|
2399 |
Socket.addrFlags = [Socket.AI_NUMERICHOST, Socket.AI_NUMERICSERV], |
|
2400 |
Socket.addrSocketType = Socket.Stream } |
|
2401 |
head <$> Socket.getAddrInfo (Just hints) (Just "127.0.0.1") (Just port) |
|
2402 |
||
2403 |
open addr = do |
|
2404 |
socket <- Socket.socket (Socket.addrFamily addr) (Socket.addrSocketType addr) |
|
2405 |
(Socket.addrProtocol addr) |
|
2406 |
Socket.connect socket $ Socket.addrAddress addr |
|
2407 |
return socket |
|
2408 |
||
2409 |
body socket = do |
|
74095 | 2410 |
Byte_Message.write_line socket password |
73178 | 2411 |
client socket |
69455 | 2412 |
\<close> |
2413 |
||
70047
96fe857a7a6f
clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents:
69968
diff
changeset
|
2414 |
export_generated_files _ |
69628 | 2415 |
|
69222 | 2416 |
end |