src/Tools/misc_legacy.ML
author wenzelm
Wed, 19 Oct 2011 15:41:12 +0200
changeset 46066 63ce9e743734
parent 45004 44adaa6db327
child 46734 fcb897b39fa3
permissions -rw-r--r--
tuned legacy signature;
wenzelm@37781
     1
(*  Title:      Tools/misc_legacy.ML
wenzelm@37781
     2
wenzelm@37781
     3
Misc legacy stuff -- to be phased out eventually.
wenzelm@37781
     4
*)
wenzelm@37781
     5
wenzelm@37781
     6
signature MISC_LEGACY =
wenzelm@37781
     7
sig
wenzelm@45004
     8
  val add_term_names: term * string list -> string list
wenzelm@45004
     9
  val add_typ_tvars: typ * (indexname * sort) list -> (indexname * sort) list
wenzelm@45004
    10
  val add_typ_tfree_names: typ * string list -> string list
wenzelm@45004
    11
  val add_typ_tfrees: typ * (string * sort) list -> (string * sort) list
wenzelm@45004
    12
  val add_term_tvars: term * (indexname * sort) list -> (indexname * sort) list
wenzelm@45004
    13
  val add_term_tfrees: term * (string * sort) list -> (string * sort) list
wenzelm@45004
    14
  val add_term_tfree_names: term * string list -> string list
wenzelm@45004
    15
  val typ_tfrees: typ -> (string * sort) list
wenzelm@45004
    16
  val typ_tvars: typ -> (indexname * sort) list
wenzelm@45004
    17
  val term_tfrees: term -> (string * sort) list
wenzelm@45004
    18
  val term_tvars: term -> (indexname * sort) list
wenzelm@45004
    19
  val add_term_vars: term * term list -> term list
wenzelm@45004
    20
  val term_vars: term -> term list
wenzelm@45004
    21
  val add_term_frees: term * term list -> term list
wenzelm@45004
    22
  val term_frees: term -> term list
wenzelm@37781
    23
  val mk_defpair: term * term -> string * term
wenzelm@37781
    24
  val get_def: theory -> xstring -> thm
wenzelm@37781
    25
  val simple_read_term: theory -> typ -> string -> term
wenzelm@37781
    26
  val METAHYPS: (thm list -> tactic) -> int -> tactic
wenzelm@37781
    27
end;
wenzelm@37781
    28
wenzelm@37781
    29
structure Misc_Legacy: MISC_LEGACY =
wenzelm@37781
    30
struct
wenzelm@37781
    31
wenzelm@45004
    32
(*iterate a function over all types in a term*)
wenzelm@45004
    33
fun it_term_types f =
wenzelm@45004
    34
let fun iter(Const(_,T), a) = f(T,a)
wenzelm@45004
    35
      | iter(Free(_,T), a) = f(T,a)
wenzelm@45004
    36
      | iter(Var(_,T), a) = f(T,a)
wenzelm@45004
    37
      | iter(Abs(_,T,t), a) = iter(t,f(T,a))
wenzelm@45004
    38
      | iter(f$u, a) = iter(f, iter(u, a))
wenzelm@45004
    39
      | iter(Bound _, a) = a
wenzelm@45004
    40
in iter end
wenzelm@45004
    41
wenzelm@45004
    42
(*Accumulates the names in the term, suppressing duplicates.
wenzelm@45004
    43
  Includes Frees and Consts.  For choosing unambiguous bound var names.*)
wenzelm@45004
    44
fun add_term_names (Const(a,_), bs) = insert (op =) (Long_Name.base_name a) bs
wenzelm@45004
    45
  | add_term_names (Free(a,_), bs) = insert (op =) a bs
wenzelm@45004
    46
  | add_term_names (f$u, bs) = add_term_names (f, add_term_names(u, bs))
wenzelm@45004
    47
  | add_term_names (Abs(_,_,t), bs) = add_term_names(t,bs)
wenzelm@45004
    48
  | add_term_names (_, bs) = bs;
wenzelm@45004
    49
wenzelm@45004
    50
(*Accumulates the TVars in a type, suppressing duplicates.*)
wenzelm@45004
    51
fun add_typ_tvars(Type(_,Ts),vs) = List.foldr add_typ_tvars vs Ts
wenzelm@45004
    52
  | add_typ_tvars(TFree(_),vs) = vs
wenzelm@45004
    53
  | add_typ_tvars(TVar(v),vs) = insert (op =) v vs;
wenzelm@45004
    54
wenzelm@45004
    55
(*Accumulates the TFrees in a type, suppressing duplicates.*)
wenzelm@45004
    56
fun add_typ_tfree_names(Type(_,Ts),fs) = List.foldr add_typ_tfree_names fs Ts
wenzelm@45004
    57
  | add_typ_tfree_names(TFree(f,_),fs) = insert (op =) f fs
wenzelm@45004
    58
  | add_typ_tfree_names(TVar(_),fs) = fs;
wenzelm@45004
    59
wenzelm@45004
    60
fun add_typ_tfrees(Type(_,Ts),fs) = List.foldr add_typ_tfrees fs Ts
wenzelm@45004
    61
  | add_typ_tfrees(TFree(f),fs) = insert (op =) f fs
wenzelm@45004
    62
  | add_typ_tfrees(TVar(_),fs) = fs;
wenzelm@45004
    63
wenzelm@45004
    64
(*Accumulates the TVars in a term, suppressing duplicates.*)
wenzelm@45004
    65
val add_term_tvars = it_term_types add_typ_tvars;
wenzelm@45004
    66
wenzelm@45004
    67
(*Accumulates the TFrees in a term, suppressing duplicates.*)
wenzelm@45004
    68
val add_term_tfrees = it_term_types add_typ_tfrees;
wenzelm@45004
    69
val add_term_tfree_names = it_term_types add_typ_tfree_names;
wenzelm@45004
    70
wenzelm@45004
    71
(*Non-list versions*)
wenzelm@45004
    72
fun typ_tfrees T = add_typ_tfrees(T,[]);
wenzelm@45004
    73
fun typ_tvars T = add_typ_tvars(T,[]);
wenzelm@45004
    74
fun term_tfrees t = add_term_tfrees(t,[]);
wenzelm@45004
    75
fun term_tvars t = add_term_tvars(t,[]);
wenzelm@45004
    76
wenzelm@45004
    77
wenzelm@45004
    78
(*Accumulates the Vars in the term, suppressing duplicates.*)
wenzelm@45004
    79
fun add_term_vars (t, vars: term list) = case t of
wenzelm@45004
    80
    Var   _ => Ord_List.insert Term_Ord.term_ord t vars
wenzelm@45004
    81
  | Abs (_,_,body) => add_term_vars(body,vars)
wenzelm@45004
    82
  | f$t =>  add_term_vars (f, add_term_vars(t, vars))
wenzelm@45004
    83
  | _ => vars;
wenzelm@45004
    84
wenzelm@45004
    85
fun term_vars t = add_term_vars(t,[]);
wenzelm@45004
    86
wenzelm@45004
    87
(*Accumulates the Frees in the term, suppressing duplicates.*)
wenzelm@45004
    88
fun add_term_frees (t, frees: term list) = case t of
wenzelm@45004
    89
    Free   _ => Ord_List.insert Term_Ord.term_ord t frees
wenzelm@45004
    90
  | Abs (_,_,body) => add_term_frees(body,frees)
wenzelm@45004
    91
  | f$t =>  add_term_frees (f, add_term_frees(t, frees))
wenzelm@45004
    92
  | _ => frees;
wenzelm@45004
    93
wenzelm@45004
    94
fun term_frees t = add_term_frees(t,[]);
wenzelm@45004
    95
wenzelm@45004
    96
wenzelm@37781
    97
fun mk_defpair (lhs, rhs) =
wenzelm@37781
    98
  (case Term.head_of lhs of
wenzelm@37781
    99
    Const (name, _) =>
wenzelm@37781
   100
      (Long_Name.base_name name ^ "_def", Logic.mk_equals (lhs, rhs))
wenzelm@37781
   101
  | _ => raise TERM ("Malformed definition: head of lhs not a constant", [lhs, rhs]));
wenzelm@37781
   102
wenzelm@37781
   103
wenzelm@37781
   104
fun get_def thy = Thm.axiom thy o Name_Space.intern (Theory.axiom_space thy) o Thm.def_name;
wenzelm@37781
   105
wenzelm@37781
   106
wenzelm@37781
   107
fun simple_read_term thy T s =
wenzelm@37781
   108
  let
wenzelm@43232
   109
    val ctxt = Proof_Context.init_global thy
wenzelm@43232
   110
      |> Proof_Context.allow_dummies
wenzelm@43232
   111
      |> Proof_Context.set_mode Proof_Context.mode_schematic;
wenzelm@37781
   112
    val parse = if T = propT then Syntax.parse_prop else Syntax.parse_term;
wenzelm@39541
   113
  in parse ctxt s |> Type.constraint T |> Syntax.check_term ctxt end;
wenzelm@37781
   114
wenzelm@37781
   115
wenzelm@37781
   116
(**** METAHYPS -- tactical for using hypotheses as meta-level assumptions
wenzelm@37781
   117
       METAHYPS (fn prems => tac prems) i
wenzelm@37781
   118
wenzelm@37781
   119
converts subgoal i, of the form !!x1...xm. [| A1;...;An] ==> A into a new
wenzelm@37781
   120
proof state A==>A, supplying A1,...,An as meta-level assumptions (in
wenzelm@37781
   121
"prems").  The parameters x1,...,xm become free variables.  If the
wenzelm@37781
   122
resulting proof state is [| B1;...;Bk] ==> C (possibly assuming A1,...,An)
wenzelm@37781
   123
then it is lifted back into the original context, yielding k subgoals.
wenzelm@37781
   124
wenzelm@37781
   125
Replaces unknowns in the context by Frees having the prefix METAHYP_
wenzelm@37781
   126
New unknowns in [| B1;...;Bk] ==> C are lifted over x1,...,xm.
wenzelm@37781
   127
DOES NOT HANDLE TYPE UNKNOWNS.
wenzelm@37781
   128
wenzelm@37781
   129
wenzelm@37781
   130
NOTE: This version does not observe the proof context, and thus cannot
wenzelm@37781
   131
work reliably.  See also Subgoal.SUBPROOF and Subgoal.FOCUS for
wenzelm@37781
   132
properly localized variants of the same idea.
wenzelm@37781
   133
****)
wenzelm@37781
   134
wenzelm@37781
   135
local
wenzelm@37781
   136
wenzelm@37781
   137
(*Strips assumptions in goal yielding  ( [x1,...,xm], [H1,...,Hn], B )
wenzelm@37781
   138
    H1,...,Hn are the hypotheses;  x1...xm are variants of the parameters.
wenzelm@37781
   139
  Main difference from strip_assums concerns parameters:
wenzelm@37781
   140
    it replaces the bound variables by free variables.  *)
wenzelm@37781
   141
fun strip_context_aux (params, Hs, Const ("==>", _) $ H $ B) =
wenzelm@37781
   142
      strip_context_aux (params, H :: Hs, B)
wenzelm@37781
   143
  | strip_context_aux (params, Hs, Const ("all",_) $ Abs (a, T, t)) =
wenzelm@43156
   144
      let val (b, u) = Syntax_Trans.variant_abs (a, T, t)
wenzelm@37781
   145
      in strip_context_aux ((b, T) :: params, Hs, u) end
wenzelm@37781
   146
  | strip_context_aux (params, Hs, B) = (rev params, rev Hs, B);
wenzelm@37781
   147
wenzelm@37781
   148
fun strip_context A = strip_context_aux ([], [], A);
wenzelm@37781
   149
wenzelm@37781
   150
(*Left-to-right replacements: ctpairs = [...,(vi,ti),...].
wenzelm@37781
   151
  Instantiates distinct free variables by terms of same type.*)
wenzelm@37781
   152
fun free_instantiate ctpairs =
wenzelm@37781
   153
  forall_elim_list (map snd ctpairs) o forall_intr_list (map fst ctpairs);
wenzelm@37781
   154
wenzelm@37781
   155
fun free_of s ((a, i), T) =
wenzelm@37781
   156
  Free (s ^ (case i of 0 => a | _ => a ^ "_" ^ string_of_int i), T)
wenzelm@37781
   157
wenzelm@37781
   158
fun mk_inst v = (Var v, free_of "METAHYP1_" v)
wenzelm@37781
   159
wenzelm@37781
   160
fun metahyps_split_prem prem =
wenzelm@37781
   161
  let (*find all vars in the hyps -- should find tvars also!*)
wenzelm@37781
   162
      val hyps_vars = fold Term.add_vars (Logic.strip_assums_hyp prem) []
wenzelm@37781
   163
      val insts = map mk_inst hyps_vars
wenzelm@37781
   164
      (*replace the hyps_vars by Frees*)
wenzelm@37781
   165
      val prem' = subst_atomic insts prem
wenzelm@37781
   166
      val (params,hyps,concl) = strip_context prem'
wenzelm@37781
   167
  in (insts,params,hyps,concl)  end;
wenzelm@37781
   168
wenzelm@37781
   169
fun metahyps_aux_tac tacf (prem,gno) state =
wenzelm@37781
   170
  let val (insts,params,hyps,concl) = metahyps_split_prem prem
wenzelm@37781
   171
      val maxidx = Thm.maxidx_of state
wenzelm@37781
   172
      val cterm = Thm.cterm_of (Thm.theory_of_thm state)
wenzelm@37781
   173
      val chyps = map cterm hyps
wenzelm@37781
   174
      val hypths = map Thm.assume chyps
wenzelm@37781
   175
      val subprems = map (Thm.forall_elim_vars 0) hypths
wenzelm@37781
   176
      val fparams = map Free params
wenzelm@37781
   177
      val cparams = map cterm fparams
wenzelm@37781
   178
      fun swap_ctpair (t,u) = (cterm u, cterm t)
wenzelm@37781
   179
      (*Subgoal variables: make Free; lift type over params*)
wenzelm@37781
   180
      fun mk_subgoal_inst concl_vars (v, T) =
wenzelm@37781
   181
          if member (op =) concl_vars (v, T)
wenzelm@37781
   182
          then ((v, T), true, free_of "METAHYP2_" (v, T))
wenzelm@37781
   183
          else ((v, T), false, free_of "METAHYP2_" (v, map #2 params ---> T))
wenzelm@37781
   184
      (*Instantiate subgoal vars by Free applied to params*)
wenzelm@37781
   185
      fun mk_ctpair (v, in_concl, u) =
wenzelm@37781
   186
          if in_concl then (cterm (Var v), cterm u)
wenzelm@37781
   187
          else (cterm (Var v), cterm (list_comb (u, fparams)))
wenzelm@37781
   188
      (*Restore Vars with higher type and index*)
wenzelm@37781
   189
      fun mk_subgoal_swap_ctpair (((a, i), T), in_concl, u as Free (_, U)) =
wenzelm@37781
   190
          if in_concl then (cterm u, cterm (Var ((a, i), T)))
wenzelm@37781
   191
          else (cterm u, cterm (Var ((a, i + maxidx), U)))
wenzelm@37781
   192
      (*Embed B in the original context of params and hyps*)
wenzelm@37781
   193
      fun embed B = list_all_free (params, Logic.list_implies (hyps, B))
wenzelm@37781
   194
      (*Strip the context using elimination rules*)
wenzelm@37781
   195
      fun elim Bhyp = implies_elim_list (forall_elim_list cparams Bhyp) hypths
wenzelm@37781
   196
      (*A form of lifting that discharges assumptions.*)
wenzelm@37781
   197
      fun relift st =
wenzelm@37781
   198
        let val prop = Thm.prop_of st
wenzelm@37781
   199
            val subgoal_vars = (*Vars introduced in the subgoals*)
wenzelm@37781
   200
              fold Term.add_vars (Logic.strip_imp_prems prop) []
wenzelm@37781
   201
            and concl_vars = Term.add_vars (Logic.strip_imp_concl prop) []
wenzelm@37781
   202
            val subgoal_insts = map (mk_subgoal_inst concl_vars) subgoal_vars
wenzelm@37781
   203
            val st' = Thm.instantiate ([], map mk_ctpair subgoal_insts) st
wenzelm@37781
   204
            val emBs = map (cterm o embed) (prems_of st')
wenzelm@37781
   205
            val Cth  = implies_elim_list st' (map (elim o Thm.assume) emBs)
wenzelm@37781
   206
        in  (*restore the unknowns to the hypotheses*)
wenzelm@37781
   207
            free_instantiate (map swap_ctpair insts @
wenzelm@37781
   208
                              map mk_subgoal_swap_ctpair subgoal_insts)
wenzelm@37781
   209
                (*discharge assumptions from state in same order*)
wenzelm@37781
   210
                (implies_intr_list emBs
wenzelm@37781
   211
                  (forall_intr_list cparams (implies_intr_list chyps Cth)))
wenzelm@37781
   212
        end
wenzelm@37781
   213
      (*function to replace the current subgoal*)
wenzelm@37781
   214
      fun next st = Thm.bicompose false (false, relift st, nprems_of st) gno state
wenzelm@37781
   215
  in Seq.maps next (tacf subprems (Thm.trivial (cterm concl))) end;
wenzelm@37781
   216
wenzelm@37781
   217
fun print_vars_terms n thm =
wenzelm@37781
   218
  let
wenzelm@37781
   219
    val thy = theory_of_thm thm
wenzelm@37781
   220
    fun typed s ty =
wenzelm@37781
   221
      "  " ^ s ^ " has type: " ^ Syntax.string_of_typ_global thy ty;
wenzelm@37781
   222
    fun find_vars (Const (c, ty)) =
wenzelm@37781
   223
          if null (Term.add_tvarsT ty []) then I
wenzelm@37781
   224
          else insert (op =) (typed c ty)
wenzelm@37781
   225
      | find_vars (Var (xi, ty)) =
wenzelm@37781
   226
          insert (op =) (typed (Term.string_of_vname xi) ty)
wenzelm@37781
   227
      | find_vars (Free _) = I
wenzelm@37781
   228
      | find_vars (Bound _) = I
wenzelm@37781
   229
      | find_vars (Abs (_, _, t)) = find_vars t
wenzelm@37781
   230
      | find_vars (t1 $ t2) = find_vars t1 #> find_vars t2;
wenzelm@37781
   231
    val prem = Logic.nth_prem (n, Thm.prop_of thm)
wenzelm@37781
   232
    val tms = find_vars prem []
wenzelm@37781
   233
  in warning (cat_lines ("Found schematic vars in assumptions:" :: tms)) end;
wenzelm@37781
   234
wenzelm@37781
   235
in
wenzelm@37781
   236
wenzelm@37781
   237
fun METAHYPS tacf n thm = SUBGOAL (metahyps_aux_tac tacf) n thm
wenzelm@37781
   238
  handle THM("assume: variables",_,_) => (print_vars_terms n thm; Seq.empty)
wenzelm@37781
   239
wenzelm@37781
   240
end;
wenzelm@37781
   241
wenzelm@37781
   242
end;
wenzelm@37781
   243