src/HOL/ind_syntax.ML
author clasohm
Fri, 03 Mar 1995 12:02:25 +0100
changeset 923 ff1574a81019
child 1430 439e1476a7f8
permissions -rw-r--r--
new version of HOL with curried function application
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
923
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     1
(*  Title: 	HOL/ind_syntax.ML
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     2
    ID:         $Id$
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     4
    Copyright   1994  University of Cambridge
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     5
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     6
Abstract Syntax functions for Inductive Definitions
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     7
See also hologic.ML and ../Pure/section-utils.ML
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     8
*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
     9
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    10
(*The structure protects these items from redeclaration (somewhat!).  The 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    11
  datatype definitions in theory files refer to these items by name!
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    12
*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    13
structure Ind_Syntax =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    14
struct
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    15
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    16
(** Abstract syntax definitions for HOL **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    17
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    18
open HOLogic;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    19
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    20
fun Int_const T = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    21
  let val sT = mk_setT T
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    22
  in  Const("op Int", [sT,sT]--->sT)  end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    23
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    24
fun mk_exists (Free(x,T),P) = exists_const T $ (absfree (x,T,P));
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    25
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    26
fun mk_all (Free(x,T),P) = all_const T $ (absfree (x,T,P));
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    27
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    28
(*Creates All(%v.v:A --> P(v)) rather than Ball(A,P) *)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    29
fun mk_all_imp (A,P) = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    30
  let val T = dest_setT (fastype_of A)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    31
  in  all_const T $ Abs("v", T, imp $ (mk_mem (Bound 0, A)) $ (P $ Bound 0))
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    32
  end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    33
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    34
(** Cartesian product type **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    35
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    36
val unitT = Type("unit",[]);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    37
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    38
fun mk_prod (T1,T2) = Type("*", [T1,T2]);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    39
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    40
(*Maps the type T1*...*Tn to [T1,...,Tn], if nested to the right*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    41
fun factors (Type("*", [T1,T2])) = T1 :: factors T2
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    42
  | factors T                    = [T];
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    43
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    44
(*Make a correctly typed ordered pair*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    45
fun mk_Pair (t1,t2) = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    46
  let val T1 = fastype_of t1
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    47
      and T2 = fastype_of t2
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    48
  in  Const("Pair", [T1, T2] ---> mk_prod(T1,T2)) $ t1 $ t2  end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    49
   
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    50
fun split_const(Ta,Tb,Tc) = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    51
    Const("split", [[Ta,Tb]--->Tc, mk_prod(Ta,Tb)] ---> Tc);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    52
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    53
(*Given u expecting arguments of types [T1,...,Tn], create term of 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    54
  type T1*...*Tn => Tc using split.  Here * associates to the LEFT*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    55
fun ap_split_l Tc u [ ]   = Abs("null", unitT, u)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    56
  | ap_split_l Tc u [_]   = u
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    57
  | ap_split_l Tc u (Ta::Tb::Ts) = ap_split_l Tc (split_const(Ta,Tb,Tc) $ u) 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    58
                                              (mk_prod(Ta,Tb) :: Ts);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    59
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    60
(*Given u expecting arguments of types [T1,...,Tn], create term of 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    61
  type T1*...*Tn => i using split.  Here * associates to the RIGHT*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    62
fun ap_split Tc u [ ]   = Abs("null", unitT, u)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    63
  | ap_split Tc u [_]   = u
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    64
  | ap_split Tc u [Ta,Tb] = split_const(Ta,Tb,Tc) $ u
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    65
  | ap_split Tc u (Ta::Ts) = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    66
      split_const(Ta, foldr1 mk_prod Ts, Tc) $ 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    67
      (Abs("v", Ta, ap_split Tc (u $ Bound(length Ts - 2)) Ts));
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    68
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    69
(** Disjoint sum type **)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    70
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    71
fun mk_sum (T1,T2) = Type("+", [T1,T2]);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    72
val Inl	= Const("Inl", dummyT)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    73
and Inr	= Const("Inr", dummyT);		(*correct types added later!*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    74
(*val elim	= Const("case", [iT-->iT, iT-->iT, iT]--->iT)*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    75
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    76
fun summands (Type("+", [T1,T2])) = summands T1 @ summands T2
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    77
  | summands T                    = [T];
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    78
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    79
(*Given the destination type, fills in correct types of an Inl/Inr nest*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    80
fun mend_sum_types (h,T) =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    81
    (case (h,T) of
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    82
	 (Const("Inl",_) $ h1, Type("+", [T1,T2])) =>
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    83
	     Const("Inl", T1 --> T) $ (mend_sum_types (h1, T1))
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    84
       | (Const("Inr",_) $ h2, Type("+", [T1,T2])) =>
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    85
	     Const("Inr", T2 --> T) $ (mend_sum_types (h2, T2))
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    86
       | _ => h);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    87
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    88
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    89
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    90
(*simple error-checking in the premises of an inductive definition*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    91
fun chk_prem rec_hd (Const("op &",_) $ _ $ _) =
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    92
	error"Premises may not be conjuctive"
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    93
  | chk_prem rec_hd (Const("op :",_) $ t $ X) = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    94
	deny (Logic.occs(rec_hd,t)) "Recursion term on left of member symbol"
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    95
  | chk_prem rec_hd t = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    96
	deny (Logic.occs(rec_hd,t)) "Recursion term in side formula";
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    97
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    98
(*Return the conclusion of a rule, of the form t:X*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
    99
fun rule_concl rl = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   100
    let val Const("Trueprop",_) $ (Const("op :",_) $ t $ X) = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   101
		Logic.strip_imp_concl rl
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   102
    in  (t,X)  end;
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   103
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   104
(*As above, but return error message if bad*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   105
fun rule_concl_msg sign rl = rule_concl rl
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   106
    handle Bind => error ("Ill-formed conclusion of introduction rule: " ^ 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   107
			  Sign.string_of_term sign rl);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   108
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   109
(*For simplifying the elimination rule*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   110
val sumprod_free_SEs = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   111
    Pair_inject ::
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   112
    map make_elim [(*Inl_neq_Inr, Inr_neq_Inl, Inl_inject, Inr_inject*)];
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   113
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   114
(*For deriving cases rules.  
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   115
  read_instantiate replaces a propositional variable by a formula variable*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   116
val equals_CollectD = 
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   117
    read_instantiate [("W","?Q")]
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   118
        (make_elim (equalityD1 RS subsetD RS CollectD));
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   119
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   120
(*Delete needless equality assumptions*)
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   121
val refl_thin = prove_goal HOL.thy "!!P. [| a=a;  P |] ==> P"
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   122
     (fn _ => [assume_tac 1]);
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   123
ff1574a81019 new version of HOL with curried function application
clasohm
parents:
diff changeset
   124
end;