src/HOL/Tools/numeral_syntax.ML
changeset 6905 9bc05ec3497b
child 7055 91764e88d932
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Tools/numeral_syntax.ML	Tue Jul 06 21:08:30 1999 +0200
     1.3 @@ -0,0 +1,103 @@
     1.4 +(*  Title:      HOL/Tools/numeral_syntax.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Markus Wenzel, TU Muenchen
     1.7 +
     1.8 +Concrete syntax for generic numerals.  Assumes consts and syntax of
     1.9 +theory HOL/Numeral.
    1.10 +*)
    1.11 +
    1.12 +signature NUMERAL_SYNTAX =
    1.13 +sig
    1.14 +  val dest_bin: term -> int
    1.15 +  val setup: (theory -> theory) list
    1.16 +end;
    1.17 +
    1.18 +structure NumeralSyntax: NUMERAL_SYNTAX =
    1.19 +struct
    1.20 +
    1.21 +
    1.22 +(* bits *)
    1.23 +
    1.24 +fun mk_bit 0 = Syntax.const "False"
    1.25 +  | mk_bit 1 = Syntax.const "True"
    1.26 +  | mk_bit _ = sys_error "mk_bit";
    1.27 +
    1.28 +fun dest_bit (Const ("False", _)) = 0
    1.29 +  | dest_bit (Const ("True", _)) = 1
    1.30 +  | dest_bit _ = raise Match;
    1.31 +
    1.32 +
    1.33 +(* bit strings *)   (*we try to handle superfluous leading digits nicely*)
    1.34 +
    1.35 +fun prefix_len _ [] = 0
    1.36 +  | prefix_len pred (x :: xs) =
    1.37 +      if pred x then 1 + prefix_len pred xs else 0;
    1.38 +
    1.39 +fun mk_bin str =
    1.40 +  let
    1.41 +    val (sign, digs) =
    1.42 +      (case Symbol.explode str of
    1.43 +        "#" :: "-" :: cs => (~1, cs)
    1.44 +      | "#" :: cs => (1, cs)
    1.45 +      | _ => raise ERROR);
    1.46 +
    1.47 +    fun bin_of 0  = []
    1.48 +      | bin_of ~1 = [~1]
    1.49 +      | bin_of n  = (n mod 2) :: bin_of (n div 2);
    1.50 +
    1.51 +    fun term_of []   = Syntax.const "Numeral.bin.Pls"
    1.52 +      | term_of [~1] = Syntax.const "Numeral.bin.Min"
    1.53 +      | term_of (b :: bs) = Syntax.const "Numeral.bin.Bit" $ term_of bs $ mk_bit b;
    1.54 +    in term_of (bin_of (sign * (#1 (Term.read_int digs)))) end;
    1.55 +
    1.56 +(*we consider all "spellings"; Min is likely to be declared elsewhere*)
    1.57 +fun bin_of (Const ("Pls", _)) = []
    1.58 +  | bin_of (Const ("bin.Pls", _)) = []
    1.59 +  | bin_of (Const ("Numeral.bin.Pls", _)) = []
    1.60 +  | bin_of (Const ("Min", _)) = [~1]
    1.61 +  | bin_of (Const ("bin.Min", _)) = [~1]
    1.62 +  | bin_of (Const ("Numeral.bin.Min", _)) = [~1]
    1.63 +  | bin_of (Const ("Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
    1.64 +  | bin_of (Const ("bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
    1.65 +  | bin_of (Const ("Numeral.bin.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
    1.66 +  | bin_of _ = raise Match;
    1.67 +
    1.68 +fun int_of [] = 0
    1.69 +  | int_of (b :: bs) = b + 2 * int_of bs;
    1.70 +
    1.71 +val dest_bin = int_of o bin_of;
    1.72 +
    1.73 +fun dest_bin_str tm =
    1.74 +  let
    1.75 +    val rev_digs = bin_of tm;
    1.76 +    val (sign, zs) =
    1.77 +      (case rev rev_digs of
    1.78 +        ~1 :: bs => ("-", prefix_len (equal 1) bs)
    1.79 +      | bs => ("", prefix_len (equal 0) bs));
    1.80 +    val num = string_of_int (abs (int_of rev_digs));
    1.81 +  in "#" ^ sign ^ implode (replicate zs "0") ^ num end;
    1.82 +
    1.83 +
    1.84 +(* translation of integer numeral tokens to and from bitstrings *)
    1.85 +
    1.86 +fun numeral_tr (*"_Numeral"*) [t as Free (str, _)] =
    1.87 +      (Syntax.const "Numeral.number_of" $
    1.88 +        (mk_bin str handle ERROR => raise TERM ("numeral_tr", [t])))
    1.89 +  | numeral_tr (*"_Numeral"*) ts = raise TERM ("numeral_tr", ts);
    1.90 +
    1.91 +fun numeral_tr' show_sorts (*"number_of"*) (Type ("fun", [_, T])) (t :: ts) =
    1.92 +      let val t' = Syntax.const "_Numeral" $ (Syntax.const "_xnum" $ Syntax.free (dest_bin_str t)) in
    1.93 +        if can Term.dest_Type T then t'
    1.94 +        else Syntax.const Syntax.constrainC $ t' $ Syntax.term_of_typ show_sorts T
    1.95 +      end
    1.96 +  | numeral_tr' _ (*"number_of"*) _ _ = raise Match;
    1.97 +
    1.98 +
    1.99 +(* theory setup *)
   1.100 +
   1.101 +val setup =
   1.102 + [Theory.add_trfuns ([], [("_Numeral", numeral_tr)], [], []),
   1.103 +  Theory.add_trfunsT [("number_of", numeral_tr'), ("Numeral.number_of", numeral_tr')]];
   1.104 +
   1.105 +
   1.106 +end;