src/Pure/IsaPlanner/isand.ML
changeset 15854 1ae0a47dcccd
parent 15574 b1d1b5bfc464
child 15915 b0e8b37642a4
equal deleted inserted replaced
15853:68b615bc110e 15854:1ae0a47dcccd
     1 (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *) 
     1 (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *) 
     2 (*  Title:      sys/isand.ML
     2 (*  Title:      isand.ML
     3     Author:     Lucas Dixon, University of Edinburgh
     3     Author:     Lucas Dixon, University of Edinburgh
     4                 lucas.dixon@ed.ac.uk
     4                 lucas.dixon@ed.ac.uk
       
     5     Updated:    26 Apr 2005
     5     Date:       6 Aug 2004
     6     Date:       6 Aug 2004
     6 *)
     7 *)
     7 (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *) 
     8 (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *) 
     8 (*  DESCRIPTION:
     9 (*  DESCRIPTION:
     9 
    10 
    10     Natural Deduction tools
    11     Natural Deduction tools
    11 
    12 
    12     For working with Isbaelle theorem in a natural detuction style,
    13     For working with Isabelle theorems in a natural detuction style.
    13     ie, not having to deal with meta level quantified varaibles,
    14     ie, not having to deal with meta level quantified varaibles,
    14     instead, we work with newly introduced frees, and hide the
    15     instead, we work with newly introduced frees, and hide the
    15     "all"'s, exporting results from theorems proved with the frees, to
    16     "all"'s, exporting results from theorems proved with the frees, to
    16     solve the all cases of the previous goal. 
    17     solve the all cases of the previous goal. This allows resolution
    17 
    18     to do proof search normally. 
    18     Note: A nice idea: allow esxporting to solve any subgoal, thus
    19 
       
    20     Note: A nice idea: allow exporting to solve any subgoal, thus
    19     allowing the interleaving of proof, or provide a structure for the
    21     allowing the interleaving of proof, or provide a structure for the
    20     ordering of proof, thus allowing proof attempts in parrelle, but
    22     ordering of proof, thus allowing proof attempts in parrell, but
    21     recording the order to apply things in.
    23     recording the order to apply things in.
    22 
    24 
    23     debugging tools:
    25     debugging tools:
    24 
    26 
    25     fun asm_mk t = (assume (cterm_of (Theory.sign_of (the_context())) t)); 
    27     fun asm_mk t = (assume (cterm_of (Theory.sign_of (the_context())) t)); 
    26     fun asm_read s =  
    28     fun asm_read s =  
    27       (assume (read_cterm (Theory.sign_of (Context.the_context())) (s,propT))); 
    29       (assume (read_cterm (Theory.sign_of (Context.the_context())) (s,propT)));
    28 
    30 
    29     THINK: are we really ok with our varify name w.r.t the prop - do
    31     THINK: are we really ok with our varify name w.r.t the prop - do
    30     we alos need to avoid names in the hidden hyps?
    32     we also need to avoid names in the hidden hyps? What about
       
    33     unification contraints in flex-flex pairs - might they also have
       
    34     extra free vars?
    31 *)
    35 *)
    32 
    36 
    33 signature ISA_ND =
    37 signature ISA_ND =
    34 sig
    38 sig
       
    39 
       
    40   (* export data *)
    35   datatype export = export of
    41   datatype export = export of
    36            {gth: Thm.thm, (* initial goal theorem *)
    42            {gth: Thm.thm, (* initial goal theorem *)
    37             sgid: int, (* subgoal id which has been fixed etc *)
    43             sgid: int, (* subgoal id which has been fixed etc *)
    38             fixes: Thm.cterm list, (* frees *)
    44             fixes: Thm.cterm list, (* frees *)
    39             assumes: Thm.cterm list} (* assumptions *)
    45             assumes: Thm.cterm list} (* assumptions *)
    40 
       
    41   val fixes_of_exp : export -> Thm.cterm list
    46   val fixes_of_exp : export -> Thm.cterm list
    42 
       
    43   val export_back : export -> Thm.thm -> Thm.thm Seq.seq
    47   val export_back : export -> Thm.thm -> Thm.thm Seq.seq
    44   val export_solution : export -> Thm.thm -> Thm.thm
    48   val export_solution : export -> Thm.thm -> Thm.thm
    45   val export_solutions : export list * Thm.thm -> Thm.thm
    49   val export_solutions : export list * Thm.thm -> Thm.thm
    46 
    50 
       
    51   (* inserting meta level params for frees in the conditions *)
    47   val allify_conditions :
    52   val allify_conditions :
    48       (Term.term -> Thm.cterm) ->
    53       (Term.term -> Thm.cterm) ->
    49       (string * Term.typ) list -> Thm.thm -> Thm.thm * Thm.cterm list
    54       (string * Term.typ) list -> Thm.thm -> Thm.thm * Thm.cterm list
    50   val allify_conditions' :
    55   val allify_conditions' :
    51       (string * Term.typ) list -> Thm.thm -> Thm.thm * Thm.cterm list
    56       (string * Term.typ) list -> Thm.thm -> Thm.thm * Thm.cterm list
    52 
    57 
    53   val assume_prems :
    58   (* meta level fixed params (i.e. !! vars) *)
    54       int -> Thm.thm -> Thm.thm list * Thm.thm * Thm.cterm list
       
    55 
       
    56   val fix_alls_term : int -> Term.term -> Term.term * Term.term list
    59   val fix_alls_term : int -> Term.term -> Term.term * Term.term list
    57   val fix_alls_cterm : int -> Thm.thm -> Thm.cterm * Thm.cterm list
    60   val fix_alls_cterm : int -> Thm.thm -> Thm.cterm * Thm.cterm list
    58   val fix_alls' : int -> Thm.thm -> Thm.thm * Thm.cterm list
    61   val fix_alls' : int -> Thm.thm -> Thm.thm * Thm.cterm list
    59   val fix_alls : int -> Thm.thm -> Thm.thm * export
    62   val fix_alls : int -> Thm.thm -> Thm.thm * export
    60 
    63 
    61   val fix_vars_to_frees : Thm.thm -> (Thm.cterm * Thm.cterm) list * Thm.thm
    64   (* meta variables in types and terms *)
    62   val schemify_frees_to_vars : Thm.cterm list -> Thm.thm -> Thm.thm
    65   val fix_tvars_to_tfrees : Thm.thm -> Thm.ctyp list * Thm.thm
    63 
    66   val fix_vars_to_frees : Thm.thm -> Thm.cterm list * Thm.thm
       
    67   val fix_vars_and_tvars : 
       
    68       Thm.thm -> (Thm.cterm list * Thm.ctyp list) * Thm.thm
       
    69   val unfix_frees : Thm.cterm list -> Thm.thm -> Thm.thm
       
    70   val unfix_tfrees : Thm.ctyp list -> Thm.thm -> Thm.thm
       
    71   val unfix_frees_and_tfrees :
       
    72       (Thm.cterm list * Thm.ctyp list) -> Thm.thm -> Thm.thm
       
    73 
       
    74   (* assumptions/subgoals *)
       
    75   val assume_prems :
       
    76       int -> Thm.thm -> Thm.thm list * Thm.thm * Thm.cterm list
    64   val fixed_subgoal_thms : Thm.thm -> Thm.thm list * (Thm.thm list -> Thm.thm)
    77   val fixed_subgoal_thms : Thm.thm -> Thm.thm list * (Thm.thm list -> Thm.thm)
    65   val fixes_and_assumes : int -> Thm.thm -> Thm.thm list * Thm.thm * export
    78   val fixes_and_assumes : int -> Thm.thm -> Thm.thm list * Thm.thm * export
    66 
       
    67   val hide_other_goals : Thm.thm -> Thm.thm * Thm.cterm list
    79   val hide_other_goals : Thm.thm -> Thm.thm * Thm.cterm list
    68   val hide_prems : Thm.thm -> Thm.thm * Thm.cterm list
    80   val hide_prems : Thm.thm -> Thm.thm * Thm.cterm list
    69 
    81 
    70   (* abstracts cterms (vars) to locally meta-all bounds *)
    82   (* abstracts cterms (vars) to locally meta-all bounds *)
    71   val prepare_goal_export : string list * Thm.cterm list -> Thm.thm 
    83   val prepare_goal_export : string list * Thm.cterm list -> Thm.thm 
    73   val solve_with : Thm.thm -> Thm.thm -> Thm.thm Seq.seq
    85   val solve_with : Thm.thm -> Thm.thm -> Thm.thm Seq.seq
    74   val subgoal_thms : Thm.thm -> Thm.thm list * (Thm.thm list -> Thm.thm)
    86   val subgoal_thms : Thm.thm -> Thm.thm list * (Thm.thm list -> Thm.thm)
    75 end
    87 end
    76 
    88 
    77 
    89 
    78 structure IsaND : ISA_ND =
    90 structure IsaND 
    79 struct
    91 : ISA_ND
       
    92 = struct
    80 
    93 
    81 (* Solve *some* subgoal of "th" directly by "sol" *)
    94 (* Solve *some* subgoal of "th" directly by "sol" *)
    82 (* Note: this is probably what Markus ment to do upon export of a
    95 (* Note: this is probably what Markus ment to do upon export of a
    83 "show" but maybe he used RS/rtac instead, which would wrongly lead to
    96 "show" but maybe he used RS/rtac instead, which would wrongly lead to
    84 failing if there are premices to the shown goal. *)
    97 failing if there are premices to the shown goal. 
       
    98 
       
    99 given: 
       
   100 sol : Thm.thm = [| Ai... |] ==> Ci
       
   101 th : Thm.thm = 
       
   102   [| ... [| Ai... |] ==> Ci ... |] ==> G
       
   103 results in: 
       
   104   [| ... [| Ai-1... |] ==> Ci-1
       
   105     [| Ai+1... |] ==> Ci+1 ...
       
   106   |] ==> G
       
   107 i.e. solves some subgoal of th that is identical to sol. 
       
   108 *)
    85 fun solve_with sol th = 
   109 fun solve_with sol th = 
    86     let fun solvei 0 = Seq.empty
   110     let fun solvei 0 = Seq.empty
    87           | solvei i = 
   111           | solvei i = 
    88             Seq.append (bicompose false (false,sol,0) i th, 
   112             Seq.append (bicompose false (false,sol,0) i th, 
    89                         solvei (i - 1))
   113                         solvei (i - 1))
    93 
   117 
    94 
   118 
    95 (* Given ctertmify function, (string,type) pairs capturing the free
   119 (* Given ctertmify function, (string,type) pairs capturing the free
    96 vars that need to be allified in the assumption, and a theorem with
   120 vars that need to be allified in the assumption, and a theorem with
    97 assumptions possibly containing the free vars, then we give back the
   121 assumptions possibly containing the free vars, then we give back the
    98 assumptions allified as hidden hyps. *)
   122 assumptions allified as hidden hyps. 
    99 (* 
   123 
   100 Given: vs 
   124 Given: x 
   101 th: A vs ==> B vs 
   125 th: A vs ==> B vs 
   102 Results in: "B vs" [!!x. A x]
   126 Results in: "B vs" [!!x. A x]
   103 *)
   127 *)
   104 fun allify_conditions ctermify Ts th = 
   128 fun allify_conditions ctermify Ts th = 
   105     let 
   129     let 
   119 
   143 
   120 fun allify_conditions' Ts th = 
   144 fun allify_conditions' Ts th = 
   121     allify_conditions (Thm.cterm_of (Thm.sign_of_thm th)) Ts th;
   145     allify_conditions (Thm.cterm_of (Thm.sign_of_thm th)) Ts th;
   122 
   146 
   123 
   147 
       
   148 
       
   149 (* These should be defined in term.ML *)
       
   150 fun dest_TVar (TVar x) = x
       
   151   | dest_TVar T = raise TYPE ("dest_TVar", [T], []);
       
   152 fun dest_TFree (TFree x) = x
       
   153   | dest_TFree T = raise TYPE ("dest_TFree", [T], []);
       
   154 
       
   155 
       
   156 (* change type-vars to fresh type frees *)
       
   157 fun fix_tvars_to_tfrees th = 
       
   158     let 
       
   159       val sign = Thm.sign_of_thm th
       
   160       val ctermify = Thm.cterm_of sign
       
   161       val ctypify = Thm.ctyp_of sign
       
   162 
       
   163       val prop = Thm.prop_of th;
       
   164       val tfree_names = map fst (Term.add_term_tfrees (prop,[]));
       
   165       val tvars = Term.add_term_tvars (prop, []);
       
   166 
       
   167       val (names',renamings) = 
       
   168           List.foldr (fn (tv as ((n,i),s),(Ns,Rs)) => 
       
   169                          let val n2 = Term.variant Ns n in 
       
   170                            (n2::Ns, (ctypify (TVar tv),
       
   171                                      ctypify (TFree (n2,s)))::Rs)
       
   172                          end) (tfree_names,[]) tvars;
       
   173 
       
   174       val fixedfrees = map snd renamings;
       
   175     in
       
   176       (fixedfrees, Thm.instantiate (renamings, []) th)
       
   177     end;
       
   178 
       
   179 (* change type-free's to type-vars *)
       
   180 fun unfix_tfrees ns th = 
       
   181     fst (Thm.varifyT' (map (fn x => dest_TFree (Thm.typ_of x)) ns) th);
   124 
   182 
   125 (* change schematic vars to fresh free vars *)
   183 (* change schematic vars to fresh free vars *)
   126 fun fix_vars_to_frees th = 
   184 fun fix_vars_to_frees th = 
   127     let 
   185     let 
   128       val ctermify = Thm.cterm_of (Thm.sign_of_thm th)
   186       val ctermify = Thm.cterm_of (Thm.sign_of_thm th)
   136                     let val n2 = Term.variant names n in
   194                     let val n2 = Term.variant names n in
   137                       ((ctermify (Var v), ctermify (Free(n2,ty))) :: insts, 
   195                       ((ctermify (Var v), ctermify (Free(n2,ty))) :: insts, 
   138                        n2 :: names)
   196                        n2 :: names)
   139                     end)
   197                     end)
   140                 (([],names), vars)
   198                 (([],names), vars)
   141     in (insts, Thm.instantiate ([], insts) th) end;
   199       val fixedfrees = map snd insts;
       
   200     in (fixedfrees, Thm.instantiate ([], insts) th) end;
   142 
   201 
   143 (* make free vars into schematic vars with index zero *)
   202 (* make free vars into schematic vars with index zero *)
   144  fun schemify_frees_to_vars frees = 
   203  fun unfix_frees frees = 
   145      apply (map (K (Drule.forall_elim_var 0)) frees) 
   204      apply (map (K (Drule.forall_elim_var 0)) frees) 
   146      o Drule.forall_intr_list frees;
   205      o Drule.forall_intr_list frees;
   147 
   206 
       
   207 (* fix term and type variables *)
       
   208 fun fix_vars_and_tvars th = 
       
   209     let val (tvars, th') = fix_tvars_to_tfrees th
       
   210       val (vars, th'') = fix_vars_to_frees th' 
       
   211     in ((vars, tvars), th'') end;
       
   212 
       
   213 (* implicit Thm.thm argument *)
       
   214 fun unfix_frees_and_tfrees (vs,tvs) = 
       
   215     (unfix_frees vs o unfix_tfrees tvs);
   148 
   216 
   149 (* datatype to capture an exported result, ie a fix or assume. *)
   217 (* datatype to capture an exported result, ie a fix or assume. *)
   150 datatype export = 
   218 datatype export = 
   151          export of {fixes : Thm.cterm list, (* fixed vars *)
   219          export of {fixes : Thm.cterm list, (* fixed vars *)
   152                     assumes : Thm.cterm list, (* hidden hyps/assumed prems *)
   220                     assumes : Thm.cterm list, (* hidden hyps/assumed prems *)