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