src/HOL/Tools/string_code.ML
author haftmann
Fri Oct 10 19:55:32 2014 +0200 (2014-10-10)
changeset 58646 cd63a4b12a33
parent 55236 8d61b0aa7a0d
child 62597 b3f2b8c906a6
permissions -rw-r--r--
specialized specification: avoid trivial instances
haftmann@37744
     1
(*  Title:      HOL/Tools/string_code.ML
haftmann@37744
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@31055
     3
haftmann@31055
     4
Code generation for character and string literals.
haftmann@31055
     5
*)
haftmann@31055
     6
haftmann@31055
     7
signature STRING_CODE =
haftmann@31055
     8
sig
haftmann@31055
     9
  val add_literal_list_string: string -> theory -> theory
haftmann@31055
    10
  val add_literal_char: string -> theory -> theory
haftmann@31205
    11
  val add_literal_string: string -> theory -> theory
haftmann@31055
    12
end;
haftmann@31055
    13
haftmann@31055
    14
structure String_Code : STRING_CODE =
haftmann@31055
    15
struct
haftmann@31055
    16
haftmann@31055
    17
open Basic_Code_Thingol;
haftmann@31055
    18
haftmann@31055
    19
val cs_nibbles = [@{const_name Nibble0}, @{const_name Nibble1},
haftmann@31055
    20
  @{const_name Nibble2}, @{const_name Nibble3},
haftmann@31055
    21
  @{const_name Nibble4}, @{const_name Nibble5},
haftmann@31055
    22
  @{const_name Nibble6}, @{const_name Nibble7},
haftmann@31055
    23
  @{const_name Nibble8}, @{const_name Nibble9},
haftmann@31055
    24
  @{const_name NibbleA}, @{const_name NibbleB},
haftmann@31055
    25
  @{const_name NibbleC}, @{const_name NibbleD},
haftmann@31055
    26
  @{const_name NibbleE}, @{const_name NibbleF}];
haftmann@55148
    27
haftmann@55148
    28
fun decode_char tt =
haftmann@55148
    29
  let
haftmann@55148
    30
    fun idx c = find_index (curry (op =) c) cs_nibbles;
haftmann@55148
    31
    fun decode ~1 _ = NONE
haftmann@55148
    32
      | decode _ ~1 = NONE
haftmann@55148
    33
      | decode n m = SOME (chr (n * 16 + m));
haftmann@55148
    34
  in case tt
haftmann@55148
    35
   of (IConst { sym = Code_Symbol.Constant c1, ... }, IConst { sym = Code_Symbol.Constant c2, ... }) => decode (idx c1) (idx c2)
haftmann@55148
    36
    | _ => NONE
haftmann@55148
    37
  end;
haftmann@55148
    38
   
haftmann@55148
    39
fun implode_string literals ts =
haftmann@55148
    40
  let
haftmann@55148
    41
    fun implode_char (IConst { sym = Code_Symbol.Constant @{const_name Char}, ... } `$ t1 `$ t2) =
haftmann@55148
    42
          decode_char (t1, t2)
haftmann@55148
    43
      | implode_char _ = NONE;
haftmann@55148
    44
    val ts' = map_filter implode_char ts;
haftmann@55148
    45
  in if length ts = length ts'
haftmann@55148
    46
    then (SOME o Code_Printer.str o Code_Printer.literal_string literals o implode) ts'
haftmann@55148
    47
    else NONE
haftmann@55148
    48
  end;
haftmann@31055
    49
haftmann@31055
    50
fun add_literal_list_string target =
haftmann@31055
    51
  let
haftmann@55148
    52
    fun pretty literals pr _ vars fxy [(t1, _), (t2, _)] =
haftmann@55148
    53
      case Option.map (cons t1) (List_Code.implode_list t2)
haftmann@55148
    54
       of SOME ts => (case implode_string literals ts
haftmann@31055
    55
             of SOME p => p
haftmann@31055
    56
              | NONE =>
haftmann@31055
    57
                  Code_Printer.literal_list literals (map (pr vars Code_Printer.NOBR) ts))
haftmann@31055
    58
        | NONE =>
haftmann@31055
    59
            List_Code.default_list (Code_Printer.infix_cons literals) (pr vars) fxy t1 t2;
haftmann@52435
    60
  in
haftmann@52435
    61
    Code_Target.set_printings (Code_Symbol.Constant (@{const_name Cons},
haftmann@55148
    62
      [(target, SOME (Code_Printer.complex_const_syntax (2, pretty)))]))
haftmann@31055
    63
  end;
haftmann@31055
    64
wenzelm@55236
    65
fun add_literal_char target thy =
haftmann@31055
    66
  let
haftmann@55148
    67
    fun pretty literals _ thm _ _ [(t1, _), (t2, _)] =
haftmann@55148
    68
      case decode_char (t1, t2)
haftmann@31055
    69
       of SOME c => (Code_Printer.str o Code_Printer.literal_char literals) c
wenzelm@55236
    70
        | NONE => Code_Printer.eqn_error thy thm "Illegal character expression";
haftmann@52435
    71
  in
haftmann@52435
    72
    Code_Target.set_printings (Code_Symbol.Constant (@{const_name Char},
wenzelm@55236
    73
      [(target, SOME (Code_Printer.complex_const_syntax (2, pretty)))])) thy
haftmann@31055
    74
  end;
haftmann@31055
    75
wenzelm@55236
    76
fun add_literal_string target thy =
haftmann@31055
    77
  let
haftmann@55148
    78
    fun pretty literals _ thm _ _ [(t, _)] =
haftmann@55148
    79
      case List_Code.implode_list t
haftmann@55148
    80
       of SOME ts => (case implode_string literals ts
haftmann@31055
    81
             of SOME p => p
wenzelm@55236
    82
              | NONE => Code_Printer.eqn_error thy thm "Illegal string literal expression")
wenzelm@55236
    83
        | NONE => Code_Printer.eqn_error thy thm "Illegal string literal expression";
haftmann@52435
    84
  in
haftmann@52435
    85
    Code_Target.set_printings (Code_Symbol.Constant (@{const_name STR},
wenzelm@55236
    86
      [(target, SOME (Code_Printer.complex_const_syntax (1, pretty)))])) thy
haftmann@31055
    87
  end;
haftmann@31055
    88
haftmann@31055
    89
end;