src/Tools/misc_legacy.ML
author wenzelm
Thu Sep 02 00:48:07 2010 +0200 (2010-09-02)
changeset 38980 af73cf0dc31f
parent 37781 2fbbf0a48cef
child 39288 f1ae2493d93f
permissions -rw-r--r--
turned show_question_marks into proper configuration option;
show_question_marks only affects regular type/term pretty printing, not raw Term.string_of_vname;
tuned;
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@37781
     8
  val mk_defpair: term * term -> string * term
wenzelm@37781
     9
  val get_def: theory -> xstring -> thm
wenzelm@37781
    10
  val simple_read_term: theory -> typ -> string -> term
wenzelm@37781
    11
  val METAHYPS: (thm list -> tactic) -> int -> tactic
wenzelm@37781
    12
end;
wenzelm@37781
    13
wenzelm@37781
    14
structure Misc_Legacy: MISC_LEGACY =
wenzelm@37781
    15
struct
wenzelm@37781
    16
wenzelm@37781
    17
fun mk_defpair (lhs, rhs) =
wenzelm@37781
    18
  (case Term.head_of lhs of
wenzelm@37781
    19
    Const (name, _) =>
wenzelm@37781
    20
      (Long_Name.base_name name ^ "_def", Logic.mk_equals (lhs, rhs))
wenzelm@37781
    21
  | _ => raise TERM ("Malformed definition: head of lhs not a constant", [lhs, rhs]));
wenzelm@37781
    22
wenzelm@37781
    23
wenzelm@37781
    24
fun get_def thy = Thm.axiom thy o Name_Space.intern (Theory.axiom_space thy) o Thm.def_name;
wenzelm@37781
    25
wenzelm@37781
    26
wenzelm@37781
    27
fun simple_read_term thy T s =
wenzelm@37781
    28
  let
wenzelm@37781
    29
    val ctxt = ProofContext.init_global thy
wenzelm@37781
    30
      |> ProofContext.allow_dummies
wenzelm@37781
    31
      |> ProofContext.set_mode ProofContext.mode_schematic;
wenzelm@37781
    32
    val parse = if T = propT then Syntax.parse_prop else Syntax.parse_term;
wenzelm@37781
    33
  in parse ctxt s |> Type_Infer.constrain T |> Syntax.check_term ctxt end;
wenzelm@37781
    34
wenzelm@37781
    35
wenzelm@37781
    36
(**** METAHYPS -- tactical for using hypotheses as meta-level assumptions
wenzelm@37781
    37
       METAHYPS (fn prems => tac prems) i
wenzelm@37781
    38
wenzelm@37781
    39
converts subgoal i, of the form !!x1...xm. [| A1;...;An] ==> A into a new
wenzelm@37781
    40
proof state A==>A, supplying A1,...,An as meta-level assumptions (in
wenzelm@37781
    41
"prems").  The parameters x1,...,xm become free variables.  If the
wenzelm@37781
    42
resulting proof state is [| B1;...;Bk] ==> C (possibly assuming A1,...,An)
wenzelm@37781
    43
then it is lifted back into the original context, yielding k subgoals.
wenzelm@37781
    44
wenzelm@37781
    45
Replaces unknowns in the context by Frees having the prefix METAHYP_
wenzelm@37781
    46
New unknowns in [| B1;...;Bk] ==> C are lifted over x1,...,xm.
wenzelm@37781
    47
DOES NOT HANDLE TYPE UNKNOWNS.
wenzelm@37781
    48
wenzelm@37781
    49
wenzelm@37781
    50
NOTE: This version does not observe the proof context, and thus cannot
wenzelm@37781
    51
work reliably.  See also Subgoal.SUBPROOF and Subgoal.FOCUS for
wenzelm@37781
    52
properly localized variants of the same idea.
wenzelm@37781
    53
****)
wenzelm@37781
    54
wenzelm@37781
    55
local
wenzelm@37781
    56
wenzelm@37781
    57
(*Strips assumptions in goal yielding  ( [x1,...,xm], [H1,...,Hn], B )
wenzelm@37781
    58
    H1,...,Hn are the hypotheses;  x1...xm are variants of the parameters.
wenzelm@37781
    59
  Main difference from strip_assums concerns parameters:
wenzelm@37781
    60
    it replaces the bound variables by free variables.  *)
wenzelm@37781
    61
fun strip_context_aux (params, Hs, Const ("==>", _) $ H $ B) =
wenzelm@37781
    62
      strip_context_aux (params, H :: Hs, B)
wenzelm@37781
    63
  | strip_context_aux (params, Hs, Const ("all",_) $ Abs (a, T, t)) =
wenzelm@37781
    64
      let val (b, u) = Syntax.variant_abs (a, T, t)
wenzelm@37781
    65
      in strip_context_aux ((b, T) :: params, Hs, u) end
wenzelm@37781
    66
  | strip_context_aux (params, Hs, B) = (rev params, rev Hs, B);
wenzelm@37781
    67
wenzelm@37781
    68
fun strip_context A = strip_context_aux ([], [], A);
wenzelm@37781
    69
wenzelm@37781
    70
(*Left-to-right replacements: ctpairs = [...,(vi,ti),...].
wenzelm@37781
    71
  Instantiates distinct free variables by terms of same type.*)
wenzelm@37781
    72
fun free_instantiate ctpairs =
wenzelm@37781
    73
  forall_elim_list (map snd ctpairs) o forall_intr_list (map fst ctpairs);
wenzelm@37781
    74
wenzelm@37781
    75
fun free_of s ((a, i), T) =
wenzelm@37781
    76
  Free (s ^ (case i of 0 => a | _ => a ^ "_" ^ string_of_int i), T)
wenzelm@37781
    77
wenzelm@37781
    78
fun mk_inst v = (Var v, free_of "METAHYP1_" v)
wenzelm@37781
    79
wenzelm@37781
    80
fun metahyps_split_prem prem =
wenzelm@37781
    81
  let (*find all vars in the hyps -- should find tvars also!*)
wenzelm@37781
    82
      val hyps_vars = fold Term.add_vars (Logic.strip_assums_hyp prem) []
wenzelm@37781
    83
      val insts = map mk_inst hyps_vars
wenzelm@37781
    84
      (*replace the hyps_vars by Frees*)
wenzelm@37781
    85
      val prem' = subst_atomic insts prem
wenzelm@37781
    86
      val (params,hyps,concl) = strip_context prem'
wenzelm@37781
    87
  in (insts,params,hyps,concl)  end;
wenzelm@37781
    88
wenzelm@37781
    89
fun metahyps_aux_tac tacf (prem,gno) state =
wenzelm@37781
    90
  let val (insts,params,hyps,concl) = metahyps_split_prem prem
wenzelm@37781
    91
      val maxidx = Thm.maxidx_of state
wenzelm@37781
    92
      val cterm = Thm.cterm_of (Thm.theory_of_thm state)
wenzelm@37781
    93
      val chyps = map cterm hyps
wenzelm@37781
    94
      val hypths = map Thm.assume chyps
wenzelm@37781
    95
      val subprems = map (Thm.forall_elim_vars 0) hypths
wenzelm@37781
    96
      val fparams = map Free params
wenzelm@37781
    97
      val cparams = map cterm fparams
wenzelm@37781
    98
      fun swap_ctpair (t,u) = (cterm u, cterm t)
wenzelm@37781
    99
      (*Subgoal variables: make Free; lift type over params*)
wenzelm@37781
   100
      fun mk_subgoal_inst concl_vars (v, T) =
wenzelm@37781
   101
          if member (op =) concl_vars (v, T)
wenzelm@37781
   102
          then ((v, T), true, free_of "METAHYP2_" (v, T))
wenzelm@37781
   103
          else ((v, T), false, free_of "METAHYP2_" (v, map #2 params ---> T))
wenzelm@37781
   104
      (*Instantiate subgoal vars by Free applied to params*)
wenzelm@37781
   105
      fun mk_ctpair (v, in_concl, u) =
wenzelm@37781
   106
          if in_concl then (cterm (Var v), cterm u)
wenzelm@37781
   107
          else (cterm (Var v), cterm (list_comb (u, fparams)))
wenzelm@37781
   108
      (*Restore Vars with higher type and index*)
wenzelm@37781
   109
      fun mk_subgoal_swap_ctpair (((a, i), T), in_concl, u as Free (_, U)) =
wenzelm@37781
   110
          if in_concl then (cterm u, cterm (Var ((a, i), T)))
wenzelm@37781
   111
          else (cterm u, cterm (Var ((a, i + maxidx), U)))
wenzelm@37781
   112
      (*Embed B in the original context of params and hyps*)
wenzelm@37781
   113
      fun embed B = list_all_free (params, Logic.list_implies (hyps, B))
wenzelm@37781
   114
      (*Strip the context using elimination rules*)
wenzelm@37781
   115
      fun elim Bhyp = implies_elim_list (forall_elim_list cparams Bhyp) hypths
wenzelm@37781
   116
      (*A form of lifting that discharges assumptions.*)
wenzelm@37781
   117
      fun relift st =
wenzelm@37781
   118
        let val prop = Thm.prop_of st
wenzelm@37781
   119
            val subgoal_vars = (*Vars introduced in the subgoals*)
wenzelm@37781
   120
              fold Term.add_vars (Logic.strip_imp_prems prop) []
wenzelm@37781
   121
            and concl_vars = Term.add_vars (Logic.strip_imp_concl prop) []
wenzelm@37781
   122
            val subgoal_insts = map (mk_subgoal_inst concl_vars) subgoal_vars
wenzelm@37781
   123
            val st' = Thm.instantiate ([], map mk_ctpair subgoal_insts) st
wenzelm@37781
   124
            val emBs = map (cterm o embed) (prems_of st')
wenzelm@37781
   125
            val Cth  = implies_elim_list st' (map (elim o Thm.assume) emBs)
wenzelm@37781
   126
        in  (*restore the unknowns to the hypotheses*)
wenzelm@37781
   127
            free_instantiate (map swap_ctpair insts @
wenzelm@37781
   128
                              map mk_subgoal_swap_ctpair subgoal_insts)
wenzelm@37781
   129
                (*discharge assumptions from state in same order*)
wenzelm@37781
   130
                (implies_intr_list emBs
wenzelm@37781
   131
                  (forall_intr_list cparams (implies_intr_list chyps Cth)))
wenzelm@37781
   132
        end
wenzelm@37781
   133
      (*function to replace the current subgoal*)
wenzelm@37781
   134
      fun next st = Thm.bicompose false (false, relift st, nprems_of st) gno state
wenzelm@37781
   135
  in Seq.maps next (tacf subprems (Thm.trivial (cterm concl))) end;
wenzelm@37781
   136
wenzelm@37781
   137
fun print_vars_terms n thm =
wenzelm@37781
   138
  let
wenzelm@37781
   139
    val thy = theory_of_thm thm
wenzelm@37781
   140
    fun typed s ty =
wenzelm@37781
   141
      "  " ^ s ^ " has type: " ^ Syntax.string_of_typ_global thy ty;
wenzelm@37781
   142
    fun find_vars (Const (c, ty)) =
wenzelm@37781
   143
          if null (Term.add_tvarsT ty []) then I
wenzelm@37781
   144
          else insert (op =) (typed c ty)
wenzelm@37781
   145
      | find_vars (Var (xi, ty)) =
wenzelm@37781
   146
          insert (op =) (typed (Term.string_of_vname xi) ty)
wenzelm@37781
   147
      | find_vars (Free _) = I
wenzelm@37781
   148
      | find_vars (Bound _) = I
wenzelm@37781
   149
      | find_vars (Abs (_, _, t)) = find_vars t
wenzelm@37781
   150
      | find_vars (t1 $ t2) = find_vars t1 #> find_vars t2;
wenzelm@37781
   151
    val prem = Logic.nth_prem (n, Thm.prop_of thm)
wenzelm@37781
   152
    val tms = find_vars prem []
wenzelm@37781
   153
  in warning (cat_lines ("Found schematic vars in assumptions:" :: tms)) end;
wenzelm@37781
   154
wenzelm@37781
   155
in
wenzelm@37781
   156
wenzelm@37781
   157
fun METAHYPS tacf n thm = SUBGOAL (metahyps_aux_tac tacf) n thm
wenzelm@37781
   158
  handle THM("assume: variables",_,_) => (print_vars_terms n thm; Seq.empty)
wenzelm@37781
   159
wenzelm@37781
   160
end;
wenzelm@37781
   161
wenzelm@37781
   162
end;
wenzelm@37781
   163