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