src/Pure/proofterm.ML
changeset 71018 d32ed8927a42
parent 71010 be689b7d81fd
child 71022 6504ea98623c
equal deleted inserted replaced
71017:c85efa2be619 71018:d32ed8927a42
    44   val thm_node_name: thm_node -> string
    44   val thm_node_name: thm_node -> string
    45   val thm_node_prop: thm_node -> term
    45   val thm_node_prop: thm_node -> term
    46   val thm_node_body: thm_node -> proof_body future
    46   val thm_node_body: thm_node -> proof_body future
    47   val thm_node_thms: thm_node -> thm list
    47   val thm_node_thms: thm_node -> thm list
    48   val join_thms: thm list -> proof_body list
    48   val join_thms: thm list -> proof_body list
    49   val consolidate: proof_body list -> unit
       
    50   val make_thm: thm_header -> thm_body -> thm
    49   val make_thm: thm_header -> thm_body -> thm
    51   val fold_proof_atoms: bool -> (proof -> 'a -> 'a) -> proof list -> 'a -> 'a
    50   val fold_proof_atoms: bool -> (proof -> 'a -> 'a) -> proof list -> 'a -> 'a
    52   val fold_body_thms:
    51   val fold_body_thms:
    53     ({serial: serial, name: string, prop: term, body: proof_body} -> 'a -> 'a) ->
    52     ({serial: serial, name: string, prop: term, body: proof_body} -> 'a -> 'a) ->
    54     proof_body list -> 'a -> 'a
    53     proof_body list -> 'a -> 'a
   174   val add_standard_vars_term: term -> (string * typ) list -> (string * typ) list
   173   val add_standard_vars_term: term -> (string * typ) list -> (string * typ) list
   175 
   174 
   176   val export_enabled: unit -> bool
   175   val export_enabled: unit -> bool
   177   val export_standard_enabled: unit -> bool
   176   val export_standard_enabled: unit -> bool
   178   val export_proof_boxes_required: theory -> bool
   177   val export_proof_boxes_required: theory -> bool
   179   val export_proof_boxes: proof list -> unit
   178   val export_proof_boxes: proof_body list -> unit
   180   val fulfill_norm_proof: theory -> (serial * proof_body) list -> proof_body -> proof_body
   179   val fulfill_norm_proof: theory -> (serial * proof_body) list -> proof_body -> proof_body
   181   val thm_proof: theory -> (class * class -> proof) ->
   180   val thm_proof: theory -> (class * class -> proof) ->
   182     (string * class list list * class -> proof) -> string * Position.T -> sort list ->
   181     (string * class list list * class -> proof) -> string * Position.T -> sort list ->
   183     term list -> term -> (serial * proof_body future) list -> proof_body -> thm * proof
   182     term list -> term -> (serial * proof_body future) list -> proof_body -> thm * proof
   184   val unconstrain_thm_proof: theory -> (class * class -> proof) ->
   183   val unconstrain_thm_proof: theory -> (class * class -> proof) ->
   221 and proof_body = PBody of
   220 and proof_body = PBody of
   222   {oracles: (string * term option) Ord_List.T,
   221   {oracles: (string * term option) Ord_List.T,
   223    thms: (serial * thm_node) Ord_List.T,
   222    thms: (serial * thm_node) Ord_List.T,
   224    proof: proof}
   223    proof: proof}
   225 and thm_body =
   224 and thm_body =
   226   Thm_Body of {export_proof: unit lazy, open_proof: proof -> proof, body: proof_body future}
   225   Thm_Body of {open_proof: proof -> proof, body: proof_body future}
   227 and thm_node =
   226 and thm_node =
   228   Thm_Node of {theory_name: string, name: string, prop: term,
   227   Thm_Node of {theory_name: string, name: string, prop: term,
   229     body: proof_body future, consolidate: unit lazy};
   228     body: proof_body future, export: unit lazy, consolidate: unit lazy};
   230 
   229 
   231 type oracle = string * term option;
   230 type oracle = string * term option;
   232 val oracle_ord = prod_ord fast_string_ord (option_ord Term_Ord.fast_term_ord);
   231 val oracle_ord = prod_ord fast_string_ord (option_ord Term_Ord.fast_term_ord);
   233 
   232 
   234 type thm = serial * thm_node;
   233 type thm = serial * thm_node;
   244   PBody {oracles = oracles, thms = thms, proof = f proof};
   243   PBody {oracles = oracles, thms = thms, proof = f proof};
   245 
   244 
   246 fun thm_header serial pos theory_name name prop types : thm_header =
   245 fun thm_header serial pos theory_name name prop types : thm_header =
   247   {serial = serial, pos = pos, theory_name = theory_name, name = name, prop = prop, types = types};
   246   {serial = serial, pos = pos, theory_name = theory_name, name = name, prop = prop, types = types};
   248 
   247 
   249 val no_export_proof = Lazy.value ();
   248 fun thm_body body = Thm_Body {open_proof = I, body = Future.value body};
   250 
       
   251 fun thm_body body =
       
   252   Thm_Body {export_proof = no_export_proof, open_proof = I, body = Future.value body};
       
   253 fun thm_body_export_proof (Thm_Body {export_proof, ...}) = export_proof;
       
   254 fun thm_body_proof_raw (Thm_Body {body, ...}) = join_proof body;
   249 fun thm_body_proof_raw (Thm_Body {body, ...}) = join_proof body;
   255 fun thm_body_proof_open (Thm_Body {open_proof, body, ...}) = open_proof (join_proof body);
   250 fun thm_body_proof_open (Thm_Body {open_proof, body, ...}) = open_proof (join_proof body);
   256 
   251 
   257 fun rep_thm_node (Thm_Node args) = args;
   252 fun rep_thm_node (Thm_Node args) = args;
   258 val thm_node_theory_name = #theory_name o rep_thm_node;
   253 val thm_node_theory_name = #theory_name o rep_thm_node;
   259 val thm_node_name = #name o rep_thm_node;
   254 val thm_node_name = #name o rep_thm_node;
   260 val thm_node_prop = #prop o rep_thm_node;
   255 val thm_node_prop = #prop o rep_thm_node;
   261 val thm_node_body = #body o rep_thm_node;
   256 val thm_node_body = #body o rep_thm_node;
   262 val thm_node_thms = thm_node_body #> Future.join #> (fn PBody {thms, ...} => thms);
   257 val thm_node_thms = thm_node_body #> Future.join #> (fn PBody {thms, ...} => thms);
       
   258 val thm_node_export = #export o rep_thm_node;
   263 val thm_node_consolidate = #consolidate o rep_thm_node;
   259 val thm_node_consolidate = #consolidate o rep_thm_node;
   264 
   260 
   265 fun join_thms (thms: thm list) =
   261 fun join_thms (thms: thm list) =
   266   Future.joins (map (thm_node_body o #2) thms);
   262   Future.joins (map (thm_node_body o #2) thms);
   267 
   263 
   268 val consolidate =
   264 val consolidate_bodies =
   269   maps (fn PBody {thms, ...} => map (thm_node_consolidate o #2) thms)
   265   maps (fn PBody {thms, ...} => map (thm_node_consolidate o #2) thms)
   270   #> Lazy.consolidate #> map Lazy.force #> ignore;
   266   #> Lazy.consolidate #> map Lazy.force #> ignore;
   271 
   267 
   272 fun make_thm_node theory_name name prop body =
   268 fun make_thm_node theory_name name prop body export =
   273   Thm_Node {theory_name = theory_name, name = name, prop = prop, body = body,
   269   let
   274     consolidate =
   270     val body' = body
       
   271       |> Options.default_bool "prune_proofs" ? (Future.map o map_proof_of) (K MinProof);
       
   272     val consolidate =
   275       Lazy.lazy_name "Proofterm.make_thm_node" (fn () =>
   273       Lazy.lazy_name "Proofterm.make_thm_node" (fn () =>
   276         let val PBody {thms, ...} = Future.join body
   274         let val PBody {thms, ...} = Future.join body'
   277         in consolidate (join_thms thms) end)};
   275         in consolidate_bodies (join_thms thms) end);
       
   276   in
       
   277     Thm_Node {theory_name = theory_name, name = name, prop = prop, body = body',
       
   278       export = export, consolidate = consolidate}
       
   279   end;
       
   280 
       
   281 val no_export = Lazy.value ();
   278 
   282 
   279 fun make_thm ({serial, theory_name, name, prop, ...}: thm_header) (Thm_Body {body, ...}) =
   283 fun make_thm ({serial, theory_name, name, prop, ...}: thm_header) (Thm_Body {body, ...}) =
   280   (serial, make_thm_node theory_name name prop body);
   284   (serial, make_thm_node theory_name name prop body no_export);
   281 
   285 
   282 
   286 
   283 (* proof atoms *)
   287 (* proof atoms *)
   284 
   288 
   285 fun fold_proof_atoms all f =
   289 fun fold_proof_atoms all f =
   337 
   341 
   338 fun no_body_proofs (Abst (x, T, prf)) = Abst (x, T, no_body_proofs prf)
   342 fun no_body_proofs (Abst (x, T, prf)) = Abst (x, T, no_body_proofs prf)
   339   | no_body_proofs (AbsP (x, t, prf)) = AbsP (x, t, no_body_proofs prf)
   343   | no_body_proofs (AbsP (x, t, prf)) = AbsP (x, t, no_body_proofs prf)
   340   | no_body_proofs (prf % t) = no_body_proofs prf % t
   344   | no_body_proofs (prf % t) = no_body_proofs prf % t
   341   | no_body_proofs (prf1 %% prf2) = no_body_proofs prf1 %% no_body_proofs prf2
   345   | no_body_proofs (prf1 %% prf2) = no_body_proofs prf1 %% no_body_proofs prf2
   342   | no_body_proofs (PThm (header, Thm_Body {export_proof, open_proof, body})) =
   346   | no_body_proofs (PThm (header, Thm_Body {open_proof, body})) =
   343       let
   347       let
   344         val body' = Future.value (no_proof_body (join_proof body));
   348         val body' = Future.value (no_proof_body (join_proof body));
   345         val thm_body' = Thm_Body {export_proof = export_proof, open_proof = open_proof, body = body'};
   349         val thm_body' = Thm_Body {open_proof = open_proof, body = body'};
   346       in PThm (header, thm_body') end
   350       in PThm (header, thm_body') end
   347   | no_body_proofs a = a;
   351   | no_body_proofs a = a;
   348 
   352 
   349 
   353 
   350 
   354 
   441   in PBody {oracles = a, thms = b, proof = c} end
   445   in PBody {oracles = a, thms = b, proof = c} end
   442 and thm consts x =
   446 and thm consts x =
   443   let
   447   let
   444     val (a, (b, (c, (d, e)))) =
   448     val (a, (b, (c, (d, e)))) =
   445       pair int (pair string (pair string (pair (term consts) (proof_body consts)))) x
   449       pair int (pair string (pair string (pair (term consts) (proof_body consts)))) x
   446   in (a, make_thm_node b c d (Future.value e)) end;
   450   in (a, make_thm_node b c d (Future.value e) no_export) end;
   447 
   451 
   448 in
   452 in
   449 
   453 
   450 val decode = proof;
   454 val decode = proof;
   451 val decode_body = proof_body;
   455 val decode_body = proof_body;
  1957 
  1961 
  1958 (** promises **)
  1962 (** promises **)
  1959 
  1963 
  1960 fun fulfill_norm_proof thy ps body0 =
  1964 fun fulfill_norm_proof thy ps body0 =
  1961   let
  1965   let
  1962     val _ = consolidate (map #2 ps @ [body0]);
  1966     val _ = consolidate_bodies (map #2 ps @ [body0]);
  1963     val PBody {oracles = oracles0, thms = thms0, proof = proof0} = body0;
  1967     val PBody {oracles = oracles0, thms = thms0, proof = proof0} = body0;
  1964     val oracles =
  1968     val oracles =
  1965       unions_oracles
  1969       unions_oracles
  1966         (fold (fn (_, PBody {oracles, ...}) => not (null oracles) ? cons oracles) ps [oracles0]);
  1970         (fold (fn (_, PBody {oracles, ...}) => not (null oracles) ? cons oracles) ps [oracles0]);
  1967     val thms =
  1971     val thms =
  2095 
  2099 
  2096 fun export_proof_boxes_required thy =
  2100 fun export_proof_boxes_required thy =
  2097   Context.theory_name thy = Context.PureN orelse
  2101   Context.theory_name thy = Context.PureN orelse
  2098     (export_enabled () andalso not (export_standard_enabled ()));
  2102     (export_enabled () andalso not (export_standard_enabled ()));
  2099 
  2103 
  2100 fun export_proof_boxes proofs =
  2104 fun export_proof_boxes bodies =
  2101   let
  2105   let
  2102     fun export_boxes (AbsP (_, _, prf)) = export_boxes prf
  2106     fun export_thm (i, thm_node) boxes =
  2103       | export_boxes (Abst (_, _, prf)) = export_boxes prf
  2107       if Inttab.defined boxes i then boxes
  2104       | export_boxes (prf1 %% prf2) = export_boxes prf1 #> export_boxes prf2
  2108       else
  2105       | export_boxes (prf % _) = export_boxes prf
  2109         boxes
  2106       | export_boxes (PThm ({serial = i, ...}, thm_body)) =
  2110         |> Inttab.update (i, thm_node_export thm_node)
  2107           (fn boxes =>
  2111         |> fold export_thm (thm_node_thms thm_node);
  2108             if Inttab.defined boxes i then boxes
  2112 
  2109             else
  2113     fun export_body (PBody {thms, ...}) = fold export_thm thms;
  2110               let
  2114 
  2111                 val prf' = thm_body_proof_raw thm_body;
  2115     val exports = (bodies, Inttab.empty) |-> fold export_body |> Inttab.dest;
  2112                 val export = thm_body_export_proof thm_body;
  2116   in List.app (Lazy.force o #2) exports end;
  2113                 val boxes' = Inttab.update (i, export) boxes;
       
  2114               in export_boxes prf' boxes' end)
       
  2115       | export_boxes _ = I;
       
  2116     val boxes = (proofs, Inttab.empty) |-> fold export_boxes |> Inttab.dest;
       
  2117   in List.app (Lazy.force o #2) boxes end;
       
  2118 
  2117 
  2119 local
  2118 local
  2120 
  2119 
  2121 fun unconstrainT_proof algebra classrel_proof arity_proof (ucontext: Logic.unconstrain_context) =
  2120 fun unconstrainT_proof algebra classrel_proof arity_proof (ucontext: Logic.unconstrain_context) =
  2122   let
  2121   let
  2158       executable = false,
  2157       executable = false,
  2159       compress = true,
  2158       compress = true,
  2160       strict = false} xml
  2159       strict = false} xml
  2161   end;
  2160   end;
  2162 
  2161 
  2163 fun export thy i prop prf =
       
  2164   if export_enabled () then
       
  2165     let
       
  2166       val _ = export_proof_boxes [prf];
       
  2167       val _ = export_proof thy i prop prf;
       
  2168     in () end
       
  2169   else ();
       
  2170 
       
  2171 fun prune proof =
       
  2172   if Options.default_bool "prune_proofs" then MinProof
       
  2173   else proof;
       
  2174 
       
  2175 fun prepare_thm_proof unconstrain thy classrel_proof arity_proof
  2162 fun prepare_thm_proof unconstrain thy classrel_proof arity_proof
  2176     (name, pos) shyps hyps concl promises body =
  2163     (name, pos) shyps hyps concl promises body =
  2177   let
  2164   let
  2178     val named = name <> "";
  2165     val named = name <> "";
  2179 
  2166 
  2186     val body0 =
  2173     val body0 =
  2187       Future.value
  2174       Future.value
  2188         (PBody {oracles = oracles0, thms = thms0,
  2175         (PBody {oracles = oracles0, thms = thms0,
  2189           proof = if proofs_enabled () then fold_rev implies_intr_proof hyps prf else MinProof});
  2176           proof = if proofs_enabled () then fold_rev implies_intr_proof hyps prf else MinProof});
  2190 
  2177 
  2191     fun publish i = map_proof_of (rew_proof thy #> tap (export thy i prop1) #> prune);
       
  2192     val open_proof = not named ? rew_proof thy;
       
  2193 
       
  2194     fun new_prf () =
  2178     fun new_prf () =
  2195       let
  2179       let
  2196         val i = serial ();
  2180         val i = serial ();
  2197         val unconstrainT =
  2181         val unconstrainT =
  2198           unconstrainT_proof (Sign.classes_of thy) classrel_proof arity_proof ucontext;
  2182           unconstrainT_proof (Sign.classes_of thy) classrel_proof arity_proof ucontext;
  2199         val postproc = map_proof_of unconstrainT #> named ? publish i;
  2183         val postproc = map_proof_of (unconstrainT #> named ? rew_proof thy);
  2200       in (i, fulfill_proof_future thy promises postproc body0) end;
  2184       in (i, fulfill_proof_future thy promises postproc body0) end;
  2201 
  2185 
  2202     val (i, body') =
  2186     val (i, body') =
  2203       (*non-deterministic, depends on unknown promises*)
  2187       (*non-deterministic, depends on unknown promises*)
  2204       (case strip_combt (fst (strip_combP prf)) of
  2188       (case strip_combt (fst (strip_combP prf)) of
  2205         (PThm ({serial = ser, name = a, prop = prop', types = NONE, ...}, thm_body'), args') =>
  2189         (PThm ({serial = ser, name = a, prop = prop', types = NONE, ...}, thm_body'), args') =>
  2206           if (a = "" orelse a = name) andalso prop' = prop1 andalso args' = args then
  2190           if (a = "" orelse a = name) andalso prop' = prop1 andalso args' = args then
  2207             let
  2191             let
  2208               val Thm_Body {body = body', ...} = thm_body';
  2192               val Thm_Body {body = body', ...} = thm_body';
  2209               val i = if a = "" andalso named then serial () else ser;
  2193               val i = if a = "" andalso named then serial () else ser;
  2210             in (i, body' |> ser <> i ? Future.map (publish i)) end
  2194             in (i, body' |> ser <> i ? Future.map (map_proof_of (rew_proof thy))) end
  2211           else new_prf ()
  2195           else new_prf ()
  2212       | _ => new_prf ());
  2196       | _ => new_prf ());
  2213 
  2197 
  2214     val export_proof =
  2198     val open_proof = not named ? rew_proof thy;
  2215       if named orelse not (export_enabled ()) then no_export_proof
  2199 
  2216       else
  2200     val export =
       
  2201       if export_enabled () then
  2217         Lazy.lazy (fn () =>
  2202         Lazy.lazy (fn () =>
  2218           join_proof body' |> open_proof |> export_proof thy i prop1 handle exn =>
  2203           join_proof body' |> open_proof |> export_proof thy i prop1 handle exn =>
  2219             if Exn.is_interrupt exn then
  2204             if Exn.is_interrupt exn then
  2220               raise Fail ("Interrupt: potential resource problems while exporting proof " ^
  2205               raise Fail ("Interrupt: potential resource problems while exporting proof " ^
  2221                 string_of_int i)
  2206                 string_of_int i)
  2222             else Exn.reraise exn);
  2207             else Exn.reraise exn)
       
  2208       else no_export;
  2223 
  2209 
  2224     val theory_name = Context.theory_long_name thy;
  2210     val theory_name = Context.theory_long_name thy;
  2225     val thm = (i, make_thm_node theory_name name prop1 body');
  2211     val thm = (i, make_thm_node theory_name name prop1 body' export);
  2226 
  2212 
  2227     val header = thm_header i ([pos, Position.thread_data ()]) theory_name name prop1 NONE;
  2213     val header = thm_header i ([pos, Position.thread_data ()]) theory_name name prop1 NONE;
  2228     val thm_body = Thm_Body {export_proof = export_proof, open_proof = open_proof, body = body'};
  2214     val thm_body = Thm_Body {open_proof = open_proof, body = body'};
  2229     val head = PThm (header, thm_body);
  2215     val head = PThm (header, thm_body);
  2230     val proof =
  2216     val proof =
  2231       if unconstrain then
  2217       if unconstrain then
  2232         proof_combt' (head, (map o Option.map o Term.map_types) (#map_atyps ucontext) args)
  2218         proof_combt' (head, (map o Option.map o Term.map_types) (#map_atyps ucontext) args)
  2233       else
  2219       else