src/Provers/IsaPlanner/isand.ML
author wenzelm
Wed, 04 Apr 2007 00:11:03 +0200
changeset 22578 b0eb5652f210
parent 20548 8ef25fe585a8
permissions -rw-r--r--
removed obsolete sign_of/sign_of_thm;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
     1
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *) 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
     2
(*  Title:      Pure/IsaPlanner/isand.ML
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
     3
    ID:		$Id$
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
     4
    Author:     Lucas Dixon, University of Edinburgh
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
     5
                lucas.dixon@ed.ac.uk
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
     6
    Updated:    26 Apr 2005
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
     7
    Date:       6 Aug 2004
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
     8
*)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
     9
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *) 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    10
(*  DESCRIPTION:
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    11
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    12
    Natural Deduction tools
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    13
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    14
    For working with Isabelle theorems in a natural detuction style.
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    15
    ie, not having to deal with meta level quantified varaibles,
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    16
    instead, we work with newly introduced frees, and hide the
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    17
    "all"'s, exporting results from theorems proved with the frees, to
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    18
    solve the all cases of the previous goal. This allows resolution
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    19
    to do proof search normally. 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    20
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    21
    Note: A nice idea: allow exporting to solve any subgoal, thus
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    22
    allowing the interleaving of proof, or provide a structure for the
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    23
    ordering of proof, thus allowing proof attempts in parrell, but
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    24
    recording the order to apply things in.
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    25
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    26
    THINK: are we really ok with our varify name w.r.t the prop - do
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    27
    we also need to avoid names in the hidden hyps? What about
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    28
    unification contraints in flex-flex pairs - might they also have
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    29
    extra free vars?
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    30
*)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    31
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    32
signature ISA_ND =
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    33
sig
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    34
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    35
  (* export data *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    36
  datatype export = export of
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    37
           {gth: Thm.thm, (* initial goal theorem *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    38
            sgid: int, (* subgoal id which has been fixed etc *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    39
            fixes: Thm.cterm list, (* frees *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    40
            assumes: Thm.cterm list} (* assumptions *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    41
  val fixes_of_exp : export -> Thm.cterm list
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    42
  val export_back : export -> Thm.thm -> Thm.thm Seq.seq
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    43
  val export_solution : export -> Thm.thm -> Thm.thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    44
  val export_solutions : export list * Thm.thm -> Thm.thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    45
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    46
  (* inserting meta level params for frees in the conditions *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    47
  val allify_conditions :
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    48
      (Term.term -> Thm.cterm) ->
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    49
      (string * Term.typ) list -> Thm.thm -> Thm.thm * Thm.cterm list
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    50
  val allify_conditions' :
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    51
      (string * Term.typ) list -> Thm.thm -> Thm.thm * Thm.cterm list
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    52
  val assume_allified :
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    53
      theory -> (string * Term.sort) list * (string * Term.typ) list
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    54
      -> Term.term -> (Thm.cterm * Thm.thm)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    55
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    56
  (* meta level fixed params (i.e. !! vars) *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    57
  val fix_alls_in_term : Term.term -> Term.term * Term.term list
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    58
  val fix_alls_term : int -> Term.term -> Term.term * Term.term list
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    59
  val fix_alls_cterm : int -> Thm.thm -> Thm.cterm * Thm.cterm list
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    60
  val fix_alls' : int -> Thm.thm -> Thm.thm * Thm.cterm list
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    61
  val fix_alls : int -> Thm.thm -> Thm.thm * export
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    62
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    63
  (* meta variables in types and terms *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    64
  val fix_tvars_to_tfrees_in_terms 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    65
      : string list (* avoid these names *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    66
        -> Term.term list -> 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    67
        (((string * int) * Term.sort) * (string * Term.sort)) list (* renamings *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    68
  val fix_vars_to_frees_in_terms
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    69
      : string list (* avoid these names *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    70
        -> Term.term list ->
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    71
        (((string * int) * Term.typ) * (string * Term.typ)) list (* renamings *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    72
  val fix_tvars_to_tfrees : Thm.thm -> Thm.ctyp list * Thm.thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    73
  val fix_vars_to_frees : Thm.thm -> Thm.cterm list * Thm.thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    74
  val fix_vars_and_tvars : 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    75
      Thm.thm -> (Thm.cterm list * Thm.ctyp list) * Thm.thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    76
  val fix_vars_upto_idx : int -> Thm.thm -> Thm.thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    77
  val fix_tvars_upto_idx : int -> Thm.thm -> Thm.thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    78
  val unfix_frees : Thm.cterm list -> Thm.thm -> Thm.thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    79
  val unfix_tfrees : Thm.ctyp list -> Thm.thm -> Thm.thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    80
  val unfix_frees_and_tfrees :
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    81
      (Thm.cterm list * Thm.ctyp list) -> Thm.thm -> Thm.thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    82
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    83
  (* assumptions/subgoals *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    84
  val assume_prems :
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    85
      int -> Thm.thm -> Thm.thm list * Thm.thm * Thm.cterm list
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    86
  val fixed_subgoal_thms : Thm.thm -> Thm.thm list * (Thm.thm list -> Thm.thm)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    87
  val fixes_and_assumes : int -> Thm.thm -> Thm.thm list * Thm.thm * export
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    88
  val hide_other_goals : Thm.thm -> Thm.thm * Thm.cterm list
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    89
  val hide_prems : Thm.thm -> Thm.thm * Thm.cterm list
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    90
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    91
  (* abstracts cterms (vars) to locally meta-all bounds *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    92
  val prepare_goal_export : string list * Thm.cterm list -> Thm.thm 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    93
                            -> int * Thm.thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    94
  val solve_with : Thm.thm -> Thm.thm -> Thm.thm Seq.seq
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    95
  val subgoal_thms : Thm.thm -> Thm.thm list * (Thm.thm list -> Thm.thm)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    96
end
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    97
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    98
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
    99
structure IsaND 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   100
: ISA_ND
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   101
= struct
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   102
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   103
(* Solve *some* subgoal of "th" directly by "sol" *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   104
(* Note: this is probably what Markus ment to do upon export of a
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   105
"show" but maybe he used RS/rtac instead, which would wrongly lead to
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   106
failing if there are premices to the shown goal. 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   107
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   108
given: 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   109
sol : Thm.thm = [| Ai... |] ==> Ci
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   110
th : Thm.thm = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   111
  [| ... [| Ai... |] ==> Ci ... |] ==> G
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   112
results in: 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   113
  [| ... [| Ai-1... |] ==> Ci-1
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   114
    [| Ai+1... |] ==> Ci+1 ...
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   115
  |] ==> G
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   116
i.e. solves some subgoal of th that is identical to sol. 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   117
*)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   118
fun solve_with sol th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   119
    let fun solvei 0 = Seq.empty
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   120
          | solvei i = 
19861
620d90091788 tuned Seq/Envir/Unify interfaces;
wenzelm
parents: 19835
diff changeset
   121
            Seq.append (bicompose false (false,sol,0) i th) (solvei (i - 1))
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   122
    in
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   123
      solvei (Thm.nprems_of th)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   124
    end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   125
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   126
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   127
(* Given ctertmify function, (string,type) pairs capturing the free
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   128
vars that need to be allified in the assumption, and a theorem with
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   129
assumptions possibly containing the free vars, then we give back the
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   130
assumptions allified as hidden hyps. 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   131
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   132
Given: x 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   133
th: A vs ==> B vs 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   134
Results in: "B vs" [!!x. A x]
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   135
*)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   136
fun allify_conditions ctermify Ts th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   137
    let 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   138
      val premts = Thm.prems_of th;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   139
    
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   140
      fun allify_prem_var (vt as (n,ty),t)  = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   141
          (Term.all ty) $ (Abs(n,ty,Term.abstract_over (Free vt, t)))
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   142
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   143
      fun allify_prem Ts p = foldr allify_prem_var p Ts
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   144
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   145
      val cTs = map (ctermify o Free) Ts
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   146
      val cterm_asms = map (ctermify o allify_prem Ts) premts
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   147
      val allifyied_asm_thms = map (Drule.forall_elim_list cTs o Thm.assume) cterm_asms
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   148
    in 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   149
      (Library.foldl (fn (x,y) => y COMP x) (th, allifyied_asm_thms), cterm_asms)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   150
    end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   151
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   152
fun allify_conditions' Ts th = 
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 20548
diff changeset
   153
    allify_conditions (Thm.cterm_of (Thm.theory_of_thm th)) Ts th;
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   154
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   155
(* allify types *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   156
fun allify_typ ts ty = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   157
    let 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   158
      fun trec (x as (TFree (s,srt))) = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   159
          (case Library.find_first (fn (s2,srt2) => s = s2) ts
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   160
            of NONE => x
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   161
             | SOME (s2,_) => TVar ((s,0),srt))
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   162
            (*  Maybe add in check here for bad sorts? 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   163
             if srt = srt2 then TVar ((s,0),srt) 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   164
               else raise  ("thaw_typ", ts, ty) *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   165
          | trec (Type (s,typs)) = Type (s, map trec typs)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   166
          | trec (v as TVar _) = v;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   167
    in trec ty end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   168
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   169
(* implicit types and term *)
20548
8ef25fe585a8 renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents: 20071
diff changeset
   170
fun allify_term_typs ty = Term.map_types (allify_typ ty);
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   171
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   172
(* allified version of term, given frees to allify over. Note that we
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   173
only allify over the types on the given allified cterm, we can't do
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   174
this for the theorem as we are not allowed type-vars in the hyp. *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   175
(* FIXME: make the allified term keep the same conclusion as it
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   176
started with, rather than a strictly more general version (ie use
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   177
instantiate with initial params we abstracted from, rather than
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   178
forall_elim_vars. *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   179
fun assume_allified sgn (tyvs,vs) t = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   180
    let
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   181
      fun allify_var (vt as (n,ty),t)  = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   182
          (Term.all ty) $ (Abs(n,ty,Term.abstract_over (Free vt, t)))
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   183
      fun allify Ts p = List.foldr allify_var p Ts
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   184
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   185
      val ctermify = Thm.cterm_of sgn;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   186
      val cvars = map (fn (n,ty) => ctermify (Var ((n,0),ty))) vs
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   187
      val allified_term = t |> allify vs;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   188
      val ct = ctermify allified_term;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   189
      val typ_allified_ct = ctermify (allify_term_typs tyvs allified_term);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   190
    in (typ_allified_ct, 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   191
        Drule.forall_elim_vars 0 (Thm.assume ct)) end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   192
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   193
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   194
(* change type-vars to fresh type frees *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   195
fun fix_tvars_to_tfrees_in_terms names ts = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   196
    let 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   197
      val tfree_names = map fst (List.foldr Term.add_term_tfrees [] ts);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   198
      val tvars = List.foldr Term.add_term_tvars [] ts;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   199
      val (names',renamings) = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   200
          List.foldr (fn (tv as ((n,i),s),(Ns,Rs)) => 
20071
8f3e1ddb50e6 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 19861
diff changeset
   201
                         let val n2 = Name.variant Ns n in 
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   202
                           (n2::Ns, (tv, (n2,s))::Rs)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   203
                         end) (tfree_names @ names,[]) tvars;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   204
    in renamings end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   205
fun fix_tvars_to_tfrees th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   206
    let 
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 20548
diff changeset
   207
      val sign = Thm.theory_of_thm th;
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   208
      val ctypify = Thm.ctyp_of sign;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   209
      val tpairs = Thm.terms_of_tpairs (Thm.tpairs_of th);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   210
      val renamings = fix_tvars_to_tfrees_in_terms 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   211
                        [] ((Thm.prop_of th) :: tpairs);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   212
      val crenamings = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   213
          map (fn (v,f) => (ctypify (TVar v), ctypify (TFree f)))
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   214
              renamings;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   215
      val fixedfrees = map snd crenamings;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   216
    in (fixedfrees, Thm.instantiate (crenamings, []) th) end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   217
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   218
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   219
(* change type-free's to type-vars in th, skipping the ones in "ns" *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   220
fun unfix_tfrees ns th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   221
    let 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   222
      val varfiytfrees = map (Term.dest_TFree o Thm.typ_of) ns
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   223
      val skiptfrees = subtract (op =) varfiytfrees (Term.add_term_tfrees (Thm.prop_of th,[]));
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   224
    in #2 (Thm.varifyT' skiptfrees th) end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   225
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   226
(* change schematic/meta vars to fresh free vars, avoiding name clashes 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   227
   with "names" *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   228
fun fix_vars_to_frees_in_terms names ts = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   229
    let 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   230
      val vars = map Term.dest_Var (List.foldr Term.add_term_vars [] ts);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   231
      val Ns = List.foldr Term.add_term_names names ts;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   232
      val (_,renamings) = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   233
          Library.foldl (fn ((Ns,Rs),v as ((n,i),ty)) => 
20071
8f3e1ddb50e6 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 19861
diff changeset
   234
                    let val n2 = Name.variant Ns n in
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   235
                      (n2 :: Ns, (v, (n2,ty)) :: Rs)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   236
                    end) ((Ns,[]), vars);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   237
    in renamings end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   238
fun fix_vars_to_frees th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   239
    let 
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 20548
diff changeset
   240
      val ctermify = Thm.cterm_of (Thm.theory_of_thm th)
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   241
      val tpairs = Thm.terms_of_tpairs (Thm.tpairs_of th);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   242
      val renamings = fix_vars_to_frees_in_terms 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   243
                        [] ([Thm.prop_of th] @ tpairs);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   244
      val crenamings = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   245
          map (fn (v,f) => (ctermify (Var v), ctermify (Free f)))
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   246
              renamings;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   247
      val fixedfrees = map snd crenamings;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   248
    in (fixedfrees, Thm.instantiate ([], crenamings) th) end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   249
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   250
fun fix_tvars_upto_idx ix th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   251
    let 
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 20548
diff changeset
   252
      val sgn = Thm.theory_of_thm th;
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   253
      val ctypify = Thm.ctyp_of sgn
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   254
      val tpairs = Thm.terms_of_tpairs (Thm.tpairs_of th);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   255
      val prop = (Thm.prop_of th);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   256
      val tvars = List.foldr Term.add_term_tvars [] (prop :: tpairs);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   257
      val ctyfixes = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   258
          map_filter 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   259
            (fn (v as ((s,i),ty)) => 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   260
                if i <= ix then SOME (ctypify (TVar v), ctypify (TFree (s,ty)))
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   261
                else NONE) tvars;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   262
    in Thm.instantiate (ctyfixes, []) th end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   263
fun fix_vars_upto_idx ix th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   264
    let 
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 20548
diff changeset
   265
      val sgn = Thm.theory_of_thm th;
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   266
      val ctermify = Thm.cterm_of sgn
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   267
      val tpairs = Thm.terms_of_tpairs (Thm.tpairs_of th);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   268
      val prop = (Thm.prop_of th);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   269
      val vars = map Term.dest_Var (List.foldr Term.add_term_vars 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   270
                                               [] (prop :: tpairs));
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   271
      val cfixes = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   272
          map_filter 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   273
            (fn (v as ((s,i),ty)) => 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   274
                if i <= ix then SOME (ctermify (Var v), ctermify (Free (s,ty)))
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   275
                else NONE) vars;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   276
    in Thm.instantiate ([], cfixes) th end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   277
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   278
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   279
(* make free vars into schematic vars with index zero *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   280
 fun unfix_frees frees = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   281
     apply (map (K (Drule.forall_elim_var 0)) frees) 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   282
     o Drule.forall_intr_list frees;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   283
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   284
(* fix term and type variables *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   285
fun fix_vars_and_tvars th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   286
    let val (tvars, th') = fix_tvars_to_tfrees th
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   287
      val (vars, th'') = fix_vars_to_frees th' 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   288
    in ((vars, tvars), th'') end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   289
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   290
(* implicit Thm.thm argument *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   291
(* assumes: vars may contain fixed versions of the frees *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   292
(* THINK: what if vs already has types varified? *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   293
fun unfix_frees_and_tfrees (vs,tvs) = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   294
    (unfix_tfrees tvs o unfix_frees vs);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   295
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   296
(* datatype to capture an exported result, ie a fix or assume. *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   297
datatype export = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   298
         export of {fixes : Thm.cterm list, (* fixed vars *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   299
                    assumes : Thm.cterm list, (* hidden hyps/assumed prems *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   300
                    sgid : int,
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   301
                    gth :  Thm.thm}; (* subgoal/goalthm *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   302
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   303
fun fixes_of_exp (export rep) = #fixes rep;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   304
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   305
(* export the result of the new goal thm, ie if we reduced teh
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   306
subgoal, then we get a new reduced subtgoal with the old
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   307
all-quantified variables *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   308
local 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   309
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   310
(* allify puts in a meta level univ quantifier for a free variavble *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   311
fun allify_term (v, t) = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   312
    let val vt = #t (Thm.rep_cterm v)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   313
      val (n,ty) = Term.dest_Free vt
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   314
    in (Term.all ty) $ (Abs(n,ty,Term.abstract_over (vt, t))) end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   315
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   316
fun allify_for_sg_term ctermify vs t =
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   317
    let val t_alls = foldr allify_term t vs;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   318
        val ct_alls = ctermify t_alls; 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   319
    in 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   320
      (ct_alls, Drule.forall_elim_list vs (Thm.assume ct_alls))
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   321
    end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   322
(* lookup type of a free var name from a list *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   323
fun lookupfree vs vn  = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   324
    case Library.find_first (fn (n,ty) => n = vn) vs of 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   325
      NONE => error ("prepare_goal_export:lookupfree: " ^ vn ^ " does not occur in the term")
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   326
    | SOME x => x;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   327
in
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   328
fun export_back (export {fixes = vs, assumes = hprems, 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   329
                         sgid = i, gth = gth}) newth = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   330
    let 
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 20548
diff changeset
   331
      val sgn = Thm.theory_of_thm newth;
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   332
      val ctermify = Thm.cterm_of sgn;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   333
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   334
      val sgs = prems_of newth;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   335
      val (sgallcts, sgthms) = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   336
          Library.split_list (map (allify_for_sg_term ctermify vs) sgs);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   337
      val minimal_newth = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   338
          (Library.foldl (fn ( newth', sgthm) => 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   339
                          Drule.compose_single (sgthm, 1, newth'))
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   340
                      (newth, sgthms));
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   341
      val allified_newth = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   342
          minimal_newth 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   343
            |> Drule.implies_intr_list hprems
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   344
            |> Drule.forall_intr_list vs 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   345
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   346
      val newth' = Drule.implies_intr_list sgallcts allified_newth
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   347
    in
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   348
      bicompose false (false, newth', (length sgallcts)) i gth
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   349
    end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   350
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   351
(* 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   352
Given "vs" : names of free variables to abstract over,
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   353
Given cterms : premices to abstract over (P1... Pn) in terms of vs,
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   354
Given a thm of the form: 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   355
P1 vs; ...; Pn vs ==> Goal(C vs)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   356
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   357
Gives back: 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   358
(n, length of given cterms which have been allified
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   359
 [| !! vs. P1 vs; !! vs. Pn vs |] ==> !! C vs) the allified thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   360
*)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   361
(* note: C may contain further premices etc 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   362
Note that cterms is the assumed facts, ie prems of "P1" that are
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   363
reintroduced in allified form.
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   364
*)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   365
fun prepare_goal_export (vs, cterms) th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   366
    let 
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 20548
diff changeset
   367
      val sgn = Thm.theory_of_thm th;
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   368
      val ctermify = Thm.cterm_of sgn;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   369
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   370
      val allfrees = map Term.dest_Free (Term.term_frees (Thm.prop_of th))
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   371
      val cfrees = map (ctermify o Free o lookupfree allfrees) vs
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   372
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   373
      val sgs = prems_of th;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   374
      val (sgallcts, sgthms) = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   375
          Library.split_list (map (allify_for_sg_term ctermify cfrees) sgs);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   376
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   377
      val minimal_th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   378
          Goal.conclude (Library.foldl (fn ( th', sgthm) => 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   379
                          Drule.compose_single (sgthm, 1, th'))
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   380
                      (th, sgthms));
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   381
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   382
      val allified_th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   383
          minimal_th 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   384
            |> Drule.implies_intr_list cterms
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   385
            |> Drule.forall_intr_list cfrees 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   386
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   387
      val th' = Drule.implies_intr_list sgallcts allified_th
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   388
    in
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   389
      ((length sgallcts), th')
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   390
    end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   391
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   392
end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   393
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   394
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   395
(* exporting function that takes a solution to the fixed/assumed goal,
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   396
and uses this to solve the subgoal in the main theorem *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   397
fun export_solution (export {fixes = cfvs, assumes = hcprems,
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   398
                             sgid = i, gth = gth}) solth = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   399
    let 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   400
      val solth' = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   401
          solth |> Drule.implies_intr_list hcprems
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   402
                |> Drule.forall_intr_list cfvs
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   403
    in Drule.compose_single (solth', i, gth) end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   404
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   405
fun export_solutions (xs,th) = foldr (uncurry export_solution) th xs;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   406
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   407
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   408
(* fix parameters of a subgoal "i", as free variables, and create an
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   409
exporting function that will use the result of this proved goal to
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   410
show the goal in the original theorem. 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   411
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   412
Note, an advantage of this over Isar is that it supports instantiation
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   413
of unkowns in the earlier theorem, ie we can do instantiation of meta
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   414
vars! 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   415
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   416
avoids constant, free and vars names. 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   417
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   418
loosely corresponds to:
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   419
Given "[| SG0; ... !! x. As ==> SGi x; ... SGm |] ==> G" : thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   420
Result: 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   421
  ("(As ==> SGi x') ==> (As ==> SGi x')" : thm, 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   422
   expf : 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   423
     ("As ==> SGi x'" : thm) -> 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   424
     ("[| SG0; ... SGi-1; SGi+1; ... SGm |] ==> G") : thm)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   425
*)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   426
fun fix_alls_in_term alledt = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   427
    let
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   428
      val t = Term.strip_all_body alledt;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   429
      val alls = rev (Term.strip_all_vars alledt);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   430
      val varnames = map (fst o fst o Term.dest_Var) (Term.term_vars t)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   431
      val names = Term.add_term_names (t,varnames);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   432
      val fvs = map Free 
20071
8f3e1ddb50e6 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 19861
diff changeset
   433
                    (Name.variant_list names (map fst alls)
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   434
                       ~~ (map snd alls));
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   435
    in ((subst_bounds (fvs,t)), fvs) end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   436
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   437
fun fix_alls_term i t = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   438
    let 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   439
      val varnames = map (fst o fst o Term.dest_Var) (Term.term_vars t)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   440
      val names = Term.add_term_names (t,varnames);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   441
      val gt = Logic.get_goal t i;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   442
      val body = Term.strip_all_body gt;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   443
      val alls = rev (Term.strip_all_vars gt);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   444
      val fvs = map Free 
20071
8f3e1ddb50e6 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 19861
diff changeset
   445
                    (Name.variant_list names (map fst alls)
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   446
                       ~~ (map snd alls));
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   447
    in ((subst_bounds (fvs,body)), fvs) end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   448
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   449
fun fix_alls_cterm i th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   450
    let
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 20548
diff changeset
   451
      val ctermify = Thm.cterm_of (Thm.theory_of_thm th);
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   452
      val (fixedbody, fvs) = fix_alls_term i (Thm.prop_of th);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   453
      val cfvs = rev (map ctermify fvs);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   454
      val ct_body = ctermify fixedbody
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   455
    in
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   456
      (ct_body, cfvs)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   457
    end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   458
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   459
fun fix_alls' i = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   460
     (apfst Thm.trivial) o (fix_alls_cterm i);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   461
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   462
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   463
(* hide other goals *) 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   464
(* note the export goal is rotated by (i - 1) and will have to be
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   465
unrotated to get backto the originial position(s) *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   466
fun hide_other_goals th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   467
    let
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   468
      (* tl beacuse fst sg is the goal we are interested in *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   469
      val cprems = tl (Drule.cprems_of th)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   470
      val aprems = map Thm.assume cprems
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   471
    in
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   472
      (Drule.implies_elim_list (Drule.rotate_prems 1 th) aprems, 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   473
       cprems)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   474
    end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   475
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   476
(* a nicer version of the above that leaves only a single subgoal (the
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   477
other subgoals are hidden hyps, that the exporter suffles about)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   478
namely the subgoal that we were trying to solve. *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   479
(* loosely corresponds to:
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   480
Given "[| SG0; ... !! x. As ==> SGi x; ... SGm |] ==> G" : thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   481
Result: 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   482
  ("(As ==> SGi x') ==> SGi x'" : thm, 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   483
   expf : 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   484
     ("SGi x'" : thm) -> 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   485
     ("[| SG0; ... SGi-1; SGi+1; ... SGm |] ==> G") : thm)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   486
*)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   487
fun fix_alls i th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   488
    let 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   489
      val (fixed_gth, fixedvars) = fix_alls' i th
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   490
      val (sml_gth, othergoals) = hide_other_goals fixed_gth
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   491
    in
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   492
      (sml_gth, export {fixes = fixedvars, 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   493
                        assumes = othergoals, 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   494
                        sgid = i, gth = th})
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   495
    end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   496
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   497
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   498
(* assume the premises of subgoal "i", this gives back a list of
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   499
assumed theorems that are the premices of subgoal i, it also gives
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   500
back a new goal thm and an exporter, the new goalthm is as the old
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   501
one, but without the premices, and the exporter will use a proof of
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   502
the new goalthm, possibly using the assumed premices, to shoe the
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   503
orginial goal.
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   504
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   505
Note: Dealing with meta vars, need to meta-level-all them in the
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   506
shyps, which we can later instantiate with a specific value.... ? 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   507
think about this... maybe need to introduce some new fixed vars and
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   508
then remove them again at the end... like I do with rw_inst. 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   509
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   510
loosely corresponds to:
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   511
Given "[| SG0; ... [| A0; ... An |] ==> SGi; ... SGm |] ==> G" : thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   512
Result: 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   513
(["A0" [A0], ... ,"An" [An]] : thm list, -- assumptions
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   514
 "SGi ==> SGi" : thm, -- new goal 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   515
 "SGi" ["A0" ... "An"] : thm ->   -- export function
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   516
    ("[| SG0 ... SGi-1, SGi+1, SGm |] ==> G" : thm) list)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   517
*)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   518
fun assume_prems i th =
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   519
    let 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   520
      val t = (prop_of th); 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   521
      val gt = Logic.get_goal t i;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   522
      val _ = case Term.strip_all_vars gt of [] => () 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   523
              | _ => error "assume_prems: goal has params"
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   524
      val body = gt;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   525
      val prems = Logic.strip_imp_prems body;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   526
      val concl = Logic.strip_imp_concl body;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   527
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 20548
diff changeset
   528
      val sgn = Thm.theory_of_thm th;
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   529
      val ctermify = Thm.cterm_of sgn;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   530
      val cprems = map ctermify prems;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   531
      val aprems = map Thm.assume cprems;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   532
      val gthi = Thm.trivial (ctermify concl);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   533
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   534
      (* fun explortf thi = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   535
          Drule.compose (Drule.implies_intr_list cprems thi, 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   536
                         i, th) *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   537
    in
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   538
      (aprems, gthi, cprems)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   539
    end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   540
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   541
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   542
(* first fix the variables, then assume the assumptions *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   543
(* loosely corresponds to:
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   544
Given 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   545
  "[| SG0; ... 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   546
      !! xs. [| A0 xs; ... An xs |] ==> SGi xs; 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   547
      ... SGm |] ==> G" : thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   548
Result: 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   549
(["A0 xs'" [A0 xs'], ... ,"An xs'" [An xs']] : thm list, -- assumptions
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   550
 "SGi xs' ==> SGi xs'" : thm,  -- new goal 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   551
 "SGi xs'" ["A0 xs'" ... "An xs'"] : thm ->   -- export function
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   552
    ("[| SG0 ... SGi-1, SGi+1, SGm |] ==> G" : thm) list)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   553
*)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   554
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   555
(* Note: the fix_alls actually pulls through all the assumptions which
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   556
means that the second export is not needed. *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   557
fun fixes_and_assumes i th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   558
    let 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   559
      val (fixgth, exp1) = fix_alls i th
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   560
      val (assumps, goalth, _) = assume_prems 1 fixgth
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   561
    in 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   562
      (assumps, goalth, exp1)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   563
    end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   564
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   565
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   566
(* Fixme: allow different order of subgoals given to expf *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   567
(* make each subgoal into a separate thm that needs to be proved *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   568
(* loosely corresponds to:
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   569
Given 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   570
  "[| SG0; ... SGm |] ==> G" : thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   571
Result: 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   572
(["SG0 ==> SG0", ... ,"SGm ==> SGm"] : thm list, -- goals
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   573
 ["SG0", ..., "SGm"] : thm list ->   -- export function
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   574
   "G" : thm)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   575
*)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   576
fun subgoal_thms th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   577
    let 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   578
      val t = (prop_of th); 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   579
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   580
      val prems = Logic.strip_imp_prems t;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   581
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 20548
diff changeset
   582
      val sgn = Thm.theory_of_thm th;
19835
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   583
      val ctermify = Thm.cterm_of sgn;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   584
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   585
      val aprems = map (Thm.trivial o ctermify) prems;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   586
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   587
      fun explortf premths = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   588
          Drule.implies_elim_list th premths
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   589
    in
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   590
      (aprems, explortf)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   591
    end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   592
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   593
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   594
(* make all the premices of a theorem hidden, and provide an unhide
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   595
function, that will bring them back out at a later point. This is
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   596
useful if you want to get back these premices, after having used the
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   597
theorem with the premices hidden *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   598
(* loosely corresponds to:
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   599
Given "As ==> G" : thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   600
Result: ("G [As]" : thm, 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   601
         "G [As]" : thm -> "As ==> G" : thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   602
*)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   603
fun hide_prems th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   604
    let 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   605
      val cprems = Drule.cprems_of th;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   606
      val aprems = map Thm.assume cprems;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   607
    (*   val unhidef = Drule.implies_intr_list cprems; *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   608
    in
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   609
      (Drule.implies_elim_list th aprems, cprems)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   610
    end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   611
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   612
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   613
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   614
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   615
(* Fixme: allow different order of subgoals in exportf *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   616
(* as above, but also fix all parameters in all subgoals, and uses
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   617
fix_alls, not fix_alls', ie doesn't leave extra asumptions as apparent
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   618
subgoals. *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   619
(* loosely corresponds to:
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   620
Given 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   621
  "[| !! x0s. A0s x0s ==> SG0 x0s; 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   622
      ...; !! xms. Ams xms ==> SGm xms|] ==> G" : thm
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   623
Result: 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   624
(["(A0s x0s' ==> SG0 x0s') ==> SG0 x0s'", 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   625
  ... ,"(Ams xms' ==> SGm xms') ==> SGm xms'"] : thm list, -- goals
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   626
 ["SG0 x0s'", ..., "SGm xms'"] : thm list ->   -- export function
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   627
   "G" : thm)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   628
*)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   629
(* requires being given solutions! *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   630
fun fixed_subgoal_thms th = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   631
    let 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   632
      val (subgoals, expf) = subgoal_thms th;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   633
(*       fun export_sg (th, exp) = exp th; *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   634
      fun export_sgs expfs solthms = 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   635
          expf (map2 (curry (op |>)) solthms expfs);
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   636
(*           expf (map export_sg (ths ~~ expfs)); *)
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   637
    in 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   638
      apsnd export_sgs (Library.split_list (map (apsnd export_solution o 
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   639
                                                 fix_alls 1) subgoals))
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   640
    end;
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   641
81d6dc597559 added updated version of IsaPlanner and substitution.
dixon
parents:
diff changeset
   642
end;