src/Pure/proofterm.ML
changeset 70538 fc9ba6fe367f
parent 70534 fb876ebbf5a7
child 70540 04ef5ee3dd4d
equal deleted inserted replaced
70537:17160e0a60b6 70538:fc9ba6fe367f
     9 signature BASIC_PROOFTERM =
     9 signature BASIC_PROOFTERM =
    10 sig
    10 sig
    11   type thm_node
    11   type thm_node
    12   type proof_serial = int
    12   type proof_serial = int
    13   type thm_header =
    13   type thm_header =
    14     {serial: proof_serial, pos: Position.T list, name: string, prop: term, types: typ list option}
    14     {serial: proof_serial, pos: Position.T list, theory_name: string, name: string,
       
    15       prop: term, types: typ list option}
    15   type thm_body
    16   type thm_body
    16   datatype proof =
    17   datatype proof =
    17      MinProof
    18      MinProof
    18    | PBound of int
    19    | PBound of int
    19    | Abst of string * typ option * proof
    20    | Abst of string * typ option * proof
    38   val proofs: int Unsynchronized.ref
    39   val proofs: int Unsynchronized.ref
    39   type pthm = proof_serial * thm_node
    40   type pthm = proof_serial * thm_node
    40   type oracle = string * term
    41   type oracle = string * term
    41   val proof_of: proof_body -> proof
    42   val proof_of: proof_body -> proof
    42   val map_proof_of: (proof -> proof) -> proof_body -> proof_body
    43   val map_proof_of: (proof -> proof) -> proof_body -> proof_body
    43   val thm_header: proof_serial -> Position.T list -> string -> term -> typ list option -> thm_header
    44   val thm_header: proof_serial -> Position.T list -> string -> string -> term -> typ list option ->
       
    45     thm_header
    44   val thm_body: proof_body -> thm_body
    46   val thm_body: proof_body -> thm_body
    45   val thm_body_proof_raw: thm_body -> proof
    47   val thm_body_proof_raw: thm_body -> proof
    46   val thm_body_proof_open: thm_body -> proof
    48   val thm_body_proof_open: thm_body -> proof
    47   val thm_node_name: thm_node -> string
    49   val thm_node_name: thm_node -> string
    48   val thm_node_prop: thm_node -> term
    50   val thm_node_prop: thm_node -> term
   187 (** datatype proof **)
   189 (** datatype proof **)
   188 
   190 
   189 type proof_serial = int;
   191 type proof_serial = int;
   190 
   192 
   191 type thm_header =
   193 type thm_header =
   192   {serial: proof_serial, pos: Position.T list, name: string, prop: term, types: typ list option};
   194   {serial: proof_serial, pos: Position.T list, theory_name: string, name: string,
       
   195     prop: term, types: typ list option};
   193 
   196 
   194 datatype proof =
   197 datatype proof =
   195    MinProof
   198    MinProof
   196  | PBound of int
   199  | PBound of int
   197  | Abst of string * typ option * proof
   200  | Abst of string * typ option * proof
   219 val join_proof = Future.join #> proof_of;
   222 val join_proof = Future.join #> proof_of;
   220 
   223 
   221 fun map_proof_of f (PBody {oracles, thms, proof}) =
   224 fun map_proof_of f (PBody {oracles, thms, proof}) =
   222   PBody {oracles = oracles, thms = thms, proof = f proof};
   225   PBody {oracles = oracles, thms = thms, proof = f proof};
   223 
   226 
   224 fun thm_header serial pos name prop types : thm_header =
   227 fun thm_header serial pos theory_name name prop types : thm_header =
   225   {serial = serial, pos = pos, name = name, prop = prop, types = types};
   228   {serial = serial, pos = pos, theory_name = theory_name, name = name, prop = prop, types = types};
   226 
   229 
   227 val no_export_proof = Lazy.value ();
   230 val no_export_proof = Lazy.value ();
   228 
   231 
   229 fun thm_body body =
   232 fun thm_body body =
   230   Thm_Body {export_proof = no_export_proof, open_proof = I, body = Future.value body};
   233   Thm_Body {export_proof = no_export_proof, open_proof = I, body = Future.value body};
   381   fn a %% b => ([], pair proof proof (a, b)),
   384   fn a %% b => ([], pair proof proof (a, b)),
   382   fn Hyp a => ([], term a),
   385   fn Hyp a => ([], term a),
   383   fn PAxm (a, b, c) => ([a], pair term (option (list typ)) (b, c)),
   386   fn PAxm (a, b, c) => ([a], pair term (option (list typ)) (b, c)),
   384   fn OfClass (a, b) => ([b], typ a),
   387   fn OfClass (a, b) => ([b], typ a),
   385   fn Oracle (a, b, c) => ([a], pair term (option (list typ)) (b, c)),
   388   fn Oracle (a, b, c) => ([a], pair term (option (list typ)) (b, c)),
   386   fn PThm ({serial, pos, name, prop, types}, Thm_Body {open_proof, body, ...}) =>
   389   fn PThm ({serial, pos, theory_name, name, prop, types}, Thm_Body {open_proof, body, ...}) =>
   387     ([int_atom serial, name],
   390     ([int_atom serial, theory_name, name],
   388       pair (list properties) (pair term (pair (option (list typ)) proof_body))
   391       pair (list properties) (pair term (pair (option (list typ)) proof_body))
   389         (map Position.properties_of pos, (prop, (types, map_proof_of open_proof (Future.join body)))))]
   392         (map Position.properties_of pos, (prop, (types, map_proof_of open_proof (Future.join body)))))]
   390 and proof_body (PBody {oracles, thms, proof = prf}) =
   393 and proof_body (PBody {oracles, thms, proof = prf}) =
   391   triple (list (pair string term)) (list pthm) proof (oracles, thms, prf)
   394   triple (list (pair string term)) (list pthm) proof (oracles, thms, prf)
   392 and pthm (a, thm_node) =
   395 and pthm (a, thm_node) =
   402   fn a %% b => ([], pair full_proof full_proof (a, b)),
   405   fn a %% b => ([], pair full_proof full_proof (a, b)),
   403   fn Hyp a => ([], term a),
   406   fn Hyp a => ([], term a),
   404   fn PAxm (name, _, SOME Ts) => ([name], list typ Ts),
   407   fn PAxm (name, _, SOME Ts) => ([name], list typ Ts),
   405   fn OfClass (T, c) => ([c], typ T),
   408   fn OfClass (T, c) => ([c], typ T),
   406   fn Oracle (name, prop, SOME Ts) => ([name], pair term (list typ) (prop, Ts)),
   409   fn Oracle (name, prop, SOME Ts) => ([name], pair term (list typ) (prop, Ts)),
   407   fn PThm ({serial, name, types = SOME Ts, ...}, _) => ([int_atom serial, name], list typ Ts)];
   410   fn PThm ({serial, theory_name, name, types = SOME Ts, ...}, _) =>
       
   411     ([int_atom serial, theory_name, name], list typ Ts)];
   408 
   412 
   409 in
   413 in
   410 
   414 
   411 val encode = proof;
   415 val encode = proof;
   412 val encode_body = proof_body;
   416 val encode_body = proof_body;
   430   fn ([], a) => op %% (pair proof proof a),
   434   fn ([], a) => op %% (pair proof proof a),
   431   fn ([], a) => Hyp (term a),
   435   fn ([], a) => Hyp (term a),
   432   fn ([a], b) => let val (c, d) = pair term (option (list typ)) b in PAxm (a, c, d) end,
   436   fn ([a], b) => let val (c, d) = pair term (option (list typ)) b in PAxm (a, c, d) end,
   433   fn ([b], a) => OfClass (typ a, b),
   437   fn ([b], a) => OfClass (typ a, b),
   434   fn ([a], b) => let val (c, d) = pair term (option (list typ)) b in Oracle (a, c, d) end,
   438   fn ([a], b) => let val (c, d) = pair term (option (list typ)) b in Oracle (a, c, d) end,
   435   fn ([a, b], c) =>
   439   fn ([a, b, c], d) =>
   436     let
   440     let
   437       val ((d, (e, (f, g)))) =
   441       val ((e, (f, (g, h)))) =
   438         pair (list properties) (pair term (pair (option (list typ)) proof_body)) c;
   442         pair (list properties) (pair term (pair (option (list typ)) proof_body)) d;
   439       val header = thm_header (int_atom a) (map Position.of_properties d) b e f;
   443       val header = thm_header (int_atom a) (map Position.of_properties e) b c f g;
   440     in PThm (header, thm_body g) end]
   444     in PThm (header, thm_body h) end]
   441 and proof_body x =
   445 and proof_body x =
   442   let val (a, b, c) = triple (list (pair string term)) (list pthm) proof x
   446   let val (a, b, c) = triple (list (pair string term)) (list pthm) proof x
   443   in PBody {oracles = a, thms = b, proof = c} end
   447   in PBody {oracles = a, thms = b, proof = c} end
   444 and pthm x =
   448 and pthm x =
   445   let val (a, (b, c, d)) = pair int (triple string term proof_body) x
   449   let val (a, (b, c, d)) = pair int (triple string term proof_body) x
   509           (proof prf1 %% Same.commit proof prf2
   513           (proof prf1 %% Same.commit proof prf2
   510             handle Same.SAME => prf1 %% proof prf2)
   514             handle Same.SAME => prf1 %% proof prf2)
   511       | proof (PAxm (a, prop, SOME Ts)) = PAxm (a, prop, SOME (typs Ts))
   515       | proof (PAxm (a, prop, SOME Ts)) = PAxm (a, prop, SOME (typs Ts))
   512       | proof (OfClass T_c) = ofclass T_c
   516       | proof (OfClass T_c) = ofclass T_c
   513       | proof (Oracle (a, prop, SOME Ts)) = Oracle (a, prop, SOME (typs Ts))
   517       | proof (Oracle (a, prop, SOME Ts)) = Oracle (a, prop, SOME (typs Ts))
   514       | proof (PThm ({serial, pos, name, prop, types = SOME Ts}, thm_body)) =
   518       | proof (PThm ({serial, pos, theory_name, name, prop, types = SOME Ts}, thm_body)) =
   515           PThm (thm_header serial pos name prop (SOME (typs Ts)), thm_body)
   519           PThm (thm_header serial pos theory_name name prop (SOME (typs Ts)), thm_body)
   516       | proof _ = raise Same.SAME;
   520       | proof _ = raise Same.SAME;
   517   in proof end;
   521   in proof end;
   518 
   522 
   519 fun map_proof_terms_same term typ = map_proof_same term typ (fn (T, c) => OfClass (typ T, c));
   523 fun map_proof_terms_same term typ = map_proof_same term typ (fn (T, c) => OfClass (typ T, c));
   520 fun map_proof_types_same typ = map_proof_terms_same (Term_Subst.map_types_same typ) typ;
   524 fun map_proof_types_same typ = map_proof_terms_same (Term_Subst.map_types_same typ) typ;
   549   | size_of_proof _ = 1;
   553   | size_of_proof _ = 1;
   550 
   554 
   551 fun change_types types (PAxm (name, prop, _)) = PAxm (name, prop, types)
   555 fun change_types types (PAxm (name, prop, _)) = PAxm (name, prop, types)
   552   | change_types (SOME [T]) (OfClass (_, c)) = OfClass (T, c)
   556   | change_types (SOME [T]) (OfClass (_, c)) = OfClass (T, c)
   553   | change_types types (Oracle (name, prop, _)) = Oracle (name, prop, types)
   557   | change_types types (Oracle (name, prop, _)) = Oracle (name, prop, types)
   554   | change_types types (PThm ({serial, pos, name, prop, types = _}, thm_body)) =
   558   | change_types types (PThm ({serial, pos, theory_name, name, prop, types = _}, thm_body)) =
   555       PThm (thm_header serial pos name prop types, thm_body)
   559       PThm (thm_header serial pos theory_name name prop types, thm_body)
   556   | change_types _ prf = prf;
   560   | change_types _ prf = prf;
   557 
   561 
   558 
   562 
   559 (* utilities *)
   563 (* utilities *)
   560 
   564 
   697           PAxm (s, prop, Same.map_option (htypeTs Envir.norm_types_same) Ts)
   701           PAxm (s, prop, Same.map_option (htypeTs Envir.norm_types_same) Ts)
   698       | norm (OfClass (T, c)) =
   702       | norm (OfClass (T, c)) =
   699           OfClass (htypeT Envir.norm_type_same T, c)
   703           OfClass (htypeT Envir.norm_type_same T, c)
   700       | norm (Oracle (s, prop, Ts)) =
   704       | norm (Oracle (s, prop, Ts)) =
   701           Oracle (s, prop, Same.map_option (htypeTs Envir.norm_types_same) Ts)
   705           Oracle (s, prop, Same.map_option (htypeTs Envir.norm_types_same) Ts)
   702       | norm (PThm ({serial = i, pos = p, name = a, prop = t, types = Ts}, thm_body)) =
   706       | norm (PThm ({serial = i, pos = p, theory_name, name = a, prop = t, types = Ts}, thm_body)) =
   703           PThm (thm_header i p a t (Same.map_option (htypeTs Envir.norm_types_same) Ts), thm_body)
   707           PThm (thm_header i p theory_name a t
       
   708             (Same.map_option (htypeTs Envir.norm_types_same) Ts), thm_body)
   704       | norm _ = raise Same.SAME;
   709       | norm _ = raise Same.SAME;
   705   in Same.commit norm end;
   710   in Same.commit norm end;
   706 
   711 
   707 
   712 
   708 (* remove some types in proof term (to save space) *)
   713 (* remove some types in proof term (to save space) *)
  1014           PAxm (s, prop, (Same.map_option o Same.map) (Logic.incr_tvar_same inc) Ts)
  1019           PAxm (s, prop, (Same.map_option o Same.map) (Logic.incr_tvar_same inc) Ts)
  1015       | lift' _ _ (OfClass (T, c)) =
  1020       | lift' _ _ (OfClass (T, c)) =
  1016           OfClass (Logic.incr_tvar_same inc T, c)
  1021           OfClass (Logic.incr_tvar_same inc T, c)
  1017       | lift' _ _ (Oracle (s, prop, Ts)) =
  1022       | lift' _ _ (Oracle (s, prop, Ts)) =
  1018           Oracle (s, prop, (Same.map_option o Same.map) (Logic.incr_tvar_same inc) Ts)
  1023           Oracle (s, prop, (Same.map_option o Same.map) (Logic.incr_tvar_same inc) Ts)
  1019       | lift' _ _ (PThm ({serial = i, pos = p, name = s, prop, types = Ts}, thm_body)) =
  1024       | lift' _ _ (PThm ({serial = i, pos = p, theory_name, name = s, prop, types = Ts}, thm_body)) =
  1020           PThm (thm_header i p s prop ((Same.map_option o Same.map) (Logic.incr_tvar inc) Ts),
  1025           PThm (thm_header i p theory_name s prop
  1021             thm_body)
  1026             ((Same.map_option o Same.map) (Logic.incr_tvar inc) Ts), thm_body)
  1022       | lift' _ _ _ = raise Same.SAME
  1027       | lift' _ _ _ = raise Same.SAME
  1023     and lifth' Us Ts prf = (lift' Us Ts prf handle Same.SAME => prf);
  1028     and lifth' Us Ts prf = (lift' Us Ts prf handle Same.SAME => prf);
  1024 
  1029 
  1025     val ps = map (Logic.lift_all inc Bi) (Logic.strip_imp_prems prop);
  1030     val ps = map (Logic.lift_all inc Bi) (Logic.strip_imp_prems prop);
  1026     val k = length ps;
  1031     val k = length ps;
  1445             NONE => raise Same.SAME
  1450             NONE => raise Same.SAME
  1446           | SOME prf' => incr_pboundvars plev tlev prf')
  1451           | SOME prf' => incr_pboundvars plev tlev prf')
  1447       | subst _ _ (PAxm (id, prop, Ts)) = PAxm (id, prop, Same.map_option substTs Ts)
  1452       | subst _ _ (PAxm (id, prop, Ts)) = PAxm (id, prop, Same.map_option substTs Ts)
  1448       | subst _ _ (OfClass (T, c)) = OfClass (substT T, c)
  1453       | subst _ _ (OfClass (T, c)) = OfClass (substT T, c)
  1449       | subst _ _ (Oracle (id, prop, Ts)) = Oracle (id, prop, Same.map_option substTs Ts)
  1454       | subst _ _ (Oracle (id, prop, Ts)) = Oracle (id, prop, Same.map_option substTs Ts)
  1450       | subst _ _ (PThm ({serial = i, pos = p, name = id, prop, types}, thm_body)) =
  1455       | subst _ _ (PThm ({serial = i, pos = p, theory_name, name = id, prop, types}, thm_body)) =
  1451           PThm (thm_header i p id prop (Same.map_option substTs types), thm_body)
  1456           PThm (thm_header i p theory_name id prop (Same.map_option substTs types), thm_body)
  1452       | subst _ _ _ = raise Same.SAME;
  1457       | subst _ _ _ = raise Same.SAME;
  1453   in fn t => subst 0 0 t handle Same.SAME => t end;
  1458   in fn t => subst 0 0 t handle Same.SAME => t end;
  1454 
  1459 
  1455 (*A fast unification filter: true unless the two terms cannot be unified.
  1460 (*A fast unification filter: true unless the two terms cannot be unified.
  1456   Terms must be NORMAL.  Treats all Vars as distinct. *)
  1461   Terms must be NORMAL.  Treats all Vars as distinct. *)
  2187       if named orelse not (export_enabled ()) then no_export_proof
  2192       if named orelse not (export_enabled ()) then no_export_proof
  2188       else Lazy.lazy (fn () => join_proof body' |> open_proof |> export_proof thy i prop1);
  2193       else Lazy.lazy (fn () => join_proof body' |> open_proof |> export_proof thy i prop1);
  2189 
  2194 
  2190     val pthm = (i, make_thm_node name prop1 body');
  2195     val pthm = (i, make_thm_node name prop1 body');
  2191 
  2196 
  2192     val header = thm_header i ([pos, Position.thread_data ()]) name prop1 NONE;
  2197     val header =
       
  2198       thm_header i ([pos, Position.thread_data ()]) (Context.theory_long_name thy) name prop1 NONE;
  2193     val thm_body = Thm_Body {export_proof = export_proof, open_proof = open_proof, body = body'};
  2199     val thm_body = Thm_Body {export_proof = export_proof, open_proof = open_proof, body = body'};
  2194     val head = PThm (header, thm_body);
  2200     val head = PThm (header, thm_body);
  2195     val proof =
  2201     val proof =
  2196       if unconstrain then
  2202       if unconstrain then
  2197         proof_combt' (head, (map o Option.map o Term.map_types) (#map_atyps ucontext) args)
  2203         proof_combt' (head, (map o Option.map o Term.map_types) (#map_atyps ucontext) args)