src/Tools/Haskell/Library.hs
author wenzelm
Sat, 10 Nov 2018 17:12:09 +0100
changeset 69280 e1d01b351724
parent 69240 16ca270090b6
child 69288 4c3704ecb0e6
permissions -rw-r--r--
more formal references;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
69227
71b48b749836 tuned message (e.g. see Options.save_prefs);
wenzelm
parents: 69226
diff changeset
     1
{- generated by Isabelle -}
69226
68f5dc2275ac tuned whitespace;
wenzelm
parents: 69225
diff changeset
     2
69225
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
     3
{-  Title:      Tools/Haskell/Library.hs
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
     4
    Author:     Makarius
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
     5
    LICENSE:    BSD 3-clause (Isabelle)
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
     6
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
     7
Basic library of Isabelle idioms.
69280
e1d01b351724 more formal references;
wenzelm
parents: 69240
diff changeset
     8
e1d01b351724 more formal references;
wenzelm
parents: 69240
diff changeset
     9
See also "$ISABELLE_HOME/src/Pure/General/basics.ML", "$ISABELLE_HOME/src/Pure/library.ML".
69225
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    10
-}
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    11
69240
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    12
module Isabelle.Library (
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    13
  (|>), (|->), (#>), (#->),
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    14
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    15
  the, the_default,
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    16
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    17
  fold, fold_rev, single, map_index, get_index,
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    18
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    19
  quote, trim_line)
69225
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    20
where
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    21
69234
2dec32c7313f more Haskell operations;
wenzelm
parents: 69227
diff changeset
    22
import Data.Maybe
2dec32c7313f more Haskell operations;
wenzelm
parents: 69227
diff changeset
    23
2dec32c7313f more Haskell operations;
wenzelm
parents: 69227
diff changeset
    24
69225
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    25
{- functions -}
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    26
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    27
(|>) :: a -> (a -> b) -> b
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    28
x |> f = f x
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    29
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    30
(|->) :: (a, b) -> (a -> b -> c) -> c
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    31
(x, y) |-> f = f x y
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    32
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    33
(#>) :: (a -> b) -> (b -> c) -> a -> c
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    34
(f #> g) x = x |> f |> g
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    35
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    36
(#->) :: (a -> (c, b)) -> (c -> b -> d) -> a -> d
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    37
(f #-> g) x  = x |> f |-> g
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    38
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    39
69234
2dec32c7313f more Haskell operations;
wenzelm
parents: 69227
diff changeset
    40
{- options -}
2dec32c7313f more Haskell operations;
wenzelm
parents: 69227
diff changeset
    41
69240
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    42
the :: Maybe a -> a
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    43
the (Just x) = x
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    44
the Nothing = error "the Nothing"
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    45
69234
2dec32c7313f more Haskell operations;
wenzelm
parents: 69227
diff changeset
    46
the_default :: a -> Maybe a -> a
2dec32c7313f more Haskell operations;
wenzelm
parents: 69227
diff changeset
    47
the_default x Nothing = x
2dec32c7313f more Haskell operations;
wenzelm
parents: 69227
diff changeset
    48
the_default _ (Just y) = y
2dec32c7313f more Haskell operations;
wenzelm
parents: 69227
diff changeset
    49
2dec32c7313f more Haskell operations;
wenzelm
parents: 69227
diff changeset
    50
69225
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    51
{- lists -}
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    52
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    53
fold :: (a -> b -> b) -> [a] -> b -> b
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    54
fold _ [] y = y
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    55
fold f (x : xs) y = fold f xs (f x y)
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    56
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    57
fold_rev :: (a -> b -> b) -> [a] -> b -> b
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    58
fold_rev _ [] y = y
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    59
fold_rev f (x : xs) y = f x (fold_rev f xs y)
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    60
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    61
single :: a -> [a]
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    62
single x = [x]
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    63
69240
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    64
map_index :: ((Int, a) -> b) -> [a] -> [b]
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    65
map_index f = map_aux 0
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    66
  where
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    67
    map_aux _ [] = []
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    68
    map_aux i (x : xs) = f (i, x) : map_aux (i + 1) xs
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    69
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    70
get_index :: (a -> Maybe b) -> [a] -> Maybe (Int, b)
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    71
get_index f = get_aux 0
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    72
  where
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    73
    get_aux _ [] = Nothing
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    74
    get_aux i (x : xs) =
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    75
      case f x of
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    76
        Nothing -> get_aux (i + 1) xs
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    77
        Just y -> Just (i, y)
16ca270090b6 more Haskell operations;
wenzelm
parents: 69234
diff changeset
    78
69225
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    79
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    80
{- strings -}
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    81
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    82
quote :: String -> String
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    83
quote s = "\"" ++ s ++ "\""
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    84
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    85
trim_line :: String -> String
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    86
trim_line line =
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    87
  case reverse line of
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    88
    '\n' : '\r' : rest -> reverse rest
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    89
    '\n' : rest -> reverse rest
bf2fecda8383 support for Isabelle tools in Haskell;
wenzelm
parents:
diff changeset
    90
    _ -> line