ported from src/Pure/General/value.ML;
authorwenzelm
Sun Nov 04 15:28:51 2018 +0100 (6 months ago)
changeset 69233560263485988
parent 69232 2b913054a9cf
child 69234 2dec32c7313f
ported from src/Pure/General/value.ML;
src/Tools/Haskell/Haskell.thy
src/Tools/Haskell/Value.hs
src/Tools/Haskell/haskell.ML
     1.1 --- a/src/Tools/Haskell/Haskell.thy	Sun Nov 04 12:07:24 2018 +0100
     1.2 +++ b/src/Tools/Haskell/Haskell.thy	Sun Nov 04 15:28:51 2018 +0100
     1.3 @@ -81,6 +81,57 @@
     1.4      _ -> line
     1.5  \<close>
     1.6  
     1.7 +generate_haskell_file Value.hs = \<open>
     1.8 +{-  Title:      Haskell/Tools/Value.hs
     1.9 +    Author:     Makarius
    1.10 +    LICENSE:    BSD 3-clause (Isabelle)
    1.11 +
    1.12 +Plain values, represented as string.
    1.13 +-}
    1.14 +
    1.15 +module Isabelle.Value
    1.16 +  (print_bool, parse_bool, print_int, parse_int, print_real, parse_real)
    1.17 +where
    1.18 +
    1.19 +import Data.Maybe
    1.20 +import qualified Data.List as List
    1.21 +import qualified Text.Read as Read
    1.22 +
    1.23 +
    1.24 +{- bool -}
    1.25 +
    1.26 +print_bool :: Bool -> String
    1.27 +print_bool True = "true"
    1.28 +print_bool False = "false"
    1.29 +
    1.30 +parse_bool :: String -> Maybe Bool
    1.31 +parse_bool "true" = Just True
    1.32 +parse_bool "false" = Just False
    1.33 +parse_bool _ = Nothing
    1.34 +
    1.35 +
    1.36 +{- int -}
    1.37 +
    1.38 +print_int :: Int -> String
    1.39 +print_int = show
    1.40 +
    1.41 +parse_int :: String -> Maybe Int
    1.42 +parse_int = Read.readMaybe
    1.43 +
    1.44 +
    1.45 +{- real -}
    1.46 +
    1.47 +print_real :: Double -> String
    1.48 +print_real x =
    1.49 +  let s = show x in
    1.50 +    case span (/= '.') s of
    1.51 +      (a, '.' : b) | List.all (== '0') b -> a
    1.52 +      _ -> s
    1.53 +
    1.54 +parse_real :: String -> Maybe Double
    1.55 +parse_real = Read.readMaybe
    1.56 +\<close>
    1.57 +
    1.58  generate_haskell_file Buffer.hs = \<open>
    1.59  {-  Title:      Tools/Haskell/Buffer.hs
    1.60      Author:     Makarius
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/Tools/Haskell/Value.hs	Sun Nov 04 15:28:51 2018 +0100
     2.3 @@ -0,0 +1,50 @@
     2.4 +{- generated by Isabelle -}
     2.5 +
     2.6 +{-  Title:      Haskell/Tools/Value.hs
     2.7 +    Author:     Makarius
     2.8 +    LICENSE:    BSD 3-clause (Isabelle)
     2.9 +
    2.10 +Plain values, represented as string.
    2.11 +-}
    2.12 +
    2.13 +module Isabelle.Value
    2.14 +  (print_bool, parse_bool, print_int, parse_int, print_real, parse_real)
    2.15 +where
    2.16 +
    2.17 +import Data.Maybe
    2.18 +import qualified Data.List as List
    2.19 +import qualified Text.Read as Read
    2.20 +
    2.21 +
    2.22 +{- bool -}
    2.23 +
    2.24 +print_bool :: Bool -> String
    2.25 +print_bool True = "true"
    2.26 +print_bool False = "false"
    2.27 +
    2.28 +parse_bool :: String -> Maybe Bool
    2.29 +parse_bool "true" = Just True
    2.30 +parse_bool "false" = Just False
    2.31 +parse_bool _ = Nothing
    2.32 +
    2.33 +
    2.34 +{- int -}
    2.35 +
    2.36 +print_int :: Int -> String
    2.37 +print_int = show
    2.38 +
    2.39 +parse_int :: String -> Maybe Int
    2.40 +parse_int = Read.readMaybe
    2.41 +
    2.42 +
    2.43 +{- real -}
    2.44 +
    2.45 +print_real :: Double -> String
    2.46 +print_real x =
    2.47 +  let s = show x in
    2.48 +    case span (/= '.') s of
    2.49 +      (a, '.' : b) | List.all (== '0') b -> a
    2.50 +      _ -> s
    2.51 +
    2.52 +parse_real :: String -> Maybe Double
    2.53 +parse_real = Read.readMaybe
     3.1 --- a/src/Tools/Haskell/haskell.ML	Sun Nov 04 12:07:24 2018 +0100
     3.2 +++ b/src/Tools/Haskell/haskell.ML	Sun Nov 04 15:28:51 2018 +0100
     3.3 @@ -47,6 +47,7 @@
     3.4  
     3.5  val source_modules =
     3.6   [\<^file>\<open>~~/src/Tools/Haskell/Library.hs\<close>,
     3.7 +  \<^file>\<open>~~/src/Tools/Haskell/Value.hs\<close>,
     3.8    \<^file>\<open>~~/src/Tools/Haskell/Buffer.hs\<close>,
     3.9    \<^file>\<open>~~/src/Tools/Haskell/Properties.hs\<close>,
    3.10    \<^file>\<open>~~/src/Tools/Haskell/Markup.hs\<close>,