| author | eberlm | 
| Mon, 11 Jan 2016 16:38:39 +0100 | |
| changeset 62128 | 3201ddb00097 | 
| parent 61150 | d85d8f5e921b | 
| child 62597 | b3f2b8c906a6 | 
| 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 | |
| 61150 | 40 | val cterm_of = Thm.cterm_of @{context};
 | 
| 41 | fun tvar S = (("'a", 0), S);
 | |
| 23575 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 42 | |
| 61150 | 43 | val zero_tvar = tvar @{sort zero};
 | 
| 44 | val zero = cterm_of (Const (@{const_name zero_class.zero}, TVar zero_tvar));
 | |
| 23575 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 45 | |
| 61150 | 46 | val one_tvar = tvar @{sort one};
 | 
| 47 | val one = cterm_of (Const (@{const_name one_class.one}, TVar one_tvar));
 | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 48 | |
| 61150 | 49 | val numeral_tvar = tvar @{sort numeral};
 | 
| 50 | val numeral = cterm_of (Const (@{const_name numeral}, @{typ num} --> TVar numeral_tvar));
 | |
| 23575 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 51 | |
| 61150 | 52 | val uminus_tvar = tvar @{sort uminus};
 | 
| 53 | val uminus = cterm_of (Const (@{const_name uminus}, TVar uminus_tvar --> TVar uminus_tvar));
 | |
| 54 | ||
| 55 | fun instT T v = Thm.instantiate_cterm ([(v, T)], []); | |
| 23575 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 56 | |
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 57 | in | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 58 | |
| 61150 | 59 | fun mk_cnumber T 0 = instT T zero_tvar zero | 
| 60 | | mk_cnumber T 1 = instT T one_tvar one | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46497diff
changeset | 61 | | mk_cnumber T i = | 
| 61150 | 62 | if i > 0 then | 
| 63 | Thm.apply (instT T numeral_tvar numeral) (mk_cnumeral i) | |
| 64 | else | |
| 65 | Thm.apply (instT T uminus_tvar uminus) | |
| 66 | (Thm.apply (instT T numeral_tvar numeral) (mk_cnumeral (~ i))); | |
| 23575 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 67 | |
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 68 | end; | 
| 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 69 | |
| 58410 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 70 | fun mk_num_syntax n = | 
| 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 71 | if n > 0 then | 
| 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 72 | (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 | 73 |       (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 | 74 |     | (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 | 75 |     | (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 | 76 | else raise Match | 
| 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 77 | |
| 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 78 | fun mk_number_syntax n = | 
| 
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
 haftmann parents: 
58399diff
changeset | 79 |   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 | 80 |   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 | 81 |   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 | 82 | |
| 25932 | 83 | |
| 84 | (* code generator *) | |
| 85 | ||
| 28090 
29af3c712d2b
distributed literal code generation out of central infrastructure
 haftmann parents: 
28064diff
changeset | 86 | local open Basic_Code_Thingol in | 
| 
29af3c712d2b
distributed literal code generation out of central infrastructure
 haftmann parents: 
28064diff
changeset | 87 | |
| 58399 | 88 | fun add_code number_of preproc print target thy = | 
| 28090 
29af3c712d2b
distributed literal code generation out of central infrastructure
 haftmann parents: 
28064diff
changeset | 89 | let | 
| 58399 | 90 | fun pretty literals _ thm _ _ [(t, _)] = | 
| 28663 
bd8438543bf2
code identifier namings are no longer imperative
 haftmann parents: 
28308diff
changeset | 91 | let | 
| 55148 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 haftmann parents: 
55147diff
changeset | 92 |         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 | 93 |           | dest_bit (IConst { sym = Code_Symbol.Constant @{const_name Num.Bit1}, ... }) = 1
 | 
| 55236 | 94 | | 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 | 95 |         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 | 96 | | dest_num (t1 `$ t2) = 2 * dest_num t2 + dest_bit t1 | 
| 55236 | 97 | | dest_num _ = Code_Printer.eqn_error thy thm "Illegal numeral expression: illegal term"; | 
| 58399 | 98 | 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 | 99 | in | 
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51314diff
changeset | 100 | thy |> Code_Target.set_printings (Code_Symbol.Constant (number_of, | 
| 55148 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 haftmann parents: 
55147diff
changeset | 101 | [(target, SOME (Code_Printer.complex_const_syntax (1, pretty)))])) | 
| 28090 
29af3c712d2b
distributed literal code generation out of central infrastructure
 haftmann parents: 
28064diff
changeset | 102 | end; | 
| 
29af3c712d2b
distributed literal code generation out of central infrastructure
 haftmann parents: 
28064diff
changeset | 103 | |
| 
29af3c712d2b
distributed literal code generation out of central infrastructure
 haftmann parents: 
28064diff
changeset | 104 | end; (*local*) | 
| 25932 | 105 | |
| 23575 
543803006b3f
Logical operations on numerals (see also HOL/hologic.ML).
 wenzelm parents: diff
changeset | 106 | end; |