src/HOL/Tools/hologic.ML
changeset 62597 b3f2b8c906a6
parent 62513 702085ca8564
child 67149 e61557884799
equal deleted inserted replaced
62596:cf79f8866bc3 62597:b3f2b8c906a6
   106   val mk_number: typ -> int -> term
   106   val mk_number: typ -> int -> term
   107   val dest_number: term -> typ * int
   107   val dest_number: term -> typ * int
   108   val code_integerT: typ
   108   val code_integerT: typ
   109   val code_naturalT: typ
   109   val code_naturalT: typ
   110   val realT: typ
   110   val realT: typ
   111   val nibbleT: typ
       
   112   val mk_nibble: int -> term
       
   113   val dest_nibble: term -> int
       
   114   val charT: typ
   111   val charT: typ
   115   val mk_char: int -> term
   112   val mk_char: int -> term
   116   val dest_char: term -> int
   113   val dest_char: term -> int
   117   val listT: typ -> typ
   114   val listT: typ -> typ
   118   val nil_const: typ -> term
   115   val nil_const: typ -> term
   592 fun dest_list (Const ("List.list.Nil", _)) = []
   589 fun dest_list (Const ("List.list.Nil", _)) = []
   593   | dest_list (Const ("List.list.Cons", _) $ t $ u) = t :: dest_list u
   590   | dest_list (Const ("List.list.Cons", _) $ t $ u) = t :: dest_list u
   594   | dest_list t = raise TERM ("dest_list", [t]);
   591   | dest_list t = raise TERM ("dest_list", [t]);
   595 
   592 
   596 
   593 
   597 (* nibble *)
   594 (* char *)
   598 
   595 
   599 val nibbleT = Type ("String.nibble", []);
   596 val charT = Type ("String.char", []);
   600 
   597 
   601 fun mk_nibble n =
   598 val Char_const = Const ("String.Char", numT --> charT);
   602   let val s =
   599 
   603     if 0 <= n andalso n <= 9 then chr (n + ord "0")
   600 fun mk_char 0 = Const ("Groups.zero_class.zero", charT)
   604     else if 10 <= n andalso n <= 15 then chr (n + ord "A" - 10)
   601   | mk_char i =
   605     else raise TERM ("mk_nibble", [])
   602       if 1 <= i andalso i <= 255 then Char_const $ mk_numeral i
   606   in Const ("String.nibble.Nibble" ^ s, nibbleT) end;
   603       else raise TERM ("mk_char", []);
   607 
   604 
   608 fun dest_nibble t =
   605 fun dest_char t =
   609   let fun err () = raise TERM ("dest_nibble", [t]) in
   606   let
   610     (case try (unprefix "String.nibble.Nibble" o fst o Term.dest_Const) t of
   607     val (T, n) = case t of
   611       NONE => err ()
   608       Const ("Groups.zero_class.zero", T) => (T, 0)
   612     | SOME c =>
   609     | (Const ("String.Char", Type ("fun", [_, T])) $ t) => (T, dest_numeral t)
   613         if size c <> 1 then err ()
   610     | _ => raise TERM ("dest_char", [t]);
   614         else if "0" <= c andalso c <= "9" then ord c - ord "0"
   611   in
   615         else if "A" <= c andalso c <= "F" then ord c - ord "A" + 10
   612     if T = charT then n
   616         else err ())
   613     else raise TERM ("dest_char", [t])
   617   end;
   614   end;
   618 
       
   619 
       
   620 (* char *)
       
   621 
       
   622 val charT = Type ("String.char", []);
       
   623 
       
   624 fun mk_char n =
       
   625   if 0 <= n andalso n <= 255 then
       
   626     Const ("String.char.Char", nibbleT --> nibbleT --> charT) $
       
   627       mk_nibble (n div 16) $ mk_nibble (n mod 16)
       
   628   else raise TERM ("mk_char", []);
       
   629 
       
   630 fun dest_char (Const ("String.char.Char", _) $ t $ u) =
       
   631       dest_nibble t * 16 + dest_nibble u
       
   632   | dest_char t = raise TERM ("dest_char", [t]);
       
   633 
   615 
   634 
   616 
   635 (* string *)
   617 (* string *)
   636 
   618 
   637 val stringT = listT charT;
   619 val stringT = listT charT;