src/HOL/Num.thy
changeset 58410 6d46ad54a2ab
parent 58310 91ea607a34d8
child 58421 37cbbd8eb460
equal deleted inserted replaced
58409:24096e89c131 58410:6d46ad54a2ab
   278 text {* Numeral syntax. *}
   278 text {* Numeral syntax. *}
   279 
   279 
   280 syntax
   280 syntax
   281   "_Numeral" :: "num_const \<Rightarrow> 'a"    ("_")
   281   "_Numeral" :: "num_const \<Rightarrow> 'a"    ("_")
   282 
   282 
       
   283 ML_file "Tools/numeral.ML"
       
   284 
   283 parse_translation {*
   285 parse_translation {*
   284   let
   286   let
   285     fun num_of_int n =
       
   286       if n > 0 then
       
   287         (case IntInf.quotRem (n, 2) of
       
   288           (0, 1) => Syntax.const @{const_syntax One}
       
   289         | (n, 0) => Syntax.const @{const_syntax Bit0} $ num_of_int n
       
   290         | (n, 1) => Syntax.const @{const_syntax Bit1} $ num_of_int n)
       
   291       else raise Match
       
   292     val numeral = Syntax.const @{const_syntax numeral}
       
   293     val uminus = Syntax.const @{const_syntax uminus}
       
   294     val one = Syntax.const @{const_syntax Groups.one}
       
   295     val zero = Syntax.const @{const_syntax Groups.zero}
       
   296     fun numeral_tr [(c as Const (@{syntax_const "_constrain"}, _)) $ t $ u] =
   287     fun numeral_tr [(c as Const (@{syntax_const "_constrain"}, _)) $ t $ u] =
   297           c $ numeral_tr [t] $ u
   288           c $ numeral_tr [t] $ u
   298       | numeral_tr [Const (num, _)] =
   289       | numeral_tr [Const (num, _)] =
   299           let
   290           (Numeral.mk_number_syntax o #value o Lexicon.read_xnum) num
   300             val {value, ...} = Lexicon.read_xnum num;
       
   301           in
       
   302             if value = 0 then zero else
       
   303             if value > 0
       
   304             then numeral $ num_of_int value
       
   305             else if value = ~1 then uminus $ one
       
   306             else uminus $ (numeral $ num_of_int (~ value))
       
   307           end
       
   308       | numeral_tr ts = raise TERM ("numeral_tr", ts);
   291       | numeral_tr ts = raise TERM ("numeral_tr", ts);
   309   in [(@{syntax_const "_Numeral"}, K numeral_tr)] end
   292   in [(@{syntax_const "_Numeral"}, K numeral_tr)] end
   310 *}
   293 *}
   311 
   294 
   312 typed_print_translation {*
   295 typed_print_translation {*
   332   in
   315   in
   333    [(@{const_syntax numeral}, num_tr')]
   316    [(@{const_syntax numeral}, num_tr')]
   334   end
   317   end
   335 *}
   318 *}
   336 
   319 
   337 ML_file "Tools/numeral.ML"
       
   338 
       
   339 
   320 
   340 subsection {* Class-specific numeral rules *}
   321 subsection {* Class-specific numeral rules *}
   341 
   322 
   342 text {*
   323 text {*
   343   @{const numeral} is a morphism.
   324   @{const numeral} is a morphism.