src/HOL/Library/Numeral_Type.thy
changeset 24406 d96eb21fc1bc
parent 24332 e3a2b75b1cf9
child 24407 61b10ffb2549
equal deleted inserted replaced
24405:30887caeba62 24406:d96eb21fc1bc
    77   unfolding univ_set
    77   unfolding univ_set
    78   by (simp only: card_Pow finite numeral_2_eq_2)
    78   by (simp only: card_Pow finite numeral_2_eq_2)
    79 
    79 
    80 subsection {* Numeral Types *}
    80 subsection {* Numeral Types *}
    81 
    81 
    82 typedef (open) pls = "UNIV :: nat set" ..
    82 typedef (open) num0 = "UNIV :: nat set" ..
    83 typedef (open) num1 = "UNIV :: unit set" ..
    83 typedef (open) num1 = "UNIV :: unit set" ..
    84 typedef (open) 'a bit0 = "UNIV :: (bool * 'a) set" ..
    84 typedef (open) 'a bit0 = "UNIV :: (bool * 'a) set" ..
    85 typedef (open) 'a bit1 = "UNIV :: (bool * 'a) option set" ..
    85 typedef (open) 'a bit1 = "UNIV :: (bool * 'a) option set" ..
    86 
    86 
    87 instance num1 :: finite
    87 instance num1 :: finite
   115 
   115 
   116 lemma card_bit1: "CARD('a::finite bit1) = Suc (2 * CARD('a))"
   116 lemma card_bit1: "CARD('a::finite bit1) = Suc (2 * CARD('a))"
   117   unfolding type_definition.card [OF type_definition_bit1]
   117   unfolding type_definition.card [OF type_definition_bit1]
   118   by (simp only: card_prod card_option card_bool)
   118   by (simp only: card_prod card_option card_bool)
   119 
   119 
   120 lemma card_pls: "CARD (pls) = 0"
   120 lemma card_num0: "CARD (num0) = 0"
   121   by (simp add: type_definition.card [OF type_definition_pls])
   121   by (simp add: type_definition.card [OF type_definition_num0])
   122 
   122 
   123 lemmas card_univ_simps [simp] =
   123 lemmas card_univ_simps [simp] =
   124   card_unit
   124   card_unit
   125   card_bool
   125   card_bool
   126   card_prod
   126   card_prod
   128   card_option
   128   card_option
   129   card_set
   129   card_set
   130   card_num1
   130   card_num1
   131   card_bit0
   131   card_bit0
   132   card_bit1
   132   card_bit1
   133   card_pls
   133   card_num0
   134 
   134 
   135 subsection {* Syntax *}
   135 subsection {* Syntax *}
   136 
   136 
   137 
   137 
   138 syntax
   138 syntax
   140   "_NumeralType0" :: type ("0")
   140   "_NumeralType0" :: type ("0")
   141   "_NumeralType1" :: type ("1")
   141   "_NumeralType1" :: type ("1")
   142 
   142 
   143 translations
   143 translations
   144   "_NumeralType1" == (type) "num1"
   144   "_NumeralType1" == (type) "num1"
   145   "_NumeralType0" == (type) "pls"
   145   "_NumeralType0" == (type) "num0"
   146 
   146 
   147 parse_translation {*
   147 parse_translation {*
   148 let
   148 let
   149 
   149 
   150 val num1_const = Syntax.const "Numeral_Type.num1";
   150 val num1_const = Syntax.const "Numeral_Type.num1";
   151 val pls_const = Syntax.const "Numeral_Type.pls";
   151 val num0_const = Syntax.const "Numeral_Type.num0";
   152 val B0_const = Syntax.const "Numeral_Type.bit0";
   152 val B0_const = Syntax.const "Numeral_Type.bit0";
   153 val B1_const = Syntax.const "Numeral_Type.bit1";
   153 val B1_const = Syntax.const "Numeral_Type.bit1";
   154 
   154 
   155 fun mk_bintype n =
   155 fun mk_bintype n =
   156   let
   156   let
   157     fun mk_bit n = if n = 0 then B0_const else B1_const;
   157     fun mk_bit n = if n = 0 then B0_const else B1_const;
   158     fun bin_of n =
   158     fun bin_of n =
   159       if n = 1 then num1_const
   159       if n = 1 then num1_const
   160       else if n = 0 then pls_const
   160       else if n = 0 then num0_const
   161       else if n = ~1 then raise TERM ("negative type numeral", [])
   161       else if n = ~1 then raise TERM ("negative type numeral", [])
   162       else
   162       else
   163         let val (q, r) = IntInf.divMod (n, 2);
   163         let val (q, r) = IntInf.divMod (n, 2);
   164         in mk_bit r $ bin_of q end;
   164         in mk_bit r $ bin_of q end;
   165   in bin_of n end;
   165   in bin_of n end;
   174 print_translation {*
   174 print_translation {*
   175 let
   175 let
   176 fun int_of [] = 0
   176 fun int_of [] = 0
   177   | int_of (b :: bs) = IntInf.fromInt b + (2 * int_of bs);
   177   | int_of (b :: bs) = IntInf.fromInt b + (2 * int_of bs);
   178 
   178 
   179 fun bin_of (Const ("pls", _)) = []
   179 fun bin_of (Const ("num0", _)) = []
   180   | bin_of (Const ("num1", _)) = [1]
   180   | bin_of (Const ("num1", _)) = [1]
   181   | bin_of (Const ("bit0", _) $ bs) = 0 :: bin_of bs
   181   | bin_of (Const ("bit0", _) $ bs) = 0 :: bin_of bs
   182   | bin_of (Const ("bit1", _) $ bs) = 1 :: bin_of bs
   182   | bin_of (Const ("bit1", _) $ bs) = 1 :: bin_of bs
   183   | bin_of t = raise TERM("bin_of", [t]);
   183   | bin_of t = raise TERM("bin_of", [t]);
   184 
   184