added term_of_string function
authorhaftmann
Fri, 21 Jul 2006 14:49:11 +0200
changeset 20181 87b2dfbf31fc
parent 20180 a751bec7cf29
child 20182 79c9ff40d760
added term_of_string function
src/HOL/List.thy
--- a/src/HOL/List.thy	Fri Jul 21 14:48:35 2006 +0200
+++ b/src/HOL/List.thy	Fri Jul 21 14:49:11 2006 +0200
@@ -2637,6 +2637,20 @@
 
 fun nibble_of_int i =
   if i <= 9 then chr (ord "0" + i) else chr (ord "A" + i - 10);
+
+fun term_of_string s =
+  let
+    val ty_n = Type ("List.nibble", []);
+    val ty_c = Type ("List.char", []);
+    val ty_l = Type ("List.list", [ty_c]);
+    fun mk_nib n = Const ("List.nibble.Nibble" ^ chr (n + (if n <= 9 then ord "0" else ord "A" - 10)), ty_n);
+    fun mk_char c =
+      if Symbol.is_ascii c andalso Symbol.is_printable c then
+        Const ("List.char.Char", ty_n --> ty_n --> ty_c) $ mk_nib (ord c div 16) $ mk_nib (ord c mod 16)
+      else error ("Printable ASCII character expected: " ^ quote c);
+    fun mk_string c t = Const ("List.list.Cons", ty_c --> ty_l --> ty_l)
+      $ mk_char c $ t;
+  in fold_rev mk_string (explode s) (Const ("List.list.Nil", ty_l)) end;
 *}
 
 print_ast_translation {*