src/HOL/Tools/string_code.ML
author haftmann
Wed Dec 08 13:34:50 2010 +0100 (2010-12-08)
changeset 41075 4bed56dc95fb
parent 38923 79d7f2b4cf71
child 48072 ace701efe203
permissions -rw-r--r--
primitive definitions of bot/top/inf/sup for bool and fun are named with canonical suffix `_def` rather than `_eq`
     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 (c1, _), IConst (c2, _)) => decode (idx c1) (idx c2)
    27     | _ => NONE
    28   end;
    29    
    30 fun implode_string char' nibbles' mk_char mk_string ts =
    31   let
    32     fun implode_char (IConst (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 mk_string 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 thm vars fxy [(t1, _), (t2, _)] =
    54       case Option.map (cons t1) (List_Code.implode_list nil' cons' t2)
    55        of SOME ts => (case implode_string char' nibbles'
    56           (Code_Printer.literal_char literals) (Code_Printer.literal_string literals) ts
    57              of SOME p => p
    58               | NONE =>
    59                   Code_Printer.literal_list literals (map (pr vars Code_Printer.NOBR) ts))
    60         | NONE =>
    61             List_Code.default_list (Code_Printer.infix_cons literals) (pr vars) fxy t1 t2;
    62   in Code_Target.add_const_syntax target
    63     @{const_name Cons} (SOME (Code_Printer.complex_const_syntax (2, (cs_summa, pretty))))
    64   end;
    65 
    66 fun add_literal_char target =
    67   let
    68     fun pretty literals nibbles' _ thm _ _ [(t1, _), (t2, _)] =
    69       case decode_char nibbles' (t1, t2)
    70        of SOME c => (Code_Printer.str o Code_Printer.literal_char literals) c
    71         | NONE => Code_Printer.eqn_error thm "Illegal character expression";
    72   in Code_Target.add_const_syntax target
    73     @{const_name Char} (SOME (Code_Printer.complex_const_syntax (2, (cs_nibbles, pretty))))
    74   end;
    75 
    76 fun add_literal_string target =
    77   let
    78     fun pretty literals (nil' :: cons' :: char' :: nibbles') _ thm _ _ [(t, _)] =
    79       case List_Code.implode_list nil' cons' t
    80        of SOME ts => (case implode_string char' nibbles'
    81           (Code_Printer.literal_char literals) (Code_Printer.literal_string literals) ts
    82              of SOME p => p
    83               | NONE => Code_Printer.eqn_error thm "Illegal message expression")
    84         | NONE => Code_Printer.eqn_error thm "Illegal message expression";
    85   in Code_Target.add_const_syntax target 
    86     @{const_name STR} (SOME (Code_Printer.complex_const_syntax (1, (cs_summa, pretty))))
    87   end;
    88 
    89 end;