src/HOL/hologic.ML
author wenzelm
Thu, 23 Nov 2006 00:51:47 +0100
changeset 21473 02054bf31c0e
parent 21455 b6be1d1b66c5
child 21550 7cc49399929a
permissions -rw-r--r--
removed dead code;

(*  Title:      HOL/hologic.ML
    ID:         $Id$
    Author:     Lawrence C Paulson and Markus Wenzel

Abstract syntax operations for HOL.
*)

signature HOLOGIC =
sig
  val typeS: sort
  val typeT: typ
  val boolN: string
  val boolT: typ
  val false_const: term
  val true_const: term
  val mk_setT: typ -> typ
  val dest_setT: typ -> typ
  val Trueprop: term
  val mk_Trueprop: term -> term
  val dest_Trueprop: term -> term
  val conj: term
  val disj: term
  val imp: term
  val Not: term
  val mk_conj: term * term -> term
  val mk_disj: term * term -> term
  val mk_imp: term * term -> term
  val mk_not: term -> term
  val dest_conj: term -> term list
  val dest_disj: term -> term list
  val dest_imp: term -> term * term
  val dest_not: term -> term
  val dest_concls: term -> term list
  val eq_const: typ -> term
  val all_const: typ -> term
  val exists_const: typ -> term
  val choice_const: typ -> term
  val Collect_const: typ -> term
  val mk_eq: term * term -> term
  val dest_eq: term -> term * term
  val mk_all: string * typ * term -> term
  val list_all: (string * typ) list * term -> term
  val mk_exists: string * typ * term -> term
  val mk_Collect: string * typ * term -> term
  val class_eq: string
  val mk_mem: term * term -> term
  val dest_mem: term -> term * term
  val mk_UNIV: typ -> term
  val mk_binop: string -> term * term -> term
  val mk_binrel: string -> term * term -> term
  val dest_bin: string -> typ -> term -> term * term
  val unitT: typ
  val is_unitT: typ -> bool
  val unit: term
  val is_unit: term -> bool
  val mk_prodT: typ * typ -> typ
  val dest_prodT: typ -> typ * typ
  val pair_const: typ -> typ -> term
  val mk_prod: term * term -> term
  val dest_prod: term -> term * term
  val mk_fst: term -> term
  val mk_snd: term -> term
  val mk_split: term -> term
  val prodT_factors: typ -> typ list
  val split_const: typ * typ * typ -> term
  val mk_tuple: typ -> term list -> term
  val natT: typ
  val zero: term
  val is_zero: term -> bool
  val mk_Suc: term -> term
  val dest_Suc: term -> term
  val mk_nat: int -> term
  val dest_nat: term -> int
  val intT: typ
  val mk_int: IntInf.int -> term
  val realT: typ
  val bitT: typ
  val B0_const: term
  val B1_const: term
  val pls_const: term
  val min_const: term
  val bit_const: term
  val number_of_const: typ -> term
  val int_of: int list -> IntInf.int
  val dest_binum: term -> IntInf.int
  val mk_binum: IntInf.int -> term
  val bin_of : term -> int list
  val listT : typ -> typ
  val mk_list: ('a -> term) -> typ -> 'a list -> term
  val dest_list: term -> term list
  val charT : typ
  val int_of_nibble : string -> int
  val mk_char : string -> term
  val dest_char : term -> int option
  val print_char : string -> string
  val stringT : typ
  val mk_string : string -> term
  val print_string : string -> string
end;


structure HOLogic: HOLOGIC =
struct

(* HOL syntax *)

val typeS: sort = ["HOL.type"];
val typeT = TypeInfer.anyT typeS;


(* bool and set *)

val boolN = "bool";
val boolT = Type (boolN, []);

val true_const =  Const ("True", boolT);
val false_const = Const ("False", boolT);

fun mk_setT T = Type ("set", [T]);

fun dest_setT (Type ("set", [T])) = T
  | dest_setT T = raise TYPE ("dest_setT: set type expected", [T], []);


(* logic *)

val Trueprop = Const ("Trueprop", boolT --> propT);

fun mk_Trueprop P = Trueprop $ P;

fun dest_Trueprop (Const ("Trueprop", _) $ P) = P
  | dest_Trueprop t = raise TERM ("dest_Trueprop", [t]);


val conj = Const ("op &", [boolT, boolT] ---> boolT)
and disj = Const ("op |", [boolT, boolT] ---> boolT)
and imp = Const ("op -->", [boolT, boolT] ---> boolT)
and Not = Const ("Not", boolT --> boolT);

fun mk_conj (t1, t2) = conj $ t1 $ t2
and mk_disj (t1, t2) = disj $ t1 $ t2
and mk_imp (t1, t2) = imp $ t1 $ t2
and mk_not t = Not $ t;

fun dest_conj (Const ("op &", _) $ t $ t') = t :: dest_conj t'
  | dest_conj t = [t];

fun dest_disj (Const ("op |", _) $ t $ t') = t :: dest_disj t'
  | dest_disj t = [t];

fun dest_imp (Const("op -->",_) $ A $ B) = (A, B)
  | dest_imp  t = raise TERM ("dest_imp", [t]);

fun dest_not (Const ("Not", _) $ t) = t
  | dest_not t = raise TERM ("dest_not", [t]);

fun imp_concl_of t = imp_concl_of (#2 (dest_imp t)) handle TERM _ => t;
val dest_concls = map imp_concl_of o dest_conj o dest_Trueprop;

fun eq_const T = Const ("op =", [T, T] ---> boolT);
fun mk_eq (t, u) = eq_const (fastype_of t) $ t $ u;

fun dest_eq (Const ("op =", _) $ lhs $ rhs) = (lhs, rhs)
  | dest_eq t = raise TERM ("dest_eq", [t])

fun all_const T = Const ("All", [T --> boolT] ---> boolT);
fun mk_all (x, T, P) = all_const T $ absfree (x, T, P);
fun list_all (xs, t) = fold_rev (fn (x, T) => fn P => all_const T $ Abs (x, T, P)) xs t;

fun exists_const T = Const ("Ex", [T --> boolT] ---> boolT);
fun mk_exists (x, T, P) = exists_const T $ absfree (x, T, P);

fun choice_const T = Const("Hilbert_Choice.Eps", (T --> boolT) --> T)

fun Collect_const T = Const ("Collect", [T --> boolT] ---> mk_setT T);
fun mk_Collect (a, T, t) = Collect_const T $ absfree (a, T, t);

val class_eq = "Code_Generator.eq";

fun mk_mem (x, A) =
  let val setT = fastype_of A in
    Const ("op :", [dest_setT setT, setT] ---> boolT) $ x $ A
  end;

fun dest_mem (Const ("op :", _) $ x $ A) = (x, A)
  | dest_mem t = raise TERM ("dest_mem", [t]);

fun mk_UNIV T = Const ("UNIV", mk_setT T);


(* binary operations and relations *)

fun mk_binop c (t, u) =
  let val T = fastype_of t in
    Const (c, [T, T] ---> T) $ t $ u
  end;

fun mk_binrel c (t, u) =
  let val T = fastype_of t in
    Const (c, [T, T] ---> boolT) $ t $ u
  end;

(*destruct the application of a binary operator. The dummyT case is a crude
  way of handling polymorphic operators.*)
fun dest_bin c T (tm as Const (c', Type ("fun", [T', _])) $ t $ u) =
      if c = c' andalso (T=T' orelse T=dummyT) then (t, u)
      else raise TERM ("dest_bin " ^ c, [tm])
  | dest_bin c _ tm = raise TERM ("dest_bin " ^ c, [tm]);


(* unit *)

val unitT = Type ("Product_Type.unit", []);

fun is_unitT (Type ("Product_Type.unit", [])) = true
  | is_unitT _ = false;

val unit = Const ("Product_Type.Unity", unitT);

fun is_unit (Const ("Product_Type.Unity", _)) = true
  | is_unit _ = false;


(* prod *)

fun mk_prodT (T1, T2) = Type ("*", [T1, T2]);

fun dest_prodT (Type ("*", [T1, T2])) = (T1, T2)
  | dest_prodT T = raise TYPE ("dest_prodT", [T], []);

fun pair_const T1 T2 = Const ("Pair", [T1, T2] ---> mk_prodT (T1, T2));

fun mk_prod (t1, t2) =
  let val T1 = fastype_of t1 and T2 = fastype_of t2 in
    pair_const T1 T2 $ t1 $ t2
  end;

fun dest_prod (Const ("Pair", _) $ t1 $ t2) = (t1, t2)
  | dest_prod t = raise TERM ("dest_prod", [t]);

fun mk_fst p =
  let val pT = fastype_of p in
    Const ("fst", pT --> fst (dest_prodT pT)) $ p
  end;

fun mk_snd p =
  let val pT = fastype_of p in
    Const ("snd", pT --> snd (dest_prodT pT)) $ p
  end;

fun split_const (A, B, C) =
  Const ("split", (A --> B --> C) --> mk_prodT (A, B) --> C);

fun mk_split t =
  (case Term.fastype_of t of
    T as (Type ("fun", [A, Type ("fun", [B, C])])) =>
      Const ("split", T --> mk_prodT (A, B) --> C) $ t
  | _ => raise TERM ("mk_split: bad body type", [t]));

(*Maps the type T1 * ... * Tn to [T1, ..., Tn], however nested*)
fun prodT_factors (Type ("*", [T1, T2])) = prodT_factors T1 @ prodT_factors T2
  | prodT_factors T = [T];

(*Makes a nested tuple from a list, following the product type structure*)
fun mk_tuple (Type ("*", [T1, T2])) tms =
        mk_prod (mk_tuple T1 tms,
                 mk_tuple T2 (Library.drop (length (prodT_factors T1), tms)))
  | mk_tuple T (t::_) = t;


(* nat *)

val natT = Type ("nat", []);

val zero = Const ("HOL.zero", natT);

fun is_zero (Const ("HOL.zero", _)) = true
  | is_zero _ = false;

fun mk_Suc t = Const ("Suc", natT --> natT) $ t;

fun dest_Suc (Const ("Suc", _) $ t) = t
  | dest_Suc t = raise TERM ("dest_Suc", [t]);

fun mk_nat 0 = zero
  | mk_nat n = mk_Suc (mk_nat (n - 1));

fun dest_nat (Const ("HOL.zero", _)) = 0
  | dest_nat (Const ("Suc", _) $ t) = dest_nat t + 1
  | dest_nat t = raise TERM ("dest_nat", [t]);


(* binary numerals and int *)

val intT = Type ("IntDef.int", []);
val bitT = Type ("Numeral.bit", []);

val B0_const = Const ("Numeral.bit.B0", bitT);
val B1_const =  Const ("Numeral.bit.B1", bitT);

val pls_const = Const ("Numeral.Pls", intT)
and min_const = Const ("Numeral.Min", intT)
and bit_const = Const ("Numeral.Bit", [intT, bitT] ---> intT);

fun number_of_const T = Const ("Numeral.number_of", intT --> T);

fun int_of [] = 0
  | int_of (b :: bs) = IntInf.fromInt b + (2 * int_of bs);

(*When called from a print translation, the Numeral qualifier will probably have
  been removed from Bit, bit.B0, etc.*)  (* FIXME !? *)
fun dest_bit (Const ("Numeral.bit.B0", _)) = 0
  | dest_bit (Const ("Numeral.bit.B1", _)) = 1
  | dest_bit (Const ("bit.B0", _)) = 0
  | dest_bit (Const ("bit.B1", _)) = 1
  | dest_bit t = raise TERM("dest_bit", [t]);

fun bin_of (Const ("Numeral.Pls", _)) = []
  | bin_of (Const ("Numeral.Min", _)) = [~1]
  | bin_of (Const ("Numeral.Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
  | bin_of (Const ("Bit", _) $ bs $ b) = dest_bit b :: bin_of bs
  | bin_of t = raise TERM("bin_of", [t]);

val dest_binum = int_of o bin_of;

fun mk_binum n =
  let
    fun mk_bit n = if n = 0 then B0_const else B1_const;
    fun bin_of n =
      if n = 0 then pls_const
      else if n = ~1 then min_const
      else
        let
          val (q,r) = IntInf.divMod (n, 2);
        in bit_const $ bin_of q $ mk_bit r end;
  in bin_of n end;

fun mk_int 0 = Const ("HOL.zero", intT)
  | mk_int 1 = Const ("HOL.one", intT)
  | mk_int i = number_of_const intT $ mk_binum i;


(* real *)

val realT = Type ("RealDef.real", []);


(* list *)

fun listT T =  Type ("List.list", [T]);

fun mk_list f T [] = Const ("List.list.Nil", listT T)
  | mk_list f T (x :: xs) = Const ("List.list.Cons",
      T --> listT T --> listT T) $ f x $
        mk_list f T xs;

fun dest_list (Const ("List.list.Nil", _)) = []
  | dest_list (Const ("List.list.Cons", _) $ x $ xs) = x :: dest_list xs
  | dest_list t = raise TERM ("dest_list", [t]);


(* char *)

val nibbleT = Type ("List.nibble", []);
val charT = Type ("List.char", []);

fun int_of_nibble h =
  if "0" <= h andalso h <= "9" then ord h - ord "0"
  else if "A" <= h andalso h <= "F" then ord h - ord "A" + 10
  else raise Match;

fun nibble_of_int i =
  if i <= 9 then chr (ord "0" + i) else chr (ord "A" + i - 10);

fun mk_char c =
  Const ("List.char.Char", nibbleT --> nibbleT --> charT) $
    Const ("List.nibble.Nibble" ^ nibble_of_int (ord c div 16), nibbleT) $
    Const ("List.nibble.Nibble" ^ nibble_of_int (ord c mod 16), nibbleT);

fun dest_char (Const ("List.char.Char", _) $ c1 $ c2) =
      let
        fun dest_nibble (Const (s, _)) = (int_of_nibble o unprefix "List.nibble.Nibble") s
          | dest_nibble _ = raise Match;
      in
        (SOME (dest_nibble c1 * 16 + dest_nibble c2)
        handle Fail _ => NONE | Match => NONE)
      end
  | dest_char _ = NONE;

fun print_char c =
  let
    val i = ord c
  in if i < 32
    then prefix "\\" (string_of_int i)
    else c
  end;


(* string *)

val stringT = Type ("List.string", []);

fun mk_string s =
  let
    val ty_n = Type ("List.nibble", []);
    val ty_c = Type ("List.char", []);
    val ty_l = Type ("List.list", [ty_c]);
    fun mk_nib n = Const ("List.nibble.Nibble" ^ chr (n + (if n <= 9 then ord "0" else ord "A" - 10)), ty_n);
    fun mk_char c =
      if Symbol.is_ascii c andalso Symbol.is_printable c then
        Const ("List.char.Char", ty_n --> ty_n --> ty_c) $ mk_nib (ord c div 16) $ mk_nib (ord c mod 16)
      else error ("Printable ASCII character expected: " ^ quote c);
    fun mk_string c t = Const ("List.list.Cons", ty_c --> ty_l --> ty_l)
      $ mk_char c $ t;
  in fold_rev mk_string (explode s) (Const ("List.list.Nil", ty_l)) end;

val print_string = quote;

end;