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