2628 | string_ast_tr asts = raise AST ("string_tr", asts); |
2628 | string_ast_tr asts = raise AST ("string_tr", asts); |
2629 in [("_Char", char_ast_tr), ("_String", string_ast_tr)] end; |
2629 in [("_Char", char_ast_tr), ("_String", string_ast_tr)] end; |
2630 *} |
2630 *} |
2631 |
2631 |
2632 ML {* |
2632 ML {* |
|
2633 structure HOList = |
|
2634 struct |
|
2635 |
|
2636 local |
|
2637 val thy = the_context (); |
|
2638 in |
|
2639 val typ_string = Type (Sign.intern_type thy "string", []); |
|
2640 fun typ_list ty = Type (Sign.intern_type thy "list", [ty]); |
|
2641 fun term_list ty f [] = Const (Sign.intern_const thy "Nil", typ_list ty) |
|
2642 | term_list ty f (x::xs) = Const (Sign.intern_const thy "Cons", |
|
2643 ty --> typ_list ty --> typ_list ty) $ f x $ term_list ty f xs; |
|
2644 end; |
|
2645 |
2633 fun int_of_nibble h = |
2646 fun int_of_nibble h = |
2634 if "0" <= h andalso h <= "9" then ord h - ord "0" |
2647 if "0" <= h andalso h <= "9" then ord h - ord "0" |
2635 else if "A" <= h andalso h <= "F" then ord h - ord "A" + 10 |
2648 else if "A" <= h andalso h <= "F" then ord h - ord "A" + 10 |
2636 else raise Match; |
2649 else raise Match; |
2637 |
2650 |
2638 fun nibble_of_int i = |
2651 fun nibble_of_int i = |
2639 if i <= 9 then chr (ord "0" + i) else chr (ord "A" + i - 10); |
2652 if i <= 9 then chr (ord "0" + i) else chr (ord "A" + i - 10); |
2640 |
2653 |
2641 fun term_of_string s = |
2654 fun term_string s = |
2642 let |
2655 let |
2643 val ty_n = Type ("List.nibble", []); |
2656 val ty_n = Type ("List.nibble", []); |
2644 val ty_c = Type ("List.char", []); |
2657 val ty_c = Type ("List.char", []); |
2645 val ty_l = Type ("List.list", [ty_c]); |
2658 val ty_l = Type ("List.list", [ty_c]); |
2646 fun mk_nib n = Const ("List.nibble.Nibble" ^ chr (n + (if n <= 9 then ord "0" else ord "A" - 10)), ty_n); |
2659 fun mk_nib n = Const ("List.nibble.Nibble" ^ chr (n + (if n <= 9 then ord "0" else ord "A" - 10)), ty_n); |
2649 Const ("List.char.Char", ty_n --> ty_n --> ty_c) $ mk_nib (ord c div 16) $ mk_nib (ord c mod 16) |
2662 Const ("List.char.Char", ty_n --> ty_n --> ty_c) $ mk_nib (ord c div 16) $ mk_nib (ord c mod 16) |
2650 else error ("Printable ASCII character expected: " ^ quote c); |
2663 else error ("Printable ASCII character expected: " ^ quote c); |
2651 fun mk_string c t = Const ("List.list.Cons", ty_c --> ty_l --> ty_l) |
2664 fun mk_string c t = Const ("List.list.Cons", ty_c --> ty_l --> ty_l) |
2652 $ mk_char c $ t; |
2665 $ mk_char c $ t; |
2653 in fold_rev mk_string (explode s) (Const ("List.list.Nil", ty_l)) end; |
2666 in fold_rev mk_string (explode s) (Const ("List.list.Nil", ty_l)) end; |
|
2667 |
|
2668 end; |
2654 *} |
2669 *} |
2655 |
2670 |
2656 print_ast_translation {* |
2671 print_ast_translation {* |
2657 let |
2672 let |
2658 fun dest_nib (Syntax.Constant c) = |
2673 fun dest_nib (Syntax.Constant c) = |
2659 (case explode c of |
2674 (case explode c of |
2660 ["N", "i", "b", "b", "l", "e", h] => int_of_nibble h |
2675 ["N", "i", "b", "b", "l", "e", h] => HOList.int_of_nibble h |
2661 | _ => raise Match) |
2676 | _ => raise Match) |
2662 | dest_nib _ = raise Match; |
2677 | dest_nib _ = raise Match; |
2663 |
2678 |
2664 fun dest_chr c1 c2 = |
2679 fun dest_chr c1 c2 = |
2665 let val c = chr (dest_nib c1 * 16 + dest_nib c2) |
2680 let val c = chr (dest_nib c1 * 16 + dest_nib c2) |
2689 (gr, HOLogic.dest_list t) |
2704 (gr, HOLogic.dest_list t) |
2690 in SOME (gr', Pretty.list "[" "]" ps) end handle TERM _ => NONE; |
2705 in SOME (gr', Pretty.list "[" "]" ps) end handle TERM _ => NONE; |
2691 |
2706 |
2692 fun dest_char (Const ("List.char.Char", _) $ c1 $ c2) = |
2707 fun dest_char (Const ("List.char.Char", _) $ c1 $ c2) = |
2693 let |
2708 let |
2694 fun dest_nibble (Const (s, _)) = (int_of_nibble o unprefix "List.nibble.Nibble") s |
2709 fun dest_nibble (Const (s, _)) = (HOList.int_of_nibble o unprefix "List.nibble.Nibble") s |
2695 | dest_nibble _ = raise Match; |
2710 | dest_nibble _ = raise Match; |
2696 in |
2711 in |
2697 (SOME (dest_nibble c1 * 16 + dest_nibble c2) |
2712 (SOME (dest_nibble c1 * 16 + dest_nibble c2) |
2698 handle Fail _ => NONE | Match => NONE) |
2713 handle Fail _ => NONE | Match => NONE) |
2699 end |
2714 end |
2737 attach (term_of) {* |
2752 attach (term_of) {* |
2738 val nibbleT = Type ("List.nibble", []); |
2753 val nibbleT = Type ("List.nibble", []); |
2739 |
2754 |
2740 fun term_of_char c = |
2755 fun term_of_char c = |
2741 Const ("List.char.Char", nibbleT --> nibbleT --> Type ("List.char", [])) $ |
2756 Const ("List.char.Char", nibbleT --> nibbleT --> Type ("List.char", [])) $ |
2742 Const ("List.nibble.Nibble" ^ nibble_of_int (ord c div 16), nibbleT) $ |
2757 Const ("List.nibble.Nibble" ^ HOList.nibble_of_int (ord c div 16), nibbleT) $ |
2743 Const ("List.nibble.Nibble" ^ nibble_of_int (ord c mod 16), nibbleT); |
2758 Const ("List.nibble.Nibble" ^ HOList.nibble_of_int (ord c mod 16), nibbleT); |
2744 *} |
2759 *} |
2745 attach (test) {* |
2760 attach (test) {* |
2746 fun gen_char i = chr (random_range (ord "a") (Int.min (ord "a" + i, ord "z"))); |
2761 fun gen_char i = chr (random_range (ord "a") (Int.min (ord "a" + i, ord "z"))); |
2747 *} |
2762 *} |
2748 |
2763 |