author | wenzelm |
Tue, 03 Aug 2021 12:39:29 +0200 | |
changeset 74106 | 4984fad0e91d |
parent 74105 | d3d6e01a6b00 |
child 74124 | d36e40f3c171 |
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 |
|
74105 | 8 |
imports Main |
69222 | 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 |
||
74095 | 1524 |
import qualified Isabelle.Bytes as Bytes |
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1525 |
import qualified Isabelle.Name as Name |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1526 |
import Isabelle.Name (Name) |
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 |
||
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1534 |
type Names = [(Name, (Name, Name))] -- external name, kind, internal name |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1535 |
data T = Completion Properties.T Int Names -- position, total length, names |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1536 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1537 |
names :: Int -> Properties.T -> Names -> T |
74091 | 1538 |
names limit props names = Completion props (length names) (take limit names) |
1539 |
||
1540 |
none :: T |
|
1541 |
none = names 0 [] [] |
|
1542 |
||
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1543 |
make :: Int -> (Name, Properties.T) -> ((Name -> Bool) -> Names) -> T |
74091 | 1544 |
make limit (name, props) make_names = |
74095 | 1545 |
if name /= "" && name /= "_" then |
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1546 |
names limit props (make_names (Bytes.isPrefixOf (Name.clean name))) |
74091 | 1547 |
else none |
1548 |
||
1549 |
markup_element :: T -> (Markup.T, XML.Body) |
|
1550 |
markup_element (Completion props total names) = |
|
1551 |
if not (null names) then |
|
1552 |
let |
|
1553 |
markup = Markup.properties props Markup.completion |
|
1554 |
body = |
|
1555 |
Encode.pair Encode.int |
|
1556 |
(Encode.list (Encode.pair Encode.string (Encode.pair Encode.string Encode.string))) |
|
1557 |
(total, names) |
|
1558 |
in (markup, body) |
|
1559 |
else (Markup.empty, []) |
|
1560 |
||
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1561 |
markup_report :: [T] -> Name |
74095 | 1562 |
markup_report [] = Bytes.empty |
74091 | 1563 |
markup_report elems = |
1564 |
YXML.string_of $ XML.Elem (Markup.report, map (XML.Elem . markup_element) elems) |
|
1565 |
||
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1566 |
make_report :: Int -> (Name, Properties.T) -> ((Name -> Bool) -> Names) -> Name |
74091 | 1567 |
make_report limit name_props make_names = |
1568 |
markup_report [make limit name_props make_names] |
|
1569 |
\<close> |
|
1570 |
||
1571 |
generate_file "Isabelle/File.hs" = \<open> |
|
1572 |
{- Title: Isabelle/File.hs |
|
1573 |
Author: Makarius |
|
1574 |
LICENSE: BSD 3-clause (Isabelle) |
|
1575 |
||
1576 |
File-system operations. |
|
1577 |
||
1578 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/General/file.ML\<close>. |
|
1579 |
-} |
|
1580 |
||
74098 | 1581 |
module Isabelle.File (read, write, append) where |
74091 | 1582 |
|
1583 |
import Prelude hiding (read) |
|
1584 |
import qualified System.IO as IO |
|
74098 | 1585 |
import qualified Data.ByteString as ByteString |
1586 |
import qualified Isabelle.Bytes as Bytes |
|
1587 |
import Isabelle.Bytes (Bytes) |
|
1588 |
||
1589 |
read :: IO.FilePath -> IO Bytes |
|
1590 |
read path = Bytes.make <$> IO.withFile path IO.ReadMode ByteString.hGetContents |
|
1591 |
||
1592 |
write :: IO.FilePath -> Bytes -> IO () |
|
1593 |
write path bs = IO.withFile path IO.WriteMode (\h -> ByteString.hPut h (Bytes.unmake bs)) |
|
1594 |
||
1595 |
append :: IO.FilePath -> Bytes -> IO () |
|
1596 |
append path bs = IO.withFile path IO.AppendMode (\h -> ByteString.hPut h (Bytes.unmake bs)) |
|
74091 | 1597 |
\<close> |
1598 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
1599 |
generate_file "Isabelle/Pretty.hs" = \<open> |
69445 | 1600 |
{- Title: Isabelle/Pretty.hs |
69248 | 1601 |
Author: Makarius |
1602 |
LICENSE: BSD 3-clause (Isabelle) |
|
1603 |
||
1604 |
Generic pretty printing module. |
|
69280 | 1605 |
|
1606 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/General/pretty.ML\<close>. |
|
69248 | 1607 |
-} |
1608 |
||
74095 | 1609 |
{-# LANGUAGE OverloadedStrings #-} |
73177 | 1610 |
{-# OPTIONS_GHC -fno-warn-missing-signatures #-} |
1611 |
||
69248 | 1612 |
module Isabelle.Pretty ( |
1613 |
T, symbolic, formatted, unformatted, |
|
1614 |
||
1615 |
str, brk_indent, brk, fbrk, breaks, fbreaks, blk, block, strs, markup, mark, mark_str, marks_str, |
|
1616 |
item, text_fold, keyword1, keyword2, text, paragraph, para, quote, cartouche, separate, |
|
1617 |
commas, enclose, enum, list, str_list, big_list) |
|
1618 |
where |
|
1619 |
||
74095 | 1620 |
import qualified Data.List as List |
1621 |
||
1622 |
import qualified Isabelle.Bytes as Bytes |
|
1623 |
import Isabelle.Bytes (Bytes) |
|
74087 | 1624 |
import Isabelle.Library hiding (quote, separate, commas) |
69248 | 1625 |
import qualified Isabelle.Buffer as Buffer |
1626 |
import qualified Isabelle.Markup as Markup |
|
1627 |
import qualified Isabelle.XML as XML |
|
1628 |
import qualified Isabelle.YXML as YXML |
|
1629 |
||
1630 |
||
1631 |
data T = |
|
1632 |
Block Markup.T Bool Int [T] |
|
1633 |
| Break Int Int |
|
74095 | 1634 |
| Str Bytes |
69248 | 1635 |
|
1636 |
||
1637 |
{- output -} |
|
1638 |
||
74095 | 1639 |
symbolic_text s = if Bytes.null s then [] else [XML.Text s] |
69248 | 1640 |
|
1641 |
symbolic_markup markup body = |
|
1642 |
if Markup.is_empty markup then body |
|
69290 | 1643 |
else [XML.Elem (markup, body)] |
69248 | 1644 |
|
1645 |
symbolic :: T -> XML.Body |
|
1646 |
symbolic (Block markup consistent indent prts) = |
|
1647 |
concatMap symbolic prts |
|
1648 |
|> symbolic_markup block_markup |
|
1649 |
|> symbolic_markup markup |
|
1650 |
where block_markup = if null prts then Markup.empty else Markup.block consistent indent |
|
74095 | 1651 |
symbolic (Break wd ind) = [XML.Elem (Markup.break wd ind, symbolic_text (Bytes.spaces wd))] |
69248 | 1652 |
symbolic (Str s) = symbolic_text s |
1653 |
||
74095 | 1654 |
formatted :: T -> Bytes |
69248 | 1655 |
formatted = YXML.string_of_body . symbolic |
1656 |
||
74095 | 1657 |
unformatted :: T -> Bytes |
69248 | 1658 |
unformatted prt = Buffer.empty |> out prt |> Buffer.content |
1659 |
where |
|
1660 |
out (Block markup _ _ prts) = |
|
1661 |
let (bg, en) = YXML.output_markup markup |
|
1662 |
in Buffer.add bg #> fold out prts #> Buffer.add en |
|
74095 | 1663 |
out (Break _ wd) = Buffer.add (Bytes.spaces wd) |
69248 | 1664 |
out (Str s) = Buffer.add s |
1665 |
||
1666 |
||
1667 |
{- derived operations to create formatting expressions -} |
|
1668 |
||
1669 |
force_nat n | n < 0 = 0 |
|
1670 |
force_nat n = n |
|
1671 |
||
74095 | 1672 |
str :: BYTES a => a -> T |
1673 |
str = Str . make_bytes |
|
69248 | 1674 |
|
1675 |
brk_indent :: Int -> Int -> T |
|
1676 |
brk_indent wd ind = Break (force_nat wd) ind |
|
1677 |
||
1678 |
brk :: Int -> T |
|
1679 |
brk wd = brk_indent wd 0 |
|
1680 |
||
1681 |
fbrk :: T |
|
74095 | 1682 |
fbrk = Str "\n" |
69248 | 1683 |
|
1684 |
breaks, fbreaks :: [T] -> [T] |
|
1685 |
breaks = List.intersperse (brk 1) |
|
1686 |
fbreaks = List.intersperse fbrk |
|
1687 |
||
1688 |
blk :: (Int, [T]) -> T |
|
1689 |
blk (indent, es) = Block Markup.empty False (force_nat indent) es |
|
1690 |
||
1691 |
block :: [T] -> T |
|
1692 |
block prts = blk (2, prts) |
|
1693 |
||
74095 | 1694 |
strs :: BYTES a => [a] -> T |
69248 | 1695 |
strs = block . breaks . map str |
1696 |
||
1697 |
markup :: Markup.T -> [T] -> T |
|
1698 |
markup m = Block m False 0 |
|
1699 |
||
1700 |
mark :: Markup.T -> T -> T |
|
1701 |
mark m prt = if m == Markup.empty then prt else markup m [prt] |
|
1702 |
||
74095 | 1703 |
mark_str :: BYTES a => (Markup.T, a) -> T |
69248 | 1704 |
mark_str (m, s) = mark m (str s) |
1705 |
||
74095 | 1706 |
marks_str :: BYTES a => ([Markup.T], a) -> T |
69248 | 1707 |
marks_str (ms, s) = fold_rev mark ms (str s) |
1708 |
||
1709 |
item :: [T] -> T |
|
1710 |
item = markup Markup.item |
|
1711 |
||
1712 |
text_fold :: [T] -> T |
|
1713 |
text_fold = markup Markup.text_fold |
|
1714 |
||
74095 | 1715 |
keyword1, keyword2 :: BYTES a => a -> T |
69248 | 1716 |
keyword1 name = mark_str (Markup.keyword1, name) |
1717 |
keyword2 name = mark_str (Markup.keyword2, name) |
|
1718 |
||
74095 | 1719 |
text :: BYTES a => a -> [T] |
1720 |
text = breaks . map str . filter (not . Bytes.null) . Bytes.space_explode Bytes.space . make_bytes |
|
69248 | 1721 |
|
1722 |
paragraph :: [T] -> T |
|
1723 |
paragraph = markup Markup.paragraph |
|
1724 |
||
74095 | 1725 |
para :: BYTES a => a -> T |
69248 | 1726 |
para = paragraph . text |
1727 |
||
1728 |
quote :: T -> T |
|
74095 | 1729 |
quote prt = blk (1, [Str "\"", prt, Str "\""]) |
69248 | 1730 |
|
1731 |
cartouche :: T -> T |
|
74095 | 1732 |
cartouche prt = blk (1, [Str "\92<open>", prt, Str "\92<close>"]) |
1733 |
||
1734 |
separate :: BYTES a => a -> [T] -> [T] |
|
69248 | 1735 |
separate sep = List.intercalate [str sep, brk 1] . map single |
1736 |
||
1737 |
commas :: [T] -> [T] |
|
74095 | 1738 |
commas = separate ("," :: Bytes) |
1739 |
||
1740 |
enclose :: BYTES a => a -> a -> [T] -> T |
|
74081 | 1741 |
enclose lpar rpar prts = block (str lpar : prts <> [str rpar]) |
69248 | 1742 |
|
74095 | 1743 |
enum :: BYTES a => a -> a -> a -> [T] -> T |
69248 | 1744 |
enum sep lpar rpar = enclose lpar rpar . separate sep |
1745 |
||
74095 | 1746 |
list :: BYTES a => a -> a -> [T] -> T |
69248 | 1747 |
list = enum "," |
1748 |
||
74095 | 1749 |
str_list :: BYTES a => a -> a -> [a] -> T |
69248 | 1750 |
str_list lpar rpar = list lpar rpar . map str |
1751 |
||
74095 | 1752 |
big_list :: BYTES a => a -> [T] -> T |
69248 | 1753 |
big_list name prts = block (fbreaks (str name : prts)) |
1754 |
\<close> |
|
1755 |
||
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1756 |
generate_file "Isabelle/Name.hs" = \<open> |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1757 |
{- Title: Isabelle/Name.hs |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1758 |
Author: Makarius |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1759 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1760 |
Names of basic logical entities (variables etc.). |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1761 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1762 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/name.ML\<close>. |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1763 |
-} |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1764 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1765 |
{-# LANGUAGE OverloadedStrings #-} |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1766 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1767 |
module Isabelle.Name ( |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1768 |
Name, clean_index, clean, |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1769 |
Context, declare, is_declared, context, make_context, variant |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1770 |
) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1771 |
where |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1772 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1773 |
import Data.Word (Word8) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1774 |
import qualified Data.Set as Set |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1775 |
import Data.Set (Set) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1776 |
import qualified Isabelle.Bytes as Bytes |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1777 |
import Isabelle.Bytes (Bytes) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1778 |
import qualified Isabelle.Symbol as Symbol |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1779 |
import Isabelle.Library |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1780 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1781 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1782 |
type Name = Bytes |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1783 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1784 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1785 |
{- suffix for internal names -} |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1786 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1787 |
underscore :: Word8 |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1788 |
underscore = Bytes.byte '_' |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1789 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1790 |
clean_index :: Name -> (Name, Int) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1791 |
clean_index x = |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1792 |
if Bytes.null x || Bytes.last x /= underscore then (x, 0) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1793 |
else |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1794 |
let |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1795 |
rev = reverse (Bytes.unpack x) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1796 |
n = length (takeWhile (== underscore) rev) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1797 |
y = Bytes.pack (reverse (drop n rev)) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1798 |
in (y, n) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1799 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1800 |
clean :: Name -> Name |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1801 |
clean = fst . clean_index |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1802 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1803 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1804 |
{- context for used names -} |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1805 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1806 |
data Context = Context (Set Name) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1807 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1808 |
declare :: Name -> Context -> Context |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1809 |
declare x (Context names) = Context (Set.insert x names) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1810 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1811 |
is_declared :: Context -> Name -> Bool |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1812 |
is_declared (Context names) x = Set.member x names |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1813 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1814 |
context :: Context |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1815 |
context = Context (Set.fromList ["", "'"]) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1816 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1817 |
make_context :: [Name] -> Context |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1818 |
make_context used = fold declare used context |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1819 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1820 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1821 |
{- generating fresh names -} |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1822 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1823 |
bump_init :: Name -> Name |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1824 |
bump_init str = str <> "a" |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1825 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1826 |
bump_string :: Name -> Name |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1827 |
bump_string str = |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1828 |
let |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1829 |
a = Bytes.byte 'a' |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1830 |
z = Bytes.byte 'z' |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1831 |
bump (b : bs) | b == z = a : bump bs |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1832 |
bump (b : bs) | a <= b && b < z = b + 1 : bs |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1833 |
bump bs = a : bs |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1834 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1835 |
rev = reverse (Bytes.unpack str) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1836 |
part2 = reverse (takeWhile (Symbol.is_ascii_quasi . Bytes.char) rev) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1837 |
part1 = reverse (bump (drop (length part2) rev)) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1838 |
in Bytes.pack (part1 <> part2) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1839 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1840 |
variant :: Name -> Context -> (Name, Context) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1841 |
variant name names = |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1842 |
let |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1843 |
vary bump x = if is_declared names x then bump x |> vary bump_string else x |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1844 |
(x, n) = clean_index name |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1845 |
x' = x |> vary bump_init |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1846 |
names' = declare x' names; |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1847 |
in (x' <> Bytes.pack (replicate n underscore), names') |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1848 |
\<close> |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1849 |
|
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
1850 |
generate_file "Isabelle/Term.hs" = \<open> |
69445 | 1851 |
{- Title: Isabelle/Term.hs |
69240 | 1852 |
Author: Makarius |
1853 |
LICENSE: BSD 3-clause (Isabelle) |
|
1854 |
||
1855 |
Lambda terms, types, sorts. |
|
69280 | 1856 |
|
1857 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/term.scala\<close>. |
|
69240 | 1858 |
-} |
1859 |
||
74095 | 1860 |
{-# LANGUAGE OverloadedStrings #-} |
1861 |
||
69240 | 1862 |
module Isabelle.Term ( |
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1863 |
Indexname, Sort, Typ(..), Term(..), |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1864 |
Free, lambda, declare_frees, incr_boundvars, subst_bound, dest_abs, |
74105 | 1865 |
type_op0, type_op1, op0, op1, op2, typed_op2, binder, |
1866 |
dummyS, dummyT, is_dummyT, propT, is_propT, (-->), dest_funT, (--->), |
|
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1867 |
aconv, list_comb, strip_comb, head_of |
74105 | 1868 |
) |
69240 | 1869 |
where |
1870 |
||
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1871 |
import Isabelle.Library |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1872 |
import qualified Isabelle.Name as Name |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1873 |
import Isabelle.Name (Name) |
74095 | 1874 |
|
74105 | 1875 |
infixr 5 --> |
1876 |
infixr ---> |
|
1877 |
||
1878 |
||
1879 |
{- types and terms -} |
|
1880 |
||
1881 |
type Indexname = (Name, Int) |
|
1882 |
||
1883 |
type Sort = [Name] |
|
1884 |
||
1885 |
data Typ = |
|
1886 |
Type (Name, [Typ]) |
|
1887 |
| TFree (Name, Sort) |
|
1888 |
| TVar (Indexname, Sort) |
|
1889 |
deriving (Show, Eq, Ord) |
|
1890 |
||
1891 |
data Term = |
|
1892 |
Const (Name, [Typ]) |
|
1893 |
| Free (Name, Typ) |
|
1894 |
| Var (Indexname, Typ) |
|
1895 |
| Bound Int |
|
1896 |
| Abs (Name, Typ, Term) |
|
1897 |
| App (Term, Term) |
|
1898 |
deriving (Show, Eq, Ord) |
|
1899 |
||
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1900 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1901 |
{- free and bound variables -} |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1902 |
|
74105 | 1903 |
type Free = (Name, Typ) |
1904 |
||
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1905 |
lambda :: Free -> Term -> Term |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1906 |
lambda (name, typ) body = Abs (name, typ, abstract 0 body) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1907 |
where |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1908 |
abstract lev (Free (x, ty)) | name == x && typ == ty = Bound lev |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1909 |
abstract lev (Abs (a, ty, t)) = Abs (a, ty, abstract (lev + 1) t) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1910 |
abstract lev (App (t, u)) = App (abstract lev t, abstract lev u) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1911 |
abstract _ t = t |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1912 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1913 |
declare_frees :: Term -> Name.Context -> Name.Context |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1914 |
declare_frees (Free (x, _)) = Name.declare x |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1915 |
declare_frees (Abs (_, _, b)) = declare_frees b |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1916 |
declare_frees (App (t, u)) = declare_frees t #> declare_frees u |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1917 |
declare_frees _ = id |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1918 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1919 |
incr_boundvars :: Int -> Term -> Term |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1920 |
incr_boundvars inc = if inc == 0 then id else incr 0 |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1921 |
where |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1922 |
incr lev (Bound i) = if i >= lev then Bound (i + inc) else Bound i |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1923 |
incr lev (Abs (a, ty, b)) = Abs (a, ty, incr (lev + 1) b) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1924 |
incr lev (App (t, u)) = App (incr lev t, incr lev u) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1925 |
incr _ t = t |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1926 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1927 |
subst_bound :: Term -> Term -> Term |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1928 |
subst_bound arg = subst 0 |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1929 |
where |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1930 |
subst lev (Bound i) = |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1931 |
if i < lev then Bound i |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1932 |
else if i == lev then incr_boundvars lev arg |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1933 |
else Bound (i - 1) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1934 |
subst lev (Abs (a, ty, b)) = Abs (a, ty, subst (lev + 1) b) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1935 |
subst lev (App (t, u)) = App (subst lev t, subst lev u) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1936 |
subst _ t = t |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1937 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1938 |
dest_abs :: Name.Context -> Term -> Maybe (Free, Term) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1939 |
dest_abs names (Abs (x, ty, b)) = |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1940 |
let |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1941 |
(x', _) = Name.variant x (declare_frees b names) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1942 |
v = (x', ty) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1943 |
in Just (v, subst_bound (Free v) b) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1944 |
dest_abs _ _ = Nothing |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1945 |
|
74105 | 1946 |
|
1947 |
{- type and term operators -} |
|
1948 |
||
1949 |
type_op0 :: Name -> (Typ, Typ -> Bool) |
|
1950 |
type_op0 name = (mk, is) |
|
1951 |
where |
|
1952 |
mk = Type (name, []) |
|
1953 |
is (Type (name, _)) = True |
|
1954 |
is _ = False |
|
1955 |
||
1956 |
type_op1 :: Name -> (Typ -> Typ, Typ -> Maybe Typ) |
|
1957 |
type_op1 name = (mk, dest) |
|
1958 |
where |
|
1959 |
mk ty = Type (name, [ty]) |
|
1960 |
dest (Type (name, [ty])) = Just ty |
|
1961 |
dest _ = Nothing |
|
1962 |
||
1963 |
type_op2 :: Name -> (Typ -> Typ -> Typ, Typ -> Maybe (Typ, Typ)) |
|
1964 |
type_op2 name = (mk, dest) |
|
1965 |
where |
|
1966 |
mk ty1 ty2 = Type (name, [ty1, ty2]) |
|
1967 |
dest (Type (name, [ty1, ty2])) = Just (ty1, ty2) |
|
1968 |
dest _ = Nothing |
|
1969 |
||
1970 |
op0 :: Name -> (Term, Term -> Bool) |
|
1971 |
op0 name = (mk, is) |
|
1972 |
where |
|
1973 |
mk = Const (name, []) |
|
1974 |
is (Const (c, _)) = c == name |
|
1975 |
is _ = False |
|
1976 |
||
1977 |
op1 :: Name -> (Term -> Term, Term -> Maybe Term) |
|
1978 |
op1 name = (mk, dest) |
|
1979 |
where |
|
1980 |
mk t = App (Const (name, []), t) |
|
1981 |
dest (App (Const (c, _), t)) | c == name = Just t |
|
1982 |
dest _ = Nothing |
|
1983 |
||
1984 |
op2 :: Name -> (Term -> Term -> Term, Term -> Maybe (Term, Term)) |
|
1985 |
op2 name = (mk, dest) |
|
1986 |
where |
|
1987 |
mk t u = App (App (Const (name, []), t), u) |
|
1988 |
dest (App (App (Const (c, _), t), u)) | c == name = Just (t, u) |
|
1989 |
dest _ = Nothing |
|
1990 |
||
1991 |
typed_op2 :: Name -> (Typ -> Term -> Term -> Term, Term -> Maybe (Typ, Term, Term)) |
|
1992 |
typed_op2 name = (mk, dest) |
|
1993 |
where |
|
1994 |
mk ty t u = App (App (Const (name, [ty]), t), u) |
|
1995 |
dest (App (App (Const (c, [ty]), t), u)) | c == name = Just (ty, t, u) |
|
1996 |
dest _ = Nothing |
|
1997 |
||
1998 |
binder :: Name -> Free -> Term -> Term |
|
1999 |
binder c (a, ty) b = App (Const (c, [ty]), lambda (a, ty) b) |
|
2000 |
||
2001 |
||
2002 |
{- type operations -} |
|
69240 | 2003 |
|
2004 |
dummyS :: Sort |
|
2005 |
dummyS = [""] |
|
2006 |
||
74105 | 2007 |
dummyT :: Typ; is_dummyT :: Typ -> Bool |
2008 |
(dummyT, is_dummyT) = type_op0 \<open>\<^type_name>\<open>dummy\<close>\<close> |
|
2009 |
||
2010 |
propT :: Typ; is_propT :: Typ -> Bool |
|
2011 |
(propT, is_propT) = type_op0 \<open>\<^type_name>\<open>prop\<close>\<close> |
|
2012 |
||
2013 |
(-->) :: Typ -> Typ -> Typ; dest_funT :: Typ -> Maybe (Typ, Typ) |
|
2014 |
((-->), dest_funT) = type_op2 \<open>\<^type_name>\<open>fun\<close>\<close> |
|
2015 |
||
2016 |
(--->) :: [Typ] -> Typ -> Typ |
|
2017 |
[] ---> b = b |
|
2018 |
(a : as) ---> b = a --> (as ---> b) |
|
2019 |
||
2020 |
||
2021 |
{- term operations -} |
|
74100 | 2022 |
|
2023 |
aconv :: Term -> Term -> Bool |
|
2024 |
aconv (App (t1, u1)) (App (t2, u2)) = aconv t1 t2 && aconv u1 u2 |
|
2025 |
aconv (Abs (_, ty1, t1)) (Abs (_, ty2, t2)) = aconv t1 t2 && ty1 == ty2 |
|
2026 |
aconv a1 a2 = a1 == a2 |
|
2027 |
||
2028 |
list_comb :: Term -> [Term] -> Term |
|
2029 |
list_comb f [] = f |
|
2030 |
list_comb f (t : ts) = list_comb (App (f, t)) ts |
|
2031 |
||
2032 |
strip_comb :: Term -> (Term, [Term]) |
|
2033 |
strip_comb tm = strip (tm, []) |
|
2034 |
where |
|
2035 |
strip (App (f, t), ts) = strip (f, t : ts) |
|
2036 |
strip x = x |
|
2037 |
||
2038 |
head_of :: Term -> Term |
|
2039 |
head_of (App (f, _)) = head_of f |
|
2040 |
head_of u = u |
|
69240 | 2041 |
\<close> |
2042 |
||
74105 | 2043 |
generate_file "Isabelle/Pure.hs" = \<open> |
2044 |
{- Title: Isabelle/Term.hs |
|
2045 |
Author: Makarius |
|
2046 |
LICENSE: BSD 3-clause (Isabelle) |
|
2047 |
||
2048 |
Support for Isabelle/Pure logic. |
|
2049 |
||
2050 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/logic.ML\<close>. |
|
2051 |
-} |
|
2052 |
||
2053 |
{-# LANGUAGE OverloadedStrings #-} |
|
2054 |
||
2055 |
module Isabelle.Pure ( |
|
2056 |
mk_forall, mk_equals, dest_equals, mk_implies, dest_implies |
|
2057 |
) |
|
2058 |
where |
|
2059 |
||
2060 |
import Isabelle.Term |
|
2061 |
||
2062 |
mk_forall :: Free -> Term -> Term |
|
2063 |
mk_forall = binder \<open>\<^const_name>\<open>Pure.all\<close>\<close> |
|
2064 |
||
2065 |
mk_equals :: Typ -> Term -> Term -> Term; dest_equals :: Term -> Maybe (Typ, Term, Term) |
|
2066 |
(mk_equals, dest_equals) = typed_op2 \<open>\<^const_name>\<open>Pure.eq\<close>\<close> |
|
2067 |
||
2068 |
mk_implies :: Term -> Term -> Term; dest_implies :: Term -> Maybe (Term, Term) |
|
2069 |
(mk_implies, dest_implies) = op2 \<open>\<^const_name>\<open>Pure.imp\<close>\<close> |
|
2070 |
\<close> |
|
2071 |
||
2072 |
generate_file "Isabelle/HOL.hs" = \<open> |
|
2073 |
{- Title: Isabelle/Term.hs |
|
2074 |
Author: Makarius |
|
2075 |
LICENSE: BSD 3-clause (Isabelle) |
|
2076 |
||
2077 |
Support for Isabelle/HOL logic. |
|
2078 |
||
2079 |
See also \<^file>\<open>$ISABELLE_HOME/src/HOL/Tools/hologic.ML\<close>. |
|
2080 |
-} |
|
2081 |
||
2082 |
{-# LANGUAGE OverloadedStrings #-} |
|
2083 |
||
2084 |
module Isabelle.HOL ( |
|
2085 |
boolT, is_boolT, mk_trueprop, dest_trueprop, |
|
2086 |
mk_setT, dest_setT, mk_mem, dest_mem, |
|
2087 |
mk_eq, dest_eq, true, is_true, false, is_false, |
|
2088 |
mk_not, dest_not, mk_conj, dest_conj, mk_disj, dest_disj, |
|
2089 |
mk_imp, dest_imp, mk_iff, dest_iff, |
|
2090 |
mk_all, mk_ex |
|
2091 |
) |
|
2092 |
where |
|
2093 |
||
2094 |
import Isabelle.Term |
|
2095 |
||
2096 |
||
2097 |
boolT :: Typ; is_boolT :: Typ -> Bool |
|
2098 |
(boolT, is_boolT) = type_op0 \<open>\<^type_name>\<open>bool\<close>\<close> |
|
2099 |
||
2100 |
mk_trueprop :: Term -> Term; dest_trueprop :: Term -> Maybe Term |
|
2101 |
(mk_trueprop, dest_trueprop) = op1 \<open>\<^const_name>\<open>Trueprop\<close>\<close> |
|
2102 |
||
2103 |
mk_setT :: Typ -> Typ; dest_setT :: Typ -> Maybe Typ |
|
2104 |
(mk_setT, dest_setT) = type_op1 \<open>\<^type_name>\<open>set\<close>\<close> |
|
2105 |
||
2106 |
mk_mem :: Typ -> Term -> Term -> Term; dest_mem :: Term -> Maybe (Typ, Term, Term) |
|
2107 |
(mk_mem, dest_mem) = typed_op2 \<open>\<^const_name>\<open>Set.member\<close>\<close> |
|
2108 |
||
2109 |
mk_eq :: Typ -> Term -> Term -> Term; dest_eq :: Term -> Maybe (Typ, Term, Term) |
|
2110 |
(mk_eq, dest_eq) = typed_op2 \<open>\<^const_name>\<open>HOL.eq\<close>\<close> |
|
2111 |
||
2112 |
true :: Term; is_true :: Term -> Bool |
|
2113 |
(true, is_true) = op0 \<open>\<^const_name>\<open>True\<close>\<close> |
|
2114 |
||
2115 |
false :: Term; is_false :: Term -> Bool |
|
2116 |
(false, is_false) = op0 \<open>\<^const_name>\<open>False\<close>\<close> |
|
2117 |
||
2118 |
mk_not :: Term -> Term; dest_not :: Term -> Maybe Term |
|
2119 |
(mk_not, dest_not) = op1 \<open>\<^const_name>\<open>Not\<close>\<close> |
|
2120 |
||
2121 |
mk_conj :: Term -> Term -> Term; dest_conj :: Term -> Maybe (Term, Term) |
|
2122 |
(mk_conj, dest_conj) = op2 \<open>\<^const_name>\<open>conj\<close>\<close> |
|
2123 |
||
2124 |
mk_disj :: Term -> Term -> Term; dest_disj :: Term -> Maybe (Term, Term) |
|
2125 |
(mk_disj, dest_disj) = op2 \<open>\<^const_name>\<open>disj\<close>\<close> |
|
2126 |
||
2127 |
mk_imp :: Term -> Term -> Term; dest_imp :: Term -> Maybe (Term, Term) |
|
2128 |
(mk_imp, dest_imp) = op2 \<open>\<^const_name>\<open>implies\<close>\<close> |
|
2129 |
||
2130 |
mk_iff :: Term -> Term -> Term |
|
2131 |
mk_iff = mk_eq boolT |
|
2132 |
||
2133 |
dest_iff :: Term -> Maybe (Term, Term) |
|
2134 |
dest_iff tm = |
|
2135 |
case dest_eq tm of |
|
2136 |
Just (ty, t, u) | ty == boolT -> Just (t, u) |
|
2137 |
_ -> Nothing |
|
2138 |
||
2139 |
mk_all :: Free -> Term -> Term |
|
2140 |
mk_all = binder \<open>\<^const_name>\<open>All\<close>\<close> |
|
2141 |
||
2142 |
mk_ex :: Free -> Term -> Term |
|
2143 |
mk_ex = binder \<open>\<^const_name>\<open>Ex\<close>\<close> |
|
2144 |
\<close> |
|
2145 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
2146 |
generate_file "Isabelle/Term_XML/Encode.hs" = \<open> |
69445 | 2147 |
{- Title: Isabelle/Term_XML/Encode.hs |
69240 | 2148 |
Author: Makarius |
2149 |
LICENSE: BSD 3-clause (Isabelle) |
|
2150 |
||
2151 |
XML data representation of lambda terms. |
|
69280 | 2152 |
|
2153 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/term_xml.ML\<close>. |
|
69240 | 2154 |
-} |
2155 |
||
2156 |
{-# LANGUAGE LambdaCase #-} |
|
2157 |
||
70845 | 2158 |
module Isabelle.Term_XML.Encode (indexname, sort, typ, typ_body, term) |
69240 | 2159 |
where |
2160 |
||
2161 |
import Isabelle.Library |
|
2162 |
import Isabelle.XML.Encode |
|
2163 |
import Isabelle.Term |
|
2164 |
||
70845 | 2165 |
indexname :: P Indexname |
2166 |
indexname (a, b) = if b == 0 then [a] else [a, int_atom b] |
|
69240 | 2167 |
|
2168 |
sort :: T Sort |
|
2169 |
sort = list string |
|
2170 |
||
2171 |
typ :: T Typ |
|
2172 |
typ ty = |
|
2173 |
ty |> variant |
|
2174 |
[\case { Type (a, b) -> Just ([a], list typ b); _ -> Nothing }, |
|
2175 |
\case { TFree (a, b) -> Just ([a], sort b); _ -> Nothing }, |
|
70845 | 2176 |
\case { TVar (a, b) -> Just (indexname a, sort b); _ -> Nothing }] |
2177 |
||
2178 |
typ_body :: T Typ |
|
2179 |
typ_body ty = if is_dummyT ty then [] else typ ty |
|
69240 | 2180 |
|
2181 |
term :: T Term |
|
2182 |
term t = |
|
2183 |
t |> variant |
|
70784
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents:
70667
diff
changeset
|
2184 |
[\case { Const (a, b) -> Just ([a], list typ b); _ -> Nothing }, |
70845 | 2185 |
\case { Free (a, b) -> Just ([a], typ_body b); _ -> Nothing }, |
2186 |
\case { Var (a, b) -> Just (indexname a, typ_body b); _ -> Nothing }, |
|
2187 |
\case { Bound a -> Just ([], int a); _ -> Nothing }, |
|
69240 | 2188 |
\case { Abs (a, b, c) -> Just ([a], pair typ term (b, c)); _ -> Nothing }, |
2189 |
\case { App a -> Just ([], pair term term a); _ -> Nothing }] |
|
2190 |
\<close> |
|
2191 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
2192 |
generate_file "Isabelle/Term_XML/Decode.hs" = \<open> |
69445 | 2193 |
{- Title: Isabelle/Term_XML/Decode.hs |
69240 | 2194 |
Author: Makarius |
2195 |
LICENSE: BSD 3-clause (Isabelle) |
|
2196 |
||
2197 |
XML data representation of lambda terms. |
|
69280 | 2198 |
|
2199 |
See also \<^file>\<open>$ISABELLE_HOME/src/Pure/term_xml.ML\<close>. |
|
69240 | 2200 |
-} |
2201 |
||
73177 | 2202 |
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-} |
2203 |
||
70845 | 2204 |
module Isabelle.Term_XML.Decode (indexname, sort, typ, typ_body, term) |
69240 | 2205 |
where |
2206 |
||
2207 |
import Isabelle.Library |
|
2208 |
import Isabelle.XML.Decode |
|
2209 |
import Isabelle.Term |
|
2210 |
||
2211 |
||
70845 | 2212 |
indexname :: P Indexname |
2213 |
indexname [a] = (a, 0) |
|
2214 |
indexname [a, b] = (a, int_atom b) |
|
2215 |
||
69240 | 2216 |
sort :: T Sort |
2217 |
sort = list string |
|
2218 |
||
2219 |
typ :: T Typ |
|
2220 |
typ ty = |
|
2221 |
ty |> variant |
|
2222 |
[\([a], b) -> Type (a, list typ b), |
|
2223 |
\([a], b) -> TFree (a, sort b), |
|
70845 | 2224 |
\(a, b) -> TVar (indexname a, sort b)] |
2225 |
||
2226 |
typ_body :: T Typ |
|
2227 |
typ_body [] = dummyT |
|
2228 |
typ_body body = typ body |
|
69240 | 2229 |
|
2230 |
term :: T Term |
|
2231 |
term t = |
|
2232 |
t |> variant |
|
70784
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents:
70667
diff
changeset
|
2233 |
[\([a], b) -> Const (a, list typ b), |
70845 | 2234 |
\([a], b) -> Free (a, typ_body b), |
2235 |
\(a, b) -> Var (indexname a, typ_body b), |
|
2236 |
\([], a) -> Bound (int a), |
|
69240 | 2237 |
\([a], b) -> let (c, d) = pair typ term b in Abs (a, c, d), |
2238 |
\([], a) -> App (pair term term a)] |
|
2239 |
\<close> |
|
2240 |
||
69459 | 2241 |
generate_file "Isabelle/UUID.hs" = \<open> |
2242 |
{- Title: Isabelle/UUID.hs |
|
2243 |
Author: Makarius |
|
2244 |
LICENSE: BSD 3-clause (Isabelle) |
|
2245 |
||
2246 |
Universally unique identifiers. |
|
2247 |
||
2248 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/General/uuid.scala\<close>. |
|
2249 |
-} |
|
2250 |
||
74095 | 2251 |
module Isabelle.UUID (T, random, print, parse) |
69459 | 2252 |
where |
2253 |
||
74095 | 2254 |
import Prelude hiding (print) |
2255 |
||
69459 | 2256 |
import Data.UUID (UUID) |
2257 |
import qualified Data.UUID as UUID |
|
2258 |
import Data.UUID.V4 (nextRandom) |
|
2259 |
||
74084 | 2260 |
import qualified Isabelle.Bytes as Bytes |
2261 |
import Isabelle.Bytes (Bytes) |
|
2262 |
||
69459 | 2263 |
|
69462 | 2264 |
type T = UUID |
2265 |
||
2266 |
random :: IO T |
|
69459 | 2267 |
random = nextRandom |
2268 |
||
74095 | 2269 |
print :: T -> Bytes |
2270 |
print = Bytes.make . UUID.toASCIIBytes |
|
2271 |
||
2272 |
parse :: Bytes -> Maybe T |
|
2273 |
parse = UUID.fromASCIIBytes . Bytes.unmake |
|
69459 | 2274 |
\<close> |
2275 |
||
69448 | 2276 |
generate_file "Isabelle/Byte_Message.hs" = \<open> |
2277 |
{- Title: Isabelle/Byte_Message.hs |
|
69446 | 2278 |
Author: Makarius |
2279 |
LICENSE: BSD 3-clause (Isabelle) |
|
2280 |
||
69448 | 2281 |
Byte-oriented messages. |
2282 |
||
2283 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/byte_message.ML\<close> |
|
2284 |
and \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/byte_message.scala\<close>. |
|
69446 | 2285 |
-} |
2286 |
||
74083 | 2287 |
{-# LANGUAGE OverloadedStrings #-} |
73177 | 2288 |
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-} |
2289 |
||
69452 | 2290 |
module Isabelle.Byte_Message ( |
69467 | 2291 |
write, write_line, |
74088 | 2292 |
read, read_block, read_line, |
69476 | 2293 |
make_message, write_message, read_message, |
73178 | 2294 |
make_line_message, write_line_message, read_line_message, |
2295 |
read_yxml, write_yxml |
|
69452 | 2296 |
) |
69446 | 2297 |
where |
2298 |
||
69449 | 2299 |
import Prelude hiding (read) |
69454 | 2300 |
import Data.Maybe |
69446 | 2301 |
import qualified Data.ByteString as ByteString |
74084 | 2302 |
import qualified Isabelle.Bytes as Bytes |
2303 |
import Isabelle.Bytes (Bytes) |
|
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
2304 |
import qualified Isabelle.UTF8 as UTF8 |
73178 | 2305 |
import qualified Isabelle.XML as XML |
2306 |
import qualified Isabelle.YXML as YXML |
|
73177 | 2307 |
|
69446 | 2308 |
import Network.Socket (Socket) |
74082 | 2309 |
import qualified Network.Socket.ByteString as Socket |
69446 | 2310 |
|
74088 | 2311 |
import Isabelle.Library |
69446 | 2312 |
import qualified Isabelle.Value as Value |
2313 |
||
2314 |
||
69452 | 2315 |
{- output operations -} |
2316 |
||
74084 | 2317 |
write :: Socket -> [Bytes] -> IO () |
2318 |
write socket = Socket.sendMany socket . map Bytes.unmake |
|
2319 |
||
2320 |
write_line :: Socket -> Bytes -> IO () |
|
74083 | 2321 |
write_line socket s = write socket [s, "\n"] |
69467 | 2322 |
|
69452 | 2323 |
|
2324 |
{- input operations -} |
|
2325 |
||
74084 | 2326 |
read :: Socket -> Int -> IO Bytes |
69452 | 2327 |
read socket n = read_body 0 [] |
69449 | 2328 |
where |
74084 | 2329 |
result = Bytes.concat . reverse |
69452 | 2330 |
read_body len ss = |
69449 | 2331 |
if len >= n then return (result ss) |
2332 |
else |
|
2333 |
(do |
|
74082 | 2334 |
s <- Socket.recv socket (min (n - len) 8192) |
69449 | 2335 |
case ByteString.length s of |
2336 |
0 -> return (result ss) |
|
74084 | 2337 |
m -> read_body (len + m) (Bytes.make s : ss)) |
2338 |
||
2339 |
read_block :: Socket -> Int -> IO (Maybe Bytes, Int) |
|
69452 | 2340 |
read_block socket n = do |
2341 |
msg <- read socket n |
|
74084 | 2342 |
let len = Bytes.length msg |
69452 | 2343 |
return (if len == n then Just msg else Nothing, len) |
69449 | 2344 |
|
74084 | 2345 |
read_line :: Socket -> IO (Maybe Bytes) |
69452 | 2346 |
read_line socket = read_body [] |
69446 | 2347 |
where |
74088 | 2348 |
result = Bytes.trim_line . Bytes.pack . reverse |
69452 | 2349 |
read_body bs = do |
74082 | 2350 |
s <- Socket.recv socket 1 |
69446 | 2351 |
case ByteString.length s of |
2352 |
0 -> return (if null bs then Nothing else Just (result bs)) |
|
2353 |
1 -> |
|
2354 |
case ByteString.head s of |
|
2355 |
10 -> return (Just (result bs)) |
|
69452 | 2356 |
b -> read_body (b : bs) |
69446 | 2357 |
|
69448 | 2358 |
|
69454 | 2359 |
{- messages with multiple chunks (arbitrary content) -} |
2360 |
||
74084 | 2361 |
make_header :: [Int] -> [Bytes] |
74095 | 2362 |
make_header ns = [space_implode "," (map Value.print_int ns), "\n"] |
69454 | 2363 |
|
74084 | 2364 |
make_message :: [Bytes] -> [Bytes] |
2365 |
make_message chunks = make_header (map Bytes.length chunks) <> chunks |
|
2366 |
||
2367 |
write_message :: Socket -> [Bytes] -> IO () |
|
69476 | 2368 |
write_message socket = write socket . make_message |
69454 | 2369 |
|
74084 | 2370 |
parse_header :: Bytes -> [Int] |
69454 | 2371 |
parse_header line = |
2372 |
let |
|
74095 | 2373 |
res = map Value.parse_nat (Bytes.space_explode (Bytes.byte ',') line) |
69454 | 2374 |
in |
2375 |
if all isJust res then map fromJust res |
|
74081 | 2376 |
else error ("Malformed message header: " <> quote (UTF8.decode line)) |
69454 | 2377 |
|
74084 | 2378 |
read_chunk :: Socket -> Int -> IO Bytes |
69454 | 2379 |
read_chunk socket n = do |
2380 |
res <- read_block socket n |
|
2381 |
return $ |
|
2382 |
case res of |
|
2383 |
(Just chunk, _) -> chunk |
|
2384 |
(Nothing, len) -> |
|
74081 | 2385 |
error ("Malformed message chunk: unexpected EOF after " <> |
2386 |
show len <> " of " <> show n <> " bytes") |
|
69454 | 2387 |
|
74084 | 2388 |
read_message :: Socket -> IO (Maybe [Bytes]) |
69454 | 2389 |
read_message socket = do |
2390 |
res <- read_line socket |
|
2391 |
case res of |
|
2392 |
Just line -> Just <$> mapM (read_chunk socket) (parse_header line) |
|
2393 |
Nothing -> return Nothing |
|
2394 |
||
2395 |
||
69448 | 2396 |
-- hybrid messages: line or length+block (with content restriction) |
2397 |
||
74084 | 2398 |
is_length :: Bytes -> Bool |
69452 | 2399 |
is_length msg = |
74084 | 2400 |
not (Bytes.null msg) && Bytes.all (\b -> 48 <= b && b <= 57) msg |
2401 |
||
2402 |
is_terminated :: Bytes -> Bool |
|
69452 | 2403 |
is_terminated msg = |
74084 | 2404 |
not (Bytes.null msg) && (Bytes.last msg == 13 || Bytes.last msg == 10) |
2405 |
||
2406 |
make_line_message :: Bytes -> [Bytes] |
|
69476 | 2407 |
make_line_message msg = |
74084 | 2408 |
let n = Bytes.length msg in |
69476 | 2409 |
if is_length msg || is_terminated msg then |
74081 | 2410 |
error ("Bad content for line message:\n" <> take 100 (UTF8.decode msg)) |
69476 | 2411 |
else |
74084 | 2412 |
(if n > 100 || Bytes.any (== 10) msg then make_header [n + 1] else []) <> [msg, "\n"] |
2413 |
||
2414 |
write_line_message :: Socket -> Bytes -> IO () |
|
69476 | 2415 |
write_line_message socket = write socket . make_line_message |
69448 | 2416 |
|
74084 | 2417 |
read_line_message :: Socket -> IO (Maybe Bytes) |
69448 | 2418 |
read_line_message socket = do |
69446 | 2419 |
opt_line <- read_line socket |
2420 |
case opt_line of |
|
2421 |
Nothing -> return Nothing |
|
2422 |
Just line -> |
|
74095 | 2423 |
case Value.parse_nat line of |
69446 | 2424 |
Nothing -> return $ Just line |
74088 | 2425 |
Just n -> fmap Bytes.trim_line . fst <$> read_block socket n |
73178 | 2426 |
|
2427 |
||
2428 |
read_yxml :: Socket -> IO (Maybe XML.Body) |
|
2429 |
read_yxml socket = do |
|
2430 |
res <- read_line_message socket |
|
74095 | 2431 |
return (YXML.parse_body <$> res) |
73178 | 2432 |
|
2433 |
write_yxml :: Socket -> XML.Body -> IO () |
|
2434 |
write_yxml socket body = |
|
74095 | 2435 |
write_line_message socket (YXML.string_of_body body) |
69446 | 2436 |
\<close> |
2437 |
||
71692 | 2438 |
generate_file "Isabelle/Isabelle_Thread.hs" = \<open> |
2439 |
{- Title: Isabelle/Isabelle_Thread.hs |
|
69473 | 2440 |
Author: Makarius |
2441 |
LICENSE: BSD 3-clause (Isabelle) |
|
2442 |
||
71692 | 2443 |
Isabelle-specific thread management. |
2444 |
||
2445 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/Concurrent/isabelle_thread.ML\<close> |
|
2446 |
and \<^file>\<open>$ISABELLE_HOME/src/Pure/Concurrent/isabelle_thread.scala\<close>. |
|
69473 | 2447 |
-} |
2448 |
||
2449 |
{-# LANGUAGE NamedFieldPuns #-} |
|
2450 |
||
71692 | 2451 |
module Isabelle.Isabelle_Thread ( |
69473 | 2452 |
ThreadId, Result, |
69494 | 2453 |
find_id, |
69473 | 2454 |
properties, change_properties, |
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2455 |
add_resource, del_resource, bracket_resource, |
69497 | 2456 |
is_stopped, expose_stopped, stop, |
69496 | 2457 |
my_uuid, stop_uuid, |
69494 | 2458 |
Fork, fork_finally, fork) |
69473 | 2459 |
where |
2460 |
||
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2461 |
import Data.Unique |
69473 | 2462 |
import Data.IORef |
2463 |
import System.IO.Unsafe |
|
2464 |
||
69494 | 2465 |
import qualified Data.List as List |
69497 | 2466 |
import Control.Monad (when, forM_) |
69473 | 2467 |
import Data.Map.Strict (Map) |
2468 |
import qualified Data.Map.Strict as Map |
|
2469 |
import Control.Exception as Exception |
|
2470 |
import Control.Concurrent (ThreadId) |
|
2471 |
import qualified Control.Concurrent as Concurrent |
|
2472 |
import Control.Concurrent.Thread (Result) |
|
2473 |
import qualified Control.Concurrent.Thread as Thread |
|
69494 | 2474 |
import qualified Isabelle.UUID as UUID |
69473 | 2475 |
import qualified Isabelle.Properties as Properties |
2476 |
||
2477 |
||
2478 |
{- thread info -} |
|
2479 |
||
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2480 |
type Resources = Map Unique (IO ()) |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2481 |
data Info = Info {uuid :: UUID.T, props :: Properties.T, stopped :: Bool, resources :: Resources} |
69473 | 2482 |
type Infos = Map ThreadId Info |
2483 |
||
2484 |
lookup_info :: Infos -> ThreadId -> Maybe Info |
|
69494 | 2485 |
lookup_info infos id = Map.lookup id infos |
2486 |
||
2487 |
init_info :: ThreadId -> UUID.T -> Infos -> (Infos, ()) |
|
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2488 |
init_info id uuid infos = (Map.insert id (Info uuid [] False Map.empty) infos, ()) |
69473 | 2489 |
|
2490 |
||
2491 |
{- global state -} |
|
2492 |
||
2493 |
{-# NOINLINE global_state #-} |
|
2494 |
global_state :: IORef Infos |
|
2495 |
global_state = unsafePerformIO (newIORef Map.empty) |
|
2496 |
||
69494 | 2497 |
find_id :: UUID.T -> IO (Maybe ThreadId) |
2498 |
find_id uuid = do |
|
2499 |
state <- readIORef global_state |
|
2500 |
return $ fst <$> List.find (\(_, Info{uuid = uuid'}) -> uuid == uuid') (Map.assocs state) |
|
2501 |
||
69473 | 2502 |
get_info :: ThreadId -> IO (Maybe Info) |
2503 |
get_info id = do |
|
2504 |
state <- readIORef global_state |
|
2505 |
return $ lookup_info state id |
|
2506 |
||
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2507 |
map_info :: ThreadId -> (Info -> Info) -> IO (Maybe Info) |
69473 | 2508 |
map_info id f = |
2509 |
atomicModifyIORef' global_state |
|
2510 |
(\infos -> |
|
2511 |
case lookup_info infos id of |
|
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2512 |
Nothing -> (infos, Nothing) |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2513 |
Just info -> |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2514 |
let info' = f info |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2515 |
in (Map.insert id info' infos, Just info')) |
69473 | 2516 |
|
69495 | 2517 |
delete_info :: ThreadId -> IO () |
2518 |
delete_info id = |
|
2519 |
atomicModifyIORef' global_state (\infos -> (Map.delete id infos, ())) |
|
2520 |
||
69473 | 2521 |
|
2522 |
{- thread properties -} |
|
2523 |
||
69498
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
2524 |
my_info :: IO (Maybe Info) |
69473 | 2525 |
my_info = do |
2526 |
id <- Concurrent.myThreadId |
|
69498
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
2527 |
get_info id |
69473 | 2528 |
|
2529 |
properties :: IO Properties.T |
|
69498
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
2530 |
properties = maybe [] props <$> my_info |
69473 | 2531 |
|
2532 |
change_properties :: (Properties.T -> Properties.T) -> IO () |
|
2533 |
change_properties f = do |
|
2534 |
id <- Concurrent.myThreadId |
|
2535 |
map_info id (\info -> info {props = f (props info)}) |
|
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2536 |
return () |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2537 |
|
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2538 |
|
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2539 |
{- managed resources -} |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2540 |
|
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2541 |
add_resource :: IO () -> IO Unique |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2542 |
add_resource resource = do |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2543 |
id <- Concurrent.myThreadId |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2544 |
u <- newUnique |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2545 |
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
|
2546 |
return u |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2547 |
|
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2548 |
del_resource :: Unique -> IO () |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2549 |
del_resource u = do |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2550 |
id <- Concurrent.myThreadId |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2551 |
map_info id (\info -> info {resources = Map.delete u (resources info)}) |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2552 |
return () |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2553 |
|
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2554 |
bracket_resource :: IO () -> IO a -> IO a |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2555 |
bracket_resource resource body = |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2556 |
Exception.bracket (add_resource resource) del_resource (const body) |
69473 | 2557 |
|
2558 |
||
2559 |
{- stop -} |
|
2560 |
||
2561 |
is_stopped :: IO Bool |
|
69498
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
2562 |
is_stopped = maybe False stopped <$> my_info |
69473 | 2563 |
|
69497 | 2564 |
expose_stopped :: IO () |
2565 |
expose_stopped = do |
|
2566 |
stopped <- is_stopped |
|
2567 |
when stopped $ throw ThreadKilled |
|
2568 |
||
69473 | 2569 |
stop :: ThreadId -> IO () |
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2570 |
stop id = do |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2571 |
info <- map_info id (\info -> info {stopped = True}) |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
2572 |
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
|
2573 |
sequence_ ops |
69473 | 2574 |
|
2575 |
||
69496 | 2576 |
{- UUID -} |
2577 |
||
69498
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
2578 |
my_uuid :: IO (Maybe UUID.T) |
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
2579 |
my_uuid = fmap uuid <$> my_info |
69496 | 2580 |
|
2581 |
stop_uuid :: UUID.T -> IO () |
|
2582 |
stop_uuid uuid = do |
|
2583 |
id <- find_id uuid |
|
2584 |
forM_ id stop |
|
2585 |
||
2586 |
||
69473 | 2587 |
{- fork -} |
2588 |
||
69494 | 2589 |
type Fork a = (ThreadId, UUID.T, IO (Result a)) |
2590 |
||
2591 |
fork_finally :: IO a -> (Either SomeException a -> IO b) -> IO (Fork b) |
|
69473 | 2592 |
fork_finally body finally = do |
69494 | 2593 |
uuid <- UUID.random |
69473 | 2594 |
(id, result) <- |
2595 |
Exception.mask (\restore -> |
|
2596 |
Thread.forkIO |
|
2597 |
(Exception.try |
|
2598 |
(do |
|
2599 |
id <- Concurrent.myThreadId |
|
69494 | 2600 |
atomicModifyIORef' global_state (init_info id uuid) |
69495 | 2601 |
restore body) |
2602 |
>>= (\res -> do id <- Concurrent.myThreadId; delete_info id; finally res))) |
|
69494 | 2603 |
return (id, uuid, result) |
2604 |
||
2605 |
fork :: IO a -> IO (Fork a) |
|
69473 | 2606 |
fork body = fork_finally body Thread.result |
2607 |
\<close> |
|
2608 |
||
69455 | 2609 |
generate_file "Isabelle/Server.hs" = \<open> |
2610 |
{- Title: Isabelle/Server.hs |
|
2611 |
Author: Makarius |
|
2612 |
LICENSE: BSD 3-clause (Isabelle) |
|
2613 |
||
2614 |
TCP server on localhost. |
|
2615 |
-} |
|
2616 |
||
69465 | 2617 |
module Isabelle.Server ( |
2618 |
localhost_name, localhost, publish_text, publish_stdout, |
|
73178 | 2619 |
server, connection |
69465 | 2620 |
) |
2621 |
where |
|
69455 | 2622 |
|
69465 | 2623 |
import Control.Monad (forever, when) |
69455 | 2624 |
import qualified Control.Exception as Exception |
2625 |
import Network.Socket (Socket) |
|
2626 |
import qualified Network.Socket as Socket |
|
69472 | 2627 |
import qualified System.IO as IO |
69455 | 2628 |
|
69462 | 2629 |
import Isabelle.Library |
74084 | 2630 |
import Isabelle.Bytes (Bytes) |
69462 | 2631 |
import qualified Isabelle.UUID as UUID |
69465 | 2632 |
import qualified Isabelle.Byte_Message as Byte_Message |
71692 | 2633 |
import qualified Isabelle.Isabelle_Thread as Isabelle_Thread |
69462 | 2634 |
|
2635 |
||
2636 |
{- server address -} |
|
69455 | 2637 |
|
69463 | 2638 |
localhost_name :: String |
2639 |
localhost_name = "127.0.0.1" |
|
2640 |
||
69455 | 2641 |
localhost :: Socket.HostAddress |
2642 |
localhost = Socket.tupleToHostAddress (127, 0, 0, 1) |
|
2643 |
||
69462 | 2644 |
publish_text :: String -> String -> UUID.T -> String |
2645 |
publish_text name address password = |
|
74081 | 2646 |
"server " <> quote name <> " = " <> address <> " (password " <> quote (show password) <> ")" |
69462 | 2647 |
|
2648 |
publish_stdout :: String -> String -> UUID.T -> IO () |
|
74095 | 2649 |
publish_stdout name address password = |
2650 |
putStrLn (publish_text name address password) |
|
69462 | 2651 |
|
2652 |
||
2653 |
{- server -} |
|
2654 |
||
2655 |
server :: (String -> UUID.T -> IO ()) -> (Socket -> IO ()) -> IO () |
|
69455 | 2656 |
server publish handle = |
69462 | 2657 |
Socket.withSocketsDo $ Exception.bracket open (Socket.close . fst) (uncurry loop) |
69455 | 2658 |
where |
74084 | 2659 |
open :: IO (Socket, Bytes) |
69455 | 2660 |
open = do |
69466 | 2661 |
server_socket <- Socket.socket Socket.AF_INET Socket.Stream Socket.defaultProtocol |
2662 |
Socket.bind server_socket (Socket.SockAddrInet 0 localhost) |
|
2663 |
Socket.listen server_socket 50 |
|
69462 | 2664 |
|
69466 | 2665 |
port <- Socket.socketPort server_socket |
74081 | 2666 |
let address = localhost_name <> ":" <> show port |
69462 | 2667 |
password <- UUID.random |
2668 |
publish address password |
|
69455 | 2669 |
|
74095 | 2670 |
return (server_socket, UUID.print password) |
69462 | 2671 |
|
74084 | 2672 |
loop :: Socket -> Bytes -> IO () |
69466 | 2673 |
loop server_socket password = forever $ do |
69480 | 2674 |
(connection, _) <- Socket.accept server_socket |
71692 | 2675 |
Isabelle_Thread.fork_finally |
69465 | 2676 |
(do |
2677 |
line <- Byte_Message.read_line connection |
|
2678 |
when (line == Just password) $ handle connection) |
|
69473 | 2679 |
(\finally -> do |
69472 | 2680 |
Socket.close connection |
69473 | 2681 |
case finally of |
69472 | 2682 |
Left exn -> IO.hPutStrLn IO.stderr $ Exception.displayException exn |
2683 |
Right () -> return ()) |
|
69455 | 2684 |
return () |
73178 | 2685 |
|
2686 |
||
2687 |
{- client connection -} |
|
2688 |
||
74095 | 2689 |
connection :: String -> Bytes -> (Socket -> IO a) -> IO a |
73178 | 2690 |
connection port password client = |
2691 |
Socket.withSocketsDo $ do |
|
2692 |
addr <- resolve |
|
2693 |
Exception.bracket (open addr) Socket.close body |
|
2694 |
where |
|
2695 |
resolve = do |
|
2696 |
let hints = |
|
2697 |
Socket.defaultHints { |
|
2698 |
Socket.addrFlags = [Socket.AI_NUMERICHOST, Socket.AI_NUMERICSERV], |
|
2699 |
Socket.addrSocketType = Socket.Stream } |
|
2700 |
head <$> Socket.getAddrInfo (Just hints) (Just "127.0.0.1") (Just port) |
|
2701 |
||
2702 |
open addr = do |
|
2703 |
socket <- Socket.socket (Socket.addrFamily addr) (Socket.addrSocketType addr) |
|
2704 |
(Socket.addrProtocol addr) |
|
2705 |
Socket.connect socket $ Socket.addrAddress addr |
|
2706 |
return socket |
|
2707 |
||
2708 |
body socket = do |
|
74095 | 2709 |
Byte_Message.write_line socket password |
73178 | 2710 |
client socket |
69455 | 2711 |
\<close> |
2712 |
||
70047
96fe857a7a6f
clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents:
69968
diff
changeset
|
2713 |
export_generated_files _ |
69628 | 2714 |
|
69222 | 2715 |
end |