src/HOL/Tools/numeral.ML
author wenzelm
Sun, 01 Mar 2009 23:36:12 +0100
changeset 30190 479806475f3c
parent 28708 a1a436f09ec6
child 31056 01ac77eb660b
permissions -rw-r--r--
use long names for old-style fold combinators;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28308
d4396a28fb29 fixed headers
haftmann
parents: 28262
diff changeset
     1
(*  Title:      HOL/Tools/numeral.ML
23575
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
     4
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
     5
Logical operations on numerals (see also HOL/hologic.ML).
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
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
     8
signature NUMERAL =
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
     9
sig
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23575
diff changeset
    10
  val mk_cnumeral: int -> cterm
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23575
diff changeset
    11
  val mk_cnumber: ctyp -> int -> cterm
25932
db0fd0ecdcd4 explicit auxiliary function for code setup
haftmann
parents: 25919
diff changeset
    12
  val add_code: string -> bool -> bool -> 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
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25967
diff changeset
    20
fun mk_cbit 0 = @{cterm "Int.Bit0"}
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25967
diff changeset
    21
  | mk_cbit 1 = @{cterm "Int.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
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 24630
diff changeset
    24
fun mk_cnumeral 0 = @{cterm "Int.Pls"}
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 24630
diff changeset
    25
  | mk_cnumeral ~1 = @{cterm "Int.Min"}
23575
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    26
  | mk_cnumeral i =
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 23575
diff changeset
    27
      let val (q, r) = Integer.div_mod i 2 in
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25967
diff changeset
    28
        Thm.capply (mk_cbit r) (mk_cnumeral q)
23575
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    29
      end;
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    30
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    31
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    32
(* number *)
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    33
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    34
local
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
val zero = @{cpat "0"};
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    37
val zeroT = Thm.ctyp_of_term zero;
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    38
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    39
val one = @{cpat "1"};
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    40
val oneT = Thm.ctyp_of_term one;
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    41
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    42
val number_of = @{cpat "number_of"};
28262
aa7ca36d67fd back to dynamic the_context(), because static @{theory} is invalidated if ML environment changes within the same code block;
wenzelm
parents: 28090
diff changeset
    43
val numberT = Thm.ctyp_of (the_context ()) (Term.range_type (Thm.typ_of (Thm.ctyp_of_term number_of)));
23575
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    44
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    45
fun instT T V = Thm.instantiate_cterm ([(V, T)], []);
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    46
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    47
in
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    48
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    49
fun mk_cnumber T 0 = instT T zeroT zero
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    50
  | mk_cnumber T 1 = instT T oneT one
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    51
  | mk_cnumber T i = Thm.capply (instT T numberT number_of) (mk_cnumeral i);
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    52
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    53
end;
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    54
25932
db0fd0ecdcd4 explicit auxiliary function for code setup
haftmann
parents: 25919
diff changeset
    55
db0fd0ecdcd4 explicit auxiliary function for code setup
haftmann
parents: 25919
diff changeset
    56
(* code generator *)
db0fd0ecdcd4 explicit auxiliary function for code setup
haftmann
parents: 25919
diff changeset
    57
28090
29af3c712d2b distributed literal code generation out of central infrastructure
haftmann
parents: 28064
diff changeset
    58
local open Basic_Code_Thingol in
29af3c712d2b distributed literal code generation out of central infrastructure
haftmann
parents: 28064
diff changeset
    59
29af3c712d2b distributed literal code generation out of central infrastructure
haftmann
parents: 28064
diff changeset
    60
fun add_code number_of negative unbounded target thy =
29af3c712d2b distributed literal code generation out of central infrastructure
haftmann
parents: 28064
diff changeset
    61
  let
29af3c712d2b distributed literal code generation out of central infrastructure
haftmann
parents: 28064
diff changeset
    62
    val pr_numeral = (Code_Printer.literal_numeral o Code_Target.the_literals thy) target;
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    63
    fun dest_numeral naming thm =
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    64
      let
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    65
        val SOME pls' = Code_Thingol.lookup_const naming @{const_name Int.Pls};
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    66
        val SOME min' = Code_Thingol.lookup_const naming @{const_name Int.Min};
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    67
        val SOME bit0' = Code_Thingol.lookup_const naming @{const_name Int.Bit0};
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    68
        val SOME bit1' = Code_Thingol.lookup_const naming @{const_name Int.Bit1};
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    69
        fun dest_bit (IConst (c, _)) = if c = bit0' then 0
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    70
              else if c = bit1' then 1
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    71
              else Code_Printer.nerror thm "Illegal numeral expression: illegal bit"
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    72
          | dest_bit _ = Code_Printer.nerror thm "Illegal numeral expression: illegal bit";
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    73
        fun dest_num (IConst (c, _)) = if c = pls' then SOME 0
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    74
              else if c = min' then
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    75
                if negative then SOME ~1 else NONE
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    76
              else Code_Printer.nerror thm "Illegal numeral expression: illegal leading digit"
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    77
          | dest_num (t1 `$ t2) =
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    78
              let val (n, b) = (dest_num t2, dest_bit t1)
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    79
              in case n of SOME n => SOME (2 * n + b) | NONE => NONE end
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    80
          | dest_num _ = Code_Printer.nerror thm "Illegal numeral expression: illegal term";
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    81
      in dest_num end;
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28663
diff changeset
    82
    fun pretty _ naming thm _ _ [(t, _)] =
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28308
diff changeset
    83
      (Code_Printer.str o pr_numeral unbounded o the_default 0 o dest_numeral naming thm) t;
28090
29af3c712d2b distributed literal code generation out of central infrastructure
haftmann
parents: 28064
diff changeset
    84
  in
29af3c712d2b distributed literal code generation out of central infrastructure
haftmann
parents: 28064
diff changeset
    85
    thy
29af3c712d2b distributed literal code generation out of central infrastructure
haftmann
parents: 28064
diff changeset
    86
    |> Code_Target.add_syntax_const target number_of (SOME (1, pretty))
29af3c712d2b distributed literal code generation out of central infrastructure
haftmann
parents: 28064
diff changeset
    87
  end;
29af3c712d2b distributed literal code generation out of central infrastructure
haftmann
parents: 28064
diff changeset
    88
29af3c712d2b distributed literal code generation out of central infrastructure
haftmann
parents: 28064
diff changeset
    89
end; (*local*)
25932
db0fd0ecdcd4 explicit auxiliary function for code setup
haftmann
parents: 25919
diff changeset
    90
23575
543803006b3f Logical operations on numerals (see also HOL/hologic.ML).
wenzelm
parents:
diff changeset
    91
end;