src/Tools/IsaPlanner/isand.ML
author wenzelm
Thu May 30 16:53:32 2013 +0200 (2013-05-30)
changeset 52242 2d634bfa1bbf
parent 49340 25fc6e0da459
child 52244 cb15da7bd550
permissions -rw-r--r--
more standard fold/fold_rev;
     1 (*  Title:      Tools/IsaPlanner/isand.ML
     2     Author:     Lucas Dixon, University of Edinburgh
     3 
     4 Natural Deduction tools.
     5 
     6 For working with Isabelle theorems in a natural detuction style.
     7 ie, not having to deal with meta level quantified varaibles,
     8 instead, we work with newly introduced frees, and hide the
     9 "all"'s, exporting results from theorems proved with the frees, to
    10 solve the all cases of the previous goal. This allows resolution
    11 to do proof search normally.
    12 
    13 Note: A nice idea: allow exporting to solve any subgoal, thus
    14 allowing the interleaving of proof, or provide a structure for the
    15 ordering of proof, thus allowing proof attempts in parrell, but
    16 recording the order to apply things in.
    17 
    18 THINK: are we really ok with our varify name w.r.t the prop - do
    19 we also need to avoid names in the hidden hyps? What about
    20 unification contraints in flex-flex pairs - might they also have
    21 extra free vars?
    22 *)
    23 
    24 signature ISA_ND =
    25 sig
    26   (* inserting meta level params for frees in the conditions *)
    27   val allify_conditions : (term -> cterm) -> (string * typ) list -> thm -> thm * cterm list
    28 
    29   val variant_names : Proof.context -> term list -> string list -> string list
    30 
    31   (* meta level fixed params (i.e. !! vars) *)
    32   val fix_alls_term : Proof.context -> int -> term -> term * term list
    33 
    34   val unfix_frees : cterm list -> thm -> thm
    35 
    36   (* assumptions/subgoals *)
    37   val fixed_subgoal_thms : Proof.context -> thm -> thm list * (thm list -> thm)
    38 end
    39 
    40 structure IsaND : ISA_ND =
    41 struct
    42 
    43 (* Given ctertmify function, (string,type) pairs capturing the free
    44 vars that need to be allified in the assumption, and a theorem with
    45 assumptions possibly containing the free vars, then we give back the
    46 assumptions allified as hidden hyps.
    47 
    48 Given: x
    49 th: A vs ==> B vs
    50 Results in: "B vs" [!!x. A x]
    51 *)
    52 fun allify_conditions ctermify Ts th =
    53     let
    54       val premts = Thm.prems_of th;
    55 
    56       fun allify_prem_var (vt as (n,ty)) t =
    57           (Logic.all_const ty) $ (Abs(n,ty,Term.abstract_over (Free vt, t)))
    58 
    59       val allify_prem = fold_rev allify_prem_var
    60 
    61       val cTs = map (ctermify o Free) Ts
    62       val cterm_asms = map (ctermify o allify_prem Ts) premts
    63       val allifyied_asm_thms = map (Drule.forall_elim_list cTs o Thm.assume) cterm_asms
    64     in (fold (curry op COMP) allifyied_asm_thms th, cterm_asms) end;
    65 
    66 (* make free vars into schematic vars with index zero *)
    67 fun unfix_frees frees =
    68      fold (K (Thm.forall_elim_var 0)) frees
    69      o Drule.forall_intr_list frees;
    70 
    71 (* datatype to capture an exported result, ie a fix or assume. *)
    72 datatype export =
    73          export of {fixes : Thm.cterm list, (* fixed vars *)
    74                     assumes : Thm.cterm list, (* hidden hyps/assumed prems *)
    75                     sgid : int,
    76                     gth :  Thm.thm}; (* subgoal/goalthm *)
    77 
    78 (* exporting function that takes a solution to the fixed/assumed goal,
    79 and uses this to solve the subgoal in the main theorem *)
    80 fun export_solution (export {fixes = cfvs, assumes = hcprems,
    81                              sgid = i, gth = gth}) solth =
    82     let
    83       val solth' =
    84           solth |> Drule.implies_intr_list hcprems
    85                 |> Drule.forall_intr_list cfvs
    86     in Drule.compose_single (solth', i, gth) end;
    87 
    88 fun variant_names ctxt ts xs =
    89   let
    90     val names =
    91       Variable.names_of ctxt
    92       |> (fold o fold_aterms)
    93           (fn Var ((a, _), _) => Name.declare a
    94             | Free (a, _) => Name.declare a
    95             | _ => I) ts;
    96   in fst (fold_map Name.variant xs names) end;
    97 
    98 (* fix parameters of a subgoal "i", as free variables, and create an
    99 exporting function that will use the result of this proved goal to
   100 show the goal in the original theorem.
   101 
   102 Note, an advantage of this over Isar is that it supports instantiation
   103 of unkowns in the earlier theorem, ie we can do instantiation of meta
   104 vars!
   105 
   106 avoids constant, free and vars names.
   107 
   108 loosely corresponds to:
   109 Given "[| SG0; ... !! x. As ==> SGi x; ... SGm |] ==> G" : thm
   110 Result:
   111   ("(As ==> SGi x') ==> (As ==> SGi x')" : thm,
   112    expf :
   113      ("As ==> SGi x'" : thm) ->
   114      ("[| SG0; ... SGi-1; SGi+1; ... SGm |] ==> G") : thm)
   115 *)
   116 fun fix_alls_term ctxt i t =
   117     let
   118       val gt = Logic.get_goal t i;
   119       val body = Term.strip_all_body gt;
   120       val alls = rev (Term.strip_all_vars gt);
   121       val xs = variant_names ctxt [t] (map fst alls);
   122       val fvs = map Free (xs ~~ map snd alls);
   123     in ((subst_bounds (fvs,body)), fvs) end;
   124 
   125 fun fix_alls_cterm ctxt i th =
   126     let
   127       val ctermify = Thm.cterm_of (Thm.theory_of_thm th);
   128       val (fixedbody, fvs) = fix_alls_term ctxt i (Thm.prop_of th);
   129       val cfvs = rev (map ctermify fvs);
   130       val ct_body = ctermify fixedbody
   131     in
   132       (ct_body, cfvs)
   133     end;
   134 
   135 fun fix_alls' ctxt i = apfst Thm.trivial o fix_alls_cterm ctxt i;
   136 
   137 
   138 (* hide other goals *)
   139 (* note the export goal is rotated by (i - 1) and will have to be
   140 unrotated to get backto the originial position(s) *)
   141 fun hide_other_goals th =
   142     let
   143       (* tl beacuse fst sg is the goal we are interested in *)
   144       val cprems = tl (Drule.cprems_of th)
   145       val aprems = map Thm.assume cprems
   146     in
   147       (Drule.implies_elim_list (Drule.rotate_prems 1 th) aprems,
   148        cprems)
   149     end;
   150 
   151 (* a nicer version of the above that leaves only a single subgoal (the
   152 other subgoals are hidden hyps, that the exporter suffles about)
   153 namely the subgoal that we were trying to solve. *)
   154 (* loosely corresponds to:
   155 Given "[| SG0; ... !! x. As ==> SGi x; ... SGm |] ==> G" : thm
   156 Result:
   157   ("(As ==> SGi x') ==> SGi x'" : thm,
   158    expf :
   159      ("SGi x'" : thm) ->
   160      ("[| SG0; ... SGi-1; SGi+1; ... SGm |] ==> G") : thm)
   161 *)
   162 fun fix_alls ctxt i th =
   163     let
   164       val (fixed_gth, fixedvars) = fix_alls' ctxt i th
   165       val (sml_gth, othergoals) = hide_other_goals fixed_gth
   166     in
   167       (sml_gth, export {fixes = fixedvars,
   168                         assumes = othergoals,
   169                         sgid = i, gth = th})
   170     end;
   171 
   172 
   173 (* Fixme: allow different order of subgoals given to expf *)
   174 (* make each subgoal into a separate thm that needs to be proved *)
   175 (* loosely corresponds to:
   176 Given
   177   "[| SG0; ... SGm |] ==> G" : thm
   178 Result:
   179 (["SG0 ==> SG0", ... ,"SGm ==> SGm"] : thm list, -- goals
   180  ["SG0", ..., "SGm"] : thm list ->   -- export function
   181    "G" : thm)
   182 *)
   183 fun subgoal_thms th =
   184     let
   185       val t = (prop_of th);
   186 
   187       val prems = Logic.strip_imp_prems t;
   188 
   189       val sgn = Thm.theory_of_thm th;
   190       val ctermify = Thm.cterm_of sgn;
   191 
   192       val aprems = map (Thm.trivial o ctermify) prems;
   193 
   194       fun explortf premths =
   195           Drule.implies_elim_list th premths
   196     in
   197       (aprems, explortf)
   198     end;
   199 
   200 
   201 (* Fixme: allow different order of subgoals in exportf *)
   202 (* as above, but also fix all parameters in all subgoals, and uses
   203 fix_alls, not fix_alls', ie doesn't leave extra asumptions as apparent
   204 subgoals. *)
   205 (* loosely corresponds to:
   206 Given
   207   "[| !! x0s. A0s x0s ==> SG0 x0s;
   208       ...; !! xms. Ams xms ==> SGm xms|] ==> G" : thm
   209 Result:
   210 (["(A0s x0s' ==> SG0 x0s') ==> SG0 x0s'",
   211   ... ,"(Ams xms' ==> SGm xms') ==> SGm xms'"] : thm list, -- goals
   212  ["SG0 x0s'", ..., "SGm xms'"] : thm list ->   -- export function
   213    "G" : thm)
   214 *)
   215 (* requires being given solutions! *)
   216 fun fixed_subgoal_thms ctxt th =
   217     let
   218       val (subgoals, expf) = subgoal_thms th;
   219 (*       fun export_sg (th, exp) = exp th; *)
   220       fun export_sgs expfs solthms =
   221           expf (map2 (curry (op |>)) solthms expfs);
   222 (*           expf (map export_sg (ths ~~ expfs)); *)
   223     in
   224       apsnd export_sgs (Library.split_list (map (apsnd export_solution o
   225                                                  fix_alls ctxt 1) subgoals))
   226     end;
   227 
   228 end;