src/HOL/String.thy
author wenzelm
Tue Jan 16 00:30:06 2001 +0100 (2001-01-16)
changeset 10909 2bbb1797bbe2
parent 10732 d4fda7d05ce5
permissions -rw-r--r--
improved string syntax (allow translation rules);
wenzelm@5121
     1
(*  Title:      HOL/String.thy
wenzelm@5121
     2
    ID:         $Id$
wenzelm@10732
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@10732
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@5121
     5
wenzelm@5121
     6
Hex chars and strings.
wenzelm@5121
     7
*)
wenzelm@5121
     8
wenzelm@10909
     9
theory String = List:
wenzelm@5121
    10
wenzelm@10732
    11
datatype nibble =
wenzelm@10909
    12
    Nibble0 | Nibble1 | Nibble2 | Nibble3 | Nibble4 | Nibble5 | Nibble6 | Nibble7
wenzelm@10909
    13
  | Nibble8 | Nibble9 | NibbleA | NibbleB | NibbleC | NibbleD | NibbleE | NibbleF
wenzelm@5121
    14
wenzelm@10732
    15
datatype char = Char nibble nibble
wenzelm@10909
    16
  -- "Note: canonical order of character encoding coincides with standard term ordering"
wenzelm@10909
    17
wenzelm@10732
    18
types string = "char list"
wenzelm@5121
    19
wenzelm@5121
    20
syntax
wenzelm@10732
    21
  "_Char" :: "xstr => char"    ("CHR _")
wenzelm@10732
    22
  "_String" :: "xstr => string"    ("_")
wenzelm@10732
    23
wenzelm@10909
    24
parse_ast_translation {*
wenzelm@10909
    25
  let
wenzelm@10909
    26
    val constants = Syntax.Appl o map Syntax.Constant;
wenzelm@10909
    27
wenzelm@10909
    28
    fun mk_nib n = "Nibble" ^ chr (n + (if n <= 9 then ord "0" else ord "A" - 10));
wenzelm@10909
    29
    fun mk_char c =
wenzelm@10909
    30
      if Symbol.is_ascii c andalso Symbol.is_printable c then
wenzelm@10909
    31
        constants ["Char", mk_nib (ord c div 16), mk_nib (ord c mod 16)]
wenzelm@10909
    32
      else error ("Printable ASCII character expected: " ^ quote c);
wenzelm@10909
    33
wenzelm@10909
    34
    fun mk_string [] = Syntax.Constant "Nil"
wenzelm@10909
    35
      | mk_string (c :: cs) = Syntax.Appl [Syntax.Constant "Cons", mk_char c, mk_string cs];
wenzelm@10909
    36
wenzelm@10909
    37
    fun char_ast_tr [Syntax.Variable xstr] =
wenzelm@10909
    38
        (case Syntax.explode_xstr xstr of
wenzelm@10909
    39
          [c] => mk_char c
wenzelm@10909
    40
        | _ => error ("Single character expected: " ^ xstr))
wenzelm@10909
    41
      | char_ast_tr asts = raise AST ("char_ast_tr", asts);
wenzelm@10909
    42
wenzelm@10909
    43
    fun string_ast_tr [Syntax.Variable xstr] =
wenzelm@10909
    44
        (case Syntax.explode_xstr xstr of
wenzelm@10909
    45
          [] => constants [Syntax.constrainC, "Nil", "string"]
wenzelm@10909
    46
        | cs => mk_string cs)
wenzelm@10909
    47
      | string_ast_tr asts = raise AST ("string_tr", asts);
wenzelm@10909
    48
  in [("_Char", char_ast_tr), ("_String", string_ast_tr)] end;
wenzelm@10909
    49
*}
wenzelm@10909
    50
wenzelm@10909
    51
print_ast_translation {*
wenzelm@10909
    52
  let
wenzelm@10909
    53
    fun dest_nib (Syntax.Constant c) =
wenzelm@10909
    54
        (case explode c of
wenzelm@10909
    55
          ["N", "i", "b", "b", "l", "e", h] =>
wenzelm@10909
    56
            if "0" <= h andalso h <= "9" then ord h - ord "0"
wenzelm@10909
    57
            else if "A" <= h andalso h <= "F" then ord h - ord "A" + 10
wenzelm@10909
    58
            else raise Match
wenzelm@10909
    59
        | _ => raise Match)
wenzelm@10909
    60
      | dest_nib _ = raise Match;
wenzelm@10909
    61
wenzelm@10909
    62
    fun dest_chr c1 c2 =
wenzelm@10909
    63
      let val c = chr (dest_nib c1 * 16 + dest_nib c2)
wenzelm@10909
    64
      in if Symbol.is_printable c then c else raise Match end;
wenzelm@10909
    65
wenzelm@10909
    66
    fun dest_char (Syntax.Appl [Syntax.Constant "Char", c1, c2]) = dest_chr c1 c2
wenzelm@10909
    67
      | dest_char _ = raise Match;
wenzelm@10909
    68
wenzelm@10909
    69
    fun xstr cs = Syntax.Appl [Syntax.Constant "_xstr", Syntax.Variable (Syntax.implode_xstr cs)];
wenzelm@10909
    70
wenzelm@10909
    71
    fun char_ast_tr' [c1, c2] = Syntax.Appl [Syntax.Constant "_Char", xstr [dest_chr c1 c2]]
wenzelm@10909
    72
      | char_ast_tr' _ = raise Match;
wenzelm@10909
    73
wenzelm@10909
    74
    fun list_ast_tr' [args] = Syntax.Appl [Syntax.Constant "_String",
wenzelm@10909
    75
            xstr (map dest_char (Syntax.unfold_ast "_args" args))]
wenzelm@10909
    76
      | list_ast_tr' ts = raise Match;
wenzelm@10909
    77
  in [("Char", char_ast_tr'), ("@list", list_ast_tr')] end;
wenzelm@10909
    78
*}
wenzelm@5121
    79
wenzelm@5121
    80
end