src/HOL/Tools/string_code.ML
author haftmann
Wed, 07 Oct 2009 14:01:26 +0200
changeset 32887 85e7ab9020ba
parent 31205 98370b26c2ce
child 33989 cb136b5f6050
permissions -rw-r--r--
added bot_boolE, top_boolI
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31055
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
     1
(* Author: Florian Haftmann, TU Muenchen
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
     2
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
     3
Code generation for character and string literals.
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
     4
*)
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
     5
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
     6
signature STRING_CODE =
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
     7
sig
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
     8
  val add_literal_list_string: string -> theory -> theory
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
     9
  val add_literal_char: string -> theory -> theory
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31055
diff changeset
    10
  val add_literal_string: string -> theory -> theory
31055
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    11
end;
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    12
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    13
structure String_Code : STRING_CODE =
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    14
struct
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    15
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    16
open Basic_Code_Thingol;
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    17
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    18
fun decode_char nibbles' tt =
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    19
  let
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    20
    fun idx c = find_index (curry (op =) c) nibbles';
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    21
    fun decode ~1 _ = NONE
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    22
      | decode _ ~1 = NONE
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    23
      | decode n m = SOME (chr (n * 16 + m));
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    24
  in case tt
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    25
   of (IConst (c1, _), IConst (c2, _)) => decode (idx c1) (idx c2)
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    26
    | _ => NONE
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    27
  end;
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    28
   
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    29
fun implode_string char' nibbles' mk_char mk_string ts =
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    30
  let
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    31
    fun implode_char (IConst (c, _) `$ t1 `$ t2) =
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    32
          if c = char' then decode_char nibbles' (t1, t2) else NONE
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    33
      | implode_char _ = NONE;
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    34
    val ts' = map_filter implode_char ts;
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    35
  in if length ts = length ts'
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    36
    then (SOME o Code_Printer.str o mk_string o implode) ts'
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    37
    else NONE
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    38
  end;
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    39
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    40
val cs_nibbles = [@{const_name Nibble0}, @{const_name Nibble1},
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    41
  @{const_name Nibble2}, @{const_name Nibble3},
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    42
  @{const_name Nibble4}, @{const_name Nibble5},
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    43
  @{const_name Nibble6}, @{const_name Nibble7},
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    44
  @{const_name Nibble8}, @{const_name Nibble9},
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    45
  @{const_name NibbleA}, @{const_name NibbleB},
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    46
  @{const_name NibbleC}, @{const_name NibbleD},
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    47
  @{const_name NibbleE}, @{const_name NibbleF}];
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    48
val cs_summa = [@{const_name Nil}, @{const_name Cons}, @{const_name Char}] @ cs_nibbles;
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    49
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    50
fun add_literal_list_string target =
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    51
  let
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    52
    fun pretty literals (nil' :: cons' :: char' :: nibbles') pr thm vars fxy [(t1, _), (t2, _)] =
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    53
      case Option.map (cons t1) (List_Code.implode_list nil' cons' t2)
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    54
       of SOME ts => (case implode_string char' nibbles'
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    55
          (Code_Printer.literal_char literals) (Code_Printer.literal_string literals) ts
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    56
             of SOME p => p
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    57
              | NONE =>
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    58
                  Code_Printer.literal_list literals (map (pr vars Code_Printer.NOBR) ts))
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    59
        | NONE =>
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    60
            List_Code.default_list (Code_Printer.infix_cons literals) (pr vars) fxy t1 t2;
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    61
  in Code_Target.add_syntax_const target
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    62
    @{const_name Cons} (SOME (2, (cs_summa, pretty)))
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    63
  end;
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    64
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    65
fun add_literal_char target =
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    66
  let
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    67
    fun pretty literals nibbles' _ thm _ _ [(t1, _), (t2, _)] =
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    68
      case decode_char nibbles' (t1, t2)
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    69
       of SOME c => (Code_Printer.str o Code_Printer.literal_char literals) c
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    70
        | NONE => Code_Printer.nerror thm "Illegal character expression";
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    71
  in Code_Target.add_syntax_const target
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    72
    @{const_name Char} (SOME (2, (cs_nibbles, pretty)))
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    73
  end;
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    74
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31055
diff changeset
    75
fun add_literal_string target =
31055
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    76
  let
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    77
    fun pretty literals (nil' :: cons' :: char' :: nibbles') _ thm _ _ [(t, _)] =
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    78
      case List_Code.implode_list nil' cons' t
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    79
       of SOME ts => (case implode_string char' nibbles'
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    80
          (Code_Printer.literal_char literals) (Code_Printer.literal_string literals) ts
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    81
             of SOME p => p
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    82
              | NONE => Code_Printer.nerror thm "Illegal message expression")
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    83
        | NONE => Code_Printer.nerror thm "Illegal message expression";
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    84
  in Code_Target.add_syntax_const target 
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    85
    @{const_name STR} (SOME (1, (cs_summa, pretty)))
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    86
  end;
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    87
2cf6efca6c71 proper structures for list and string code generation stuff
haftmann
parents:
diff changeset
    88
end;