src/HOL/Tools/hologic.ML
changeset 31048 ac146fc38b51
parent 30453 1e7e0d171653
child 31135 e2d777dcf161
equal deleted inserted replaced
31047:c13b0406c039 31048:ac146fc38b51
   114   val mk_list: typ -> term list -> term
   114   val mk_list: typ -> term list -> term
   115   val dest_list: term -> term list
   115   val dest_list: term -> term list
   116   val stringT: typ
   116   val stringT: typ
   117   val mk_string: string -> term
   117   val mk_string: string -> term
   118   val dest_string: term -> string
   118   val dest_string: term -> string
       
   119   val message_stringT: typ
       
   120   val mk_message_string: string -> term
       
   121   val dest_message_string: term -> string
   119 end;
   122 end;
   120 
   123 
   121 structure HOLogic: HOLOGIC =
   124 structure HOLogic: HOLOGIC =
   122 struct
   125 struct
   123 
   126 
   508 (* real *)
   511 (* real *)
   509 
   512 
   510 val realT = Type ("RealDef.real", []);
   513 val realT = Type ("RealDef.real", []);
   511 
   514 
   512 
   515 
       
   516 (* list *)
       
   517 
       
   518 fun listT T = Type ("List.list", [T]);
       
   519 
       
   520 fun nil_const T = Const ("List.list.Nil", listT T);
       
   521 
       
   522 fun cons_const T =
       
   523   let val lT = listT T
       
   524   in Const ("List.list.Cons", T --> lT --> lT) end;
       
   525 
       
   526 fun mk_list T ts =
       
   527   let
       
   528     val lT = listT T;
       
   529     val Nil = Const ("List.list.Nil", lT);
       
   530     fun Cons t u = Const ("List.list.Cons", T --> lT --> lT) $ t $ u;
       
   531   in fold_rev Cons ts Nil end;
       
   532 
       
   533 fun dest_list (Const ("List.list.Nil", _)) = []
       
   534   | dest_list (Const ("List.list.Cons", _) $ t $ u) = t :: dest_list u
       
   535   | dest_list t = raise TERM ("dest_list", [t]);
       
   536 
       
   537 
   513 (* nibble *)
   538 (* nibble *)
   514 
   539 
   515 val nibbleT = Type ("List.nibble", []);
   540 val nibbleT = Type ("String.nibble", []);
   516 
   541 
   517 fun mk_nibble n =
   542 fun mk_nibble n =
   518   let val s =
   543   let val s =
   519     if 0 <= n andalso n <= 9 then chr (n + ord "0")
   544     if 0 <= n andalso n <= 9 then chr (n + ord "0")
   520     else if 10 <= n andalso n <= 15 then chr (n + ord "A" - 10)
   545     else if 10 <= n andalso n <= 15 then chr (n + ord "A" - 10)
   521     else raise TERM ("mk_nibble", [])
   546     else raise TERM ("mk_nibble", [])
   522   in Const ("List.nibble.Nibble" ^ s, nibbleT) end;
   547   in Const ("String.nibble.Nibble" ^ s, nibbleT) end;
   523 
   548 
   524 fun dest_nibble t =
   549 fun dest_nibble t =
   525   let fun err () = raise TERM ("dest_nibble", [t]) in
   550   let fun err () = raise TERM ("dest_nibble", [t]) in
   526     (case try (unprefix "List.nibble.Nibble" o fst o Term.dest_Const) t of
   551     (case try (unprefix "String.nibble.Nibble" o fst o Term.dest_Const) t of
   527       NONE => err ()
   552       NONE => err ()
   528     | SOME c =>
   553     | SOME c =>
   529         if size c <> 1 then err ()
   554         if size c <> 1 then err ()
   530         else if "0" <= c andalso c <= "9" then ord c - ord "0"
   555         else if "0" <= c andalso c <= "9" then ord c - ord "0"
   531         else if "A" <= c andalso c <= "F" then ord c - ord "A" + 10
   556         else if "A" <= c andalso c <= "F" then ord c - ord "A" + 10
   533   end;
   558   end;
   534 
   559 
   535 
   560 
   536 (* char *)
   561 (* char *)
   537 
   562 
   538 val charT = Type ("List.char", []);
   563 val charT = Type ("String.char", []);
   539 
   564 
   540 fun mk_char n =
   565 fun mk_char n =
   541   if 0 <= n andalso n <= 255 then
   566   if 0 <= n andalso n <= 255 then
   542     Const ("List.char.Char", nibbleT --> nibbleT --> charT) $
   567     Const ("String.char.Char", nibbleT --> nibbleT --> charT) $
   543       mk_nibble (n div 16) $ mk_nibble (n mod 16)
   568       mk_nibble (n div 16) $ mk_nibble (n mod 16)
   544   else raise TERM ("mk_char", []);
   569   else raise TERM ("mk_char", []);
   545 
   570 
   546 fun dest_char (Const ("List.char.Char", _) $ t $ u) =
   571 fun dest_char (Const ("String.char.Char", _) $ t $ u) =
   547       dest_nibble t * 16 + dest_nibble u
   572       dest_nibble t * 16 + dest_nibble u
   548   | dest_char t = raise TERM ("dest_char", [t]);
   573   | dest_char t = raise TERM ("dest_char", [t]);
   549 
   574 
   550 
   575 
   551 (* list *)
       
   552 
       
   553 fun listT T = Type ("List.list", [T]);
       
   554 
       
   555 fun nil_const T = Const ("List.list.Nil", listT T);
       
   556 
       
   557 fun cons_const T =
       
   558   let val lT = listT T
       
   559   in Const ("List.list.Cons", T --> lT --> lT) end;
       
   560 
       
   561 fun mk_list T ts =
       
   562   let
       
   563     val lT = listT T;
       
   564     val Nil = Const ("List.list.Nil", lT);
       
   565     fun Cons t u = Const ("List.list.Cons", T --> lT --> lT) $ t $ u;
       
   566   in fold_rev Cons ts Nil end;
       
   567 
       
   568 fun dest_list (Const ("List.list.Nil", _)) = []
       
   569   | dest_list (Const ("List.list.Cons", _) $ t $ u) = t :: dest_list u
       
   570   | dest_list t = raise TERM ("dest_list", [t]);
       
   571 
       
   572 
       
   573 (* string *)
   576 (* string *)
   574 
   577 
   575 val stringT = Type ("List.string", []);
   578 val stringT = Type ("String.string", []);
   576 
   579 
   577 val mk_string = mk_list charT o map (mk_char o ord) o explode;
   580 val mk_string = mk_list charT o map (mk_char o ord) o explode;
   578 val dest_string = implode o map (chr o dest_char) o dest_list;
   581 val dest_string = implode o map (chr o dest_char) o dest_list;
   579 
   582 
       
   583 
       
   584 (* message_string *)
       
   585 
       
   586 val message_stringT = Type ("String.message_string", []);
       
   587 
       
   588 fun mk_message_string s = Const ("String.message_string.STR", stringT --> message_stringT)
       
   589       $ mk_string s;
       
   590 fun dest_message_string (Const ("String.message_string.STR", _) $ t) =
       
   591       dest_string t
       
   592   | dest_message_string t = raise TERM ("dest_message_string", [t]);
       
   593 
   580 end;
   594 end;