src/Tools/Haskell/Haskell.thy
changeset 74216 a308ed696b58
parent 74214 e16ac8907148
child 74217 736374547a7f
equal deleted inserted replaced
74215:7515abfe18cf 74216:a308ed696b58
    23 
    23 
    24 module Isabelle.Bytes (
    24 module Isabelle.Bytes (
    25   Bytes,
    25   Bytes,
    26   make, unmake, pack, unpack,
    26   make, unmake, pack, unpack,
    27   empty, null, length, index, all, any,
    27   empty, null, length, index, all, any,
    28   head, last, take, drop, isPrefixOf, isSuffixOf,
    28   head, last, take, drop, isPrefixOf, isSuffixOf, try_unprefix, try_unsuffix,
    29   concat, space, spaces, char, all_char, any_char, byte, singleton
    29   concat, space, spaces, char, all_char, any_char, byte, singleton
    30 )
    30 )
    31 where
    31 where
    32 
    32 
    33 import Prelude hiding (null, length, all, any, head, last, take, drop, concat)
    33 import Prelude hiding (null, length, all, any, head, last, take, drop, concat)
    78 
    78 
    79 last :: Bytes -> Word8
    79 last :: Bytes -> Word8
    80 last bytes = index bytes (length bytes - 1)
    80 last bytes = index bytes (length bytes - 1)
    81 
    81 
    82 take :: Int -> Bytes -> Bytes
    82 take :: Int -> Bytes -> Bytes
    83 take n = pack . List.take n . unpack
    83 take n bs
       
    84   | n == 0 = empty
       
    85   | n >= length bs = bs
       
    86   | otherwise = pack (List.take n (unpack bs))
    84 
    87 
    85 drop :: Int -> Bytes -> Bytes
    88 drop :: Int -> Bytes -> Bytes
    86 drop n = pack . List.drop n . unpack
    89 drop n bs
       
    90   | n == 0 = bs
       
    91   | n >= length bs = empty
       
    92   | otherwise = pack (List.drop n (unpack bs))
    87 
    93 
    88 isPrefixOf :: Bytes -> Bytes -> Bool
    94 isPrefixOf :: Bytes -> Bytes -> Bool
    89 isPrefixOf bs1 bs2 =
    95 isPrefixOf bs1 bs2 =
    90   n1 <= n2 && List.all (\i -> index bs1 i == index bs2 i) [0 .. n1 - 1]
    96   n1 <= n2 && List.all (\i -> index bs1 i == index bs2 i) [0 .. n1 - 1]
    91   where n1 = length bs1; n2 = length bs2
    97   where n1 = length bs1; n2 = length bs2
    92 
    98 
    93 isSuffixOf :: Bytes -> Bytes -> Bool
    99 isSuffixOf :: Bytes -> Bytes -> Bool
    94 isSuffixOf bs1 bs2 =
   100 isSuffixOf bs1 bs2 =
    95   n1 <= n2 && List.all (\i -> index bs1 i == index bs2 (i + k)) [0 .. n1 - 1]
   101   n1 <= n2 && List.all (\i -> index bs1 i == index bs2 (i + k)) [0 .. n1 - 1]
    96   where n1 = length bs1; n2 = length bs2; k = n2 - n1
   102   where n1 = length bs1; n2 = length bs2; k = n2 - n1
       
   103 
       
   104 try_unprefix :: Bytes -> Bytes -> Maybe Bytes
       
   105 try_unprefix bs1 bs2 =
       
   106   if isPrefixOf bs1 bs2 then Just (drop (length bs1) bs2)
       
   107   else Nothing
       
   108 
       
   109 try_unsuffix :: Bytes -> Bytes -> Maybe Bytes
       
   110 try_unsuffix bs1 bs2 =
       
   111   if isSuffixOf bs1 bs2 then Just (take (length bs2 - length bs1) bs2)
       
   112   else Nothing
    97 
   113 
    98 concat :: [Bytes] -> Bytes
   114 concat :: [Bytes] -> Bytes
    99 concat = mconcat
   115 concat = mconcat
   100 
   116 
   101 space :: Word8
   117 space :: Word8
  3150 -}
  3166 -}
  3151 
  3167 
  3152 {-# LANGUAGE OverloadedStrings #-}
  3168 {-# LANGUAGE OverloadedStrings #-}
  3153 
  3169 
  3154 module Isabelle.Server (
  3170 module Isabelle.Server (
  3155   localhost_name, localhost_unprefix, localhost, publish_text, publish_stdout,
  3171   localhost_name, localhost_prefix, localhost, publish_text, publish_stdout,
  3156   server, connection
  3172   server, connection
  3157 )
  3173 )
  3158 where
  3174 where
  3159 
  3175 
  3160 import Control.Monad (forever, when)
  3176 import Control.Monad (forever, when)
  3177 localhost_name :: Bytes
  3193 localhost_name :: Bytes
  3178 localhost_name = "127.0.0.1"
  3194 localhost_name = "127.0.0.1"
  3179 
  3195 
  3180 localhost_prefix :: Bytes
  3196 localhost_prefix :: Bytes
  3181 localhost_prefix = localhost_name <> ":"
  3197 localhost_prefix = localhost_name <> ":"
  3182 
       
  3183 localhost_unprefix :: Bytes -> Maybe Bytes
       
  3184 localhost_unprefix address =
       
  3185   if Bytes.isPrefixOf localhost_prefix address
       
  3186   then Just $ Bytes.drop (Bytes.length localhost_prefix) address
       
  3187   else Nothing
       
  3188 
  3198 
  3189 localhost :: Socket.HostAddress
  3199 localhost :: Socket.HostAddress
  3190 localhost = Socket.tupleToHostAddress (127, 0, 0, 1)
  3200 localhost = Socket.tupleToHostAddress (127, 0, 0, 1)
  3191 
  3201 
  3192 publish_text :: Bytes -> Bytes -> UUID.T -> Bytes
  3202 publish_text :: Bytes -> Bytes -> UUID.T -> Bytes
  3700 where
  3710 where
  3701 
  3711 
  3702 import Data.Maybe (fromMaybe)
  3712 import Data.Maybe (fromMaybe)
  3703 import Control.Exception (throw, AsyncException (UserInterrupt))
  3713 import Control.Exception (throw, AsyncException (UserInterrupt))
  3704 import Network.Socket (Socket)
  3714 import Network.Socket (Socket)
       
  3715 import qualified Isabelle.Bytes as Bytes
  3705 import Isabelle.Bytes (Bytes)
  3716 import Isabelle.Bytes (Bytes)
  3706 import qualified Isabelle.Byte_Message as Byte_Message
  3717 import qualified Isabelle.Byte_Message as Byte_Message
  3707 import qualified Isabelle.Time as Time
  3718 import qualified Isabelle.Time as Time
  3708 import Isabelle.Timing (Timing (..))
  3719 import Isabelle.Timing (Timing (..))
  3709 import qualified Isabelle.Options as Options
  3720 import qualified Isabelle.Options as Options
  3735         isabelle_tmp <- getenv "ISABELLE_TMP"
  3746         isabelle_tmp <- getenv "ISABELLE_TMP"
  3736         Byte_Message.write_message socket (run isabelle_tmp)
  3747         Byte_Message.write_message socket (run isabelle_tmp)
  3737         loop Nothing socket)
  3748         loop Nothing socket)
  3738   where
  3749   where
  3739     port =
  3750     port =
  3740       case Server.localhost_unprefix address of
  3751       case Bytes.try_unprefix Server.localhost_prefix address of
  3741         Just port -> make_string port
  3752         Just port -> make_string port
  3742         Nothing -> errorWithoutStackTrace "Bad bash_process server address"
  3753         Nothing -> errorWithoutStackTrace "Bad bash_process server address"
  3743 
  3754 
  3744     script = Bash.get_script params
  3755     script = Bash.get_script params
  3745     input = Bash.get_input params
  3756     input = Bash.get_input params