author | wenzelm |
Fri, 20 Sep 2024 14:28:13 +0200 | |
changeset 80910 | 406a85a25189 |
parent 80601 | 4e8845bbcd81 |
child 80913 | 46f59511b7bb |
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, |
|
74216 | 28 |
head, last, take, drop, isPrefixOf, isSuffixOf, try_unprefix, try_unsuffix, |
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 |
|
74216 | 83 |
take n bs |
84 |
| n == 0 = empty |
|
85 |
| n >= length bs = bs |
|
86 |
| otherwise = pack (List.take n (unpack bs)) |
|
74084 | 87 |
|
88 |
drop :: Int -> Bytes -> Bytes |
|
74216 | 89 |
drop n bs |
90 |
| n == 0 = bs |
|
91 |
| n >= length bs = empty |
|
92 |
| otherwise = pack (List.drop n (unpack bs)) |
|
74084 | 93 |
|
74099 | 94 |
isPrefixOf :: Bytes -> Bytes -> Bool |
95 |
isPrefixOf bs1 bs2 = |
|
96 |
n1 <= n2 && List.all (\i -> index bs1 i == index bs2 i) [0 .. n1 - 1] |
|
97 |
where n1 = length bs1; n2 = length bs2 |
|
98 |
||
99 |
isSuffixOf :: Bytes -> Bytes -> Bool |
|
100 |
isSuffixOf bs1 bs2 = |
|
101 |
n1 <= n2 && List.all (\i -> index bs1 i == index bs2 (i + k)) [0 .. n1 - 1] |
|
102 |
where n1 = length bs1; n2 = length bs2; k = n2 - n1 |
|
103 |
||
74216 | 104 |
try_unprefix :: Bytes -> Bytes -> Maybe Bytes |
105 |
try_unprefix bs1 bs2 = |
|
106 |
if isPrefixOf bs1 bs2 then Just (drop (length bs1) bs2) |
|
107 |
else Nothing |
|
108 |
||
109 |
try_unsuffix :: Bytes -> Bytes -> Maybe Bytes |
|
110 |
try_unsuffix bs1 bs2 = |
|
111 |
if isSuffixOf bs1 bs2 then Just (take (length bs2 - length bs1) bs2) |
|
112 |
else Nothing |
|
113 |
||
74084 | 114 |
concat :: [Bytes] -> Bytes |
115 |
concat = mconcat |
|
74088 | 116 |
|
74095 | 117 |
space :: Word8 |
118 |
space = 32 |
|
119 |
||
120 |
small_spaces :: Array Int Bytes |
|
121 |
small_spaces = array (0, 64) [(i, pack (replicate i space)) | i <- [0 .. 64]] |
|
122 |
||
123 |
spaces :: Int -> Bytes |
|
124 |
spaces n = |
|
125 |
if n < 64 then small_spaces ! n |
|
126 |
else concat ((small_spaces ! (n `mod` 64)) : replicate (n `div` 64) (small_spaces ! 64)) |
|
127 |
||
128 |
char :: Word8 -> Char |
|
129 |
char = toEnum . fromEnum |
|
130 |
||
74135 | 131 |
all_char :: (Char -> Bool) -> Bytes -> Bool |
132 |
all_char pred = all (pred . char) |
|
133 |
||
134 |
any_char :: (Char -> Bool) -> Bytes -> Bool |
|
135 |
any_char pred = any (pred . char) |
|
136 |
||
74095 | 137 |
byte :: Char -> Word8 |
138 |
byte = toEnum . fromEnum |
|
139 |
||
74132 | 140 |
singletons :: Array Word8 Bytes |
141 |
singletons = |
|
74137 | 142 |
array (minBound, maxBound) |
143 |
[(i, make (ByteString.singleton i)) | i <- [minBound .. maxBound]] |
|
74132 | 144 |
|
145 |
singleton :: Word8 -> Bytes |
|
146 |
singleton b = singletons ! b |
|
74084 | 147 |
\<close> |
148 |
||
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
149 |
generate_file "Isabelle/UTF8.hs" = \<open> |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
150 |
{- Title: Isabelle/UTF8.hs |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
151 |
Author: Makarius |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
152 |
LICENSE: BSD 3-clause (Isabelle) |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
153 |
|
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
154 |
Variations on UTF-8. |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
155 |
|
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
156 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/General/utf8.ML\<close> |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
157 |
and \<^file>\<open>$ISABELLE_HOME/src/Pure/General/utf8.scala\<close>. |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
158 |
-} |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
159 |
|
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
160 |
{-# LANGUAGE MultiParamTypeClasses #-} |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
161 |
{-# LANGUAGE TypeSynonymInstances #-} |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
162 |
{-# LANGUAGE FlexibleInstances #-} |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
163 |
{-# LANGUAGE InstanceSigs #-} |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
164 |
|
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
165 |
module Isabelle.UTF8 ( |
74098 | 166 |
setup, setup3, |
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
167 |
Recode (..) |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
168 |
) |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
169 |
where |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
170 |
|
74098 | 171 |
import qualified System.IO as IO |
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
172 |
import Data.Text (Text) |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
173 |
import qualified Data.Text as Text |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
174 |
import qualified Data.Text.Encoding as Encoding |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
175 |
import qualified Data.Text.Encoding.Error as Error |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
176 |
|
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
177 |
import Data.ByteString (ByteString) |
74084 | 178 |
|
179 |
import qualified Isabelle.Bytes as Bytes |
|
180 |
import Isabelle.Bytes (Bytes) |
|
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
181 |
|
74098 | 182 |
setup :: IO.Handle -> IO () |
183 |
setup h = do |
|
184 |
IO.hSetEncoding h IO.utf8 |
|
185 |
IO.hSetNewlineMode h IO.noNewlineTranslation |
|
186 |
||
187 |
setup3 :: IO.Handle -> IO.Handle -> IO.Handle -> IO () |
|
188 |
setup3 h1 h2 h3 = do |
|
189 |
setup h1 |
|
190 |
setup h2 |
|
191 |
setup h3 |
|
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
192 |
|
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
193 |
class Recode a b where |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
194 |
encode :: a -> b |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
195 |
decode :: b -> a |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
196 |
|
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
197 |
instance Recode Text ByteString where |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
198 |
encode :: Text -> ByteString |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
199 |
encode = Encoding.encodeUtf8 |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
200 |
decode :: ByteString -> Text |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
201 |
decode = Encoding.decodeUtf8With Error.lenientDecode |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
202 |
|
74084 | 203 |
instance Recode Text Bytes where |
204 |
encode :: Text -> Bytes |
|
205 |
encode = Bytes.make . encode |
|
206 |
decode :: Bytes -> Text |
|
207 |
decode = decode . Bytes.unmake |
|
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
208 |
|
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
209 |
instance Recode String ByteString where |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
210 |
encode :: String -> ByteString |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
211 |
encode = encode . Text.pack |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
212 |
decode :: ByteString -> String |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
213 |
decode = Text.unpack . decode |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
214 |
|
74084 | 215 |
instance Recode String Bytes where |
216 |
encode :: String -> Bytes |
|
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
217 |
encode = encode . Text.pack |
74084 | 218 |
decode :: Bytes -> String |
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
219 |
decode = Text.unpack . decode |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
220 |
\<close> |
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
221 |
|
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
222 |
generate_file "Isabelle/Library.hs" = \<open> |
69445 | 223 |
{- Title: Isabelle/Library.hs |
69225 | 224 |
Author: Makarius |
225 |
LICENSE: BSD 3-clause (Isabelle) |
|
226 |
||
227 |
Basic library of Isabelle idioms. |
|
69280 | 228 |
|
74178 | 229 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/General/basics.ML\<close> |
230 |
and \<^file>\<open>$ISABELLE_HOME/src/Pure/library.ML\<close>. |
|
69225 | 231 |
-} |
232 |
||
74086 | 233 |
{-# LANGUAGE OverloadedStrings #-} |
74093 | 234 |
{-# LANGUAGE TypeSynonymInstances #-} |
235 |
{-# LANGUAGE FlexibleInstances #-} |
|
74134 | 236 |
{-# LANGUAGE InstanceSigs #-} |
74086 | 237 |
|
69240 | 238 |
module Isabelle.Library ( |
239 |
(|>), (|->), (#>), (#->), |
|
240 |
||
74327 | 241 |
fold, fold_rev, fold_map, single, the_single, singletonM, |
242 |
map_index, get_index, separate, |
|
69240 | 243 |
|
74093 | 244 |
StringLike, STRING (..), TEXT (..), BYTES (..), |
74128 | 245 |
show_bytes, show_text, |
74093 | 246 |
|
74185 | 247 |
proper_string, enclose, quote, space_implode, commas, commas_quote, cat_lines, |
74213 | 248 |
space_explode, split_lines, trim_line, trim_split_lines, |
74211 | 249 |
|
250 |
getenv, getenv_strict) |
|
69225 | 251 |
where |
252 |
||
74211 | 253 |
import System.Environment (lookupEnv) |
254 |
import Data.Maybe (fromMaybe) |
|
74093 | 255 |
import qualified Data.Text as Text |
256 |
import Data.Text (Text) |
|
74096 | 257 |
import qualified Data.Text.Lazy as Lazy |
74086 | 258 |
import Data.String (IsString) |
69453 | 259 |
import qualified Data.List.Split as Split |
69491 | 260 |
import qualified Isabelle.Symbol as Symbol |
74130 | 261 |
import qualified Isabelle.Bytes as Bytes |
74093 | 262 |
import Isabelle.Bytes (Bytes) |
263 |
import qualified Isabelle.UTF8 as UTF8 |
|
69234 | 264 |
|
265 |
||
69225 | 266 |
{- functions -} |
267 |
||
268 |
(|>) :: a -> (a -> b) -> b |
|
269 |
x |> f = f x |
|
270 |
||
271 |
(|->) :: (a, b) -> (a -> b -> c) -> c |
|
272 |
(x, y) |-> f = f x y |
|
273 |
||
274 |
(#>) :: (a -> b) -> (b -> c) -> a -> c |
|
275 |
(f #> g) x = x |> f |> g |
|
276 |
||
277 |
(#->) :: (a -> (c, b)) -> (c -> b -> d) -> a -> d |
|
278 |
(f #-> g) x = x |> f |-> g |
|
279 |
||
280 |
||
281 |
{- lists -} |
|
282 |
||
283 |
fold :: (a -> b -> b) -> [a] -> b -> b |
|
284 |
fold _ [] y = y |
|
285 |
fold f (x : xs) y = fold f xs (f x y) |
|
286 |
||
287 |
fold_rev :: (a -> b -> b) -> [a] -> b -> b |
|
288 |
fold_rev _ [] y = y |
|
289 |
fold_rev f (x : xs) y = f x (fold_rev f xs y) |
|
290 |
||
74327 | 291 |
fold_map :: (a -> b -> (c, b)) -> [a] -> b -> ([c], b) |
292 |
fold_map _ [] y = ([], y) |
|
293 |
fold_map f (x : xs) y = |
|
294 |
let |
|
295 |
(x', y') = f x y |
|
296 |
(xs', y'') = fold_map f xs y' |
|
297 |
in (x' : xs', y'') |
|
298 |
||
69225 | 299 |
single :: a -> [a] |
300 |
single x = [x] |
|
301 |
||
74203 | 302 |
the_single :: [a] -> a |
303 |
the_single [x] = x |
|
304 |
the_single _ = undefined |
|
305 |
||
74204 | 306 |
singletonM :: Monad m => ([a] -> m [b]) -> a -> m b |
307 |
singletonM f x = the_single <$> f [x] |
|
308 |
||
69240 | 309 |
map_index :: ((Int, a) -> b) -> [a] -> [b] |
310 |
map_index f = map_aux 0 |
|
311 |
where |
|
312 |
map_aux _ [] = [] |
|
313 |
map_aux i (x : xs) = f (i, x) : map_aux (i + 1) xs |
|
314 |
||
315 |
get_index :: (a -> Maybe b) -> [a] -> Maybe (Int, b) |
|
316 |
get_index f = get_aux 0 |
|
317 |
where |
|
318 |
get_aux _ [] = Nothing |
|
319 |
get_aux i (x : xs) = |
|
320 |
case f x of |
|
321 |
Nothing -> get_aux (i + 1) xs |
|
322 |
Just y -> Just (i, y) |
|
323 |
||
74086 | 324 |
separate :: a -> [a] -> [a] |
74871 | 325 |
separate s (x : xs@(_ : _)) = x : s : separate s xs |
74086 | 326 |
separate _ xs = xs; |
327 |
||
69225 | 328 |
|
74093 | 329 |
{- string-like interfaces -} |
330 |
||
74132 | 331 |
class (IsString a, Monoid a, Eq a, Ord a) => StringLike a where |
332 |
space_explode :: Char -> a -> [a] |
|
74134 | 333 |
trim_line :: a -> a |
74130 | 334 |
|
74136 | 335 |
gen_trim_line :: Int -> (Int -> Char) -> (Int -> a -> a) -> a -> a |
336 |
gen_trim_line n at trim s = |
|
337 |
if n >= 2 && at (n - 2) == '\r' && at (n - 1) == '\n' then trim (n - 2) s |
|
338 |
else if n >= 1 && Symbol.is_ascii_line_terminator (at (n - 1)) then trim (n - 1) s |
|
339 |
else s |
|
340 |
||
74130 | 341 |
instance StringLike String where |
74134 | 342 |
space_explode :: Char -> String -> [String] |
74132 | 343 |
space_explode c = Split.split (Split.dropDelims (Split.whenElt (== c))) |
74134 | 344 |
trim_line :: String -> String |
74871 | 345 |
trim_line s = gen_trim_line (length s) (s !!) take s |
74130 | 346 |
|
347 |
instance StringLike Text where |
|
74134 | 348 |
space_explode :: Char -> Text -> [Text] |
74132 | 349 |
space_explode c str = |
74130 | 350 |
if Text.null str then [] |
74132 | 351 |
else if Text.all (/= c) str then [str] |
352 |
else map Text.pack $ space_explode c $ Text.unpack str |
|
74134 | 353 |
trim_line :: Text -> Text |
74136 | 354 |
trim_line s = gen_trim_line (Text.length s) (Text.index s) Text.take s |
74130 | 355 |
|
356 |
instance StringLike Lazy.Text where |
|
74134 | 357 |
space_explode :: Char -> Lazy.Text -> [Lazy.Text] |
74132 | 358 |
space_explode c str = |
74130 | 359 |
if Lazy.null str then [] |
74132 | 360 |
else if Lazy.all (/= c) str then [str] |
361 |
else map Lazy.pack $ space_explode c $ Lazy.unpack str |
|
74134 | 362 |
trim_line :: Lazy.Text -> Lazy.Text |
363 |
trim_line = Lazy.fromStrict . trim_line . Lazy.toStrict |
|
74130 | 364 |
|
365 |
instance StringLike Bytes where |
|
74134 | 366 |
space_explode :: Char -> Bytes -> [Bytes] |
74132 | 367 |
space_explode c str = |
74130 | 368 |
if Bytes.null str then [] |
74135 | 369 |
else if Bytes.all_char (/= c) str then [str] |
74132 | 370 |
else |
371 |
explode (Bytes.unpack str) |
|
372 |
where |
|
373 |
explode rest = |
|
374 |
case span (/= (Bytes.byte c)) rest of |
|
375 |
(_, []) -> [Bytes.pack rest] |
|
376 |
(prfx, _ : rest') -> Bytes.pack prfx : explode rest' |
|
74134 | 377 |
trim_line :: Bytes -> Bytes |
74136 | 378 |
trim_line s = gen_trim_line (Bytes.length s) (Bytes.char . Bytes.index s) Bytes.take s |
74093 | 379 |
|
380 |
class StringLike a => STRING a where make_string :: a -> String |
|
381 |
instance STRING String where make_string = id |
|
382 |
instance STRING Text where make_string = Text.unpack |
|
74096 | 383 |
instance STRING Lazy.Text where make_string = Lazy.unpack |
74093 | 384 |
instance STRING Bytes where make_string = UTF8.decode |
385 |
||
386 |
class StringLike a => TEXT a where make_text :: a -> Text |
|
387 |
instance TEXT String where make_text = Text.pack |
|
388 |
instance TEXT Text where make_text = id |
|
74096 | 389 |
instance TEXT Lazy.Text where make_text = Lazy.toStrict |
74093 | 390 |
instance TEXT Bytes where make_text = UTF8.decode |
391 |
||
392 |
class StringLike a => BYTES a where make_bytes :: a -> Bytes |
|
393 |
instance BYTES String where make_bytes = UTF8.encode |
|
394 |
instance BYTES Text where make_bytes = UTF8.encode |
|
74096 | 395 |
instance BYTES Lazy.Text where make_bytes = UTF8.encode . Lazy.toStrict |
74093 | 396 |
instance BYTES Bytes where make_bytes = id |
397 |
||
74128 | 398 |
show_bytes :: Show a => a -> Bytes |
399 |
show_bytes = make_bytes . show |
|
400 |
||
401 |
show_text :: Show a => a -> Text |
|
402 |
show_text = make_text . show |
|
403 |
||
74093 | 404 |
|
69225 | 405 |
{- strings -} |
406 |
||
74093 | 407 |
proper_string :: StringLike a => a -> Maybe a |
74086 | 408 |
proper_string s = if s == "" then Nothing else Just s |
409 |
||
74185 | 410 |
enclose :: StringLike a => a -> a -> a -> a |
411 |
enclose lpar rpar str = lpar <> str <> rpar |
|
412 |
||
74093 | 413 |
quote :: StringLike a => a -> a |
74185 | 414 |
quote = enclose "\"" "\"" |
69225 | 415 |
|
74093 | 416 |
space_implode :: StringLike a => a -> [a] -> a |
74086 | 417 |
space_implode s = mconcat . separate s |
418 |
||
74093 | 419 |
commas, commas_quote :: StringLike a => [a] -> a |
69453 | 420 |
commas = space_implode ", " |
421 |
commas_quote = commas . map quote |
|
422 |
||
74132 | 423 |
split_lines :: StringLike a => a -> [a] |
424 |
split_lines = space_explode '\n' |
|
425 |
||
74093 | 426 |
cat_lines :: StringLike a => [a] -> a |
69453 | 427 |
cat_lines = space_implode "\n" |
74211 | 428 |
|
74213 | 429 |
trim_split_lines :: StringLike a => a -> [a] |
430 |
trim_split_lines = trim_line #> split_lines #> map trim_line |
|
431 |
||
74211 | 432 |
|
433 |
{- getenv -} |
|
434 |
||
435 |
getenv :: Bytes -> IO Bytes |
|
436 |
getenv x = do |
|
437 |
y <- lookupEnv (make_string x) |
|
438 |
return $ make_bytes $ fromMaybe "" y |
|
439 |
||
440 |
getenv_strict :: Bytes -> IO Bytes |
|
441 |
getenv_strict x = do |
|
442 |
y <- getenv x |
|
443 |
if Bytes.null y then |
|
444 |
errorWithoutStackTrace $ make_string ("Undefined Isabelle environment variable: " <> quote x) |
|
445 |
else return y |
|
69225 | 446 |
\<close> |
447 |
||
74091 | 448 |
|
449 |
generate_file "Isabelle/Symbol.hs" = \<open> |
|
450 |
{- Title: Isabelle/Symbols.hs |
|
451 |
Author: Makarius |
|
452 |
LICENSE: BSD 3-clause (Isabelle) |
|
453 |
||
454 |
Isabelle text symbols. |
|
74172 | 455 |
|
456 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/General/symbol.ML\<close> |
|
457 |
and \<^file>\<open>$ISABELLE_HOME/src/Pure/General/symbol_explode.ML\<close>. |
|
74091 | 458 |
-} |
459 |
||
74172 | 460 |
{-# LANGUAGE OverloadedStrings #-} |
461 |
||
462 |
module Isabelle.Symbol ( |
|
74177
a8b032dede5c
treat Symbol.eof as in ML (but: presently unused);
wenzelm
parents:
74176
diff
changeset
|
463 |
Symbol, eof, is_eof, not_eof, |
a8b032dede5c
treat Symbol.eof as in ML (but: presently unused);
wenzelm
parents:
74176
diff
changeset
|
464 |
|
74172 | 465 |
is_ascii_letter, is_ascii_digit, is_ascii_hex, is_ascii_quasi, |
466 |
is_ascii_blank, is_ascii_line_terminator, is_ascii_letdig, |
|
467 |
is_ascii_identifier, |
|
468 |
||
74177
a8b032dede5c
treat Symbol.eof as in ML (but: presently unused);
wenzelm
parents:
74176
diff
changeset
|
469 |
explode |
74172 | 470 |
) |
471 |
where |
|
472 |
||
473 |
import Data.Word (Word8) |
|
474 |
import qualified Isabelle.Bytes as Bytes |
|
475 |
import Isabelle.Bytes (Bytes) |
|
476 |
||
74091 | 477 |
|
74177
a8b032dede5c
treat Symbol.eof as in ML (but: presently unused);
wenzelm
parents:
74176
diff
changeset
|
478 |
{- type -} |
a8b032dede5c
treat Symbol.eof as in ML (but: presently unused);
wenzelm
parents:
74176
diff
changeset
|
479 |
|
a8b032dede5c
treat Symbol.eof as in ML (but: presently unused);
wenzelm
parents:
74176
diff
changeset
|
480 |
type Symbol = Bytes |
a8b032dede5c
treat Symbol.eof as in ML (but: presently unused);
wenzelm
parents:
74176
diff
changeset
|
481 |
|
a8b032dede5c
treat Symbol.eof as in ML (but: presently unused);
wenzelm
parents:
74176
diff
changeset
|
482 |
eof :: Symbol |
a8b032dede5c
treat Symbol.eof as in ML (but: presently unused);
wenzelm
parents:
74176
diff
changeset
|
483 |
eof = "" |
a8b032dede5c
treat Symbol.eof as in ML (but: presently unused);
wenzelm
parents:
74176
diff
changeset
|
484 |
|
a8b032dede5c
treat Symbol.eof as in ML (but: presently unused);
wenzelm
parents:
74176
diff
changeset
|
485 |
is_eof, not_eof :: Symbol -> Bool |
a8b032dede5c
treat Symbol.eof as in ML (but: presently unused);
wenzelm
parents:
74176
diff
changeset
|
486 |
is_eof = Bytes.null |
a8b032dede5c
treat Symbol.eof as in ML (but: presently unused);
wenzelm
parents:
74176
diff
changeset
|
487 |
not_eof = not . is_eof |
a8b032dede5c
treat Symbol.eof as in ML (but: presently unused);
wenzelm
parents:
74176
diff
changeset
|
488 |
|
a8b032dede5c
treat Symbol.eof as in ML (but: presently unused);
wenzelm
parents:
74176
diff
changeset
|
489 |
|
74091 | 490 |
{- ASCII characters -} |
491 |
||
492 |
is_ascii_letter :: Char -> Bool |
|
493 |
is_ascii_letter c = 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' |
|
494 |
||
495 |
is_ascii_digit :: Char -> Bool |
|
496 |
is_ascii_digit c = '0' <= c && c <= '9' |
|
497 |
||
498 |
is_ascii_hex :: Char -> Bool |
|
499 |
is_ascii_hex c = '0' <= c && c <= '9' || 'A' <= c && c <= 'F' || 'a' <= c && c <= 'f' |
|
500 |
||
501 |
is_ascii_quasi :: Char -> Bool |
|
502 |
is_ascii_quasi c = c == '_' || c == '\'' |
|
503 |
||
504 |
is_ascii_blank :: Char -> Bool |
|
74172 | 505 |
is_ascii_blank c = c `elem` (" \t\n\11\f\r" :: String) |
74091 | 506 |
|
507 |
is_ascii_line_terminator :: Char -> Bool |
|
508 |
is_ascii_line_terminator c = c == '\r' || c == '\n' |
|
509 |
||
510 |
is_ascii_letdig :: Char -> Bool |
|
511 |
is_ascii_letdig c = is_ascii_letter c || is_ascii_digit c || is_ascii_quasi c |
|
512 |
||
513 |
is_ascii_identifier :: String -> Bool |
|
514 |
is_ascii_identifier s = |
|
515 |
not (null s) && is_ascii_letter (head s) && all is_ascii_letdig s |
|
74172 | 516 |
|
517 |
||
518 |
{- explode symbols: ASCII, UTF8, named -} |
|
519 |
||
520 |
is_utf8 :: Word8 -> Bool |
|
521 |
is_utf8 b = b >= 128 |
|
522 |
||
523 |
is_utf8_trailer :: Word8 -> Bool |
|
524 |
is_utf8_trailer b = 128 <= b && b < 192 |
|
525 |
||
526 |
is_utf8_control :: Word8 -> Bool |
|
527 |
is_utf8_control b = 128 <= b && b < 160 |
|
528 |
||
529 |
(|>) :: a -> (a -> b) -> b |
|
530 |
x |> f = f x |
|
531 |
||
532 |
explode :: Bytes -> [Symbol] |
|
533 |
explode string = scan 0 |
|
534 |
where |
|
535 |
byte = Bytes.index string |
|
536 |
substring i j = |
|
537 |
if i == j - 1 then Bytes.singleton (byte i) |
|
538 |
else Bytes.pack (map byte [i .. j - 1]) |
|
539 |
||
540 |
n = Bytes.length string |
|
541 |
test pred i = i < n && pred (byte i) |
|
542 |
test_char pred i = i < n && pred (Bytes.char (byte i)) |
|
543 |
many pred i = if test pred i then many pred (i + 1) else i |
|
544 |
maybe_char c i = if test_char (== c) i then i + 1 else i |
|
545 |
maybe_ascii_id i = |
|
546 |
if test_char is_ascii_letter i |
|
547 |
then many (is_ascii_letdig . Bytes.char) (i + 1) |
|
548 |
else i |
|
549 |
||
550 |
scan i = |
|
551 |
if i < n then |
|
552 |
let |
|
553 |
b = byte i |
|
554 |
c = Bytes.char b |
|
555 |
in |
|
556 |
{-encoded newline-} |
|
557 |
if c == '\r' then "\n" : scan (maybe_char '\n' (i + 1)) |
|
558 |
{-pseudo utf8: encoded ascii control-} |
|
559 |
else if b == 192 && test is_utf8_control (i + 1) && not (test is_utf8 (i + 2)) |
|
560 |
then Bytes.singleton (byte (i + 1) - 128) : scan (i + 2) |
|
561 |
{-utf8-} |
|
562 |
else if is_utf8 b then |
|
563 |
let j = many is_utf8_trailer (i + 1) |
|
564 |
in substring i j : scan j |
|
565 |
{-named symbol-} |
|
566 |
else if c == '\\' && test_char (== '<') (i + 1) then |
|
567 |
let j = (i + 2) |> maybe_char '^' |> maybe_ascii_id |> maybe_char '>' |
|
568 |
in substring i j : scan j |
|
569 |
{-single character-} |
|
570 |
else Bytes.singleton b : scan (i + 1) |
|
571 |
else [] |
|
74091 | 572 |
\<close> |
573 |
||
74095 | 574 |
generate_file "Isabelle/Buffer.hs" = \<open> |
575 |
{- Title: Isabelle/Buffer.hs |
|
576 |
Author: Makarius |
|
577 |
LICENSE: BSD 3-clause (Isabelle) |
|
578 |
||
579 |
Efficient buffer of byte strings. |
|
580 |
||
74178 | 581 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/General/buffer.ML\<close>. |
74095 | 582 |
-} |
583 |
||
74231 | 584 |
module Isabelle.Buffer (T, empty, add, content, build, build_content) |
74095 | 585 |
where |
586 |
||
587 |
import qualified Isabelle.Bytes as Bytes |
|
588 |
import Isabelle.Bytes (Bytes) |
|
74231 | 589 |
import Isabelle.Library |
74095 | 590 |
|
591 |
||
592 |
newtype T = Buffer [Bytes] |
|
593 |
||
594 |
empty :: T |
|
595 |
empty = Buffer [] |
|
596 |
||
597 |
add :: Bytes -> T -> T |
|
598 |
add b (Buffer bs) = Buffer (if Bytes.null b then bs else b : bs) |
|
599 |
||
600 |
content :: T -> Bytes |
|
601 |
content (Buffer bs) = Bytes.concat (reverse bs) |
|
74231 | 602 |
|
603 |
build :: (T -> T) -> T |
|
604 |
build f = f empty |
|
605 |
||
606 |
build_content :: (T -> T) -> Bytes |
|
607 |
build_content f = build f |> content |
|
74095 | 608 |
\<close> |
609 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
610 |
generate_file "Isabelle/Value.hs" = \<open> |
73246 | 611 |
{- Title: Isabelle/Value.hs |
69233 | 612 |
Author: Makarius |
613 |
LICENSE: BSD 3-clause (Isabelle) |
|
614 |
||
615 |
Plain values, represented as string. |
|
69280 | 616 |
|
74178 | 617 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/General/value.ML\<close>. |
69233 | 618 |
-} |
619 |
||
74095 | 620 |
{-# LANGUAGE OverloadedStrings #-} |
621 |
||
69233 | 622 |
module Isabelle.Value |
69452 | 623 |
(print_bool, parse_bool, parse_nat, print_int, parse_int, print_real, parse_real) |
69233 | 624 |
where |
625 |
||
626 |
import qualified Data.List as List |
|
627 |
import qualified Text.Read as Read |
|
74095 | 628 |
import Isabelle.Bytes (Bytes) |
629 |
import Isabelle.Library |
|
69233 | 630 |
|
631 |
||
632 |
{- bool -} |
|
633 |
||
74095 | 634 |
print_bool :: Bool -> Bytes |
69233 | 635 |
print_bool True = "true" |
636 |
print_bool False = "false" |
|
637 |
||
74095 | 638 |
parse_bool :: Bytes -> Maybe Bool |
69233 | 639 |
parse_bool "true" = Just True |
640 |
parse_bool "false" = Just False |
|
641 |
parse_bool _ = Nothing |
|
642 |
||
643 |
||
69452 | 644 |
{- nat -} |
645 |
||
74095 | 646 |
parse_nat :: Bytes -> Maybe Int |
69452 | 647 |
parse_nat s = |
74095 | 648 |
case Read.readMaybe (make_string s) of |
69452 | 649 |
Just n | n >= 0 -> Just n |
650 |
_ -> Nothing |
|
651 |
||
652 |
||
69233 | 653 |
{- int -} |
654 |
||
74095 | 655 |
print_int :: Int -> Bytes |
74128 | 656 |
print_int = show_bytes |
74095 | 657 |
|
658 |
parse_int :: Bytes -> Maybe Int |
|
659 |
parse_int = Read.readMaybe . make_string |
|
69233 | 660 |
|
661 |
||
662 |
{- real -} |
|
663 |
||
74095 | 664 |
print_real :: Double -> Bytes |
69233 | 665 |
print_real x = |
666 |
let s = show x in |
|
667 |
case span (/= '.') s of |
|
74095 | 668 |
(a, '.' : b) | List.all (== '0') b -> make_bytes a |
669 |
_ -> make_bytes s |
|
670 |
||
671 |
parse_real :: Bytes -> Maybe Double |
|
672 |
parse_real = Read.readMaybe . make_string |
|
69225 | 673 |
\<close> |
674 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
675 |
generate_file "Isabelle/Properties.hs" = \<open> |
69445 | 676 |
{- Title: Isabelle/Properties.hs |
69225 | 677 |
Author: Makarius |
678 |
LICENSE: BSD 3-clause (Isabelle) |
|
679 |
||
680 |
Property lists. |
|
69280 | 681 |
|
74178 | 682 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/General/properties.ML\<close>. |
69225 | 683 |
-} |
684 |
||
69477 | 685 |
module Isabelle.Properties (Entry, T, defined, get, get_value, put, remove) |
69225 | 686 |
where |
687 |
||
688 |
import qualified Data.List as List |
|
74095 | 689 |
import Isabelle.Bytes (Bytes) |
690 |
||
691 |
||
692 |
type Entry = (Bytes, Bytes) |
|
69225 | 693 |
type T = [Entry] |
694 |
||
74095 | 695 |
defined :: T -> Bytes -> Bool |
69225 | 696 |
defined props name = any (\(a, _) -> a == name) props |
697 |
||
74095 | 698 |
get :: T -> Bytes -> Maybe Bytes |
69225 | 699 |
get props name = List.lookup name props |
700 |
||
74095 | 701 |
get_value :: (Bytes -> Maybe a) -> T -> Bytes -> Maybe a |
74871 | 702 |
get_value parse props name = maybe Nothing parse (get props name) |
69477 | 703 |
|
69225 | 704 |
put :: Entry -> T -> T |
705 |
put entry props = entry : remove (fst entry) props |
|
706 |
||
74095 | 707 |
remove :: Bytes -> T -> T |
69225 | 708 |
remove name props = |
709 |
if defined props name then filter (\(a, _) -> a /= name) props |
|
710 |
else props |
|
711 |
\<close> |
|
712 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
713 |
generate_file "Isabelle/Markup.hs" = \<open> |
73246 | 714 |
{- Title: Isabelle/Markup.hs |
69225 | 715 |
Author: Makarius |
716 |
LICENSE: BSD 3-clause (Isabelle) |
|
717 |
||
718 |
Quasi-abstract markup elements. |
|
69280 | 719 |
|
74178 | 720 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/markup.ML\<close>. |
69225 | 721 |
-} |
722 |
||
74095 | 723 |
{-# LANGUAGE OverloadedStrings #-} |
73177 | 724 |
{-# OPTIONS_GHC -fno-warn-missing-signatures #-} |
725 |
||
69234 | 726 |
module Isabelle.Markup ( |
727 |
T, empty, is_empty, properties, |
|
728 |
||
729 |
nameN, name, xnameN, xname, kindN, |
|
730 |
||
69315 | 731 |
bindingN, binding, entityN, entity, defN, refN, |
732 |
||
69288 | 733 |
completionN, completion, no_completionN, no_completion, |
734 |
||
78021 | 735 |
lineN, end_lineN, offsetN, end_offsetN, labelN, fileN, idN, positionN, position, |
74182 | 736 |
position_properties, def_name, |
69234 | 737 |
|
69291 | 738 |
expressionN, expression, |
739 |
||
71489 | 740 |
pathN, path, urlN, url, docN, doc, |
69291 | 741 |
|
69248 | 742 |
markupN, consistentN, unbreakableN, indentN, widthN, |
743 |
blockN, block, breakN, break, fbreakN, fbreak, itemN, item, |
|
744 |
||
69968
1a400b14fd3a
clarified spell-checking (see also 30233285270a);
wenzelm
parents:
69794
diff
changeset
|
745 |
wordsN, words, |
69234 | 746 |
|
747 |
tfreeN, tfree, tvarN, tvar, freeN, free, skolemN, skolem, boundN, bound, varN, var, |
|
748 |
numeralN, numeral, literalN, literal, delimiterN, delimiter, inner_stringN, inner_string, |
|
69320 | 749 |
inner_cartoucheN, inner_cartouche, |
69234 | 750 |
token_rangeN, token_range, |
751 |
sortingN, sorting, typingN, typing, class_parameterN, class_parameter, |
|
752 |
||
753 |
antiquotedN, antiquoted, antiquoteN, antiquote, |
|
754 |
||
755 |
paragraphN, paragraph, text_foldN, text_fold, |
|
756 |
||
757 |
keyword1N, keyword1, keyword2N, keyword2, keyword3N, keyword3, quasi_keywordN, quasi_keyword, |
|
758 |
improperN, improper, operatorN, operator, stringN, string, alt_stringN, alt_string, |
|
69320 | 759 |
verbatimN, verbatim, cartoucheN, cartouche, commentN, comment, comment1N, comment1, |
760 |
comment2N, comment2, comment3N, comment3, |
|
69234 | 761 |
|
70667 | 762 |
forkedN, forked, joinedN, joined, runningN, running, finishedN, finished, |
69794 | 763 |
failedN, failed, canceledN, canceled, initializedN, initialized, finalizedN, finalized, |
764 |
consolidatedN, consolidated, |
|
765 |
||
69234 | 766 |
writelnN, writeln, stateN, state, informationN, information, tracingN, tracing, |
767 |
warningN, warning, legacyN, legacy, errorN, error, reportN, report, no_reportN, no_report, |
|
768 |
||
769 |
intensifyN, intensify, |
|
770 |
Output, no_output) |
|
69225 | 771 |
where |
772 |
||
69248 | 773 |
import Prelude hiding (words, error, break) |
74182 | 774 |
import Data.Map.Strict (Map) |
775 |
import qualified Data.Map.Strict as Map |
|
69234 | 776 |
|
777 |
import Isabelle.Library |
|
69225 | 778 |
import qualified Isabelle.Properties as Properties |
69248 | 779 |
import qualified Isabelle.Value as Value |
74095 | 780 |
import qualified Isabelle.Bytes as Bytes |
781 |
import Isabelle.Bytes (Bytes) |
|
69225 | 782 |
|
783 |
||
69234 | 784 |
{- basic markup -} |
785 |
||
74095 | 786 |
type T = (Bytes, Properties.T) |
69225 | 787 |
|
788 |
empty :: T |
|
789 |
empty = ("", []) |
|
790 |
||
791 |
is_empty :: T -> Bool |
|
792 |
is_empty ("", _) = True |
|
793 |
is_empty _ = False |
|
794 |
||
69234 | 795 |
properties :: Properties.T -> T -> T |
796 |
properties more_props (elem, props) = |
|
797 |
(elem, fold_rev Properties.put more_props props) |
|
798 |
||
74095 | 799 |
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
|
800 |
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
|
801 |
|
74095 | 802 |
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
|
803 |
markup_string name prop = \s -> (name, [(prop, s)]) |
69234 | 804 |
|
805 |
||
806 |
{- misc properties -} |
|
807 |
||
74095 | 808 |
nameN :: Bytes |
69234 | 809 |
nameN = \<open>Markup.nameN\<close> |
810 |
||
74095 | 811 |
name :: Bytes -> T -> T |
69234 | 812 |
name a = properties [(nameN, a)] |
813 |
||
74095 | 814 |
xnameN :: Bytes |
69234 | 815 |
xnameN = \<open>Markup.xnameN\<close> |
816 |
||
74095 | 817 |
xname :: Bytes -> T -> T |
69234 | 818 |
xname a = properties [(xnameN, a)] |
819 |
||
74095 | 820 |
kindN :: Bytes |
69234 | 821 |
kindN = \<open>Markup.kindN\<close> |
822 |
||
823 |
||
69315 | 824 |
{- formal entities -} |
825 |
||
74095 | 826 |
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
|
827 |
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
|
828 |
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
|
829 |
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
|
830 |
|
74095 | 831 |
entityN :: Bytes |
69315 | 832 |
entityN = \<open>Markup.entityN\<close> |
74095 | 833 |
entity :: Bytes -> Bytes -> T |
69315 | 834 |
entity kind name = |
835 |
(entityN, |
|
74095 | 836 |
(if Bytes.null name then [] else [(nameN, name)]) <> |
837 |
(if Bytes.null kind then [] else [(kindN, kind)])) |
|
838 |
||
839 |
defN :: Bytes |
|
69315 | 840 |
defN = \<open>Markup.defN\<close> |
841 |
||
74095 | 842 |
refN :: Bytes |
69315 | 843 |
refN = \<open>Markup.refN\<close> |
844 |
||
845 |
||
69288 | 846 |
{- completion -} |
847 |
||
74095 | 848 |
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
|
849 |
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
|
850 |
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
|
851 |
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
|
852 |
|
74095 | 853 |
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
|
854 |
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
|
855 |
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
|
856 |
no_completion = markup_elem no_completionN |
69288 | 857 |
|
858 |
||
69234 | 859 |
{- position -} |
860 |
||
74095 | 861 |
lineN, end_lineN :: Bytes |
69234 | 862 |
lineN = \<open>Markup.lineN\<close> |
863 |
end_lineN = \<open>Markup.end_lineN\<close> |
|
864 |
||
74095 | 865 |
offsetN, end_offsetN :: Bytes |
69234 | 866 |
offsetN = \<open>Markup.offsetN\<close> |
867 |
end_offsetN = \<open>Markup.end_offsetN\<close> |
|
868 |
||
78021 | 869 |
labelN, fileN, idN :: Bytes |
870 |
labelN = \<open>Markup.labelN\<close> |
|
69234 | 871 |
fileN = \<open>Markup.fileN\<close> |
872 |
idN = \<open>Markup.idN\<close> |
|
873 |
||
74095 | 874 |
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
|
875 |
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
|
876 |
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
|
877 |
position = markup_elem positionN |
69234 | 878 |
|
74182 | 879 |
position_properties :: [Bytes] |
78021 | 880 |
position_properties = [lineN, offsetN, end_offsetN, labelN, fileN, idN] |
74182 | 881 |
|
882 |
||
883 |
{- position "def" names -} |
|
884 |
||
885 |
make_def :: Bytes -> Bytes |
|
886 |
make_def a = "def_" <> a |
|
887 |
||
888 |
def_names :: Map Bytes Bytes |
|
889 |
def_names = Map.fromList $ map (\a -> (a, make_def a)) position_properties |
|
890 |
||
891 |
def_name :: Bytes -> Bytes |
|
892 |
def_name a = |
|
893 |
case Map.lookup a def_names of |
|
894 |
Just b -> b |
|
895 |
Nothing -> make_def a |
|
896 |
||
69234 | 897 |
|
69291 | 898 |
{- expression -} |
899 |
||
74095 | 900 |
expressionN :: Bytes |
69291 | 901 |
expressionN = \<open>Markup.expressionN\<close> |
902 |
||
74095 | 903 |
expression :: Bytes -> T |
69291 | 904 |
expression kind = (expressionN, if kind == "" then [] else [(kindN, kind)]) |
905 |
||
906 |
||
907 |
{- external resources -} |
|
908 |
||
74095 | 909 |
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
|
910 |
pathN = \<open>Markup.pathN\<close> |
74095 | 911 |
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
|
912 |
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
|
913 |
|
74095 | 914 |
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
|
915 |
urlN = \<open>Markup.urlN\<close> |
74095 | 916 |
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
|
917 |
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
|
918 |
|
74095 | 919 |
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
|
920 |
docN = \<open>Markup.docN\<close> |
74095 | 921 |
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
|
922 |
doc = markup_string docN nameN |
69291 | 923 |
|
924 |
||
69248 | 925 |
{- pretty printing -} |
926 |
||
74095 | 927 |
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
|
928 |
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
|
929 |
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
|
930 |
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
|
931 |
indentN = \<open>Markup.indentN\<close> |
69248 | 932 |
|
74095 | 933 |
widthN :: Bytes |
69248 | 934 |
widthN = \<open>Markup.widthN\<close> |
935 |
||
74095 | 936 |
blockN :: Bytes |
69248 | 937 |
blockN = \<open>Markup.blockN\<close> |
938 |
block :: Bool -> Int -> T |
|
939 |
block c i = |
|
940 |
(blockN, |
|
74081 | 941 |
(if c then [(consistentN, Value.print_bool c)] else []) <> |
69248 | 942 |
(if i /= 0 then [(indentN, Value.print_int i)] else [])) |
943 |
||
74095 | 944 |
breakN :: Bytes |
69248 | 945 |
breakN = \<open>Markup.breakN\<close> |
946 |
break :: Int -> Int -> T |
|
947 |
break w i = |
|
948 |
(breakN, |
|
74081 | 949 |
(if w /= 0 then [(widthN, Value.print_int w)] else []) <> |
69248 | 950 |
(if i /= 0 then [(indentN, Value.print_int i)] else [])) |
951 |
||
74095 | 952 |
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
|
953 |
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
|
954 |
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
|
955 |
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
|
956 |
|
74095 | 957 |
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
|
958 |
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
|
959 |
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
|
960 |
item = markup_elem itemN |
69248 | 961 |
|
962 |
||
69234 | 963 |
{- text properties -} |
964 |
||
74095 | 965 |
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
|
966 |
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
|
967 |
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
|
968 |
words = markup_elem wordsN |
69234 | 969 |
|
970 |
||
971 |
{- inner syntax -} |
|
972 |
||
74095 | 973 |
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
|
974 |
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
|
975 |
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
|
976 |
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
|
977 |
|
74095 | 978 |
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
|
979 |
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
|
980 |
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
|
981 |
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
|
982 |
|
74095 | 983 |
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
|
984 |
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
|
985 |
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
|
986 |
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
|
987 |
|
74095 | 988 |
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
|
989 |
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
|
990 |
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
|
991 |
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
|
992 |
|
74095 | 993 |
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
|
994 |
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
|
995 |
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
|
996 |
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
|
997 |
|
74095 | 998 |
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
|
999 |
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
|
1000 |
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
|
1001 |
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
|
1002 |
|
74095 | 1003 |
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
|
1004 |
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
|
1005 |
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
|
1006 |
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
|
1007 |
|
74095 | 1008 |
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
|
1009 |
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
|
1010 |
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
|
1011 |
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
|
1012 |
|
74095 | 1013 |
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
|
1014 |
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
|
1015 |
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
|
1016 |
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
|
1017 |
|
74095 | 1018 |
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
|
1019 |
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
|
1020 |
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
|
1021 |
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
|
1022 |
|
74095 | 1023 |
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
|
1024 |
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
|
1025 |
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
|
1026 |
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
|
1027 |
|
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1028 |
|
74095 | 1029 |
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
|
1030 |
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
|
1031 |
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
|
1032 |
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
|
1033 |
|
d300574cee4e
more robust type signatures, notably for the sake of haskell-stack-trace-plugin-0.1.1.1;
wenzelm
parents:
73178
diff
changeset
|
1034 |
|
74095 | 1035 |
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
|
1036 |
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
|
1037 |
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
|
1038 |
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
|
1039 |
|
74095 | 1040 |
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
|
1041 |
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
|
1042 |
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
|
1043 |
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
|
1044 |
|
74095 | 1045 |
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
|
1046 |
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
|
1047 |
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
|
1048 |
class_parameter = markup_elem class_parameterN |
69234 | 1049 |
|
1050 |
||
1051 |
{- antiquotations -} |
|
1052 |
||
74095 | 1053 |
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
|
1054 |
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
|
1055 |
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
|
1056 |
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
|
1057 |
|
74095 | 1058 |
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
|
1059 |
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
|
1060 |
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
|
1061 |
antiquote = markup_elem antiquoteN |
69234 | 1062 |
|
1063 |
||
1064 |
{- text structure -} |
|
1065 |
||
74095 | 1066 |
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
|
1067 |
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
|
1068 |
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
|
1069 |
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
|
1070 |
|
74095 | 1071 |
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
|
1072 |
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
|
1073 |
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
|
1074 |
text_fold = markup_elem text_foldN |
69234 | 1075 |
|
1076 |
||
1077 |
{- outer syntax -} |
|
1078 |
||
74095 | 1079 |
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
|
1080 |
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
|
1081 |
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
|
1082 |
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
|
1083 |
|
74095 | 1084 |
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
|
1085 |
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
|
1086 |
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
|
1087 |
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
|
1088 |
|
74095 | 1089 |
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
|
1090 |
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
|
1091 |
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
|
1092 |
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
|
1093 |
|
74095 | 1094 |
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
|
1095 |
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
|
1096 |
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
|
1097 |
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
|
1098 |
|
74095 | 1099 |
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
|
1100 |
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
|
1101 |
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
|
1102 |
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
|
1103 |
|
74095 | 1104 |
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
|
1105 |
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
|
1106 |
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
|
1107 |
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
|
1108 |
|
74095 | 1109 |
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
|
1110 |
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
|
1111 |
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
|
1112 |
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
|
1113 |
|
74095 | 1114 |
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
|
1115 |
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
|
1116 |
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
|
1117 |
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
|
1118 |
|
74095 | 1119 |
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
|
1120 |
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
|
1121 |
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
|
1122 |
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
|
1123 |
|
74095 | 1124 |
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
|
1125 |
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
|
1126 |
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
|
1127 |
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
|
1128 |
|
74095 | 1129 |
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
|
1130 |
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
|
1131 |
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
|
1132 |
comment = markup_elem commentN |
69234 | 1133 |
|
1134 |
||
69320 | 1135 |
{- comments -} |
1136 |
||
74095 | 1137 |
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
|
1138 |
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
|
1139 |
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
|
1140 |
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
|
1141 |
|
74095 | 1142 |
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
|
1143 |
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
|
1144 |
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
|
1145 |
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
|
1146 |
|
74095 | 1147 |
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
|
1148 |
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
|
1149 |
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
|
1150 |
comment3 = markup_elem comment3N |
69320 | 1151 |
|
1152 |
||
69793 | 1153 |
{- command status -} |
1154 |
||
70667 | 1155 |
forkedN, joinedN, runningN, finishedN, failedN, canceledN, |
74095 | 1156 |
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
|
1157 |
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
|
1158 |
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
|
1159 |
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
|
1160 |
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
|
1161 |
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
|
1162 |
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
|
1163 |
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
|
1164 |
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
|
1165 |
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
|
1166 |
|
70667 | 1167 |
forked, joined, running, finished, failed, canceled, |
69793 | 1168 |
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
|
1169 |
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
|
1170 |
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
|
1171 |
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
|
1172 |
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
|
1173 |
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
|
1174 |
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
|
1175 |
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
|
1176 |
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
|
1177 |
consolidated = markup_elem consolidatedN |
69793 | 1178 |
|
1179 |
||
69234 | 1180 |
{- messages -} |
1181 |
||
74095 | 1182 |
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
|
1183 |
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
|
1184 |
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
|
1185 |
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
|
1186 |
|
74095 | 1187 |
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
|
1188 |
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
|
1189 |
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
|
1190 |
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
|
1191 |
|
74095 | 1192 |
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
|
1193 |
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
|
1194 |
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
|
1195 |
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
|
1196 |
|
74095 | 1197 |
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
|
1198 |
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
|
1199 |
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
|
1200 |
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
|
1201 |
|
74095 | 1202 |
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
|
1203 |
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
|
1204 |
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
|
1205 |
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
|
1206 |
|
74095 | 1207 |
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
|
1208 |
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
|
1209 |
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
|
1210 |
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
|
1211 |
|
74095 | 1212 |
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
|
1213 |
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
|
1214 |
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
|
1215 |
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
|
1216 |
|
74095 | 1217 |
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
|
1218 |
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
|
1219 |
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
|
1220 |
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
|
1221 |
|
74095 | 1222 |
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
|
1223 |
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
|
1224 |
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
|
1225 |
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
|
1226 |
|
74095 | 1227 |
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
|
1228 |
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
|
1229 |
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
|
1230 |
intensify = markup_elem intensifyN |
69234 | 1231 |
|
1232 |
||
1233 |
{- output -} |
|
69225 | 1234 |
|
74095 | 1235 |
type Output = (Bytes, Bytes) |
69225 | 1236 |
|
1237 |
no_output :: Output |
|
1238 |
no_output = ("", "") |
|
69222 | 1239 |
\<close> |
1240 |
||
74167 | 1241 |
generate_file "Isabelle/Position.hs" = \<open> |
1242 |
{- Title: Isabelle/Position.hs |
|
1243 |
Author: Makarius |
|
1244 |
LICENSE: BSD 3-clause (Isabelle) |
|
1245 |
||
74173 | 1246 |
Source positions starting from 1; values <= 0 mean "absent". Count Isabelle |
1247 |
symbols, not UTF8 bytes nor UTF16 characters. Position range specifies a |
|
1248 |
right-open interval offset .. end_offset (exclusive). |
|
74167 | 1249 |
|
74178 | 1250 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/General/position.ML\<close>. |
74167 | 1251 |
-} |
1252 |
||
1253 |
{-# LANGUAGE OverloadedStrings #-} |
|
1254 |
||
1255 |
module Isabelle.Position ( |
|
78307 | 1256 |
T, line_of, column_of, offset_of, end_offset_of, label_of, file_of, id_of, |
1257 |
start, none, label, put_file, file, file_only, put_id, id, id_only, |
|
74174 | 1258 |
symbol, symbol_explode, symbol_explode_string, shift_offsets, |
74184 | 1259 |
of_properties, properties_of, def_properties_of, entity_markup, make_entity_markup, |
74187 | 1260 |
Report, Report_Text, is_reported, is_reported_range, here, |
74167 | 1261 |
Range, no_range, no_range_position, range_position, range |
74184 | 1262 |
) |
1263 |
where |
|
74167 | 1264 |
|
74185 | 1265 |
import Prelude hiding (id) |
74168
f0b2136e2204
tuned signature: prefer existing Haskell operations;
wenzelm
parents:
74167
diff
changeset
|
1266 |
import Data.Maybe (isJust, fromMaybe) |
74182 | 1267 |
import Data.Bifunctor (first) |
74167 | 1268 |
import qualified Isabelle.Properties as Properties |
1269 |
import qualified Isabelle.Bytes as Bytes |
|
1270 |
import qualified Isabelle.Value as Value |
|
1271 |
import Isabelle.Bytes (Bytes) |
|
1272 |
import qualified Isabelle.Markup as Markup |
|
1273 |
import qualified Isabelle.YXML as YXML |
|
1274 |
import Isabelle.Library |
|
74173 | 1275 |
import qualified Isabelle.Symbol as Symbol |
1276 |
import Isabelle.Symbol (Symbol) |
|
74167 | 1277 |
|
1278 |
||
1279 |
{- position -} |
|
1280 |
||
1281 |
data T = |
|
1282 |
Position { |
|
1283 |
_line :: Int, |
|
1284 |
_column :: Int, |
|
1285 |
_offset :: Int, |
|
1286 |
_end_offset :: Int, |
|
78021 | 1287 |
_label :: Bytes, |
74167 | 1288 |
_file :: Bytes, |
1289 |
_id :: Bytes } |
|
1290 |
deriving (Eq, Ord) |
|
1291 |
||
74173 | 1292 |
valid, invalid :: Int -> Bool |
1293 |
valid i = i > 0 |
|
1294 |
invalid = not . valid |
|
74167 | 1295 |
|
1296 |
maybe_valid :: Int -> Maybe Int |
|
74173 | 1297 |
maybe_valid i = if valid i then Just i else Nothing |
1298 |
||
1299 |
if_valid :: Int -> Int -> Int |
|
1300 |
if_valid i i' = if valid i then i' else i |
|
74167 | 1301 |
|
1302 |
||
1303 |
{- fields -} |
|
1304 |
||
1305 |
line_of, column_of, offset_of, end_offset_of :: T -> Maybe Int |
|
1306 |
line_of = maybe_valid . _line |
|
1307 |
column_of = maybe_valid . _column |
|
1308 |
offset_of = maybe_valid . _offset |
|
1309 |
end_offset_of = maybe_valid . _end_offset |
|
1310 |
||
78021 | 1311 |
label_of :: T -> Maybe Bytes |
1312 |
label_of = proper_string . _label |
|
1313 |
||
74167 | 1314 |
file_of :: T -> Maybe Bytes |
1315 |
file_of = proper_string . _file |
|
1316 |
||
1317 |
id_of :: T -> Maybe Bytes |
|
1318 |
id_of = proper_string . _id |
|
1319 |
||
1320 |
||
1321 |
{- make position -} |
|
1322 |
||
1323 |
start :: T |
|
78021 | 1324 |
start = Position 1 1 1 0 Bytes.empty Bytes.empty Bytes.empty |
74167 | 1325 |
|
1326 |
none :: T |
|
78021 | 1327 |
none = Position 0 0 0 0 Bytes.empty Bytes.empty Bytes.empty |
1328 |
||
1329 |
label :: Bytes -> T -> T |
|
1330 |
label label pos = pos { _label = label } |
|
74167 | 1331 |
|
1332 |
put_file :: Bytes -> T -> T |
|
1333 |
put_file file pos = pos { _file = file } |
|
1334 |
||
1335 |
file :: Bytes -> T |
|
1336 |
file file = put_file file start |
|
1337 |
||
1338 |
file_only :: Bytes -> T |
|
1339 |
file_only file = put_file file none |
|
1340 |
||
1341 |
put_id :: Bytes -> T -> T |
|
1342 |
put_id id pos = pos { _id = id } |
|
1343 |
||
74185 | 1344 |
id :: Bytes -> T |
1345 |
id id = put_id id start |
|
1346 |
||
1347 |
id_only :: Bytes -> T |
|
1348 |
id_only id = put_id id none |
|
1349 |
||
74167 | 1350 |
|
74176 | 1351 |
{- count position -} |
1352 |
||
1353 |
count_line :: Symbol -> Int -> Int |
|
1354 |
count_line "\n" line = if_valid line (line + 1) |
|
1355 |
count_line _ line = line |
|
1356 |
||
1357 |
count_column :: Symbol -> Int -> Int |
|
1358 |
count_column "\n" column = if_valid column 1 |
|
74177
a8b032dede5c
treat Symbol.eof as in ML (but: presently unused);
wenzelm
parents:
74176
diff
changeset
|
1359 |
count_column s column = if Symbol.not_eof s then if_valid column (column + 1) else column |
74176 | 1360 |
|
1361 |
count_offset :: Symbol -> Int -> Int |
|
74177
a8b032dede5c
treat Symbol.eof as in ML (but: presently unused);
wenzelm
parents:
74176
diff
changeset
|
1362 |
count_offset s offset = if Symbol.not_eof s then if_valid offset (offset + 1) else offset |
74173 | 1363 |
|
74174 | 1364 |
symbol :: Symbol -> T -> T |
1365 |
symbol s pos = |
|
74167 | 1366 |
pos { |
74176 | 1367 |
_line = count_line s (_line pos), |
1368 |
_column = count_column s (_column pos), |
|
1369 |
_offset = count_offset s (_offset pos) } |
|
74173 | 1370 |
|
74174 | 1371 |
symbol_explode :: BYTES a => a -> T -> T |
1372 |
symbol_explode = fold symbol . Symbol.explode . make_bytes |
|
1373 |
||
1374 |
symbol_explode_string :: String -> T -> T |
|
1375 |
symbol_explode_string = symbol_explode |
|
74167 | 1376 |
|
1377 |
||
1378 |
{- shift offsets -} |
|
1379 |
||
1380 |
shift_offsets :: Int -> T -> T |
|
1381 |
shift_offsets shift pos = pos { _offset = offset', _end_offset = end_offset' } |
|
1382 |
where |
|
1383 |
offset = _offset pos |
|
1384 |
end_offset = _end_offset pos |
|
1385 |
offset' = if invalid offset || invalid shift then offset else offset + shift |
|
1386 |
end_offset' = if invalid end_offset || invalid shift then end_offset else end_offset + shift |
|
1387 |
||
1388 |
||
1389 |
{- markup properties -} |
|
1390 |
||
1391 |
get_string :: Properties.T -> Bytes -> Bytes |
|
74168
f0b2136e2204
tuned signature: prefer existing Haskell operations;
wenzelm
parents:
74167
diff
changeset
|
1392 |
get_string props name = fromMaybe "" (Properties.get_value Just props name) |
74167 | 1393 |
|
1394 |
get_int :: Properties.T -> Bytes -> Int |
|
74168
f0b2136e2204
tuned signature: prefer existing Haskell operations;
wenzelm
parents:
74167
diff
changeset
|
1395 |
get_int props name = fromMaybe 0 (Properties.get_value Value.parse_int props name) |
74167 | 1396 |
|
1397 |
of_properties :: Properties.T -> T |
|
1398 |
of_properties props = |
|
1399 |
none { |
|
1400 |
_line = get_int props Markup.lineN, |
|
1401 |
_offset = get_int props Markup.offsetN, |
|
1402 |
_end_offset = get_int props Markup.end_offsetN, |
|
78021 | 1403 |
_label = get_string props Markup.labelN, |
74167 | 1404 |
_file = get_string props Markup.fileN, |
1405 |
_id = get_string props Markup.idN } |
|
1406 |
||
1407 |
string_entry :: Bytes -> Bytes -> Properties.T |
|
1408 |
string_entry k s = if Bytes.null s then [] else [(k, s)] |
|
1409 |
||
1410 |
int_entry :: Bytes -> Int -> Properties.T |
|
1411 |
int_entry k i = if invalid i then [] else [(k, Value.print_int i)] |
|
1412 |
||
1413 |
properties_of :: T -> Properties.T |
|
1414 |
properties_of pos = |
|
1415 |
int_entry Markup.lineN (_line pos) ++ |
|
1416 |
int_entry Markup.offsetN (_offset pos) ++ |
|
1417 |
int_entry Markup.end_offsetN (_end_offset pos) ++ |
|
78021 | 1418 |
string_entry Markup.labelN (_label pos) ++ |
74167 | 1419 |
string_entry Markup.fileN (_file pos) ++ |
1420 |
string_entry Markup.idN (_id pos) |
|
1421 |
||
1422 |
def_properties_of :: T -> Properties.T |
|
74182 | 1423 |
def_properties_of = properties_of #> map (first Markup.def_name) |
74167 | 1424 |
|
1425 |
entity_markup :: Bytes -> (Bytes, T) -> Markup.T |
|
1426 |
entity_markup kind (name, pos) = |
|
1427 |
Markup.entity kind name |> Markup.properties (def_properties_of pos) |
|
1428 |
||
74184 | 1429 |
make_entity_markup :: Bool -> Int -> Bytes -> (Bytes, T) -> Markup.T |
1430 |
make_entity_markup def serial kind (name, pos) = |
|
1431 |
let |
|
1432 |
props = |
|
1433 |
if def then (Markup.defN, Value.print_int serial) : properties_of pos |
|
1434 |
else (Markup.refN, Value.print_int serial) : def_properties_of pos |
|
1435 |
in Markup.entity kind name |> Markup.properties props |
|
74167 | 1436 |
|
1437 |
||
1438 |
{- reports -} |
|
1439 |
||
74187 | 1440 |
type Report = (T, Markup.T) |
1441 |
type Report_Text = (Report, Bytes) |
|
1442 |
||
74167 | 1443 |
is_reported :: T -> Bool |
1444 |
is_reported pos = isJust (offset_of pos) && isJust (id_of pos) |
|
1445 |
||
1446 |
is_reported_range :: T -> Bool |
|
1447 |
is_reported_range pos = is_reported pos && isJust (end_offset_of pos) |
|
1448 |
||
1449 |
||
1450 |
{- here: user output -} |
|
1451 |
||
1452 |
here :: T -> Bytes |
|
1453 |
here pos = if Bytes.null s2 then "" else s1 <> m1 <> s2 <> m2 |
|
1454 |
where |
|
1455 |
props = properties_of pos |
|
1456 |
(m1, m2) = YXML.output_markup (Markup.properties props Markup.position) |
|
1457 |
(s1, s2) = |
|
1458 |
case (line_of pos, file_of pos) of |
|
1459 |
(Just i, Nothing) -> (" ", "(line " <> Value.print_int i <> ")") |
|
1460 |
(Just i, Just name) -> (" ", "(line " <> Value.print_int i <> " of " <> quote name <> ")") |
|
1461 |
(Nothing, Just name) -> (" ", "(file " <> quote name <> ")") |
|
1462 |
_ -> if is_reported pos then ("", "\092<^here>") else ("", "") |
|
1463 |
||
1464 |
||
1465 |
{- range -} |
|
1466 |
||
1467 |
type Range = (T, T) |
|
1468 |
||
1469 |
no_range :: Range |
|
1470 |
no_range = (none, none) |
|
1471 |
||
1472 |
no_range_position :: T -> T |
|
1473 |
no_range_position pos = pos { _end_offset = 0 } |
|
1474 |
||
1475 |
range_position :: Range -> T |
|
1476 |
range_position (pos, pos') = pos { _end_offset = _offset pos' } |
|
1477 |
||
1478 |
range :: Range -> Range |
|
1479 |
range (pos, pos') = (range_position (pos, pos'), no_range_position pos') |
|
1480 |
\<close> |
|
1481 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
1482 |
generate_file "Isabelle/XML.hs" = \<open> |
69445 | 1483 |
{- Title: Isabelle/XML.hs |
69225 | 1484 |
Author: Makarius |
1485 |
LICENSE: BSD 3-clause (Isabelle) |
|
1486 |
||
1487 |
Untyped XML trees and representation of ML values. |
|
69280 | 1488 |
|
74178 | 1489 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/xml.ML\<close>. |
69225 | 1490 |
-} |
1491 |
||
74095 | 1492 |
{-# LANGUAGE OverloadedStrings #-} |
73177 | 1493 |
{-# OPTIONS_GHC -fno-warn-missing-signatures #-} |
1494 |
||
69225 | 1495 |
module Isabelle.XML (Attributes, Body, Tree(..), wrap_elem, unwrap_elem, content_of) |
1496 |
where |
|
1497 |
||
1498 |
import Isabelle.Library |
|
1499 |
import qualified Isabelle.Properties as Properties |
|
1500 |
import qualified Isabelle.Markup as Markup |
|
1501 |
import qualified Isabelle.Buffer as Buffer |
|
74095 | 1502 |
import qualified Isabelle.Bytes as Bytes |
1503 |
import Isabelle.Bytes (Bytes) |
|
69225 | 1504 |
|
1505 |
||
1506 |
{- types -} |
|
1507 |
||
1508 |
type Attributes = Properties.T |
|
1509 |
type Body = [Tree] |
|
74095 | 1510 |
data Tree = Elem (Markup.T, Body) | Text Bytes |
69225 | 1511 |
|
1512 |
||
1513 |
{- wrapped elements -} |
|
1514 |
||
69482 | 1515 |
wrap_elem :: ((Markup.T, Body), [Tree]) -> Tree |
69236 | 1516 |
wrap_elem (((a, atts), body1), body2) = |
69290 | 1517 |
Elem ((\<open>XML.xml_elemN\<close>, (\<open>XML.xml_nameN\<close>, a) : atts), Elem ((\<open>XML.xml_bodyN\<close>, []), body1) : body2) |
69225 | 1518 |
|
69482 | 1519 |
unwrap_elem :: Tree -> Maybe ((Markup.T, Body), [Tree]) |
69236 | 1520 |
unwrap_elem |
69290 | 1521 |
(Elem ((\<open>XML.xml_elemN\<close>, (\<open>XML.xml_nameN\<close>, a) : atts), Elem ((\<open>XML.xml_bodyN\<close>, []), body1) : body2)) = |
69236 | 1522 |
Just (((a, atts), body1), body2) |
69225 | 1523 |
unwrap_elem _ = Nothing |
1524 |
||
1525 |
||
1526 |
{- text content -} |
|
1527 |
||
69482 | 1528 |
add_content :: Tree -> Buffer.T -> Buffer.T |
69225 | 1529 |
add_content tree = |
1530 |
case unwrap_elem tree of |
|
1531 |
Just (_, ts) -> fold add_content ts |
|
1532 |
Nothing -> |
|
1533 |
case tree of |
|
69290 | 1534 |
Elem (_, ts) -> fold add_content ts |
69225 | 1535 |
Text s -> Buffer.add s |
1536 |
||
74095 | 1537 |
content_of :: Body -> Bytes |
74231 | 1538 |
content_of = Buffer.build_content . fold add_content |
69225 | 1539 |
|
1540 |
||
1541 |
{- string representation -} |
|
1542 |
||
74095 | 1543 |
encode_char :: Char -> String |
1544 |
encode_char '<' = "<" |
|
1545 |
encode_char '>' = ">" |
|
1546 |
encode_char '&' = "&" |
|
1547 |
encode_char '\'' = "'" |
|
1548 |
encode_char '\"' = """ |
|
1549 |
encode_char c = [c] |
|
1550 |
||
1551 |
encode_text :: Bytes -> Bytes |
|
1552 |
encode_text = make_bytes . concatMap (encode_char . Bytes.char) . Bytes.unpack |
|
69225 | 1553 |
|
1554 |
instance Show Tree where |
|
1555 |
show tree = |
|
74231 | 1556 |
make_string $ Buffer.build_content (show_tree tree) |
69225 | 1557 |
where |
69290 | 1558 |
show_tree (Elem ((name, atts), [])) = |
69225 | 1559 |
Buffer.add "<" #> Buffer.add (show_elem name atts) #> Buffer.add "/>" |
69290 | 1560 |
show_tree (Elem ((name, atts), ts)) = |
69225 | 1561 |
Buffer.add "<" #> Buffer.add (show_elem name atts) #> Buffer.add ">" #> |
1562 |
fold show_tree ts #> |
|
1563 |
Buffer.add "</" #> Buffer.add name #> Buffer.add ">" |
|
74095 | 1564 |
show_tree (Text s) = Buffer.add (encode_text s) |
69225 | 1565 |
|
1566 |
show_elem name atts = |
|
80910 | 1567 |
implode_space (name : map (\(a, x) -> a <> "=\"" <> encode_text x <> "\"") atts) |
69225 | 1568 |
\<close> |
1569 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
1570 |
generate_file "Isabelle/XML/Encode.hs" = \<open> |
69445 | 1571 |
{- Title: Isabelle/XML/Encode.hs |
69240 | 1572 |
Author: Makarius |
1573 |
LICENSE: BSD 3-clause (Isabelle) |
|
1574 |
||
1575 |
XML as data representation language. |
|
69280 | 1576 |
|
74178 | 1577 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/xml.ML\<close>. |
69240 | 1578 |
-} |
1579 |
||
74095 | 1580 |
{-# LANGUAGE OverloadedStrings #-} |
73177 | 1581 |
{-# OPTIONS_GHC -fno-warn-missing-signatures #-} |
1582 |
||
69240 | 1583 |
module Isabelle.XML.Encode ( |
70845 | 1584 |
A, T, V, P, |
69240 | 1585 |
|
1586 |
int_atom, bool_atom, unit_atom, |
|
1587 |
||
71490 | 1588 |
tree, properties, string, int, bool, unit, pair, triple, list, option, variant |
69240 | 1589 |
) |
1590 |
where |
|
1591 |
||
74168
f0b2136e2204
tuned signature: prefer existing Haskell operations;
wenzelm
parents:
74167
diff
changeset
|
1592 |
import Data.Maybe (fromJust) |
f0b2136e2204
tuned signature: prefer existing Haskell operations;
wenzelm
parents:
74167
diff
changeset
|
1593 |
|
69240 | 1594 |
import Isabelle.Library |
74095 | 1595 |
import Isabelle.Bytes (Bytes) |
69240 | 1596 |
import qualified Isabelle.Value as Value |
1597 |
import qualified Isabelle.Properties as Properties |
|
1598 |
import qualified Isabelle.XML as XML |
|
1599 |
||
1600 |
||
74095 | 1601 |
type A a = a -> Bytes |
69240 | 1602 |
type T a = a -> XML.Body |
74095 | 1603 |
type V a = a -> Maybe ([Bytes], XML.Body) |
1604 |
type P a = a -> [Bytes] |
|
69240 | 1605 |
|
1606 |
||
1607 |
-- atomic values |
|
1608 |
||
1609 |
int_atom :: A Int |
|
1610 |
int_atom = Value.print_int |
|
1611 |
||
1612 |
bool_atom :: A Bool |
|
1613 |
bool_atom False = "0" |
|
1614 |
bool_atom True = "1" |
|
1615 |
||
1616 |
unit_atom :: A () |
|
1617 |
unit_atom () = "" |
|
1618 |
||
1619 |
||
1620 |
-- structural nodes |
|
1621 |
||
69290 | 1622 |
node ts = XML.Elem ((":", []), ts) |
69240 | 1623 |
|
1624 |
vector = map_index (\(i, x) -> (int_atom i, x)) |
|
1625 |
||
69290 | 1626 |
tagged (tag, (xs, ts)) = XML.Elem ((int_atom tag, vector xs), ts) |
69240 | 1627 |
|
1628 |
||
1629 |
-- representation of standard types |
|
1630 |
||
1631 |
tree :: T XML.Tree |
|
1632 |
tree t = [t] |
|
1633 |
||
1634 |
properties :: T Properties.T |
|
69290 | 1635 |
properties props = [XML.Elem ((":", props), [])] |
69240 | 1636 |
|
74095 | 1637 |
string :: T Bytes |
69240 | 1638 |
string "" = [] |
1639 |
string s = [XML.Text s] |
|
1640 |
||
1641 |
int :: T Int |
|
1642 |
int = string . int_atom |
|
1643 |
||
1644 |
bool :: T Bool |
|
1645 |
bool = string . bool_atom |
|
1646 |
||
1647 |
unit :: T () |
|
1648 |
unit = string . unit_atom |
|
1649 |
||
1650 |
pair :: T a -> T b -> T (a, b) |
|
1651 |
pair f g (x, y) = [node (f x), node (g y)] |
|
1652 |
||
1653 |
triple :: T a -> T b -> T c -> T (a, b, c) |
|
1654 |
triple f g h (x, y, z) = [node (f x), node (g y), node (h z)] |
|
1655 |
||
1656 |
list :: T a -> T [a] |
|
1657 |
list f xs = map (node . f) xs |
|
1658 |
||
71490 | 1659 |
option :: T a -> T (Maybe a) |
1660 |
option _ Nothing = [] |
|
1661 |
option f (Just x) = [node (f x)] |
|
1662 |
||
69240 | 1663 |
variant :: [V a] -> T a |
74168
f0b2136e2204
tuned signature: prefer existing Haskell operations;
wenzelm
parents:
74167
diff
changeset
|
1664 |
variant fs x = [tagged (fromJust (get_index (\f -> f x) fs))] |
69240 | 1665 |
\<close> |
1666 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
1667 |
generate_file "Isabelle/XML/Decode.hs" = \<open> |
69445 | 1668 |
{- Title: Isabelle/XML/Decode.hs |
69240 | 1669 |
Author: Makarius |
1670 |
LICENSE: BSD 3-clause (Isabelle) |
|
1671 |
||
1672 |
XML as data representation language. |
|
69280 | 1673 |
|
74178 | 1674 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/xml.ML\<close>. |
69240 | 1675 |
-} |
1676 |
||
74095 | 1677 |
{-# LANGUAGE OverloadedStrings #-} |
73177 | 1678 |
{-# OPTIONS_GHC -fno-warn-missing-signatures #-} |
1679 |
||
69240 | 1680 |
module Isabelle.XML.Decode ( |
70845 | 1681 |
A, T, V, P, |
69240 | 1682 |
|
1683 |
int_atom, bool_atom, unit_atom, |
|
1684 |
||
71490 | 1685 |
tree, properties, string, int, bool, unit, pair, triple, list, option, variant |
69240 | 1686 |
) |
1687 |
where |
|
1688 |
||
1689 |
import Isabelle.Library |
|
74095 | 1690 |
import Isabelle.Bytes (Bytes) |
69240 | 1691 |
import qualified Isabelle.Value as Value |
1692 |
import qualified Isabelle.Properties as Properties |
|
1693 |
import qualified Isabelle.XML as XML |
|
1694 |
||
1695 |
||
74095 | 1696 |
type A a = Bytes -> a |
69240 | 1697 |
type T a = XML.Body -> a |
74095 | 1698 |
type V a = ([Bytes], XML.Body) -> a |
1699 |
type P a = [Bytes] -> a |
|
69240 | 1700 |
|
1701 |
err_atom = error "Malformed XML atom" |
|
1702 |
err_body = error "Malformed XML body" |
|
1703 |
||
1704 |
||
1705 |
{- atomic values -} |
|
1706 |
||
1707 |
int_atom :: A Int |
|
1708 |
int_atom s = |
|
1709 |
case Value.parse_int s of |
|
1710 |
Just i -> i |
|
1711 |
Nothing -> err_atom |
|
1712 |
||
1713 |
bool_atom :: A Bool |
|
1714 |
bool_atom "0" = False |
|
1715 |
bool_atom "1" = True |
|
1716 |
bool_atom _ = err_atom |
|
1717 |
||
1718 |
unit_atom :: A () |
|
1719 |
unit_atom "" = () |
|
1720 |
unit_atom _ = err_atom |
|
1721 |
||
1722 |
||
1723 |
{- structural nodes -} |
|
1724 |
||
69290 | 1725 |
node (XML.Elem ((":", []), ts)) = ts |
69240 | 1726 |
node _ = err_body |
1727 |
||
1728 |
vector atts = |
|
1729 |
map_index (\(i, (a, x)) -> if int_atom a == i then x else err_atom) atts |
|
1730 |
||
69290 | 1731 |
tagged (XML.Elem ((name, atts), ts)) = (int_atom name, (vector atts, ts)) |
69240 | 1732 |
tagged _ = err_body |
1733 |
||
1734 |
||
1735 |
{- representation of standard types -} |
|
1736 |
||
1737 |
tree :: T XML.Tree |
|
1738 |
tree [t] = t |
|
1739 |
tree _ = err_body |
|
1740 |
||
1741 |
properties :: T Properties.T |
|
69290 | 1742 |
properties [XML.Elem ((":", props), [])] = props |
69240 | 1743 |
properties _ = err_body |
1744 |
||
74095 | 1745 |
string :: T Bytes |
69240 | 1746 |
string [] = "" |
1747 |
string [XML.Text s] = s |
|
1748 |
string _ = err_body |
|
1749 |
||
1750 |
int :: T Int |
|
1751 |
int = int_atom . string |
|
1752 |
||
1753 |
bool :: T Bool |
|
1754 |
bool = bool_atom . string |
|
1755 |
||
1756 |
unit :: T () |
|
1757 |
unit = unit_atom . string |
|
1758 |
||
1759 |
pair :: T a -> T b -> T (a, b) |
|
1760 |
pair f g [t1, t2] = (f (node t1), g (node t2)) |
|
1761 |
pair _ _ _ = err_body |
|
1762 |
||
1763 |
triple :: T a -> T b -> T c -> T (a, b, c) |
|
1764 |
triple f g h [t1, t2, t3] = (f (node t1), g (node t2), h (node t3)) |
|
1765 |
triple _ _ _ _ = err_body |
|
1766 |
||
1767 |
list :: T a -> T [a] |
|
1768 |
list f ts = map (f . node) ts |
|
1769 |
||
1770 |
option :: T a -> T (Maybe a) |
|
1771 |
option _ [] = Nothing |
|
1772 |
option f [t] = Just (f (node t)) |
|
1773 |
option _ _ = err_body |
|
1774 |
||
1775 |
variant :: [V a] -> T a |
|
1776 |
variant fs [t] = (fs !! tag) (xs, ts) |
|
1777 |
where (tag, (xs, ts)) = tagged t |
|
1778 |
variant _ _ = err_body |
|
1779 |
\<close> |
|
1780 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
1781 |
generate_file "Isabelle/YXML.hs" = \<open> |
69445 | 1782 |
{- Title: Isabelle/YXML.hs |
69288 | 1783 |
Author: Makarius |
1784 |
LICENSE: BSD 3-clause (Isabelle) |
|
1785 |
||
1786 |
Efficient text representation of XML trees. Suitable for direct |
|
1787 |
inlining into plain text. |
|
1788 |
||
74178 | 1789 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/yxml.ML\<close>. |
69288 | 1790 |
-} |
1791 |
||
74095 | 1792 |
{-# LANGUAGE OverloadedStrings #-} |
73177 | 1793 |
{-# OPTIONS_GHC -fno-warn-missing-signatures -fno-warn-incomplete-patterns #-} |
1794 |
||
69288 | 1795 |
module Isabelle.YXML (charX, charY, strX, strY, detect, output_markup, |
1796 |
buffer_body, buffer, string_of_body, string_of, parse_body, parse) |
|
1797 |
where |
|
1798 |
||
1799 |
import qualified Data.List as List |
|
74095 | 1800 |
import Data.Word (Word8) |
69288 | 1801 |
|
1802 |
import Isabelle.Library |
|
74095 | 1803 |
import qualified Isabelle.Bytes as Bytes |
1804 |
import Isabelle.Bytes (Bytes) |
|
69288 | 1805 |
import qualified Isabelle.Markup as Markup |
1806 |
import qualified Isabelle.XML as XML |
|
1807 |
import qualified Isabelle.Buffer as Buffer |
|
1808 |
||
1809 |
||
1810 |
{- markers -} |
|
1811 |
||
74095 | 1812 |
charX, charY :: Word8 |
1813 |
charX = 5 |
|
1814 |
charY = 6 |
|
1815 |
||
1816 |
strX, strY, strXY, strXYX :: Bytes |
|
1817 |
strX = Bytes.singleton charX |
|
1818 |
strY = Bytes.singleton charY |
|
74081 | 1819 |
strXY = strX <> strY |
1820 |
strXYX = strXY <> strX |
|
69288 | 1821 |
|
74095 | 1822 |
detect :: Bytes -> Bool |
1823 |
detect = Bytes.any (\c -> c == charX || c == charY) |
|
69288 | 1824 |
|
1825 |
||
1826 |
{- output -} |
|
1827 |
||
1828 |
output_markup :: Markup.T -> Markup.Output |
|
1829 |
output_markup markup@(name, atts) = |
|
1830 |
if Markup.is_empty markup then Markup.no_output |
|
74095 | 1831 |
else (strXY <> name <> Bytes.concat (map (\(a, x) -> strY <> a <> "=" <> x) atts) <> strX, strXYX) |
69288 | 1832 |
|
1833 |
buffer_attrib (a, x) = |
|
1834 |
Buffer.add strY #> Buffer.add a #> Buffer.add "=" #> Buffer.add x |
|
1835 |
||
1836 |
buffer_body :: XML.Body -> Buffer.T -> Buffer.T |
|
1837 |
buffer_body = fold buffer |
|
1838 |
||
1839 |
buffer :: XML.Tree -> Buffer.T -> Buffer.T |
|
69290 | 1840 |
buffer (XML.Elem ((name, atts), ts)) = |
69288 | 1841 |
Buffer.add strXY #> Buffer.add name #> fold buffer_attrib atts #> Buffer.add strX #> |
1842 |
buffer_body ts #> |
|
1843 |
Buffer.add strXYX |
|
1844 |
buffer (XML.Text s) = Buffer.add s |
|
1845 |
||
74095 | 1846 |
string_of_body :: XML.Body -> Bytes |
74231 | 1847 |
string_of_body = Buffer.build_content . buffer_body |
69288 | 1848 |
|
74095 | 1849 |
string_of :: XML.Tree -> Bytes |
69288 | 1850 |
string_of = string_of_body . single |
1851 |
||
1852 |
||
1853 |
{- parse -} |
|
1854 |
||
1855 |
-- split: fields or non-empty tokens |
|
1856 |
||
74095 | 1857 |
split :: Bool -> Word8 -> [Word8] -> [[Word8]] |
69288 | 1858 |
split _ _ [] = [] |
1859 |
split fields sep str = splitting str |
|
1860 |
where |
|
1861 |
splitting rest = |
|
1862 |
case span (/= sep) rest of |
|
1863 |
(_, []) -> cons rest [] |
|
1864 |
(prfx, _ : rest') -> cons prfx (splitting rest') |
|
1865 |
cons item = if fields || not (null item) then (:) item else id |
|
1866 |
||
1867 |
||
1868 |
-- structural errors |
|
1869 |
||
74095 | 1870 |
err :: Bytes -> a |
1871 |
err msg = error (make_string ("Malformed YXML: " <> msg)) |
|
1872 |
||
69288 | 1873 |
err_attribute = err "bad attribute" |
1874 |
err_element = err "bad element" |
|
74095 | 1875 |
|
1876 |
err_unbalanced :: Bytes -> a |
|
1877 |
err_unbalanced name = |
|
1878 |
if Bytes.null name then err "unbalanced element" |
|
1879 |
else err ("unbalanced element " <> quote name) |
|
69288 | 1880 |
|
1881 |
||
1882 |
-- stack operations |
|
1883 |
||
1884 |
add x ((elem, body) : pending) = (elem, x : body) : pending |
|
1885 |
||
74095 | 1886 |
push name atts pending = |
1887 |
if Bytes.null name then err_element |
|
1888 |
else ((name, atts), []) : pending |
|
1889 |
||
1890 |
pop (((name, atts), body) : pending) = |
|
1891 |
if Bytes.null name then err_unbalanced name |
|
1892 |
else add (XML.Elem ((name, atts), reverse body)) pending |
|
69288 | 1893 |
|
1894 |
||
1895 |
-- parsing |
|
1896 |
||
1897 |
parse_attrib s = |
|
74095 | 1898 |
case List.elemIndex (Bytes.byte '=') s of |
1899 |
Just i | i > 0 -> (Bytes.pack $ take i s, Bytes.pack $ drop (i + 1) s) |
|
69288 | 1900 |
_ -> err_attribute |
1901 |
||
74095 | 1902 |
parse_chunk [[], []] = pop |
1903 |
parse_chunk ([] : name : atts) = push (Bytes.pack name) (map parse_attrib atts) |
|
1904 |
parse_chunk txts = fold (add . XML.Text . Bytes.pack) txts |
|
1905 |
||
1906 |
parse_body :: Bytes -> XML.Body |
|
69288 | 1907 |
parse_body source = |
74095 | 1908 |
case fold parse_chunk chunks [((Bytes.empty, []), [])] of |
1909 |
[((name, _), result)] | Bytes.null name -> reverse result |
|
69288 | 1910 |
((name, _), _) : _ -> err_unbalanced name |
74095 | 1911 |
where chunks = source |> Bytes.unpack |> split False charX |> map (split True charY) |
1912 |
||
1913 |
parse :: Bytes -> XML.Tree |
|
69288 | 1914 |
parse source = |
1915 |
case parse_body source of |
|
1916 |
[result] -> result |
|
1917 |
[] -> XML.Text "" |
|
1918 |
_ -> err "multiple results" |
|
1919 |
\<close> |
|
1920 |
||
74091 | 1921 |
generate_file "Isabelle/Completion.hs" = \<open> |
1922 |
{- Title: Isabelle/Completion.hs |
|
1923 |
Author: Makarius |
|
1924 |
LICENSE: BSD 3-clause (Isabelle) |
|
1925 |
||
1926 |
Completion of names. |
|
1927 |
||
74178 | 1928 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/General/completion.ML\<close>. |
74091 | 1929 |
-} |
1930 |
||
74095 | 1931 |
{-# LANGUAGE OverloadedStrings #-} |
1932 |
||
74091 | 1933 |
module Isabelle.Completion ( |
1934 |
Name, T, names, none, make, markup_element, markup_report, make_report |
|
1935 |
) where |
|
1936 |
||
74095 | 1937 |
import qualified Isabelle.Bytes as Bytes |
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1938 |
import qualified Isabelle.Name as Name |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1939 |
import Isabelle.Name (Name) |
74091 | 1940 |
import qualified Isabelle.Properties as Properties |
1941 |
import qualified Isabelle.Markup as Markup |
|
74129 | 1942 |
import Isabelle.XML.Classes |
74091 | 1943 |
import qualified Isabelle.XML as XML |
1944 |
import qualified Isabelle.YXML as YXML |
|
1945 |
||
1946 |
||
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1947 |
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
|
1948 |
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
|
1949 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1950 |
names :: Int -> Properties.T -> Names -> T |
74091 | 1951 |
names limit props names = Completion props (length names) (take limit names) |
1952 |
||
1953 |
none :: T |
|
1954 |
none = names 0 [] [] |
|
1955 |
||
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1956 |
make :: Int -> (Name, Properties.T) -> ((Name -> Bool) -> Names) -> T |
74091 | 1957 |
make limit (name, props) make_names = |
74095 | 1958 |
if name /= "" && name /= "_" then |
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1959 |
names limit props (make_names (Bytes.isPrefixOf (Name.clean name))) |
74091 | 1960 |
else none |
1961 |
||
1962 |
markup_element :: T -> (Markup.T, XML.Body) |
|
1963 |
markup_element (Completion props total names) = |
|
1964 |
if not (null names) then |
|
74129 | 1965 |
(Markup.properties props Markup.completion, encode (total, names)) |
74091 | 1966 |
else (Markup.empty, []) |
1967 |
||
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1968 |
markup_report :: [T] -> Name |
74095 | 1969 |
markup_report [] = Bytes.empty |
74091 | 1970 |
markup_report elems = |
1971 |
YXML.string_of $ XML.Elem (Markup.report, map (XML.Elem . markup_element) elems) |
|
1972 |
||
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
1973 |
make_report :: Int -> (Name, Properties.T) -> ((Name -> Bool) -> Names) -> Name |
74091 | 1974 |
make_report limit name_props make_names = |
1975 |
markup_report [make limit name_props make_names] |
|
1976 |
\<close> |
|
1977 |
||
1978 |
generate_file "Isabelle/File.hs" = \<open> |
|
1979 |
{- Title: Isabelle/File.hs |
|
1980 |
Author: Makarius |
|
1981 |
LICENSE: BSD 3-clause (Isabelle) |
|
1982 |
||
1983 |
File-system operations. |
|
1984 |
||
74178 | 1985 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/General/file.ML\<close>. |
74091 | 1986 |
-} |
1987 |
||
74098 | 1988 |
module Isabelle.File (read, write, append) where |
74091 | 1989 |
|
1990 |
import Prelude hiding (read) |
|
1991 |
import qualified System.IO as IO |
|
74098 | 1992 |
import qualified Data.ByteString as ByteString |
1993 |
import qualified Isabelle.Bytes as Bytes |
|
1994 |
import Isabelle.Bytes (Bytes) |
|
1995 |
||
1996 |
read :: IO.FilePath -> IO Bytes |
|
1997 |
read path = Bytes.make <$> IO.withFile path IO.ReadMode ByteString.hGetContents |
|
1998 |
||
1999 |
write :: IO.FilePath -> Bytes -> IO () |
|
2000 |
write path bs = IO.withFile path IO.WriteMode (\h -> ByteString.hPut h (Bytes.unmake bs)) |
|
2001 |
||
2002 |
append :: IO.FilePath -> Bytes -> IO () |
|
2003 |
append path bs = IO.withFile path IO.AppendMode (\h -> ByteString.hPut h (Bytes.unmake bs)) |
|
74091 | 2004 |
\<close> |
2005 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
2006 |
generate_file "Isabelle/Pretty.hs" = \<open> |
69445 | 2007 |
{- Title: Isabelle/Pretty.hs |
69248 | 2008 |
Author: Makarius |
2009 |
LICENSE: BSD 3-clause (Isabelle) |
|
2010 |
||
2011 |
Generic pretty printing module. |
|
69280 | 2012 |
|
74178 | 2013 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/General/pretty.ML\<close>. |
69248 | 2014 |
-} |
2015 |
||
74095 | 2016 |
{-# LANGUAGE OverloadedStrings #-} |
73177 | 2017 |
{-# OPTIONS_GHC -fno-warn-missing-signatures #-} |
2018 |
||
69248 | 2019 |
module Isabelle.Pretty ( |
2020 |
T, symbolic, formatted, unformatted, |
|
2021 |
||
2022 |
str, brk_indent, brk, fbrk, breaks, fbreaks, blk, block, strs, markup, mark, mark_str, marks_str, |
|
2023 |
item, text_fold, keyword1, keyword2, text, paragraph, para, quote, cartouche, separate, |
|
2024 |
commas, enclose, enum, list, str_list, big_list) |
|
2025 |
where |
|
2026 |
||
74095 | 2027 |
import qualified Data.List as List |
2028 |
||
2029 |
import qualified Isabelle.Bytes as Bytes |
|
2030 |
import Isabelle.Bytes (Bytes) |
|
74185 | 2031 |
import Isabelle.Library hiding (enclose, quote, separate, commas) |
69248 | 2032 |
import qualified Isabelle.Buffer as Buffer |
2033 |
import qualified Isabelle.Markup as Markup |
|
2034 |
import qualified Isabelle.XML as XML |
|
2035 |
import qualified Isabelle.YXML as YXML |
|
2036 |
||
2037 |
||
2038 |
data T = |
|
2039 |
Block Markup.T Bool Int [T] |
|
2040 |
| Break Int Int |
|
74095 | 2041 |
| Str Bytes |
69248 | 2042 |
|
2043 |
||
2044 |
{- output -} |
|
2045 |
||
74095 | 2046 |
symbolic_text s = if Bytes.null s then [] else [XML.Text s] |
69248 | 2047 |
|
2048 |
symbolic_markup markup body = |
|
2049 |
if Markup.is_empty markup then body |
|
69290 | 2050 |
else [XML.Elem (markup, body)] |
69248 | 2051 |
|
2052 |
symbolic :: T -> XML.Body |
|
2053 |
symbolic (Block markup consistent indent prts) = |
|
2054 |
concatMap symbolic prts |
|
2055 |
|> symbolic_markup block_markup |
|
2056 |
|> symbolic_markup markup |
|
2057 |
where block_markup = if null prts then Markup.empty else Markup.block consistent indent |
|
74095 | 2058 |
symbolic (Break wd ind) = [XML.Elem (Markup.break wd ind, symbolic_text (Bytes.spaces wd))] |
69248 | 2059 |
symbolic (Str s) = symbolic_text s |
2060 |
||
74095 | 2061 |
formatted :: T -> Bytes |
69248 | 2062 |
formatted = YXML.string_of_body . symbolic |
2063 |
||
74095 | 2064 |
unformatted :: T -> Bytes |
74231 | 2065 |
unformatted = Buffer.build_content . out |
69248 | 2066 |
where |
2067 |
out (Block markup _ _ prts) = |
|
2068 |
let (bg, en) = YXML.output_markup markup |
|
2069 |
in Buffer.add bg #> fold out prts #> Buffer.add en |
|
74095 | 2070 |
out (Break _ wd) = Buffer.add (Bytes.spaces wd) |
69248 | 2071 |
out (Str s) = Buffer.add s |
2072 |
||
2073 |
||
2074 |
{- derived operations to create formatting expressions -} |
|
2075 |
||
2076 |
force_nat n | n < 0 = 0 |
|
2077 |
force_nat n = n |
|
2078 |
||
74095 | 2079 |
str :: BYTES a => a -> T |
2080 |
str = Str . make_bytes |
|
69248 | 2081 |
|
2082 |
brk_indent :: Int -> Int -> T |
|
2083 |
brk_indent wd ind = Break (force_nat wd) ind |
|
2084 |
||
2085 |
brk :: Int -> T |
|
2086 |
brk wd = brk_indent wd 0 |
|
2087 |
||
2088 |
fbrk :: T |
|
74095 | 2089 |
fbrk = Str "\n" |
69248 | 2090 |
|
2091 |
breaks, fbreaks :: [T] -> [T] |
|
2092 |
breaks = List.intersperse (brk 1) |
|
2093 |
fbreaks = List.intersperse fbrk |
|
2094 |
||
2095 |
blk :: (Int, [T]) -> T |
|
2096 |
blk (indent, es) = Block Markup.empty False (force_nat indent) es |
|
2097 |
||
2098 |
block :: [T] -> T |
|
2099 |
block prts = blk (2, prts) |
|
2100 |
||
74095 | 2101 |
strs :: BYTES a => [a] -> T |
69248 | 2102 |
strs = block . breaks . map str |
2103 |
||
2104 |
markup :: Markup.T -> [T] -> T |
|
2105 |
markup m = Block m False 0 |
|
2106 |
||
2107 |
mark :: Markup.T -> T -> T |
|
2108 |
mark m prt = if m == Markup.empty then prt else markup m [prt] |
|
2109 |
||
74095 | 2110 |
mark_str :: BYTES a => (Markup.T, a) -> T |
69248 | 2111 |
mark_str (m, s) = mark m (str s) |
2112 |
||
74095 | 2113 |
marks_str :: BYTES a => ([Markup.T], a) -> T |
69248 | 2114 |
marks_str (ms, s) = fold_rev mark ms (str s) |
2115 |
||
2116 |
item :: [T] -> T |
|
2117 |
item = markup Markup.item |
|
2118 |
||
2119 |
text_fold :: [T] -> T |
|
2120 |
text_fold = markup Markup.text_fold |
|
2121 |
||
74095 | 2122 |
keyword1, keyword2 :: BYTES a => a -> T |
69248 | 2123 |
keyword1 name = mark_str (Markup.keyword1, name) |
2124 |
keyword2 name = mark_str (Markup.keyword2, name) |
|
2125 |
||
74095 | 2126 |
text :: BYTES a => a -> [T] |
74132 | 2127 |
text = breaks . map str . filter (not . Bytes.null) . space_explode ' ' . make_bytes |
69248 | 2128 |
|
2129 |
paragraph :: [T] -> T |
|
2130 |
paragraph = markup Markup.paragraph |
|
2131 |
||
74095 | 2132 |
para :: BYTES a => a -> T |
69248 | 2133 |
para = paragraph . text |
2134 |
||
2135 |
quote :: T -> T |
|
74095 | 2136 |
quote prt = blk (1, [Str "\"", prt, Str "\""]) |
69248 | 2137 |
|
2138 |
cartouche :: T -> T |
|
74095 | 2139 |
cartouche prt = blk (1, [Str "\92<open>", prt, Str "\92<close>"]) |
2140 |
||
2141 |
separate :: BYTES a => a -> [T] -> [T] |
|
69248 | 2142 |
separate sep = List.intercalate [str sep, brk 1] . map single |
2143 |
||
2144 |
commas :: [T] -> [T] |
|
74095 | 2145 |
commas = separate ("," :: Bytes) |
2146 |
||
2147 |
enclose :: BYTES a => a -> a -> [T] -> T |
|
74081 | 2148 |
enclose lpar rpar prts = block (str lpar : prts <> [str rpar]) |
69248 | 2149 |
|
74095 | 2150 |
enum :: BYTES a => a -> a -> a -> [T] -> T |
69248 | 2151 |
enum sep lpar rpar = enclose lpar rpar . separate sep |
2152 |
||
74095 | 2153 |
list :: BYTES a => a -> a -> [T] -> T |
69248 | 2154 |
list = enum "," |
2155 |
||
74095 | 2156 |
str_list :: BYTES a => a -> a -> [a] -> T |
69248 | 2157 |
str_list lpar rpar = list lpar rpar . map str |
2158 |
||
74095 | 2159 |
big_list :: BYTES a => a -> [T] -> T |
69248 | 2160 |
big_list name prts = block (fbreaks (str name : prts)) |
2161 |
\<close> |
|
2162 |
||
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2163 |
generate_file "Isabelle/Name.hs" = \<open> |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2164 |
{- Title: Isabelle/Name.hs |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2165 |
Author: Makarius |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2166 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2167 |
Names of basic logical entities (variables etc.). |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2168 |
|
74178 | 2169 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/name.ML\<close>. |
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2170 |
-} |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2171 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2172 |
{-# LANGUAGE OverloadedStrings #-} |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2173 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2174 |
module Isabelle.Name ( |
74194 | 2175 |
Name, |
2176 |
uu, uu_, aT, |
|
74217 | 2177 |
clean_index, clean, internal, skolem, is_internal, is_skolem, dest_internal, dest_skolem, |
80601 | 2178 |
Context, declare, declare_renamed, is_declared, declared, context, make_context, |
74327 | 2179 |
variant, variant_list |
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2180 |
) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2181 |
where |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2182 |
|
74315
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2183 |
import Data.Maybe (fromMaybe) |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2184 |
import Data.Map.Strict (Map) |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2185 |
import qualified Data.Map.Strict as Map |
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2186 |
import Data.Word (Word8) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2187 |
import qualified Isabelle.Bytes as Bytes |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2188 |
import Isabelle.Bytes (Bytes) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2189 |
import qualified Isabelle.Symbol as Symbol |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2190 |
import Isabelle.Library |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2191 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2192 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2193 |
type Name = Bytes |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2194 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2195 |
|
74194 | 2196 |
{- common defaults -} |
2197 |
||
2198 |
uu, uu_, aT :: Name |
|
2199 |
uu = "uu" |
|
2200 |
uu_ = "uu_" |
|
2201 |
aT = "'a" |
|
2202 |
||
2203 |
||
74196 | 2204 |
{- internal names -- NB: internal subsumes skolem -} |
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2205 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2206 |
underscore :: Word8 |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2207 |
underscore = Bytes.byte '_' |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2208 |
|
74196 | 2209 |
internal, skolem :: Name -> Name |
2210 |
internal x = x <> "_" |
|
2211 |
skolem x = x <> "__" |
|
2212 |
||
2213 |
is_internal, is_skolem :: Name -> Bool |
|
2214 |
is_internal = Bytes.isSuffixOf "_" |
|
2215 |
is_skolem = Bytes.isSuffixOf "__" |
|
2216 |
||
74217 | 2217 |
dest_internal, dest_skolem :: Name -> Maybe Name |
2218 |
dest_internal = Bytes.try_unsuffix "_" |
|
2219 |
dest_skolem = Bytes.try_unsuffix "__" |
|
2220 |
||
74315
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2221 |
clean_index :: (Name, Int) -> (Name, Int) |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2222 |
clean_index (x, i) = |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2223 |
case dest_internal x of |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2224 |
Nothing -> (x, i) |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2225 |
Just x' -> clean_index (x', i + 1) |
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2226 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2227 |
clean :: Name -> Name |
74315
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2228 |
clean x = fst (clean_index (x, 0)) |
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2229 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2230 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2231 |
{- context for used names -} |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2232 |
|
74315
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2233 |
newtype Context = Context (Map Name (Maybe Name)) {-declared names with latest renaming-} |
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2234 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2235 |
declare :: Name -> Context -> Context |
74315
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2236 |
declare x (Context names) = |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2237 |
Context ( |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2238 |
let a = clean x |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2239 |
in if Map.member a names then names else Map.insert a Nothing names) |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2240 |
|
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2241 |
declare_renaming :: (Name, Name) -> Context -> Context |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2242 |
declare_renaming (x, x') (Context names) = |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2243 |
Context (Map.insert (clean x) (Just (clean x')) names) |
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2244 |
|
80601 | 2245 |
declare_renamed :: (Name, Name) -> Context -> Context |
2246 |
declare_renamed (x, x') = |
|
2247 |
(if clean x /= clean x' then declare_renaming (x, x') else id) #> declare x' |
|
2248 |
||
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2249 |
is_declared :: Context -> Name -> Bool |
74315
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2250 |
is_declared (Context names) x = Map.member x names |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2251 |
|
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2252 |
declared :: Context -> Name -> Maybe (Maybe Name) |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2253 |
declared (Context names) a = Map.lookup a names |
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2254 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2255 |
context :: Context |
74315
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2256 |
context = Context Map.empty |> fold declare ["", "'"] |
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2257 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2258 |
make_context :: [Name] -> Context |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2259 |
make_context used = fold declare used context |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2260 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2261 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2262 |
{- generating fresh names -} |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2263 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2264 |
bump_init :: Name -> Name |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2265 |
bump_init str = str <> "a" |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2266 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2267 |
bump_string :: Name -> Name |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2268 |
bump_string str = |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2269 |
let |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2270 |
a = Bytes.byte 'a' |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2271 |
z = Bytes.byte 'z' |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2272 |
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
|
2273 |
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
|
2274 |
bump bs = a : bs |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2275 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2276 |
rev = reverse (Bytes.unpack str) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2277 |
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
|
2278 |
part1 = reverse (bump (drop (length part2) rev)) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2279 |
in Bytes.pack (part1 <> part2) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2280 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2281 |
variant :: Name -> Context -> (Name, Context) |
74315
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2282 |
variant name ctxt = |
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2283 |
let |
74315
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2284 |
vary x = |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2285 |
case declared ctxt x of |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2286 |
Nothing -> x |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2287 |
Just x' -> vary (bump_string (fromMaybe x x')) |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2288 |
|
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2289 |
(x, n) = clean_index (name, 0) |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2290 |
(x', ctxt') = |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2291 |
if not (is_declared ctxt x) then (x, declare x ctxt) |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2292 |
else |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2293 |
let |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2294 |
x0 = bump_init x |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2295 |
x' = vary x0 |
80601 | 2296 |
ctxt' = ctxt |> declare_renamed (x0, x') |
74315
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2297 |
in (x', ctxt') |
30a0f5879d90
clarified operations: follow Isabelle/ML more closely;
wenzelm
parents:
74310
diff
changeset
|
2298 |
in (x' <> Bytes.pack (replicate n underscore), ctxt') |
74327 | 2299 |
|
2300 |
variant_list :: [Name] -> [Name] -> [Name] |
|
2301 |
variant_list used names = fst (make_context used |> fold_map variant names) |
|
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2302 |
\<close> |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2303 |
|
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
2304 |
generate_file "Isabelle/Term.hs" = \<open> |
69445 | 2305 |
{- Title: Isabelle/Term.hs |
69240 | 2306 |
Author: Makarius |
2307 |
LICENSE: BSD 3-clause (Isabelle) |
|
2308 |
||
2309 |
Lambda terms, types, sorts. |
|
69280 | 2310 |
|
74178 | 2311 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/term.scala\<close>. |
69240 | 2312 |
-} |
2313 |
||
74095 | 2314 |
{-# LANGUAGE OverloadedStrings #-} |
2315 |
||
69240 | 2316 |
module Isabelle.Term ( |
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2317 |
Indexname, Sort, Typ(..), Term(..), |
74214 | 2318 |
Free, lambda, declare_frees, incr_boundvars, subst_bound, dest_lambda, strip_lambda, |
74197 | 2319 |
type_op0, type_op1, op0, op1, op2, typed_op0, typed_op1, typed_op2, binder, |
74105 | 2320 |
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
|
2321 |
aconv, list_comb, strip_comb, head_of |
74105 | 2322 |
) |
69240 | 2323 |
where |
2324 |
||
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2325 |
import Isabelle.Library |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2326 |
import qualified Isabelle.Name as Name |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2327 |
import Isabelle.Name (Name) |
74095 | 2328 |
|
74105 | 2329 |
infixr 5 --> |
2330 |
infixr ---> |
|
2331 |
||
2332 |
||
2333 |
{- types and terms -} |
|
2334 |
||
2335 |
type Indexname = (Name, Int) |
|
2336 |
||
2337 |
type Sort = [Name] |
|
2338 |
||
2339 |
data Typ = |
|
2340 |
Type (Name, [Typ]) |
|
2341 |
| TFree (Name, Sort) |
|
2342 |
| TVar (Indexname, Sort) |
|
2343 |
deriving (Show, Eq, Ord) |
|
2344 |
||
2345 |
data Term = |
|
2346 |
Const (Name, [Typ]) |
|
2347 |
| Free (Name, Typ) |
|
2348 |
| Var (Indexname, Typ) |
|
2349 |
| Bound Int |
|
2350 |
| Abs (Name, Typ, Term) |
|
2351 |
| App (Term, Term) |
|
80568
fbb655bf62d4
clarified Isabelle/Haskell type Term, following Isabelle/Scala (see 446b887e23c7);
wenzelm
parents:
79741
diff
changeset
|
2352 |
| OFCLASS (Typ, Name) |
74105 | 2353 |
deriving (Show, Eq, Ord) |
2354 |
||
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2355 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2356 |
{- free and bound variables -} |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2357 |
|
74105 | 2358 |
type Free = (Name, Typ) |
2359 |
||
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2360 |
lambda :: Free -> Term -> Term |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2361 |
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
|
2362 |
where |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2363 |
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
|
2364 |
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
|
2365 |
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
|
2366 |
abstract _ t = t |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2367 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2368 |
declare_frees :: Term -> Name.Context -> Name.Context |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2369 |
declare_frees (Free (x, _)) = Name.declare x |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2370 |
declare_frees (Abs (_, _, b)) = declare_frees b |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2371 |
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
|
2372 |
declare_frees _ = id |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2373 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2374 |
incr_boundvars :: Int -> Term -> Term |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2375 |
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
|
2376 |
where |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2377 |
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
|
2378 |
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
|
2379 |
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
|
2380 |
incr _ t = t |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2381 |
|
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2382 |
subst_bound :: Term -> Term -> Term |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2383 |
subst_bound arg = subst 0 |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2384 |
where |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2385 |
subst lev (Bound i) = |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2386 |
if i < lev then Bound i |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2387 |
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
|
2388 |
else Bound (i - 1) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2389 |
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
|
2390 |
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
|
2391 |
subst _ t = t |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2392 |
|
74214 | 2393 |
dest_lambda :: Name.Context -> Term -> Maybe (Free, Term) |
2394 |
dest_lambda names (Abs (x, ty, b)) = |
|
74106
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2395 |
let |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2396 |
(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
|
2397 |
v = (x', ty) |
4984fad0e91d
more operations, notably free and bound variables as in Isabelle/Pure;
wenzelm
parents:
74105
diff
changeset
|
2398 |
in Just (v, subst_bound (Free v) b) |
74214 | 2399 |
dest_lambda _ _ = Nothing |
2400 |
||
2401 |
strip_lambda :: Name.Context -> Term -> ([Free], Term) |
|
2402 |
strip_lambda names tm = |
|
2403 |
case dest_lambda names tm of |
|
74124 | 2404 |
Just (v, t) -> |
74214 | 2405 |
let (vs, t') = strip_lambda names t' |
74124 | 2406 |
in (v : vs, t') |
2407 |
Nothing -> ([], tm) |
|
2408 |
||
74105 | 2409 |
|
2410 |
{- type and term operators -} |
|
2411 |
||
2412 |
type_op0 :: Name -> (Typ, Typ -> Bool) |
|
2413 |
type_op0 name = (mk, is) |
|
2414 |
where |
|
2415 |
mk = Type (name, []) |
|
74205 | 2416 |
is (Type (c, _)) = c == name |
74105 | 2417 |
is _ = False |
2418 |
||
2419 |
type_op1 :: Name -> (Typ -> Typ, Typ -> Maybe Typ) |
|
2420 |
type_op1 name = (mk, dest) |
|
2421 |
where |
|
2422 |
mk ty = Type (name, [ty]) |
|
74205 | 2423 |
dest (Type (c, [ty])) | c == name = Just ty |
74105 | 2424 |
dest _ = Nothing |
2425 |
||
2426 |
type_op2 :: Name -> (Typ -> Typ -> Typ, Typ -> Maybe (Typ, Typ)) |
|
2427 |
type_op2 name = (mk, dest) |
|
2428 |
where |
|
2429 |
mk ty1 ty2 = Type (name, [ty1, ty2]) |
|
74205 | 2430 |
dest (Type (c, [ty1, ty2])) | c == name = Just (ty1, ty2) |
74105 | 2431 |
dest _ = Nothing |
2432 |
||
2433 |
op0 :: Name -> (Term, Term -> Bool) |
|
2434 |
op0 name = (mk, is) |
|
2435 |
where |
|
2436 |
mk = Const (name, []) |
|
2437 |
is (Const (c, _)) = c == name |
|
2438 |
is _ = False |
|
2439 |
||
2440 |
op1 :: Name -> (Term -> Term, Term -> Maybe Term) |
|
2441 |
op1 name = (mk, dest) |
|
2442 |
where |
|
2443 |
mk t = App (Const (name, []), t) |
|
2444 |
dest (App (Const (c, _), t)) | c == name = Just t |
|
2445 |
dest _ = Nothing |
|
2446 |
||
2447 |
op2 :: Name -> (Term -> Term -> Term, Term -> Maybe (Term, Term)) |
|
2448 |
op2 name = (mk, dest) |
|
2449 |
where |
|
2450 |
mk t u = App (App (Const (name, []), t), u) |
|
2451 |
dest (App (App (Const (c, _), t), u)) | c == name = Just (t, u) |
|
2452 |
dest _ = Nothing |
|
2453 |
||
74197 | 2454 |
typed_op0 :: Name -> (Typ -> Term, Term -> Maybe Typ) |
2455 |
typed_op0 name = (mk, dest) |
|
2456 |
where |
|
2457 |
mk ty = Const (name, [ty]) |
|
2458 |
dest (Const (c, [ty])) | c == name = Just ty |
|
2459 |
dest _ = Nothing |
|
2460 |
||
74188 | 2461 |
typed_op1 :: Name -> (Typ -> Term -> Term, Term -> Maybe (Typ, Term)) |
2462 |
typed_op1 name = (mk, dest) |
|
2463 |
where |
|
2464 |
mk ty t = App (Const (name, [ty]), t) |
|
2465 |
dest (App (Const (c, [ty]), t)) | c == name = Just (ty, t) |
|
2466 |
dest _ = Nothing |
|
2467 |
||
74105 | 2468 |
typed_op2 :: Name -> (Typ -> Term -> Term -> Term, Term -> Maybe (Typ, Term, Term)) |
2469 |
typed_op2 name = (mk, dest) |
|
2470 |
where |
|
2471 |
mk ty t u = App (App (Const (name, [ty]), t), u) |
|
2472 |
dest (App (App (Const (c, [ty]), t), u)) | c == name = Just (ty, t, u) |
|
2473 |
dest _ = Nothing |
|
2474 |
||
74124 | 2475 |
binder :: Name -> (Free -> Term -> Term, Name.Context -> Term -> Maybe (Free, Term)) |
2476 |
binder name = (mk, dest) |
|
2477 |
where |
|
2478 |
mk (a, ty) b = App (Const (name, [ty]), lambda (a, ty) b) |
|
74214 | 2479 |
dest names (App (Const (c, _), t)) | c == name = dest_lambda names t |
74124 | 2480 |
dest _ _ = Nothing |
74105 | 2481 |
|
2482 |
||
2483 |
{- type operations -} |
|
69240 | 2484 |
|
2485 |
dummyS :: Sort |
|
2486 |
dummyS = [""] |
|
2487 |
||
74105 | 2488 |
dummyT :: Typ; is_dummyT :: Typ -> Bool |
2489 |
(dummyT, is_dummyT) = type_op0 \<open>\<^type_name>\<open>dummy\<close>\<close> |
|
2490 |
||
2491 |
propT :: Typ; is_propT :: Typ -> Bool |
|
2492 |
(propT, is_propT) = type_op0 \<open>\<^type_name>\<open>prop\<close>\<close> |
|
2493 |
||
2494 |
(-->) :: Typ -> Typ -> Typ; dest_funT :: Typ -> Maybe (Typ, Typ) |
|
2495 |
((-->), dest_funT) = type_op2 \<open>\<^type_name>\<open>fun\<close>\<close> |
|
2496 |
||
2497 |
(--->) :: [Typ] -> Typ -> Typ |
|
2498 |
[] ---> b = b |
|
2499 |
(a : as) ---> b = a --> (as ---> b) |
|
2500 |
||
2501 |
||
2502 |
{- term operations -} |
|
74100 | 2503 |
|
2504 |
aconv :: Term -> Term -> Bool |
|
2505 |
aconv (App (t1, u1)) (App (t2, u2)) = aconv t1 t2 && aconv u1 u2 |
|
2506 |
aconv (Abs (_, ty1, t1)) (Abs (_, ty2, t2)) = aconv t1 t2 && ty1 == ty2 |
|
2507 |
aconv a1 a2 = a1 == a2 |
|
2508 |
||
2509 |
list_comb :: Term -> [Term] -> Term |
|
2510 |
list_comb f [] = f |
|
2511 |
list_comb f (t : ts) = list_comb (App (f, t)) ts |
|
2512 |
||
2513 |
strip_comb :: Term -> (Term, [Term]) |
|
2514 |
strip_comb tm = strip (tm, []) |
|
2515 |
where |
|
2516 |
strip (App (f, t), ts) = strip (f, t : ts) |
|
2517 |
strip x = x |
|
2518 |
||
2519 |
head_of :: Term -> Term |
|
2520 |
head_of (App (f, _)) = head_of f |
|
2521 |
head_of u = u |
|
69240 | 2522 |
\<close> |
2523 |
||
74105 | 2524 |
generate_file "Isabelle/Pure.hs" = \<open> |
2525 |
{- Title: Isabelle/Term.hs |
|
2526 |
Author: Makarius |
|
2527 |
LICENSE: BSD 3-clause (Isabelle) |
|
2528 |
||
2529 |
Support for Isabelle/Pure logic. |
|
2530 |
||
74178 | 2531 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/logic.ML\<close>. |
74105 | 2532 |
-} |
2533 |
||
2534 |
{-# LANGUAGE OverloadedStrings #-} |
|
2535 |
||
2536 |
module Isabelle.Pure ( |
|
74188 | 2537 |
mk_forall_op, dest_forall_op, mk_forall, dest_forall, |
2538 |
mk_equals, dest_equals, mk_implies, dest_implies |
|
74105 | 2539 |
) |
2540 |
where |
|
2541 |
||
74124 | 2542 |
import qualified Isabelle.Name as Name |
74105 | 2543 |
import Isabelle.Term |
2544 |
||
74188 | 2545 |
mk_forall_op :: Typ -> Term -> Term; dest_forall_op :: Term -> Maybe (Typ, Term) |
2546 |
(mk_forall_op, dest_forall_op) = typed_op1 \<open>\<^const_name>\<open>Pure.all\<close>\<close> |
|
2547 |
||
74124 | 2548 |
mk_forall :: Free -> Term -> Term; dest_forall :: Name.Context -> Term -> Maybe (Free, Term) |
2549 |
(mk_forall, dest_forall) = binder \<open>\<^const_name>\<open>Pure.all\<close>\<close> |
|
74105 | 2550 |
|
2551 |
mk_equals :: Typ -> Term -> Term -> Term; dest_equals :: Term -> Maybe (Typ, Term, Term) |
|
2552 |
(mk_equals, dest_equals) = typed_op2 \<open>\<^const_name>\<open>Pure.eq\<close>\<close> |
|
2553 |
||
2554 |
mk_implies :: Term -> Term -> Term; dest_implies :: Term -> Maybe (Term, Term) |
|
2555 |
(mk_implies, dest_implies) = op2 \<open>\<^const_name>\<open>Pure.imp\<close>\<close> |
|
2556 |
\<close> |
|
2557 |
||
2558 |
generate_file "Isabelle/HOL.hs" = \<open> |
|
2559 |
{- Title: Isabelle/Term.hs |
|
2560 |
Author: Makarius |
|
2561 |
LICENSE: BSD 3-clause (Isabelle) |
|
2562 |
||
2563 |
Support for Isabelle/HOL logic. |
|
2564 |
||
74178 | 2565 |
See \<^file>\<open>$ISABELLE_HOME/src/HOL/Tools/hologic.ML\<close>. |
74105 | 2566 |
-} |
2567 |
||
2568 |
{-# LANGUAGE OverloadedStrings #-} |
|
2569 |
||
2570 |
module Isabelle.HOL ( |
|
2571 |
boolT, is_boolT, mk_trueprop, dest_trueprop, |
|
2572 |
mk_setT, dest_setT, mk_mem, dest_mem, |
|
2573 |
mk_eq, dest_eq, true, is_true, false, is_false, |
|
2574 |
mk_not, dest_not, mk_conj, dest_conj, mk_disj, dest_disj, |
|
2575 |
mk_imp, dest_imp, mk_iff, dest_iff, |
|
74188 | 2576 |
mk_all_op, dest_all_op, mk_ex_op, dest_ex_op, |
74197 | 2577 |
mk_all, dest_all, mk_ex, dest_ex, |
2578 |
mk_undefined, dest_undefined |
|
74105 | 2579 |
) |
2580 |
where |
|
2581 |
||
74124 | 2582 |
import qualified Isabelle.Name as Name |
74105 | 2583 |
import Isabelle.Term |
2584 |
||
2585 |
||
2586 |
boolT :: Typ; is_boolT :: Typ -> Bool |
|
2587 |
(boolT, is_boolT) = type_op0 \<open>\<^type_name>\<open>bool\<close>\<close> |
|
2588 |
||
2589 |
mk_trueprop :: Term -> Term; dest_trueprop :: Term -> Maybe Term |
|
2590 |
(mk_trueprop, dest_trueprop) = op1 \<open>\<^const_name>\<open>Trueprop\<close>\<close> |
|
2591 |
||
2592 |
mk_setT :: Typ -> Typ; dest_setT :: Typ -> Maybe Typ |
|
2593 |
(mk_setT, dest_setT) = type_op1 \<open>\<^type_name>\<open>set\<close>\<close> |
|
2594 |
||
2595 |
mk_mem :: Typ -> Term -> Term -> Term; dest_mem :: Term -> Maybe (Typ, Term, Term) |
|
2596 |
(mk_mem, dest_mem) = typed_op2 \<open>\<^const_name>\<open>Set.member\<close>\<close> |
|
2597 |
||
2598 |
mk_eq :: Typ -> Term -> Term -> Term; dest_eq :: Term -> Maybe (Typ, Term, Term) |
|
2599 |
(mk_eq, dest_eq) = typed_op2 \<open>\<^const_name>\<open>HOL.eq\<close>\<close> |
|
2600 |
||
2601 |
true :: Term; is_true :: Term -> Bool |
|
2602 |
(true, is_true) = op0 \<open>\<^const_name>\<open>True\<close>\<close> |
|
2603 |
||
2604 |
false :: Term; is_false :: Term -> Bool |
|
2605 |
(false, is_false) = op0 \<open>\<^const_name>\<open>False\<close>\<close> |
|
2606 |
||
2607 |
mk_not :: Term -> Term; dest_not :: Term -> Maybe Term |
|
2608 |
(mk_not, dest_not) = op1 \<open>\<^const_name>\<open>Not\<close>\<close> |
|
2609 |
||
2610 |
mk_conj :: Term -> Term -> Term; dest_conj :: Term -> Maybe (Term, Term) |
|
2611 |
(mk_conj, dest_conj) = op2 \<open>\<^const_name>\<open>conj\<close>\<close> |
|
2612 |
||
2613 |
mk_disj :: Term -> Term -> Term; dest_disj :: Term -> Maybe (Term, Term) |
|
2614 |
(mk_disj, dest_disj) = op2 \<open>\<^const_name>\<open>disj\<close>\<close> |
|
2615 |
||
2616 |
mk_imp :: Term -> Term -> Term; dest_imp :: Term -> Maybe (Term, Term) |
|
2617 |
(mk_imp, dest_imp) = op2 \<open>\<^const_name>\<open>implies\<close>\<close> |
|
2618 |
||
2619 |
mk_iff :: Term -> Term -> Term |
|
2620 |
mk_iff = mk_eq boolT |
|
2621 |
||
2622 |
dest_iff :: Term -> Maybe (Term, Term) |
|
2623 |
dest_iff tm = |
|
2624 |
case dest_eq tm of |
|
2625 |
Just (ty, t, u) | ty == boolT -> Just (t, u) |
|
2626 |
_ -> Nothing |
|
2627 |
||
74188 | 2628 |
mk_all_op :: Typ -> Term -> Term; dest_all_op :: Term -> Maybe (Typ, Term) |
2629 |
(mk_all_op, dest_all_op) = typed_op1 \<open>\<^const_name>\<open>All\<close>\<close> |
|
2630 |
||
2631 |
mk_ex_op :: Typ -> Term -> Term; dest_ex_op :: Term -> Maybe (Typ, Term) |
|
2632 |
(mk_ex_op, dest_ex_op) = typed_op1 \<open>\<^const_name>\<open>Ex\<close>\<close> |
|
2633 |
||
74124 | 2634 |
mk_all :: Free -> Term -> Term; dest_all :: Name.Context -> Term -> Maybe (Free, Term) |
2635 |
(mk_all, dest_all) = binder \<open>\<^const_name>\<open>All\<close>\<close> |
|
2636 |
||
2637 |
mk_ex :: Free -> Term -> Term; dest_ex :: Name.Context -> Term -> Maybe (Free, Term) |
|
2638 |
(mk_ex, dest_ex) = binder \<open>\<^const_name>\<open>Ex\<close>\<close> |
|
74197 | 2639 |
|
2640 |
mk_undefined :: Typ -> Term; dest_undefined :: Term -> Maybe Typ |
|
2641 |
(mk_undefined, dest_undefined) = typed_op0 \<open>\<^const_name>\<open>undefined\<close>\<close> |
|
74105 | 2642 |
\<close> |
2643 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
2644 |
generate_file "Isabelle/Term_XML/Encode.hs" = \<open> |
69445 | 2645 |
{- Title: Isabelle/Term_XML/Encode.hs |
69240 | 2646 |
Author: Makarius |
2647 |
LICENSE: BSD 3-clause (Isabelle) |
|
2648 |
||
2649 |
XML data representation of lambda terms. |
|
69280 | 2650 |
|
74178 | 2651 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/term_xml.ML\<close>. |
69240 | 2652 |
-} |
2653 |
||
2654 |
{-# LANGUAGE LambdaCase #-} |
|
2655 |
||
80589 | 2656 |
module Isabelle.Term_XML.Encode (indexname, sort, typ, term) |
69240 | 2657 |
where |
2658 |
||
2659 |
import Isabelle.Library |
|
2660 |
import Isabelle.XML.Encode |
|
2661 |
import Isabelle.Term |
|
2662 |
||
70845 | 2663 |
indexname :: P Indexname |
2664 |
indexname (a, b) = if b == 0 then [a] else [a, int_atom b] |
|
69240 | 2665 |
|
2666 |
sort :: T Sort |
|
2667 |
sort = list string |
|
2668 |
||
2669 |
typ :: T Typ |
|
2670 |
typ ty = |
|
2671 |
ty |> variant |
|
2672 |
[\case { Type (a, b) -> Just ([a], list typ b); _ -> Nothing }, |
|
2673 |
\case { TFree (a, b) -> Just ([a], sort b); _ -> Nothing }, |
|
70845 | 2674 |
\case { TVar (a, b) -> Just (indexname a, sort b); _ -> Nothing }] |
2675 |
||
80589 | 2676 |
var_type :: T Typ |
2677 |
var_type ty = if is_dummyT ty then [] else typ ty |
|
69240 | 2678 |
|
2679 |
term :: T Term |
|
2680 |
term t = |
|
2681 |
t |> variant |
|
70784
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents:
70667
diff
changeset
|
2682 |
[\case { Const (a, b) -> Just ([a], list typ b); _ -> Nothing }, |
80589 | 2683 |
\case { Free (a, b) -> Just ([a], var_type b); _ -> Nothing }, |
2684 |
\case { Var (a, b) -> Just (indexname a, var_type b); _ -> Nothing }, |
|
70845 | 2685 |
\case { Bound a -> Just ([], int a); _ -> Nothing }, |
69240 | 2686 |
\case { Abs (a, b, c) -> Just ([a], pair typ term (b, c)); _ -> Nothing }, |
80568
fbb655bf62d4
clarified Isabelle/Haskell type Term, following Isabelle/Scala (see 446b887e23c7);
wenzelm
parents:
79741
diff
changeset
|
2687 |
\case { App a -> Just ([], pair term term a); _ -> Nothing }, |
fbb655bf62d4
clarified Isabelle/Haskell type Term, following Isabelle/Scala (see 446b887e23c7);
wenzelm
parents:
79741
diff
changeset
|
2688 |
\case { OFCLASS (a, b) -> Just ([b], typ a); _ -> Nothing }] |
69240 | 2689 |
\<close> |
2690 |
||
69444
c3c9440cbf9b
more formal Haskell project setup, with dependencies on packages from "stackage";
wenzelm
parents:
69381
diff
changeset
|
2691 |
generate_file "Isabelle/Term_XML/Decode.hs" = \<open> |
69445 | 2692 |
{- Title: Isabelle/Term_XML/Decode.hs |
69240 | 2693 |
Author: Makarius |
2694 |
LICENSE: BSD 3-clause (Isabelle) |
|
2695 |
||
2696 |
XML data representation of lambda terms. |
|
69280 | 2697 |
|
74178 | 2698 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/term_xml.ML\<close>. |
69240 | 2699 |
-} |
2700 |
||
73177 | 2701 |
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-} |
2702 |
||
80589 | 2703 |
module Isabelle.Term_XML.Decode (indexname, sort, typ, term) |
69240 | 2704 |
where |
2705 |
||
2706 |
import Isabelle.Library |
|
2707 |
import Isabelle.XML.Decode |
|
2708 |
import Isabelle.Term |
|
2709 |
||
2710 |
||
70845 | 2711 |
indexname :: P Indexname |
2712 |
indexname [a] = (a, 0) |
|
2713 |
indexname [a, b] = (a, int_atom b) |
|
2714 |
||
69240 | 2715 |
sort :: T Sort |
2716 |
sort = list string |
|
2717 |
||
2718 |
typ :: T Typ |
|
2719 |
typ ty = |
|
2720 |
ty |> variant |
|
2721 |
[\([a], b) -> Type (a, list typ b), |
|
2722 |
\([a], b) -> TFree (a, sort b), |
|
70845 | 2723 |
\(a, b) -> TVar (indexname a, sort b)] |
2724 |
||
80589 | 2725 |
var_type :: T Typ |
2726 |
var_type [] = dummyT |
|
2727 |
var_type body = typ body |
|
69240 | 2728 |
|
2729 |
term :: T Term |
|
2730 |
term t = |
|
2731 |
t |> variant |
|
70784
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents:
70667
diff
changeset
|
2732 |
[\([a], b) -> Const (a, list typ b), |
80589 | 2733 |
\([a], b) -> Free (a, var_type b), |
2734 |
\(a, b) -> Var (indexname a, var_type b), |
|
70845 | 2735 |
\([], a) -> Bound (int a), |
69240 | 2736 |
\([a], b) -> let (c, d) = pair typ term b in Abs (a, c, d), |
80568
fbb655bf62d4
clarified Isabelle/Haskell type Term, following Isabelle/Scala (see 446b887e23c7);
wenzelm
parents:
79741
diff
changeset
|
2737 |
\([], a) -> App (pair term term a), |
fbb655bf62d4
clarified Isabelle/Haskell type Term, following Isabelle/Scala (see 446b887e23c7);
wenzelm
parents:
79741
diff
changeset
|
2738 |
\([a], b) -> OFCLASS (typ b, a)] |
69240 | 2739 |
\<close> |
2740 |
||
74129 | 2741 |
generate_file "Isabelle/XML/Classes.hs" = \<open> |
2742 |
{- generated by Isabelle -} |
|
2743 |
||
2744 |
{- Title: Isabelle/XML/Classes.hs |
|
2745 |
Author: Makarius |
|
2746 |
LICENSE: BSD 3-clause (Isabelle) |
|
2747 |
||
2748 |
Type classes for XML data representation. |
|
2749 |
-} |
|
2750 |
||
2751 |
{-# LANGUAGE FlexibleInstances #-} |
|
2752 |
||
2753 |
module Isabelle.XML.Classes |
|
2754 |
(Encode_Atom(..), Decode_Atom(..), Encode (..), Decode (..)) |
|
2755 |
where |
|
2756 |
||
2757 |
import qualified Isabelle.XML as XML |
|
2758 |
import qualified Isabelle.XML.Encode as Encode |
|
2759 |
import qualified Isabelle.XML.Decode as Decode |
|
2760 |
import qualified Isabelle.Term_XML.Encode as Encode |
|
2761 |
import qualified Isabelle.Term_XML.Decode as Decode |
|
2762 |
import qualified Isabelle.Properties as Properties |
|
2763 |
import Isabelle.Bytes (Bytes) |
|
2764 |
import Isabelle.Term (Typ, Term) |
|
2765 |
||
2766 |
||
2767 |
class Encode_Atom a where encode_atom :: Encode.A a |
|
2768 |
class Decode_Atom a where decode_atom :: Decode.A a |
|
2769 |
||
2770 |
instance Encode_Atom Int where encode_atom = Encode.int_atom |
|
2771 |
instance Decode_Atom Int where decode_atom = Decode.int_atom |
|
2772 |
||
2773 |
instance Encode_Atom Bool where encode_atom = Encode.bool_atom |
|
2774 |
instance Decode_Atom Bool where decode_atom = Decode.bool_atom |
|
2775 |
||
2776 |
instance Encode_Atom () where encode_atom = Encode.unit_atom |
|
2777 |
instance Decode_Atom () where decode_atom = Decode.unit_atom |
|
2778 |
||
2779 |
||
2780 |
class Encode a where encode :: Encode.T a |
|
2781 |
class Decode a where decode :: Decode.T a |
|
2782 |
||
2783 |
instance Encode Bytes where encode = Encode.string |
|
2784 |
instance Decode Bytes where decode = Decode.string |
|
2785 |
||
2786 |
instance Encode Int where encode = Encode.int |
|
2787 |
instance Decode Int where decode = Decode.int |
|
2788 |
||
2789 |
instance Encode Bool where encode = Encode.bool |
|
2790 |
instance Decode Bool where decode = Decode.bool |
|
2791 |
||
2792 |
instance Encode () where encode = Encode.unit |
|
2793 |
instance Decode () where decode = Decode.unit |
|
2794 |
||
2795 |
instance (Encode a, Encode b) => Encode (a, b) |
|
2796 |
where encode = Encode.pair encode encode |
|
2797 |
instance (Decode a, Decode b) => Decode (a, b) |
|
2798 |
where decode = Decode.pair decode decode |
|
2799 |
||
2800 |
instance (Encode a, Encode b, Encode c) => Encode (a, b, c) |
|
2801 |
where encode = Encode.triple encode encode encode |
|
2802 |
instance (Decode a, Decode b, Decode c) => Decode (a, b, c) |
|
2803 |
where decode = Decode.triple decode decode decode |
|
2804 |
||
2805 |
instance Encode a => Encode [a] where encode = Encode.list encode |
|
2806 |
instance Decode a => Decode [a] where decode = Decode.list decode |
|
2807 |
||
2808 |
instance Encode a => Encode (Maybe a) where encode = Encode.option encode |
|
2809 |
instance Decode a => Decode (Maybe a) where decode = Decode.option decode |
|
2810 |
||
2811 |
instance Encode XML.Tree where encode = Encode.tree |
|
2812 |
instance Decode XML.Tree where decode = Decode.tree |
|
2813 |
||
2814 |
instance Encode Properties.T where encode = Encode.properties |
|
2815 |
instance Decode Properties.T where decode = Decode.properties |
|
2816 |
||
2817 |
instance Encode Typ where encode = Encode.typ |
|
2818 |
instance Decode Typ where decode = Decode.typ |
|
2819 |
||
2820 |
instance Encode Term where encode = Encode.term |
|
2821 |
instance Decode Term where decode = Decode.term |
|
2822 |
\<close> |
|
2823 |
||
69459 | 2824 |
generate_file "Isabelle/UUID.hs" = \<open> |
2825 |
{- Title: Isabelle/UUID.hs |
|
2826 |
Author: Makarius |
|
2827 |
LICENSE: BSD 3-clause (Isabelle) |
|
2828 |
||
2829 |
Universally unique identifiers. |
|
2830 |
||
2831 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/General/uuid.scala\<close>. |
|
2832 |
-} |
|
2833 |
||
74095 | 2834 |
module Isabelle.UUID (T, random, print, parse) |
69459 | 2835 |
where |
2836 |
||
74095 | 2837 |
import Prelude hiding (print) |
2838 |
||
69459 | 2839 |
import Data.UUID (UUID) |
2840 |
import qualified Data.UUID as UUID |
|
2841 |
import Data.UUID.V4 (nextRandom) |
|
2842 |
||
74084 | 2843 |
import qualified Isabelle.Bytes as Bytes |
2844 |
import Isabelle.Bytes (Bytes) |
|
2845 |
||
69459 | 2846 |
|
69462 | 2847 |
type T = UUID |
2848 |
||
2849 |
random :: IO T |
|
69459 | 2850 |
random = nextRandom |
2851 |
||
74095 | 2852 |
print :: T -> Bytes |
2853 |
print = Bytes.make . UUID.toASCIIBytes |
|
2854 |
||
2855 |
parse :: Bytes -> Maybe T |
|
2856 |
parse = UUID.fromASCIIBytes . Bytes.unmake |
|
69459 | 2857 |
\<close> |
2858 |
||
69448 | 2859 |
generate_file "Isabelle/Byte_Message.hs" = \<open> |
2860 |
{- Title: Isabelle/Byte_Message.hs |
|
69446 | 2861 |
Author: Makarius |
2862 |
LICENSE: BSD 3-clause (Isabelle) |
|
2863 |
||
69448 | 2864 |
Byte-oriented messages. |
2865 |
||
2866 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/byte_message.ML\<close> |
|
2867 |
and \<^file>\<open>$ISABELLE_HOME/src/Pure/PIDE/byte_message.scala\<close>. |
|
69446 | 2868 |
-} |
2869 |
||
74083 | 2870 |
{-# LANGUAGE OverloadedStrings #-} |
73177 | 2871 |
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-} |
2872 |
||
69452 | 2873 |
module Isabelle.Byte_Message ( |
69467 | 2874 |
write, write_line, |
74088 | 2875 |
read, read_block, read_line, |
74187 | 2876 |
make_message, write_message, read_message, |
2877 |
exchange_message, exchange_message0, |
|
73178 | 2878 |
make_line_message, write_line_message, read_line_message, |
2879 |
read_yxml, write_yxml |
|
69452 | 2880 |
) |
69446 | 2881 |
where |
2882 |
||
69449 | 2883 |
import Prelude hiding (read) |
69454 | 2884 |
import Data.Maybe |
69446 | 2885 |
import qualified Data.ByteString as ByteString |
74084 | 2886 |
import qualified Isabelle.Bytes as Bytes |
2887 |
import Isabelle.Bytes (Bytes) |
|
74135 | 2888 |
import qualified Isabelle.Symbol as Symbol |
74080
5b68a5cd7061
prefer UTF8 implementation from Data.Text.Encoding (foreign C);
wenzelm
parents:
73246
diff
changeset
|
2889 |
import qualified Isabelle.UTF8 as UTF8 |
73178 | 2890 |
import qualified Isabelle.XML as XML |
2891 |
import qualified Isabelle.YXML as YXML |
|
73177 | 2892 |
|
69446 | 2893 |
import Network.Socket (Socket) |
74082 | 2894 |
import qualified Network.Socket.ByteString as Socket |
69446 | 2895 |
|
74088 | 2896 |
import Isabelle.Library |
69446 | 2897 |
import qualified Isabelle.Value as Value |
2898 |
||
2899 |
||
69452 | 2900 |
{- output operations -} |
2901 |
||
74084 | 2902 |
write :: Socket -> [Bytes] -> IO () |
2903 |
write socket = Socket.sendMany socket . map Bytes.unmake |
|
2904 |
||
2905 |
write_line :: Socket -> Bytes -> IO () |
|
74083 | 2906 |
write_line socket s = write socket [s, "\n"] |
69467 | 2907 |
|
69452 | 2908 |
|
2909 |
{- input operations -} |
|
2910 |
||
74084 | 2911 |
read :: Socket -> Int -> IO Bytes |
69452 | 2912 |
read socket n = read_body 0 [] |
69449 | 2913 |
where |
74084 | 2914 |
result = Bytes.concat . reverse |
69452 | 2915 |
read_body len ss = |
69449 | 2916 |
if len >= n then return (result ss) |
2917 |
else |
|
2918 |
(do |
|
74082 | 2919 |
s <- Socket.recv socket (min (n - len) 8192) |
69449 | 2920 |
case ByteString.length s of |
2921 |
0 -> return (result ss) |
|
74084 | 2922 |
m -> read_body (len + m) (Bytes.make s : ss)) |
2923 |
||
2924 |
read_block :: Socket -> Int -> IO (Maybe Bytes, Int) |
|
69452 | 2925 |
read_block socket n = do |
2926 |
msg <- read socket n |
|
74084 | 2927 |
let len = Bytes.length msg |
69452 | 2928 |
return (if len == n then Just msg else Nothing, len) |
69449 | 2929 |
|
74084 | 2930 |
read_line :: Socket -> IO (Maybe Bytes) |
69452 | 2931 |
read_line socket = read_body [] |
69446 | 2932 |
where |
74134 | 2933 |
result = trim_line . Bytes.pack . reverse |
69452 | 2934 |
read_body bs = do |
74082 | 2935 |
s <- Socket.recv socket 1 |
69446 | 2936 |
case ByteString.length s of |
2937 |
0 -> return (if null bs then Nothing else Just (result bs)) |
|
2938 |
1 -> |
|
2939 |
case ByteString.head s of |
|
2940 |
10 -> return (Just (result bs)) |
|
69452 | 2941 |
b -> read_body (b : bs) |
69446 | 2942 |
|
69448 | 2943 |
|
69454 | 2944 |
{- messages with multiple chunks (arbitrary content) -} |
2945 |
||
74084 | 2946 |
make_header :: [Int] -> [Bytes] |
74095 | 2947 |
make_header ns = [space_implode "," (map Value.print_int ns), "\n"] |
69454 | 2948 |
|
74084 | 2949 |
make_message :: [Bytes] -> [Bytes] |
2950 |
make_message chunks = make_header (map Bytes.length chunks) <> chunks |
|
2951 |
||
2952 |
write_message :: Socket -> [Bytes] -> IO () |
|
69476 | 2953 |
write_message socket = write socket . make_message |
69454 | 2954 |
|
74084 | 2955 |
parse_header :: Bytes -> [Int] |
69454 | 2956 |
parse_header line = |
2957 |
let |
|
74132 | 2958 |
res = map Value.parse_nat (space_explode ',' line) |
69454 | 2959 |
in |
2960 |
if all isJust res then map fromJust res |
|
74081 | 2961 |
else error ("Malformed message header: " <> quote (UTF8.decode line)) |
69454 | 2962 |
|
74084 | 2963 |
read_chunk :: Socket -> Int -> IO Bytes |
69454 | 2964 |
read_chunk socket n = do |
2965 |
res <- read_block socket n |
|
2966 |
return $ |
|
2967 |
case res of |
|
2968 |
(Just chunk, _) -> chunk |
|
2969 |
(Nothing, len) -> |
|
74081 | 2970 |
error ("Malformed message chunk: unexpected EOF after " <> |
2971 |
show len <> " of " <> show n <> " bytes") |
|
69454 | 2972 |
|
74084 | 2973 |
read_message :: Socket -> IO (Maybe [Bytes]) |
69454 | 2974 |
read_message socket = do |
2975 |
res <- read_line socket |
|
2976 |
case res of |
|
2977 |
Just line -> Just <$> mapM (read_chunk socket) (parse_header line) |
|
2978 |
Nothing -> return Nothing |
|
2979 |
||
74186 | 2980 |
exchange_message :: Socket -> [Bytes] -> IO (Maybe [Bytes]) |
2981 |
exchange_message socket msg = do |
|
2982 |
write_message socket msg |
|
2983 |
read_message socket |
|
2984 |
||
74187 | 2985 |
exchange_message0 :: Socket -> [Bytes] -> IO () |
2986 |
exchange_message0 socket msg = do |
|
2987 |
_ <- exchange_message socket msg |
|
2988 |
return () |
|
2989 |
||
69454 | 2990 |
|
69448 | 2991 |
-- hybrid messages: line or length+block (with content restriction) |
2992 |
||
74084 | 2993 |
is_length :: Bytes -> Bool |
69452 | 2994 |
is_length msg = |
74135 | 2995 |
not (Bytes.null msg) && Bytes.all_char (\c -> '0' <= c && c <= '9') msg |
74084 | 2996 |
|
2997 |
is_terminated :: Bytes -> Bool |
|
69452 | 2998 |
is_terminated msg = |
74135 | 2999 |
not (Bytes.null msg) && Symbol.is_ascii_line_terminator (Bytes.char $ Bytes.last msg) |
74084 | 3000 |
|
3001 |
make_line_message :: Bytes -> [Bytes] |
|
69476 | 3002 |
make_line_message msg = |
74084 | 3003 |
let n = Bytes.length msg in |
69476 | 3004 |
if is_length msg || is_terminated msg then |
74081 | 3005 |
error ("Bad content for line message:\n" <> take 100 (UTF8.decode msg)) |
69476 | 3006 |
else |
74135 | 3007 |
(if n > 100 || Bytes.any_char (== '\n') msg then make_header [n + 1] else []) <> [msg, "\n"] |
74084 | 3008 |
|
3009 |
write_line_message :: Socket -> Bytes -> IO () |
|
69476 | 3010 |
write_line_message socket = write socket . make_line_message |
69448 | 3011 |
|
74084 | 3012 |
read_line_message :: Socket -> IO (Maybe Bytes) |
69448 | 3013 |
read_line_message socket = do |
69446 | 3014 |
opt_line <- read_line socket |
3015 |
case opt_line of |
|
3016 |
Nothing -> return Nothing |
|
3017 |
Just line -> |
|
74095 | 3018 |
case Value.parse_nat line of |
69446 | 3019 |
Nothing -> return $ Just line |
74134 | 3020 |
Just n -> fmap trim_line . fst <$> read_block socket n |
73178 | 3021 |
|
3022 |
||
3023 |
read_yxml :: Socket -> IO (Maybe XML.Body) |
|
3024 |
read_yxml socket = do |
|
3025 |
res <- read_line_message socket |
|
74095 | 3026 |
return (YXML.parse_body <$> res) |
73178 | 3027 |
|
3028 |
write_yxml :: Socket -> XML.Body -> IO () |
|
3029 |
write_yxml socket body = |
|
74095 | 3030 |
write_line_message socket (YXML.string_of_body body) |
69446 | 3031 |
\<close> |
3032 |
||
71692 | 3033 |
generate_file "Isabelle/Isabelle_Thread.hs" = \<open> |
3034 |
{- Title: Isabelle/Isabelle_Thread.hs |
|
69473 | 3035 |
Author: Makarius |
3036 |
LICENSE: BSD 3-clause (Isabelle) |
|
3037 |
||
71692 | 3038 |
Isabelle-specific thread management. |
3039 |
||
3040 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/Concurrent/isabelle_thread.ML\<close> |
|
3041 |
and \<^file>\<open>$ISABELLE_HOME/src/Pure/Concurrent/isabelle_thread.scala\<close>. |
|
69473 | 3042 |
-} |
3043 |
||
3044 |
{-# LANGUAGE NamedFieldPuns #-} |
|
3045 |
||
71692 | 3046 |
module Isabelle.Isabelle_Thread ( |
69473 | 3047 |
ThreadId, Result, |
69494 | 3048 |
find_id, |
69473 | 3049 |
properties, change_properties, |
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3050 |
add_resource, del_resource, bracket_resource, |
69497 | 3051 |
is_stopped, expose_stopped, stop, |
69496 | 3052 |
my_uuid, stop_uuid, |
69494 | 3053 |
Fork, fork_finally, fork) |
69473 | 3054 |
where |
3055 |
||
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3056 |
import Data.Unique |
69473 | 3057 |
import Data.IORef |
3058 |
import System.IO.Unsafe |
|
3059 |
||
69494 | 3060 |
import qualified Data.List as List |
69497 | 3061 |
import Control.Monad (when, forM_) |
69473 | 3062 |
import Data.Map.Strict (Map) |
3063 |
import qualified Data.Map.Strict as Map |
|
3064 |
import Control.Exception as Exception |
|
3065 |
import Control.Concurrent (ThreadId) |
|
3066 |
import qualified Control.Concurrent as Concurrent |
|
3067 |
import Control.Concurrent.Thread (Result) |
|
3068 |
import qualified Control.Concurrent.Thread as Thread |
|
69494 | 3069 |
import qualified Isabelle.UUID as UUID |
69473 | 3070 |
import qualified Isabelle.Properties as Properties |
3071 |
||
3072 |
||
3073 |
{- thread info -} |
|
3074 |
||
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3075 |
type Resources = Map Unique (IO ()) |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3076 |
data Info = Info {uuid :: UUID.T, props :: Properties.T, stopped :: Bool, resources :: Resources} |
69473 | 3077 |
type Infos = Map ThreadId Info |
3078 |
||
3079 |
lookup_info :: Infos -> ThreadId -> Maybe Info |
|
69494 | 3080 |
lookup_info infos id = Map.lookup id infos |
3081 |
||
3082 |
init_info :: ThreadId -> UUID.T -> Infos -> (Infos, ()) |
|
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3083 |
init_info id uuid infos = (Map.insert id (Info uuid [] False Map.empty) infos, ()) |
69473 | 3084 |
|
3085 |
||
3086 |
{- global state -} |
|
3087 |
||
3088 |
{-# NOINLINE global_state #-} |
|
3089 |
global_state :: IORef Infos |
|
3090 |
global_state = unsafePerformIO (newIORef Map.empty) |
|
3091 |
||
69494 | 3092 |
find_id :: UUID.T -> IO (Maybe ThreadId) |
3093 |
find_id uuid = do |
|
3094 |
state <- readIORef global_state |
|
3095 |
return $ fst <$> List.find (\(_, Info{uuid = uuid'}) -> uuid == uuid') (Map.assocs state) |
|
3096 |
||
69473 | 3097 |
get_info :: ThreadId -> IO (Maybe Info) |
3098 |
get_info id = do |
|
3099 |
state <- readIORef global_state |
|
3100 |
return $ lookup_info state id |
|
3101 |
||
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3102 |
map_info :: ThreadId -> (Info -> Info) -> IO (Maybe Info) |
69473 | 3103 |
map_info id f = |
3104 |
atomicModifyIORef' global_state |
|
3105 |
(\infos -> |
|
3106 |
case lookup_info infos id of |
|
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3107 |
Nothing -> (infos, Nothing) |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3108 |
Just info -> |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3109 |
let info' = f info |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3110 |
in (Map.insert id info' infos, Just info')) |
69473 | 3111 |
|
69495 | 3112 |
delete_info :: ThreadId -> IO () |
3113 |
delete_info id = |
|
3114 |
atomicModifyIORef' global_state (\infos -> (Map.delete id infos, ())) |
|
3115 |
||
69473 | 3116 |
|
3117 |
{- thread properties -} |
|
3118 |
||
69498
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
3119 |
my_info :: IO (Maybe Info) |
69473 | 3120 |
my_info = do |
3121 |
id <- Concurrent.myThreadId |
|
69498
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
3122 |
get_info id |
69473 | 3123 |
|
3124 |
properties :: IO Properties.T |
|
69498
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
3125 |
properties = maybe [] props <$> my_info |
69473 | 3126 |
|
3127 |
change_properties :: (Properties.T -> Properties.T) -> IO () |
|
3128 |
change_properties f = do |
|
3129 |
id <- Concurrent.myThreadId |
|
3130 |
map_info id (\info -> info {props = f (props info)}) |
|
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3131 |
return () |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3132 |
|
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3133 |
|
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3134 |
{- managed resources -} |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3135 |
|
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3136 |
add_resource :: IO () -> IO Unique |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3137 |
add_resource resource = do |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3138 |
id <- Concurrent.myThreadId |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3139 |
u <- newUnique |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3140 |
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
|
3141 |
return u |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3142 |
|
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3143 |
del_resource :: Unique -> IO () |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3144 |
del_resource u = do |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3145 |
id <- Concurrent.myThreadId |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3146 |
map_info id (\info -> info {resources = Map.delete u (resources info)}) |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3147 |
return () |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3148 |
|
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3149 |
bracket_resource :: IO () -> IO a -> IO a |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3150 |
bracket_resource resource body = |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3151 |
Exception.bracket (add_resource resource) del_resource (const body) |
69473 | 3152 |
|
3153 |
||
3154 |
{- stop -} |
|
3155 |
||
3156 |
is_stopped :: IO Bool |
|
69498
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
3157 |
is_stopped = maybe False stopped <$> my_info |
69473 | 3158 |
|
69497 | 3159 |
expose_stopped :: IO () |
3160 |
expose_stopped = do |
|
3161 |
stopped <- is_stopped |
|
3162 |
when stopped $ throw ThreadKilled |
|
3163 |
||
69473 | 3164 |
stop :: ThreadId -> IO () |
69499
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3165 |
stop id = do |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3166 |
info <- map_info id (\info -> info {stopped = True}) |
638fdbbc7d1f
more Haskell operations: managed resources for threads;
wenzelm
parents:
69498
diff
changeset
|
3167 |
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
|
3168 |
sequence_ ops |
69473 | 3169 |
|
3170 |
||
69496 | 3171 |
{- UUID -} |
3172 |
||
69498
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
3173 |
my_uuid :: IO (Maybe UUID.T) |
22e958b76bf6
more robust: suitable defaults for unmanaged threads;
wenzelm
parents:
69497
diff
changeset
|
3174 |
my_uuid = fmap uuid <$> my_info |
69496 | 3175 |
|
3176 |
stop_uuid :: UUID.T -> IO () |
|
3177 |
stop_uuid uuid = do |
|
3178 |
id <- find_id uuid |
|
3179 |
forM_ id stop |
|
3180 |
||
3181 |
||
69473 | 3182 |
{- fork -} |
3183 |
||
69494 | 3184 |
type Fork a = (ThreadId, UUID.T, IO (Result a)) |
3185 |
||
3186 |
fork_finally :: IO a -> (Either SomeException a -> IO b) -> IO (Fork b) |
|
69473 | 3187 |
fork_finally body finally = do |
69494 | 3188 |
uuid <- UUID.random |
69473 | 3189 |
(id, result) <- |
3190 |
Exception.mask (\restore -> |
|
3191 |
Thread.forkIO |
|
3192 |
(Exception.try |
|
3193 |
(do |
|
3194 |
id <- Concurrent.myThreadId |
|
69494 | 3195 |
atomicModifyIORef' global_state (init_info id uuid) |
69495 | 3196 |
restore body) |
3197 |
>>= (\res -> do id <- Concurrent.myThreadId; delete_info id; finally res))) |
|
69494 | 3198 |
return (id, uuid, result) |
3199 |
||
3200 |
fork :: IO a -> IO (Fork a) |
|
69473 | 3201 |
fork body = fork_finally body Thread.result |
3202 |
\<close> |
|
3203 |
||
69455 | 3204 |
generate_file "Isabelle/Server.hs" = \<open> |
3205 |
{- Title: Isabelle/Server.hs |
|
3206 |
Author: Makarius |
|
3207 |
LICENSE: BSD 3-clause (Isabelle) |
|
3208 |
||
3209 |
TCP server on localhost. |
|
3210 |
-} |
|
3211 |
||
74127 | 3212 |
{-# LANGUAGE OverloadedStrings #-} |
3213 |
||
69465 | 3214 |
module Isabelle.Server ( |
74216 | 3215 |
localhost_name, localhost_prefix, localhost, publish_text, publish_stdout, |
73178 | 3216 |
server, connection |
69465 | 3217 |
) |
3218 |
where |
|
69455 | 3219 |
|
69465 | 3220 |
import Control.Monad (forever, when) |
69455 | 3221 |
import qualified Control.Exception as Exception |
3222 |
import Network.Socket (Socket) |
|
3223 |
import qualified Network.Socket as Socket |
|
69472 | 3224 |
import qualified System.IO as IO |
74127 | 3225 |
import qualified Data.ByteString.Char8 as Char8 |
69455 | 3226 |
|
69462 | 3227 |
import Isabelle.Library |
74127 | 3228 |
import qualified Isabelle.Bytes as Bytes |
74084 | 3229 |
import Isabelle.Bytes (Bytes) |
69462 | 3230 |
import qualified Isabelle.UUID as UUID |
69465 | 3231 |
import qualified Isabelle.Byte_Message as Byte_Message |
71692 | 3232 |
import qualified Isabelle.Isabelle_Thread as Isabelle_Thread |
69462 | 3233 |
|
3234 |
||
3235 |
{- server address -} |
|
69455 | 3236 |
|
74127 | 3237 |
localhost_name :: Bytes |
69463 | 3238 |
localhost_name = "127.0.0.1" |
3239 |
||
74211 | 3240 |
localhost_prefix :: Bytes |
3241 |
localhost_prefix = localhost_name <> ":" |
|
3242 |
||
69455 | 3243 |
localhost :: Socket.HostAddress |
3244 |
localhost = Socket.tupleToHostAddress (127, 0, 0, 1) |
|
3245 |
||
74127 | 3246 |
publish_text :: Bytes -> Bytes -> UUID.T -> Bytes |
69462 | 3247 |
publish_text name address password = |
74127 | 3248 |
"server " <> quote name <> " = " <> address <> |
74128 | 3249 |
" (password " <> quote (show_bytes password) <> ")" |
74127 | 3250 |
|
3251 |
publish_stdout :: Bytes -> Bytes -> UUID.T -> IO () |
|
74095 | 3252 |
publish_stdout name address password = |
74127 | 3253 |
Char8.putStrLn (Bytes.unmake $ publish_text name address password) |
69462 | 3254 |
|
3255 |
||
3256 |
{- server -} |
|
3257 |
||
74127 | 3258 |
server :: (Bytes -> UUID.T -> IO ()) -> (Socket -> IO ()) -> IO () |
69455 | 3259 |
server publish handle = |
69462 | 3260 |
Socket.withSocketsDo $ Exception.bracket open (Socket.close . fst) (uncurry loop) |
69455 | 3261 |
where |
74084 | 3262 |
open :: IO (Socket, Bytes) |
69455 | 3263 |
open = do |
69466 | 3264 |
server_socket <- Socket.socket Socket.AF_INET Socket.Stream Socket.defaultProtocol |
3265 |
Socket.bind server_socket (Socket.SockAddrInet 0 localhost) |
|
3266 |
Socket.listen server_socket 50 |
|
69462 | 3267 |
|
69466 | 3268 |
port <- Socket.socketPort server_socket |
74128 | 3269 |
let address = localhost_name <> ":" <> show_bytes port |
69462 | 3270 |
password <- UUID.random |
3271 |
publish address password |
|
69455 | 3272 |
|
74095 | 3273 |
return (server_socket, UUID.print password) |
69462 | 3274 |
|
74084 | 3275 |
loop :: Socket -> Bytes -> IO () |
69466 | 3276 |
loop server_socket password = forever $ do |
69480 | 3277 |
(connection, _) <- Socket.accept server_socket |
71692 | 3278 |
Isabelle_Thread.fork_finally |
69465 | 3279 |
(do |
3280 |
line <- Byte_Message.read_line connection |
|
3281 |
when (line == Just password) $ handle connection) |
|
69473 | 3282 |
(\finally -> do |
69472 | 3283 |
Socket.close connection |
69473 | 3284 |
case finally of |
69472 | 3285 |
Left exn -> IO.hPutStrLn IO.stderr $ Exception.displayException exn |
3286 |
Right () -> return ()) |
|
69455 | 3287 |
return () |
73178 | 3288 |
|
3289 |
||
3290 |
{- client connection -} |
|
3291 |
||
74095 | 3292 |
connection :: String -> Bytes -> (Socket -> IO a) -> IO a |
73178 | 3293 |
connection port password client = |
3294 |
Socket.withSocketsDo $ do |
|
3295 |
addr <- resolve |
|
3296 |
Exception.bracket (open addr) Socket.close body |
|
3297 |
where |
|
3298 |
resolve = do |
|
3299 |
let hints = |
|
3300 |
Socket.defaultHints { |
|
3301 |
Socket.addrFlags = [Socket.AI_NUMERICHOST, Socket.AI_NUMERICSERV], |
|
3302 |
Socket.addrSocketType = Socket.Stream } |
|
74211 | 3303 |
head <$> Socket.getAddrInfo (Just hints) (Just $ make_string localhost_name) (Just port) |
73178 | 3304 |
|
3305 |
open addr = do |
|
3306 |
socket <- Socket.socket (Socket.addrFamily addr) (Socket.addrSocketType addr) |
|
3307 |
(Socket.addrProtocol addr) |
|
3308 |
Socket.connect socket $ Socket.addrAddress addr |
|
3309 |
return socket |
|
3310 |
||
3311 |
body socket = do |
|
74095 | 3312 |
Byte_Message.write_line socket password |
73178 | 3313 |
client socket |
69455 | 3314 |
\<close> |
3315 |
||
74161 | 3316 |
generate_file "Isabelle/Time.hs" = \<open> |
3317 |
{- Title: Isabelle/Time.hs |
|
3318 |
Author: Makarius |
|
3319 |
LICENSE: BSD 3-clause (Isabelle) |
|
3320 |
||
3321 |
Time based on milliseconds. |
|
3322 |
||
3323 |
See \<^file>\<open>~~/src/Pure/General/time.scala\<close> |
|
3324 |
-} |
|
3325 |
||
3326 |
{-# LANGUAGE OverloadedStrings #-} |
|
3327 |
||
3328 |
module Isabelle.Time ( |
|
3329 |
Time, seconds, minutes, ms, zero, is_zero, is_relevant, |
|
74209 | 3330 |
get_seconds, get_minutes, get_ms, message, now |
74161 | 3331 |
) |
3332 |
where |
|
3333 |
||
3334 |
import Text.Printf (printf) |
|
74209 | 3335 |
import Data.Time.Clock.POSIX (getPOSIXTime) |
74161 | 3336 |
import Isabelle.Bytes (Bytes) |
3337 |
import Isabelle.Library |
|
3338 |
||
3339 |
||
74218 | 3340 |
newtype Time = Time Int |
74161 | 3341 |
|
75066 | 3342 |
instance Eq Time where Time a == Time b = a == b |
3343 |
instance Ord Time where compare (Time a) (Time b) = compare a b |
|
3344 |
instance Num Time where |
|
3345 |
fromInteger = Time . fromInteger |
|
3346 |
Time a + Time b = Time (a + b) |
|
3347 |
Time a - Time b = Time (a - b) |
|
3348 |
Time a * Time b = Time (a * b) |
|
3349 |
abs (Time a) = Time (abs a) |
|
3350 |
signum (Time a) = Time (signum a) |
|
74161 | 3351 |
|
3352 |
seconds :: Double -> Time |
|
3353 |
seconds s = Time (round (s * 1000.0)) |
|
3354 |
||
3355 |
minutes :: Double -> Time |
|
3356 |
minutes m = Time (round (m * 60000.0)) |
|
3357 |
||
3358 |
ms :: Int -> Time |
|
3359 |
ms = Time |
|
3360 |
||
3361 |
zero :: Time |
|
3362 |
zero = ms 0 |
|
3363 |
||
3364 |
is_zero :: Time -> Bool |
|
3365 |
is_zero (Time ms) = ms == 0 |
|
3366 |
||
3367 |
is_relevant :: Time -> Bool |
|
3368 |
is_relevant (Time ms) = ms >= 1 |
|
3369 |
||
3370 |
get_seconds :: Time -> Double |
|
3371 |
get_seconds (Time ms) = fromIntegral ms / 1000.0 |
|
3372 |
||
3373 |
get_minutes :: Time -> Double |
|
3374 |
get_minutes (Time ms) = fromIntegral ms / 60000.0 |
|
3375 |
||
3376 |
get_ms :: Time -> Int |
|
3377 |
get_ms (Time ms) = ms |
|
3378 |
||
3379 |
instance Show Time where |
|
3380 |
show t = printf "%.3f" (get_seconds t) |
|
3381 |
||
3382 |
message :: Time -> Bytes |
|
3383 |
message t = make_bytes (show t) <> "s" |
|
74209 | 3384 |
|
3385 |
now :: IO Time |
|
3386 |
now = do |
|
3387 |
t <- getPOSIXTime |
|
3388 |
return $ Time (round (realToFrac t * 1000.0 :: Double)) |
|
74161 | 3389 |
\<close> |
3390 |
||
3391 |
generate_file "Isabelle/Timing.hs" = \<open> |
|
3392 |
{- Title: Isabelle/Timing.hs |
|
3393 |
Author: Makarius |
|
3394 |
LICENSE: BSD 3-clause (Isabelle) |
|
3395 |
||
3396 |
Support for time measurement. |
|
3397 |
||
3398 |
See \<^file>\<open>~~/src/Pure/General/timing.ML\<close> |
|
3399 |
and \<^file>\<open>~~/src/Pure/General/timing.scala\<close> |
|
3400 |
-} |
|
3401 |
||
3402 |
module Isabelle.Timing ( |
|
3403 |
Timing (..), zero, is_zero, is_relevant |
|
3404 |
) |
|
3405 |
where |
|
3406 |
||
3407 |
import qualified Isabelle.Time as Time |
|
3408 |
import Isabelle.Time (Time) |
|
3409 |
||
3410 |
data Timing = Timing {elapsed :: Time, cpu :: Time, gc :: Time} |
|
3411 |
deriving (Show, Eq) |
|
3412 |
||
3413 |
zero :: Timing |
|
3414 |
zero = Timing Time.zero Time.zero Time.zero |
|
3415 |
||
3416 |
is_zero :: Timing -> Bool |
|
3417 |
is_zero t = Time.is_zero (elapsed t) && Time.is_zero (cpu t) && Time.is_zero (gc t) |
|
3418 |
||
3419 |
is_relevant :: Timing -> Bool |
|
3420 |
is_relevant t = Time.is_relevant (elapsed t) || Time.is_relevant (cpu t) || Time.is_relevant (gc t) |
|
3421 |
\<close> |
|
3422 |
||
3423 |
generate_file "Isabelle/Bash.hs" = \<open> |
|
3424 |
{- Title: Isabelle/Bash.hs |
|
3425 |
Author: Makarius |
|
3426 |
LICENSE: BSD 3-clause (Isabelle) |
|
3427 |
||
3428 |
Support for GNU bash. |
|
3429 |
||
3430 |
See \<^file>\<open>$ISABELLE_HOME/src/Pure/System/bash.ML\<close> |
|
3431 |
-} |
|
3432 |
||
3433 |
{-# LANGUAGE OverloadedStrings #-} |
|
3434 |
||
3435 |
module Isabelle.Bash ( |
|
3436 |
string, strings, |
|
3437 |
||
3438 |
Params, |
|
3439 |
get_script, get_input, get_cwd, get_putenv, get_redirect, |
|
3440 |
get_timeout, get_description, |
|
3441 |
script, input, cwd, putenv, redirect, timeout, description, |
|
74211 | 3442 |
server_run, server_kill, |
3443 |
server_uuid, server_interrupt, server_failure, server_result |
|
74161 | 3444 |
) |
3445 |
where |
|
3446 |
||
3447 |
import Text.Printf (printf) |
|
3448 |
import qualified Isabelle.Symbol as Symbol |
|
3449 |
import qualified Isabelle.Bytes as Bytes |
|
3450 |
import Isabelle.Bytes (Bytes) |
|
3451 |
import qualified Isabelle.Time as Time |
|
3452 |
import Isabelle.Time (Time) |
|
3453 |
import Isabelle.Library |
|
3454 |
||
3455 |
||
3456 |
{- concrete syntax -} |
|
3457 |
||
3458 |
string :: Bytes -> Bytes |
|
3459 |
string str = |
|
3460 |
if Bytes.null str then "\"\"" |
|
3461 |
else str |> Bytes.unpack |> map trans |> Bytes.concat |
|
3462 |
where |
|
3463 |
trans b = |
|
3464 |
case Bytes.char b of |
|
3465 |
'\t' -> "$'\\t'" |
|
3466 |
'\n' -> "$'\\n'" |
|
3467 |
'\f' -> "$'\\f'" |
|
3468 |
'\r' -> "$'\\r'" |
|
3469 |
c -> |
|
74169 | 3470 |
if Symbol.is_ascii_letter c || Symbol.is_ascii_digit c || c `elem` ("+,-./:_" :: String) |
74161 | 3471 |
then Bytes.singleton b |
3472 |
else if b < 32 || b >= 127 then make_bytes (printf "$'\\x%02x'" b :: String) |
|
3473 |
else "\\" <> Bytes.singleton b |
|
3474 |
||
3475 |
strings :: [Bytes] -> Bytes |
|
80910 | 3476 |
strings = implode_space . map string |
74161 | 3477 |
|
3478 |
||
3479 |
{- server parameters -} |
|
3480 |
||
3481 |
data Params = Params { |
|
3482 |
_script :: Bytes, |
|
3483 |
_input :: Bytes, |
|
3484 |
_cwd :: Maybe Bytes, |
|
3485 |
_putenv :: [(Bytes, Bytes)], |
|
3486 |
_redirect :: Bool, |
|
3487 |
_timeout :: Time, |
|
3488 |
_description :: Bytes} |
|
3489 |
deriving (Show, Eq) |
|
3490 |
||
3491 |
get_script :: Params -> Bytes |
|
3492 |
get_script = _script |
|
3493 |
||
3494 |
get_input :: Params -> Bytes |
|
3495 |
get_input = _input |
|
3496 |
||
3497 |
get_cwd :: Params -> Maybe Bytes |
|
3498 |
get_cwd = _cwd |
|
3499 |
||
3500 |
get_putenv :: Params -> [(Bytes, Bytes)] |
|
3501 |
get_putenv = _putenv |
|
3502 |
||
3503 |
get_redirect :: Params -> Bool |
|
3504 |
get_redirect = _redirect |
|
3505 |
||
3506 |
get_timeout :: Params -> Time |
|
3507 |
get_timeout = _timeout |
|
3508 |
||
3509 |
get_description :: Params -> Bytes |
|
3510 |
get_description = _description |
|
3511 |
||
3512 |
script :: Bytes -> Params |
|
3513 |
script script = Params script "" Nothing [] False Time.zero "" |
|
3514 |
||
3515 |
input :: Bytes -> Params -> Params |
|
3516 |
input input params = params { _input = input } |
|
3517 |
||
3518 |
cwd :: Bytes -> Params -> Params |
|
3519 |
cwd cwd params = params { _cwd = Just cwd } |
|
3520 |
||
3521 |
putenv :: [(Bytes, Bytes)] -> Params -> Params |
|
3522 |
putenv putenv params = params { _putenv = putenv } |
|
3523 |
||
3524 |
redirect :: Params -> Params |
|
3525 |
redirect params = params { _redirect = True } |
|
3526 |
||
3527 |
timeout :: Time -> Params -> Params |
|
3528 |
timeout timeout params = params { _timeout = timeout } |
|
3529 |
||
3530 |
description :: Bytes -> Params -> Params |
|
3531 |
description description params = params { _description = description } |
|
74211 | 3532 |
|
3533 |
||
3534 |
{- server messages -} |
|
3535 |
||
3536 |
server_run, server_kill :: Bytes |
|
3537 |
server_run = \<open>Bash.server_run\<close>; |
|
3538 |
server_kill = \<open>Bash.server_kill\<close>; |
|
3539 |
||
3540 |
server_uuid, server_interrupt, server_failure, server_result :: Bytes |
|
3541 |
server_uuid = \<open>Bash.server_uuid\<close>; |
|
3542 |
server_interrupt = \<open>Bash.server_interrupt\<close>; |
|
3543 |
server_failure = \<open>Bash.server_failure\<close>; |
|
3544 |
server_result = \<open>Bash.server_result\<close>; |
|
74161 | 3545 |
\<close> |
3546 |
||
3547 |
generate_file "Isabelle/Process_Result.hs" = \<open> |
|
3548 |
{- Title: Isabelle/Process_Result.hs |
|
3549 |
Author: Makarius |
|
3550 |
LICENSE: BSD 3-clause (Isabelle) |
|
3551 |
||
3552 |
Result of system process. |
|
3553 |
||
3554 |
See \<^file>\<open>~~/src/Pure/System/process_result.ML\<close> |
|
3555 |
and \<^file>\<open>~~/src/Pure/System/process_result.scala\<close> |
|
3556 |
-} |
|
3557 |
||
3558 |
{-# LANGUAGE OverloadedStrings #-} |
|
3559 |
||
3560 |
module Isabelle.Process_Result ( |
|
74310 | 3561 |
ok_rc, error_rc, failure_rc, interrupt_rc , timeout_rc, |
74161 | 3562 |
|
3563 |
T, make, rc, out_lines, err_lines, timing, timing_elapsed, out, err, ok, check |
|
3564 |
) |
|
3565 |
where |
|
3566 |
||
3567 |
import Isabelle.Time (Time) |
|
3568 |
import qualified Isabelle.Timing as Timing |
|
3569 |
import Isabelle.Timing (Timing) |
|
3570 |
import Isabelle.Bytes (Bytes) |
|
3571 |
import Isabelle.Library |
|
3572 |
||
3573 |
||
74310 | 3574 |
ok_rc, error_rc, failure_rc, interrupt_rc , timeout_rc :: Int |
3575 |
ok_rc = 0 |
|
3576 |
error_rc = 1 |
|
3577 |
failure_rc = 2 |
|
74161 | 3578 |
interrupt_rc = 130 |
3579 |
timeout_rc = 142 |
|
3580 |
||
3581 |
data T = |
|
3582 |
Process_Result { |
|
3583 |
_rc :: Int, |
|
3584 |
_out_lines :: [Bytes], |
|
3585 |
_err_lines :: [Bytes], |
|
3586 |
_timing :: Timing} |
|
3587 |
deriving (Show, Eq) |
|
3588 |
||
3589 |
make :: Int -> [Bytes] -> [Bytes] -> Timing -> T |
|
3590 |
make = Process_Result |
|
3591 |
||
3592 |
rc :: T -> Int |
|
3593 |
rc = _rc |
|
3594 |
||
3595 |
out_lines :: T -> [Bytes] |
|
3596 |
out_lines = _out_lines |
|
3597 |
||
3598 |
err_lines :: T -> [Bytes] |
|
3599 |
err_lines = _err_lines |
|
3600 |
||
3601 |
timing :: T -> Timing |
|
3602 |
timing = _timing |
|
3603 |
||
3604 |
timing_elapsed :: T -> Time |
|
3605 |
timing_elapsed = Timing.elapsed . timing |
|
3606 |
||
3607 |
out :: T -> Bytes |
|
3608 |
out = trim_line . cat_lines . out_lines |
|
3609 |
||
3610 |
err :: T -> Bytes |
|
3611 |
err = trim_line . cat_lines . err_lines |
|
3612 |
||
3613 |
ok :: T -> Bool |
|
74310 | 3614 |
ok result = rc result == ok_rc |
74161 | 3615 |
|
3616 |
check :: T -> T |
|
3617 |
check result = if ok result then result else error (make_string $ err result) |
|
3618 |
\<close> |
|
3619 |
||
3620 |
generate_file "Isabelle/Options.hs" = \<open> |
|
3621 |
{- Title: Isabelle/Options.hs |
|
3622 |
Author: Makarius |
|
3623 |
LICENSE: BSD 3-clause (Isabelle) |
|
3624 |
||
3625 |
System options with external string representation. |
|
3626 |
||
3627 |
See \<^file>\<open>~~/src/Pure/System/options.ML\<close> |
|
3628 |
and \<^file>\<open>~~/src/Pure/System/options.scala\<close> |
|
3629 |
-} |
|
3630 |
||
3631 |
{-# LANGUAGE OverloadedStrings #-} |
|
3632 |
{-# LANGUAGE InstanceSigs #-} |
|
3633 |
||
3634 |
module Isabelle.Options ( |
|
3635 |
boolT, intT, realT, stringT, unknownT, |
|
3636 |
||
3637 |
T, typ, bool, int, real, seconds, string, |
|
3638 |
decode |
|
3639 |
) |
|
3640 |
where |
|
3641 |
||
3642 |
import qualified Data.Map.Strict as Map |
|
3643 |
import Data.Map.Strict (Map) |
|
3644 |
import qualified Isabelle.Properties as Properties |
|
3645 |
import Isabelle.Bytes (Bytes) |
|
3646 |
import qualified Isabelle.Value as Value |
|
3647 |
import qualified Isabelle.Time as Time |
|
3648 |
import Isabelle.Time (Time) |
|
3649 |
import Isabelle.Library |
|
3650 |
import qualified Isabelle.XML.Decode as Decode |
|
3651 |
import Isabelle.XML.Classes (Decode (..)) |
|
3652 |
||
3653 |
||
3654 |
{- representation -} |
|
3655 |
||
3656 |
boolT :: Bytes |
|
3657 |
boolT = "bool" |
|
3658 |
||
3659 |
intT :: Bytes |
|
3660 |
intT = "int" |
|
3661 |
||
3662 |
realT :: Bytes |
|
3663 |
realT = "real" |
|
3664 |
||
3665 |
stringT :: Bytes |
|
3666 |
stringT = "string" |
|
3667 |
||
3668 |
unknownT :: Bytes |
|
3669 |
unknownT = "unknown" |
|
3670 |
||
3671 |
data Opt = Opt { |
|
3672 |
_pos :: Properties.T, |
|
3673 |
_name :: Bytes, |
|
3674 |
_typ :: Bytes, |
|
3675 |
_value :: Bytes } |
|
3676 |
||
75015 | 3677 |
newtype T = Options (Map Bytes Opt) |
74161 | 3678 |
|
3679 |
||
3680 |
{- check -} |
|
3681 |
||
3682 |
check_name :: T -> Bytes -> Opt |
|
3683 |
check_name (Options map) name = |
|
3684 |
case Map.lookup name map of |
|
3685 |
Just opt | _typ opt /= unknownT -> opt |
|
3686 |
_ -> error (make_string ("Unknown system option " <> quote name)) |
|
3687 |
||
3688 |
check_type :: T -> Bytes -> Bytes -> Opt |
|
3689 |
check_type options name typ = |
|
3690 |
let |
|
3691 |
opt = check_name options name |
|
3692 |
t = _typ opt |
|
3693 |
in |
|
3694 |
if t == typ then opt |
|
3695 |
else error (make_string ("Ill-typed system option " <> quote name <> " : " <> t <> " vs. " <> typ)) |
|
3696 |
||
3697 |
||
3698 |
{- get typ -} |
|
3699 |
||
3700 |
typ :: T -> Bytes -> Bytes |
|
3701 |
typ options name = _typ (check_name options name) |
|
3702 |
||
3703 |
||
3704 |
{- get value -} |
|
3705 |
||
3706 |
get :: Bytes -> (Bytes -> Maybe a) -> T -> Bytes -> a |
|
3707 |
get typ parse options name = |
|
3708 |
let opt = check_type options name typ in |
|
3709 |
case parse (_value opt) of |
|
3710 |
Just x -> x |
|
3711 |
Nothing -> |
|
3712 |
error (make_string ("Malformed value for system option " <> quote name <> |
|
3713 |
" : " <> typ <> " =\n" <> quote (_value opt))) |
|
3714 |
||
3715 |
bool :: T -> Bytes -> Bool |
|
3716 |
bool = get boolT Value.parse_bool |
|
3717 |
||
3718 |
int :: T -> Bytes -> Int |
|
3719 |
int = get intT Value.parse_int |
|
3720 |
||
3721 |
real :: T -> Bytes -> Double |
|
3722 |
real = get realT Value.parse_real |
|
3723 |
||
3724 |
seconds :: T -> Bytes -> Time |
|
3725 |
seconds options = Time.seconds . real options |
|
3726 |
||
3727 |
string :: T -> Bytes -> Bytes |
|
3728 |
string = get stringT Just |
|
3729 |
||
3730 |
||
3731 |
{- decode -} |
|
3732 |
||
3733 |
instance Decode T where |
|
3734 |
decode :: Decode.T T |
|
3735 |
decode = |
|
3736 |
let |
|
3737 |
decode_entry :: Decode.T (Bytes, Opt) |
|
3738 |
decode_entry body = |
|
3739 |
let |
|
3740 |
(pos, (name, (typ, value))) = |
|
3741 |
Decode.pair Decode.properties (Decode.pair Decode.string (Decode.pair Decode.string Decode.string)) body |
|
3742 |
in (name, Opt { _pos = pos, _name = name, _typ = typ, _value = value }) |
|
3743 |
in Options . Map.fromList . Decode.list decode_entry |
|
3744 |
\<close> |
|
3745 |
||
74211 | 3746 |
generate_file "Isabelle/Isabelle_System.hs" = \<open> |
3747 |
{- Title: Isabelle/Isabelle_System.hs |
|
3748 |
Author: Makarius |
|
3749 |
LICENSE: BSD 3-clause (Isabelle) |
|
3750 |
||
3751 |
Isabelle system support. |
|
3752 |
||
3753 |
See \<^file>\<open>~~/src/Pure/System/isabelle_system.ML\<close> |
|
3754 |
and \<^file>\<open>~~/src/Pure/System/isabelle_system.scala\<close> |
|
3755 |
-} |
|
3756 |
||
3757 |
{-# LANGUAGE OverloadedStrings #-} |
|
3758 |
||
3759 |
module Isabelle.Isabelle_System ( |
|
3760 |
bash_process, bash_process0 |
|
3761 |
) |
|
3762 |
where |
|
3763 |
||
3764 |
import Data.Maybe (fromMaybe) |
|
3765 |
import Control.Exception (throw, AsyncException (UserInterrupt)) |
|
3766 |
import Network.Socket (Socket) |
|
74216 | 3767 |
import qualified Isabelle.Bytes as Bytes |
74211 | 3768 |
import Isabelle.Bytes (Bytes) |
3769 |
import qualified Isabelle.Byte_Message as Byte_Message |
|
3770 |
import qualified Isabelle.Time as Time |
|
3771 |
import Isabelle.Timing (Timing (..)) |
|
3772 |
import qualified Isabelle.Options as Options |
|
3773 |
import qualified Isabelle.Bash as Bash |
|
3774 |
import qualified Isabelle.Process_Result as Process_Result |
|
3775 |
import qualified Isabelle.XML.Encode as Encode |
|
3776 |
import qualified Isabelle.YXML as YXML |
|
3777 |
import qualified Isabelle.Value as Value |
|
3778 |
import qualified Isabelle.Server as Server |
|
3779 |
import qualified Isabelle.Isabelle_Thread as Isabelle_Thread |
|
3780 |
import Isabelle.Library |
|
3781 |
||
3782 |
||
3783 |
{- bash_process -} |
|
3784 |
||
3785 |
absolute_path :: Bytes -> Bytes -- FIXME dummy |
|
3786 |
absolute_path = id |
|
3787 |
||
3788 |
bash_process :: Options.T -> Bash.Params -> IO Process_Result.T |
|
3789 |
bash_process options = bash_process0 address password |
|
3790 |
where |
|
3791 |
address = Options.string options "bash_process_address" |
|
3792 |
password = Options.string options "bash_process_password" |
|
3793 |
||
3794 |
bash_process0 :: Bytes -> Bytes -> Bash.Params -> IO Process_Result.T |
|
3795 |
bash_process0 address password params = do |
|
3796 |
Server.connection port password |
|
3797 |
(\socket -> do |
|
3798 |
isabelle_tmp <- getenv "ISABELLE_TMP" |
|
3799 |
Byte_Message.write_message socket (run isabelle_tmp) |
|
3800 |
loop Nothing socket) |
|
3801 |
where |
|
3802 |
port = |
|
74216 | 3803 |
case Bytes.try_unprefix Server.localhost_prefix address of |
74211 | 3804 |
Just port -> make_string port |
3805 |
Nothing -> errorWithoutStackTrace "Bad bash_process server address" |
|
3806 |
||
3807 |
script = Bash.get_script params |
|
3808 |
input = Bash.get_input params |
|
3809 |
cwd = Bash.get_cwd params |
|
3810 |
putenv = Bash.get_putenv params |
|
3811 |
redirect = Bash.get_redirect params |
|
3812 |
timeout = Bash.get_timeout params |
|
3813 |
description = Bash.get_description params |
|
3814 |
||
3815 |
run :: Bytes -> [Bytes] |
|
3816 |
run isabelle_tmp = |
|
3817 |
[Bash.server_run, script, input, |
|
3818 |
YXML.string_of_body (Encode.option (Encode.string . absolute_path) cwd), |
|
3819 |
YXML.string_of_body |
|
3820 |
(Encode.list (Encode.pair Encode.string Encode.string) |
|
3821 |
(("ISABELLE_TMP", isabelle_tmp) : putenv)), |
|
3822 |
Value.print_bool redirect, |
|
3823 |
Value.print_real (Time.get_seconds timeout), |
|
3824 |
description] |
|
3825 |
||
3826 |
kill :: Maybe Bytes -> IO () |
|
3827 |
kill maybe_uuid = do |
|
3828 |
case maybe_uuid of |
|
3829 |
Just uuid -> |
|
3830 |
Server.connection port password (\socket -> |
|
3831 |
Byte_Message.write_message socket [Bash.server_kill, uuid]) |
|
3832 |
Nothing -> return () |
|
3833 |
||
3834 |
err = errorWithoutStackTrace "Malformed result from bash_process server" |
|
3835 |
the = fromMaybe err |
|
3836 |
||
3837 |
loop :: Maybe Bytes -> Socket -> IO Process_Result.T |
|
3838 |
loop maybe_uuid socket = do |
|
3839 |
result <- Isabelle_Thread.bracket_resource (kill maybe_uuid) (Byte_Message.read_message socket) |
|
3840 |
case result of |
|
3841 |
Just [head, uuid] | head == Bash.server_uuid -> loop (Just uuid) socket |
|
3842 |
Just [head] | head == Bash.server_interrupt -> throw UserInterrupt |
|
3843 |
Just [head, msg] | head == Bash.server_failure -> errorWithoutStackTrace $ make_string msg |
|
3844 |
Just (head : a : b : c : d : lines) | head == Bash.server_result -> |
|
3845 |
let |
|
3846 |
rc = the $ Value.parse_int a |
|
3847 |
elapsed = Time.ms $ the $ Value.parse_int b |
|
3848 |
cpu = Time.ms $ the $ Value.parse_int c |
|
3849 |
timing = Timing elapsed cpu Time.zero |
|
3850 |
n = the $ Value.parse_int d |
|
3851 |
out_lines = take n lines |
|
3852 |
err_lines = drop n lines |
|
3853 |
in return $ Process_Result.make rc out_lines err_lines timing |
|
3854 |
_ -> err |
|
3855 |
\<close> |
|
3856 |
||
75068 | 3857 |
generate_file "Isabelle/Cache.hs" = \<open> |
3858 |
{- Title: Isabelle/Cache.hs |
|
3859 |
Author: Makarius |
|
3860 |
LICENSE: BSD 3-clause (Isabelle) |
|
3861 |
||
3862 |
Cache for slow computations. |
|
3863 |
-} |
|
3864 |
||
3865 |
module Isabelle.Cache ( |
|
3866 |
T, init, apply, prune |
|
3867 |
) |
|
3868 |
where |
|
3869 |
||
3870 |
import Prelude hiding (init) |
|
3871 |
import Data.IORef |
|
3872 |
import Data.Map.Strict (Map) |
|
3873 |
import qualified Data.Map.Strict as Map |
|
3874 |
import qualified Data.List as List |
|
3875 |
||
3876 |
import Isabelle.Time (Time) |
|
3877 |
import qualified Isabelle.Time as Time |
|
3878 |
||
3879 |
||
3880 |
data Entry v = Entry {_value :: v, _access :: Time, _timing :: Time} |
|
3881 |
||
3882 |
newtype T k v = Cache (IORef (Map k (Entry v))) |
|
3883 |
||
3884 |
init :: IO (T k v) |
|
3885 |
init = Cache <$> newIORef Map.empty |
|
3886 |
||
3887 |
commit :: Ord k => T k v -> k -> Entry v -> IO v |
|
3888 |
commit (Cache ref) x e = do |
|
3889 |
atomicModifyIORef' ref (\entries -> |
|
3890 |
let |
|
3891 |
entry = |
|
3892 |
case Map.lookup x entries of |
|
3893 |
Just e' | _access e' > _access e -> e' |
|
3894 |
_ -> e |
|
3895 |
in (Map.insert x entry entries, _value entry)) |
|
3896 |
||
75072 | 3897 |
apply :: Ord k => T k v -> k -> IO v -> IO v |
3898 |
apply cache@(Cache ref) x body = do |
|
75068 | 3899 |
start <- Time.now |
3900 |
entries <- readIORef ref |
|
3901 |
case Map.lookup x entries of |
|
3902 |
Just entry -> do |
|
3903 |
commit cache x (entry {_access = start}) |
|
3904 |
Nothing -> do |
|
75072 | 3905 |
y <- body |
75068 | 3906 |
stop <- Time.now |
3907 |
commit cache x (Entry y start (stop - start)) |
|
3908 |
||
3909 |
prune :: Ord k => T k v -> Int -> Time -> IO () |
|
3910 |
prune (Cache ref) max_size min_timing = do |
|
3911 |
atomicModifyIORef' ref (\entries -> |
|
3912 |
let |
|
3913 |
sort = List.sortBy (\(_, e1) (_, e2) -> compare (_access e2) (_access e1)) |
|
79741
513829904beb
more scalable: avoid potentially expensive ordering of underlying key data type, e.g. in MESON.Cache of Naproche;
wenzelm
parents:
78307
diff
changeset
|
3914 |
entries1 = Map.filter (\e -> _timing e >= min_timing) entries |
75068 | 3915 |
entries2 = |
3916 |
if Map.size entries1 <= max_size then entries1 |
|
3917 |
else Map.fromList $ List.take max_size $ sort $ Map.toList entries1 |
|
3918 |
in (entries2, ())) |
|
3919 |
\<close> |
|
3920 |
||
70047
96fe857a7a6f
clarified signature: more explicit operations for corresponding Isar commands;
wenzelm
parents:
69968
diff
changeset
|
3921 |
export_generated_files _ |
69628 | 3922 |
|
69222 | 3923 |
end |