
kleing@24332: (*
kleing@24332:   ID:     $Id$
kleing@24332:   Author: Brian Huffman
kleing@24332: 
kleing@24332:   Numeral Syntax for Types
kleing@24332: *)
kleing@24332: 
kleing@24332: header "Numeral Syntax for Types"
kleing@24332: 
kleing@24332: theory Numeral_Type
kleing@24332:   imports Infinite_Set
kleing@24332: begin
kleing@24332: 
kleing@24332: subsection {* Preliminary lemmas *}
kleing@24332: (* These should be moved elsewhere *)
kleing@24332: 
kleing@24332: lemma inj_Inl [simp]: "inj_on Inl A"
kleing@24332:   by (rule inj_onI, simp)
kleing@24332: 
kleing@24332: lemma inj_Inr [simp]: "inj_on Inr A"
kleing@24332:   by (rule inj_onI, simp)
kleing@24332: 
kleing@24332: lemma inj_Some [simp]: "inj_on Some A"
kleing@24332:   by (rule inj_onI, simp)
kleing@24332: 
kleing@24332: lemma card_Plus:
kleing@24332:   "[| finite A; finite B |] ==> card (A <+> B) = card A + card B"
kleing@24332:   unfolding Plus_def
kleing@24332:   apply (subgoal_tac "Inl ` A \<inter> Inr ` B = {}")
kleing@24332:   apply (simp add: card_Un_disjoint card_image)
kleing@24332:   apply fast
kleing@24332:   done
kleing@24332: 
kleing@24332: lemma (in type_definition) univ:
kleing@24332:   "UNIV = Abs ` A"
kleing@24332: proof
kleing@24332:   show "Abs ` A \<subseteq> UNIV" by (rule subset_UNIV)
kleing@24332:   show "UNIV \<subseteq> Abs ` A"
kleing@24332:   proof
kleing@24332:     fix x :: 'b
kleing@24332:     have "x = Abs (Rep x)" by (rule Rep_inverse [symmetric])
kleing@24332:     moreover have "Rep x \<in> A" by (rule Rep)
kleing@24332:     ultimately show "x \<in> Abs ` A" by (rule image_eqI)
kleing@24332:   qed
kleing@24332: qed
kleing@24332: 
kleing@24332: lemma (in type_definition) card: "card (UNIV :: 'b set) = card A"
kleing@24332:   by (simp add: univ card_image inj_on_def Abs_inject)
kleing@24332: 
kleing@24332: 
kleing@24332: subsection {* Cardinalities of types *}
kleing@24332: 
kleing@24332: syntax "_type_card" :: "type => nat" ("(1CARD/(1'(_')))")
kleing@24332: 
kleing@24332: translations "CARD(t)" => "card (UNIV::t set)"
kleing@24332: 
kleing@24332: lemma card_unit: "CARD(unit) = 1"
kleing@24332:   unfolding univ_unit by simp
kleing@24332: 
kleing@24332: lemma card_bool: "CARD(bool) = 2"
kleing@24332:   unfolding univ_bool by simp
kleing@24332: 
kleing@24332: lemma card_prod: "CARD('a::finite \<times> 'b::finite) = CARD('a) * CARD('b)"
kleing@24332:   unfolding univ_prod by (simp only: card_cartesian_product)
kleing@24332: 
kleing@24332: lemma card_sum: "CARD('a::finite + 'b::finite) = CARD('a) + CARD('b)"
kleing@24332:   unfolding univ_sum by (simp only: finite card_Plus)
kleing@24332: 
kleing@24332: lemma card_option: "CARD('a::finite option) = Suc CARD('a)"
kleing@24332:   unfolding univ_option
kleing@24332:   apply (subgoal_tac "(None::'a option) \<notin> range Some")
kleing@24332:   apply (simp add: finite card_image)
kleing@24332:   apply fast
kleing@24332:   done
kleing@24332: 
kleing@24332: lemma card_set: "CARD('a::finite set) = 2 ^ CARD('a)"
kleing@24332:   unfolding univ_set
kleing@24332:   by (simp only: card_Pow finite numeral_2_eq_2)
kleing@24332: 
kleing@24332: subsection {* Numeral Types *}
kleing@24332: 
huffman@24406: typedef (open) num0 = "UNIV :: nat set" ..
kleing@24332: typedef (open) num1 = "UNIV :: unit set" ..
kleing@24332: typedef (open) 'a bit0 = "UNIV :: (bool * 'a) set" ..
kleing@24332: typedef (open) 'a bit1 = "UNIV :: (bool * 'a) option set" ..
kleing@24332: 
kleing@24332: instance num1 :: finite
kleing@24332: proof
kleing@24332:   show "finite (UNIV::num1 set)"
kleing@24332:     unfolding type_definition.univ [OF type_definition_num1]
kleing@24332:     using finite by (rule finite_imageI)
kleing@24332: qed
kleing@24332: 
kleing@24332: instance bit0 :: (finite) finite
kleing@24332: proof
kleing@24332:   show "finite (UNIV::'a bit0 set)"
kleing@24332:     unfolding type_definition.univ [OF type_definition_bit0]
kleing@24332:     using finite by (rule finite_imageI)
kleing@24332: qed
kleing@24332: 
kleing@24332: instance bit1 :: (finite) finite
kleing@24332: proof
kleing@24332:   show "finite (UNIV::'a bit1 set)"
kleing@24332:     unfolding type_definition.univ [OF type_definition_bit1]
kleing@24332:     using finite by (rule finite_imageI)
kleing@24332: qed
kleing@24332: 
kleing@24332: lemma card_num1: "CARD(num1) = 1"
kleing@24332:   unfolding type_definition.card [OF type_definition_num1]
kleing@24332:   by (simp only: card_unit)
kleing@24332: 
kleing@24332: lemma card_bit0: "CARD('a::finite bit0) = 2 * CARD('a)"
kleing@24332:   unfolding type_definition.card [OF type_definition_bit0]
kleing@24332:   by (simp only: card_prod card_bool)
kleing@24332: 
kleing@24332: lemma card_bit1: "CARD('a::finite bit1) = Suc (2 * CARD('a))"
kleing@24332:   unfolding type_definition.card [OF type_definition_bit1]
kleing@24332:   by (simp only: card_prod card_option card_bool)
kleing@24332: 
huffman@24406: lemma card_num0: "CARD (num0) = 0"
huffman@24406:   by (simp add: type_definition.card [OF type_definition_num0])
kleing@24332: 
kleing@24332: lemmas card_univ_simps [simp] =
kleing@24332:   card_unit
kleing@24332:   card_bool
kleing@24332:   card_prod
kleing@24332:   card_sum
kleing@24332:   card_option
kleing@24332:   card_set
kleing@24332:   card_num1
kleing@24332:   card_bit0
kleing@24332:   card_bit1
huffman@24406:   card_num0
kleing@24332: 
kleing@24332: subsection {* Syntax *}
kleing@24332: 
kleing@24332: 
kleing@24332: syntax
kleing@24332:   "_NumeralType" :: "num_const => type"  ("_")
kleing@24332:   "_NumeralType0" :: type ("0")
kleing@24332:   "_NumeralType1" :: type ("1")
kleing@24332: 
kleing@24332: translations
kleing@24332:   "_NumeralType1" == (type) "num1"
huffman@24406:   "_NumeralType0" == (type) "num0"
kleing@24332: 
kleing@24332: parse_translation {*
kleing@24332: let
kleing@24332: 
kleing@24332: val num1_const = Syntax.const "Numeral_Type.num1";
huffman@24406: val num0_const = Syntax.const "Numeral_Type.num0";
kleing@24332: val B0_const = Syntax.const "Numeral_Type.bit0";
kleing@24332: val B1_const = Syntax.const "Numeral_Type.bit1";
kleing@24332: 
kleing@24332: fun mk_bintype n =
kleing@24332:   let
kleing@24332:     fun mk_bit n = if n = 0 then B0_const else B1_const;
kleing@24332:     fun bin_of n =
kleing@24332:       if n = 1 then num1_const
huffman@24406:       else if n = 0 then num0_const
kleing@24332:       else if n = ~1 then raise TERM ("negative type numeral", [])
kleing@24332:       else
kleing@24332:         let val (q, r) = IntInf.divMod (n, 2);
kleing@24332:         in mk_bit r $ bin_of q end;
kleing@24332:   in bin_of n end;
kleing@24332: 
kleing@24332: fun numeral_tr (*"_NumeralType"*) [Const (str, _)] =
kleing@24332:       mk_bintype (valOf (IntInf.fromString str))
kleing@24332:   | numeral_tr (*"_NumeralType"*) ts = raise TERM ("numeral_tr", ts);
kleing@24332: 
kleing@24332: in [("_NumeralType", numeral_tr)] end;
kleing@24332: *}
kleing@24332: 
kleing@24332: print_translation {*
kleing@24332: let
kleing@24332: fun int_of [] = 0
kleing@24332:   | int_of (b :: bs) = IntInf.fromInt b + (2 * int_of bs);
kleing@24332: 
huffman@24406: fun bin_of (Const ("num0", _)) = []
kleing@24332:   | bin_of (Const ("num1", _)) = [1]
kleing@24332:   | bin_of (Const ("bit0", _) $ bs) = 0 :: bin_of bs
kleing@24332:   | bin_of (Const ("bit1", _) $ bs) = 1 :: bin_of bs
kleing@24332:   | bin_of t = raise TERM("bin_of", [t]);
kleing@24332: 
kleing@24332: fun bit_tr' b [t] =
kleing@24332:   let
kleing@24332:     val rev_digs = b :: bin_of t handle TERM _ => raise Match
kleing@24332:     val i = int_of rev_digs;
kleing@24332:     val num = IntInf.toString (IntInf.abs i);
kleing@24332:   in
kleing@24332:     Syntax.const "_NumeralType" $ Syntax.free num
kleing@24332:   end
kleing@24332:   | bit_tr' b _ = raise Match;
kleing@24332: 
kleing@24332: in [("bit0", bit_tr' 0), ("bit1", bit_tr' 1)] end;
kleing@24332: *}
kleing@24332: 
kleing@24332: 
kleing@24332: subsection {* Classes with at values least 1 and 2  *}
kleing@24332: 
kleing@24332: text {* Class finite already captures "at least 1" *}
kleing@24332: 
kleing@24332: lemma zero_less_card_finite:
kleing@24332:   "0 < CARD('a::finite)"
kleing@24332: proof (cases "CARD('a::finite) = 0")
kleing@24332:   case False thus ?thesis by (simp del: card_0_eq)
kleing@24332: next
kleing@24332:   case True
kleing@24332:   thus ?thesis by (simp add: finite)
kleing@24332: qed
kleing@24332: 
kleing@24332: lemma one_le_card_finite:
kleing@24332:   "Suc 0 <= CARD('a::finite)"
kleing@24332:   by (simp add: less_Suc_eq_le [symmetric] zero_less_card_finite)
kleing@24332: 
kleing@24332: 
kleing@24332: text {* Class for cardinality "at least 2" *}
kleing@24332: 
kleing@24332: class card2 = finite + 
kleing@24332:   assumes two_le_card: "2 <= CARD('a)"
kleing@24332: 
kleing@24332: lemma one_less_card: "Suc 0 < CARD('a::card2)"
kleing@24332:   using two_le_card [where 'a='a] by simp
kleing@24332: 
kleing@24332: instance bit0 :: (finite) card2
kleing@24332:   by intro_classes (simp add: one_le_card_finite)
kleing@24332: 
kleing@24332: instance bit1 :: (finite) card2
kleing@24332:   by intro_classes (simp add: one_le_card_finite)
kleing@24332: 
kleing@24332: subsection {* Examples *}
kleing@24332: 
kleing@24332: term "TYPE(10)"
kleing@24332: 
kleing@24332: lemma "CARD(0) = 0" by simp
kleing@24332: lemma "CARD(17) = 17" by simp
kleing@24332:   
kleing@24332: end




