src/Pure/General/string.ML
author wenzelm
Fri, 08 Dec 2023 18:51:18 +0100
changeset 79212 601aa36071ba
parent 77847 3bb6468d202e
permissions -rw-r--r--
clarified signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
77847
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/string.ML
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
     3
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
     4
Additional operations for structure String, following Isabelle/ML conventions.
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
     5
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
     6
NB: Isabelle normally works with Symbol.symbol, which is represented as a
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
     7
small string fragment. Raw type char is rarely useful.
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
     8
*)
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
     9
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    10
signature STRING =
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    11
sig
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    12
  include STRING
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    13
  val nth: string -> int -> char
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    14
  val exists: (char -> bool) -> string -> bool
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    15
  val forall: (char -> bool) -> string -> bool
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    16
  val member: string -> char -> bool
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    17
  val fold: (char -> 'a -> 'a) -> string -> 'a -> 'a
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    18
  val fold_rev: (char -> 'a -> 'a) -> string -> 'a -> 'a
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    19
end;
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    20
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    21
structure String: STRING =
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    22
struct
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    23
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    24
open String;
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    25
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    26
fun nth str i = sub (str, i);
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    27
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    28
fun exists pred str =
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    29
  let
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    30
    val n = size str;
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    31
    fun ex i = Int.< (i, n) andalso (pred (nth str i) orelse ex (i + 1));
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    32
  in ex 0 end;
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    33
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    34
fun forall pred = not o exists (not o pred);
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    35
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    36
fun member str c = exists (fn c' => c = c') str;
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    37
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    38
fun fold f str x0 =
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    39
  let
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    40
    val n = size str;
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    41
    fun iter (x, i) = if Int.< (i, n) then iter (f (nth str i) x, i + 1) else x;
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    42
  in iter (x0, 0) end;
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    43
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    44
fun fold_rev f str x0 =
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    45
  let
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    46
    val n = size str;
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    47
    fun iter (x, i) = if Int.>= (i, 0) then iter (f (nth str i) x, i - 1) else x;
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    48
  in iter (x0, n - 1) end;
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    49
3bb6468d202e more operations, following Isabelle/ML conventions;
wenzelm
parents:
diff changeset
    50
end;