69222
|
1 |
(* Title: Tools/Haskell/Haskell.thy
|
|
2 |
Author: Makarius
|
69225
|
3 |
|
|
4 |
Support for Isabelle tools in Haskell.
|
69222
|
5 |
*)
|
|
6 |
|
|
7 |
theory Haskell
|
|
8 |
imports Pure
|
|
9 |
keywords "generate_haskell_file" "export_haskell_file" :: thy_decl
|
|
10 |
begin
|
|
11 |
|
69225
|
12 |
ML_file "haskell.ML"
|
|
13 |
|
|
14 |
|
|
15 |
section \<open>Commands\<close>
|
|
16 |
|
69222
|
17 |
ML \<open>
|
|
18 |
Outer_Syntax.command \<^command_keyword>\<open>generate_haskell_file\<close> "generate Haskell file"
|
69225
|
19 |
(Parse.position Parse.path -- (\<^keyword>\<open>=\<close> |-- Parse.input Parse.embedded)
|
|
20 |
>> Haskell.generate_file_cmd);
|
|
21 |
|
|
22 |
Outer_Syntax.command \<^command_keyword>\<open>export_haskell_file\<close> "export Haskell file"
|
|
23 |
(Parse.name -- (\<^keyword>\<open>=\<close> |-- Parse.input Parse.embedded)
|
|
24 |
>> Haskell.export_file_cmd);
|
|
25 |
\<close>
|
|
26 |
|
|
27 |
|
|
28 |
section \<open>Source modules\<close>
|
|
29 |
|
69226
|
30 |
generate_haskell_file Library.hs = \<open>
|
|
31 |
{- Title: Tools/Haskell/Library.hs
|
69225
|
32 |
Author: Makarius
|
|
33 |
LICENSE: BSD 3-clause (Isabelle)
|
|
34 |
|
|
35 |
Basic library of Isabelle idioms.
|
|
36 |
-}
|
|
37 |
|
|
38 |
module Isabelle.Library
|
|
39 |
((|>), (|->), (#>), (#->), fold, fold_rev, single, quote, trim_line)
|
|
40 |
where
|
|
41 |
|
|
42 |
{- functions -}
|
|
43 |
|
|
44 |
(|>) :: a -> (a -> b) -> b
|
|
45 |
x |> f = f x
|
|
46 |
|
|
47 |
(|->) :: (a, b) -> (a -> b -> c) -> c
|
|
48 |
(x, y) |-> f = f x y
|
|
49 |
|
|
50 |
(#>) :: (a -> b) -> (b -> c) -> a -> c
|
|
51 |
(f #> g) x = x |> f |> g
|
|
52 |
|
|
53 |
(#->) :: (a -> (c, b)) -> (c -> b -> d) -> a -> d
|
|
54 |
(f #-> g) x = x |> f |-> g
|
|
55 |
|
|
56 |
|
|
57 |
{- lists -}
|
|
58 |
|
|
59 |
fold :: (a -> b -> b) -> [a] -> b -> b
|
|
60 |
fold _ [] y = y
|
|
61 |
fold f (x : xs) y = fold f xs (f x y)
|
|
62 |
|
|
63 |
fold_rev :: (a -> b -> b) -> [a] -> b -> b
|
|
64 |
fold_rev _ [] y = y
|
|
65 |
fold_rev f (x : xs) y = f x (fold_rev f xs y)
|
|
66 |
|
|
67 |
single :: a -> [a]
|
|
68 |
single x = [x]
|
|
69 |
|
|
70 |
|
|
71 |
{- strings -}
|
|
72 |
|
|
73 |
quote :: String -> String
|
|
74 |
quote s = "\"" ++ s ++ "\""
|
|
75 |
|
|
76 |
trim_line :: String -> String
|
|
77 |
trim_line line =
|
|
78 |
case reverse line of
|
|
79 |
'\n' : '\r' : rest -> reverse rest
|
|
80 |
'\n' : rest -> reverse rest
|
|
81 |
_ -> line
|
|
82 |
\<close>
|
|
83 |
|
69233
|
84 |
generate_haskell_file Value.hs = \<open>
|
|
85 |
{- Title: Haskell/Tools/Value.hs
|
|
86 |
Author: Makarius
|
|
87 |
LICENSE: BSD 3-clause (Isabelle)
|
|
88 |
|
|
89 |
Plain values, represented as string.
|
|
90 |
-}
|
|
91 |
|
|
92 |
module Isabelle.Value
|
|
93 |
(print_bool, parse_bool, print_int, parse_int, print_real, parse_real)
|
|
94 |
where
|
|
95 |
|
|
96 |
import Data.Maybe
|
|
97 |
import qualified Data.List as List
|
|
98 |
import qualified Text.Read as Read
|
|
99 |
|
|
100 |
|
|
101 |
{- bool -}
|
|
102 |
|
|
103 |
print_bool :: Bool -> String
|
|
104 |
print_bool True = "true"
|
|
105 |
print_bool False = "false"
|
|
106 |
|
|
107 |
parse_bool :: String -> Maybe Bool
|
|
108 |
parse_bool "true" = Just True
|
|
109 |
parse_bool "false" = Just False
|
|
110 |
parse_bool _ = Nothing
|
|
111 |
|
|
112 |
|
|
113 |
{- int -}
|
|
114 |
|
|
115 |
print_int :: Int -> String
|
|
116 |
print_int = show
|
|
117 |
|
|
118 |
parse_int :: String -> Maybe Int
|
|
119 |
parse_int = Read.readMaybe
|
|
120 |
|
|
121 |
|
|
122 |
{- real -}
|
|
123 |
|
|
124 |
print_real :: Double -> String
|
|
125 |
print_real x =
|
|
126 |
let s = show x in
|
|
127 |
case span (/= '.') s of
|
|
128 |
(a, '.' : b) | List.all (== '0') b -> a
|
|
129 |
_ -> s
|
|
130 |
|
|
131 |
parse_real :: String -> Maybe Double
|
|
132 |
parse_real = Read.readMaybe
|
|
133 |
\<close>
|
|
134 |
|
69226
|
135 |
generate_haskell_file Buffer.hs = \<open>
|
|
136 |
{- Title: Tools/Haskell/Buffer.hs
|
69225
|
137 |
Author: Makarius
|
|
138 |
LICENSE: BSD 3-clause (Isabelle)
|
|
139 |
|
|
140 |
Efficient text buffers.
|
|
141 |
-}
|
69222
|
142 |
|
69225
|
143 |
module Isabelle.Buffer (T, empty, add, content)
|
|
144 |
where
|
|
145 |
|
|
146 |
newtype T = Buffer [String]
|
|
147 |
|
|
148 |
empty :: T
|
|
149 |
empty = Buffer []
|
|
150 |
|
|
151 |
add :: String -> T -> T
|
|
152 |
add "" buf = buf
|
|
153 |
add x (Buffer xs) = Buffer (x : xs)
|
|
154 |
|
|
155 |
content :: T -> String
|
|
156 |
content (Buffer xs) = concat (reverse xs)
|
|
157 |
\<close>
|
|
158 |
|
69226
|
159 |
generate_haskell_file Properties.hs = \<open>
|
|
160 |
{- Title: Tools/Haskell/Properties.hs
|
69225
|
161 |
Author: Makarius
|
|
162 |
LICENSE: BSD 3-clause (Isabelle)
|
|
163 |
|
|
164 |
Property lists.
|
|
165 |
-}
|
|
166 |
|
|
167 |
module Isabelle.Properties (Entry, T, defined, get, put, remove)
|
|
168 |
where
|
|
169 |
|
|
170 |
import qualified Data.List as List
|
|
171 |
|
|
172 |
|
|
173 |
type Entry = (String, String)
|
|
174 |
type T = [Entry]
|
|
175 |
|
|
176 |
defined :: T -> String -> Bool
|
|
177 |
defined props name = any (\(a, _) -> a == name) props
|
|
178 |
|
|
179 |
get :: T -> String -> Maybe String
|
|
180 |
get props name = List.lookup name props
|
|
181 |
|
|
182 |
put :: Entry -> T -> T
|
|
183 |
put entry props = entry : remove (fst entry) props
|
|
184 |
|
|
185 |
remove :: String -> T -> T
|
|
186 |
remove name props =
|
|
187 |
if defined props name then filter (\(a, _) -> a /= name) props
|
|
188 |
else props
|
|
189 |
\<close>
|
|
190 |
|
69226
|
191 |
generate_haskell_file Markup.hs = \<open>
|
|
192 |
{- Title: Haskell/Tools/Markup.hs
|
69225
|
193 |
Author: Makarius
|
|
194 |
LICENSE: BSD 3-clause (Isabelle)
|
|
195 |
|
|
196 |
Quasi-abstract markup elements.
|
|
197 |
-}
|
|
198 |
|
|
199 |
module Isabelle.Markup (T, empty, is_empty, Output, no_output)
|
|
200 |
where
|
|
201 |
|
|
202 |
import qualified Isabelle.Properties as Properties
|
|
203 |
|
|
204 |
|
|
205 |
type T = (String, Properties.T)
|
|
206 |
|
|
207 |
empty :: T
|
|
208 |
empty = ("", [])
|
|
209 |
|
|
210 |
is_empty :: T -> Bool
|
|
211 |
is_empty ("", _) = True
|
|
212 |
is_empty _ = False
|
|
213 |
|
|
214 |
|
|
215 |
type Output = (String, String)
|
|
216 |
|
|
217 |
no_output :: Output
|
|
218 |
no_output = ("", "")
|
69222
|
219 |
\<close>
|
|
220 |
|
69226
|
221 |
generate_haskell_file XML.hs = \<open>
|
|
222 |
{- Title: Tools/Haskell/XML.hs
|
69225
|
223 |
Author: Makarius
|
|
224 |
LICENSE: BSD 3-clause (Isabelle)
|
|
225 |
|
|
226 |
Untyped XML trees and representation of ML values.
|
|
227 |
-}
|
|
228 |
|
|
229 |
module Isabelle.XML (Attributes, Body, Tree(..), wrap_elem, unwrap_elem, content_of)
|
|
230 |
where
|
|
231 |
|
|
232 |
import qualified Data.List as List
|
|
233 |
|
|
234 |
import Isabelle.Library
|
|
235 |
import qualified Isabelle.Properties as Properties
|
|
236 |
import qualified Isabelle.Markup as Markup
|
|
237 |
import qualified Isabelle.Buffer as Buffer
|
|
238 |
|
|
239 |
|
|
240 |
{- types -}
|
|
241 |
|
|
242 |
type Attributes = Properties.T
|
|
243 |
type Body = [Tree]
|
|
244 |
data Tree = Elem Markup.T Body | Text String
|
|
245 |
|
|
246 |
|
|
247 |
{- wrapped elements -}
|
|
248 |
|
|
249 |
xml_elemN = "xml_elem";
|
|
250 |
xml_nameN = "xml_name";
|
|
251 |
xml_bodyN = "xml_body";
|
|
252 |
|
|
253 |
wrap_elem (((a, atts), body1), body2) =
|
|
254 |
Elem (xml_elemN, (xml_nameN, a) : atts) (Elem (xml_bodyN, []) body1 : body2)
|
|
255 |
|
|
256 |
unwrap_elem (Elem (name, (n, a) : atts) (Elem (name', atts') body1 : body2)) =
|
|
257 |
if name == xml_elemN && n == xml_nameN && name' == xml_bodyN && null atts'
|
|
258 |
then Just (((a, atts), body1), body2) else Nothing
|
|
259 |
unwrap_elem _ = Nothing
|
|
260 |
|
|
261 |
|
|
262 |
{- text content -}
|
|
263 |
|
|
264 |
add_content tree =
|
|
265 |
case unwrap_elem tree of
|
|
266 |
Just (_, ts) -> fold add_content ts
|
|
267 |
Nothing ->
|
|
268 |
case tree of
|
|
269 |
Elem _ ts -> fold add_content ts
|
|
270 |
Text s -> Buffer.add s
|
|
271 |
|
|
272 |
content_of body = Buffer.empty |> fold add_content body |> Buffer.content
|
|
273 |
|
|
274 |
|
|
275 |
{- string representation -}
|
|
276 |
|
|
277 |
encode '<' = "<"
|
|
278 |
encode '>' = ">"
|
|
279 |
encode '&' = "&"
|
|
280 |
encode '\'' = "'"
|
|
281 |
encode '\"' = """
|
|
282 |
encode c = [c]
|
|
283 |
|
|
284 |
instance Show Tree where
|
|
285 |
show tree =
|
|
286 |
Buffer.empty |> show_tree tree |> Buffer.content
|
|
287 |
where
|
|
288 |
show_tree (Elem (name, atts) []) =
|
|
289 |
Buffer.add "<" #> Buffer.add (show_elem name atts) #> Buffer.add "/>"
|
|
290 |
show_tree (Elem (name, atts) ts) =
|
|
291 |
Buffer.add "<" #> Buffer.add (show_elem name atts) #> Buffer.add ">" #>
|
|
292 |
fold show_tree ts #>
|
|
293 |
Buffer.add "</" #> Buffer.add name #> Buffer.add ">"
|
|
294 |
show_tree (Text s) = Buffer.add (show_text s)
|
|
295 |
|
|
296 |
show_elem name atts =
|
|
297 |
unwords (name : map (\(a, x) -> a ++ "=\"" ++ show_text x ++ "\"") atts)
|
|
298 |
|
|
299 |
show_text = concatMap encode
|
|
300 |
\<close>
|
|
301 |
|
69226
|
302 |
generate_haskell_file YXML.hs = \<open>
|
|
303 |
{- Title: Tools/Haskell/YXML.hs
|
69225
|
304 |
Author: Makarius
|
|
305 |
LICENSE: BSD 3-clause (Isabelle)
|
|
306 |
|
|
307 |
Efficient text representation of XML trees. Suitable for direct
|
|
308 |
inlining into plain text.
|
|
309 |
-}
|
|
310 |
|
|
311 |
module Isabelle.YXML (charX, charY, strX, strY, detect,
|
|
312 |
buffer_body, buffer, string_of_body, string_of, parse_body, parse)
|
|
313 |
where
|
|
314 |
|
|
315 |
import qualified Data.Char as Char
|
|
316 |
import qualified Data.List as List
|
69222
|
317 |
|
69225
|
318 |
import Isabelle.Library
|
|
319 |
import qualified Isabelle.Markup as Markup
|
|
320 |
import qualified Isabelle.XML as XML
|
|
321 |
import qualified Isabelle.Buffer as Buffer
|
|
322 |
|
|
323 |
|
|
324 |
{- markers -}
|
|
325 |
|
|
326 |
charX, charY :: Char
|
|
327 |
charX = Char.chr 5
|
|
328 |
charY = Char.chr 6
|
|
329 |
|
|
330 |
strX, strY, strXY, strXYX :: String
|
|
331 |
strX = [charX]
|
|
332 |
strY = [charY]
|
|
333 |
strXY = strX ++ strY
|
|
334 |
strXYX = strXY ++ strX
|
|
335 |
|
|
336 |
detect :: String -> Bool
|
|
337 |
detect = any (\c -> c == charX || c == charY)
|
|
338 |
|
|
339 |
|
|
340 |
{- output -}
|
|
341 |
|
|
342 |
buffer_attrib (a, x) =
|
|
343 |
Buffer.add strY #> Buffer.add a #> Buffer.add "=" #> Buffer.add x
|
|
344 |
|
|
345 |
buffer_body :: XML.Body -> Buffer.T -> Buffer.T
|
|
346 |
buffer_body = fold buffer
|
|
347 |
|
|
348 |
buffer :: XML.Tree -> Buffer.T -> Buffer.T
|
|
349 |
buffer (XML.Elem (name, atts) ts) =
|
|
350 |
Buffer.add strXY #> Buffer.add name #> fold buffer_attrib atts #> Buffer.add strX #>
|
|
351 |
buffer_body ts #>
|
|
352 |
Buffer.add strXYX
|
|
353 |
buffer (XML.Text s) = Buffer.add s
|
|
354 |
|
|
355 |
string_of_body :: XML.Body -> String
|
|
356 |
string_of_body body = Buffer.empty |> buffer_body body |> Buffer.content
|
|
357 |
|
|
358 |
string_of :: XML.Tree -> String
|
|
359 |
string_of = string_of_body . single
|
|
360 |
|
|
361 |
|
|
362 |
{- parse -}
|
|
363 |
|
|
364 |
-- split: fields or non-empty tokens
|
|
365 |
|
|
366 |
split :: Bool -> Char -> String -> [String]
|
|
367 |
split _ _ [] = []
|
|
368 |
split fields sep str = splitting str
|
|
369 |
where
|
|
370 |
splitting rest =
|
|
371 |
case span (/= sep) rest of
|
|
372 |
(_, []) -> cons rest []
|
|
373 |
(prfx, _ : rest') -> cons prfx (splitting rest')
|
|
374 |
cons item = if fields || not (null item) then (:) item else id
|
|
375 |
|
|
376 |
|
|
377 |
-- structural errors
|
|
378 |
|
|
379 |
err msg = error ("Malformed YXML: " ++ msg)
|
|
380 |
err_attribute = err "bad attribute"
|
|
381 |
err_element = err "bad element"
|
|
382 |
err_unbalanced "" = err "unbalanced element"
|
|
383 |
err_unbalanced name = err ("unbalanced element " ++ quote name)
|
|
384 |
|
|
385 |
|
|
386 |
-- stack operations
|
|
387 |
|
|
388 |
add x ((elem, body) : pending) = (elem, x : body) : pending
|
|
389 |
|
|
390 |
push "" _ _ = err_element
|
|
391 |
push name atts pending = ((name, atts), []) : pending
|
|
392 |
|
|
393 |
pop ((("", _), _) : _) = err_unbalanced ""
|
|
394 |
pop ((markup, body) : pending) = add (XML.Elem markup (reverse body)) pending
|
|
395 |
|
|
396 |
|
|
397 |
-- parsing
|
|
398 |
|
|
399 |
parse_attrib s =
|
|
400 |
case List.elemIndex '=' s of
|
|
401 |
Just i | i > 0 -> (take i s, drop (i + 1) s)
|
|
402 |
_ -> err_attribute
|
|
403 |
|
|
404 |
parse_chunk ["", ""] = pop
|
|
405 |
parse_chunk ("" : name : atts) = push name (map parse_attrib atts)
|
|
406 |
parse_chunk txts = fold (add . XML.Text) txts
|
|
407 |
|
|
408 |
parse_body :: String -> XML.Body
|
|
409 |
parse_body source =
|
|
410 |
case fold parse_chunk chunks [(("", []), [])] of
|
|
411 |
[(("", _), result)] -> reverse result
|
|
412 |
((name, _), _) : _ -> err_unbalanced name
|
|
413 |
where chunks = split False charX source |> map (split True charY)
|
|
414 |
|
|
415 |
parse :: String -> XML.Tree
|
|
416 |
parse source =
|
|
417 |
case parse_body source of
|
|
418 |
[result] -> result
|
|
419 |
[] -> XML.Text ""
|
|
420 |
_ -> err "multiple results"
|
69222
|
421 |
\<close>
|
|
422 |
|
|
423 |
end
|