| author | Andreas Lochbihler | 
| Wed, 11 Feb 2015 15:03:21 +0100 | |
| changeset 59523 | 860fb1c65553 | 
| parent 58410 | 6d46ad54a2ab | 
| child 59586 | ddf6deaadfe8 | 
| permissions | -rw-r--r-- | 
| 28308 | 1 | (* Title: HOL/Tools/numeral.ML | 
| 23575 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 2 | Author: Makarius | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 3 | |
| 51314 
eac4bb5adbf9
just one HOLogic.Trueprop_conv, with regular exception CTERM;
 wenzelm parents: 
48072diff
changeset | 4 | Logical operations on numerals (see also HOL/Tools/hologic.ML). | 
| 23575 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 5 | *) | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 6 | |
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 7 | signature NUMERAL = | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 8 | sig | 
| 24630 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 wenzelm parents: 
23575diff
changeset | 9 | val mk_cnumeral: int -> cterm | 
| 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 wenzelm parents: 
23575diff
changeset | 10 | val mk_cnumber: ctyp -> int -> cterm | 
| 58410 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 11 | val mk_number_syntax: int -> term | 
| 58399 | 12 | val add_code: string -> (int -> int) -> (Code_Printer.literals -> int -> string) -> string -> theory -> theory | 
| 23575 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 13 | end; | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 14 | |
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 15 | structure Numeral: NUMERAL = | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 16 | struct | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 17 | |
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 18 | (* numeral *) | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 19 | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 20 | fun mk_cbit 0 = @{cterm "Num.Bit0"}
 | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 21 |   | mk_cbit 1 = @{cterm "Num.Bit1"}
 | 
| 23575 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 22 |   | mk_cbit _ = raise CTERM ("mk_cbit", []);
 | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 23 | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 24 | fun mk_cnumeral i = | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 25 | let | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 26 |     fun mk 1 = @{cterm "Num.One"}
 | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 27 | | mk i = | 
| 24630 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 wenzelm parents: 
23575diff
changeset | 28 | let val (q, r) = Integer.div_mod i 2 in | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 29 | Thm.apply (mk_cbit r) (mk q) | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 30 | end | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 31 | in | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 32 |     if i > 0 then mk i else raise CTERM ("mk_cnumeral: negative input", [])
 | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 33 | end | 
| 23575 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 34 | |
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 35 | |
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 36 | (* number *) | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 37 | |
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 38 | local | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 39 | |
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 40 | val zero = @{cpat "0"};
 | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 41 | val zeroT = Thm.ctyp_of_term zero; | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 42 | |
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 43 | val one = @{cpat "1"};
 | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 44 | val oneT = Thm.ctyp_of_term one; | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 45 | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 46 | val numeral = @{cpat "numeral"};
 | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 47 | val numeralT = Thm.ctyp_of @{theory} (Term.range_type (Thm.typ_of (Thm.ctyp_of_term numeral)));
 | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 48 | |
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
52435diff
changeset | 49 | val uminus = @{cpat "uminus"};
 | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
52435diff
changeset | 50 | val uminusT = Thm.ctyp_of @{theory} (Term.range_type (Thm.typ_of (Thm.ctyp_of_term uminus)));
 | 
| 23575 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 51 | |
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 52 | fun instT T V = Thm.instantiate_cterm ([(V, T)], []); | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 53 | |
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 54 | in | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 55 | |
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 56 | fun mk_cnumber T 0 = instT T zeroT zero | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 57 | | mk_cnumber T 1 = instT T oneT one | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 58 | | mk_cnumber T i = | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 59 | if i > 0 then Thm.apply (instT T numeralT numeral) (mk_cnumeral i) | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
52435diff
changeset | 60 | else Thm.apply (instT T uminusT uminus) (Thm.apply (instT T numeralT numeral) (mk_cnumeral (~i))); | 
| 23575 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 61 | |
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 62 | end; | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 63 | |
| 58410 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 64 | fun mk_num_syntax n = | 
| 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 65 | if n > 0 then | 
| 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 66 | (case IntInf.quotRem (n, 2) of | 
| 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 67 |       (0, 1) => Syntax.const @{const_syntax One}
 | 
| 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 68 |     | (n, 0) => Syntax.const @{const_syntax Bit0} $ mk_num_syntax n
 | 
| 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 69 |     | (n, 1) => Syntax.const @{const_syntax Bit1} $ mk_num_syntax n)
 | 
| 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 70 | else raise Match | 
| 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 71 | |
| 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 72 | fun mk_number_syntax n = | 
| 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 73 |   if n = 0 then Syntax.const @{const_syntax Groups.zero}
 | 
| 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 74 |   else if n = 1 then Syntax.const @{const_syntax Groups.one}
 | 
| 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 75 |   else Syntax.const @{const_syntax numeral} $ mk_num_syntax n;
 | 
| 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 76 | |
| 25932 | 77 | |
| 78 | (* code generator *) | |
| 79 | ||
| 28090 
29af3c712d2b
distributed literal code generation out of central infrastructure
 haftmann parents: 
28064diff
changeset | 80 | local open Basic_Code_Thingol in | 
| 
29af3c712d2b
distributed literal code generation out of central infrastructure
 haftmann parents: 
28064diff
changeset | 81 | |
| 58399 | 82 | fun add_code number_of preproc print target thy = | 
| 28090 
29af3c712d2b
distributed literal code generation out of central infrastructure
 haftmann parents: 
28064diff
changeset | 83 | let | 
| 58399 | 84 | fun pretty literals _ thm _ _ [(t, _)] = | 
| 28663 
bd8438543bf2
code identifier namings are no longer imperative
 haftmann parents: 
28308diff
changeset | 85 | let | 
| 55148 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 haftmann parents: 
55147diff
changeset | 86 |         fun dest_bit (IConst { sym = Code_Symbol.Constant @{const_name Num.Bit0}, ... }) = 0
 | 
| 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 haftmann parents: 
55147diff
changeset | 87 |           | dest_bit (IConst { sym = Code_Symbol.Constant @{const_name Num.Bit1}, ... }) = 1
 | 
| 55236 | 88 | | dest_bit _ = Code_Printer.eqn_error thy thm "Illegal numeral expression: illegal bit"; | 
| 55148 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 haftmann parents: 
55147diff
changeset | 89 |         fun dest_num (IConst { sym = Code_Symbol.Constant @{const_name Num.One}, ... }) = 1
 | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 90 | | dest_num (t1 `$ t2) = 2 * dest_num t2 + dest_bit t1 | 
| 55236 | 91 | | dest_num _ = Code_Printer.eqn_error thy thm "Illegal numeral expression: illegal term"; | 
| 58399 | 92 | in (Code_Printer.str o print literals o preproc o dest_num) t end; | 
| 28090 
29af3c712d2b
distributed literal code generation out of central infrastructure
 haftmann parents: 
28064diff
changeset | 93 | in | 
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51314diff
changeset | 94 | thy |> Code_Target.set_printings (Code_Symbol.Constant (number_of, | 
| 55148 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 haftmann parents: 
55147diff
changeset | 95 | [(target, SOME (Code_Printer.complex_const_syntax (1, pretty)))])) | 
| 28090 
29af3c712d2b
distributed literal code generation out of central infrastructure
 haftmann parents: 
28064diff
changeset | 96 | end; | 
| 
29af3c712d2b
distributed literal code generation out of central infrastructure
 haftmann parents: 
28064diff
changeset | 97 | |
| 
29af3c712d2b
distributed literal code generation out of central infrastructure
 haftmann parents: 
28064diff
changeset | 98 | end; (*local*) | 
| 25932 | 99 | |
| 23575 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 100 | end; |