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