src/Pure/zterm.ML
changeset 79270 90c5aadcc4b2
parent 79269 2c65d3356ef9
child 79271 b14b289caaf6
equal deleted inserted replaced
79269:2c65d3356ef9 79270:90c5aadcc4b2
   244   val typ_of: ztyp -> typ
   244   val typ_of: ztyp -> typ
   245   val term_of: theory -> zterm -> term
   245   val term_of: theory -> zterm -> term
   246   val could_beta_contract: zterm -> bool
   246   val could_beta_contract: zterm -> bool
   247   val norm_type: Type.tyenv -> ztyp -> ztyp
   247   val norm_type: Type.tyenv -> ztyp -> ztyp
   248   val norm_term: theory -> Envir.env -> zterm -> zterm
   248   val norm_term: theory -> Envir.env -> zterm -> zterm
   249   val norm_proof: theory -> Envir.env -> zproof -> zproof
   249   val norm_proof: theory -> Envir.env -> term list -> zproof -> zproof
   250   type zboxes = (zterm * zproof future) Inttab.table
   250   type zboxes = (zterm * zproof future) Inttab.table
   251   val empty_zboxes: zboxes
   251   val empty_zboxes: zboxes
   252   val union_zboxes: zboxes -> zboxes -> zboxes
   252   val union_zboxes: zboxes -> zboxes -> zboxes
   253   val box_proof: theory -> term list -> term -> zboxes * zproof -> zboxes * zproof
   253   val box_proof: theory -> term list -> term -> zboxes * zproof -> zboxes * zproof
   254   val axiom_proof:  theory -> string -> term -> zproof
   254   val axiom_proof:  theory -> string -> term -> zproof
   275   val rotate_proof: theory -> term list -> term -> (string * typ) list ->
   275   val rotate_proof: theory -> term list -> term -> (string * typ) list ->
   276     term list -> int -> zproof -> zproof
   276     term list -> int -> zproof -> zproof
   277   val permute_prems_proof: theory -> term list -> int -> int -> zproof -> zproof
   277   val permute_prems_proof: theory -> term list -> int -> int -> zproof -> zproof
   278   val incr_indexes_proof: int -> zproof -> zproof
   278   val incr_indexes_proof: int -> zproof -> zproof
   279   val lift_proof: theory -> term -> int -> term list -> zproof -> zproof
   279   val lift_proof: theory -> term -> int -> term list -> zproof -> zproof
   280   val assumption_proof: theory -> Envir.env -> term list -> term -> int -> zproof -> zproof
   280   val assumption_proof: theory -> Envir.env -> term list -> term -> int -> term list ->
       
   281     zproof -> zproof
   281   val bicompose_proof: theory -> Envir.env -> int -> bool -> term list -> term list ->
   282   val bicompose_proof: theory -> Envir.env -> int -> bool -> term list -> term list ->
   282     term option -> term list -> int -> int -> zproof -> zproof -> zproof
   283     term option -> term list -> int -> int -> term list -> zproof -> zproof -> zproof
   283 end;
   284 end;
   284 
   285 
   285 structure ZTerm: ZTERM =
   286 structure ZTerm: ZTERM =
   286 struct
   287 struct
   287 
   288 
   391           (ZAbs (a, typ T, Same.commit term t) handle Same.SAME => ZAbs (a, T, term t))
   392           (ZAbs (a, typ T, Same.commit term t) handle Same.SAME => ZAbs (a, T, term t))
   392       | term (ZApp (t, u)) =
   393       | term (ZApp (t, u)) =
   393           (ZApp (term t, Same.commit term u) handle Same.SAME => ZApp (t, term u))
   394           (ZApp (term t, Same.commit term u) handle Same.SAME => ZApp (t, term u))
   394       | term (ZClass (T, c)) = ZClass (typ T, c);
   395       | term (ZClass (T, c)) = ZClass (typ T, c);
   395   in term end;
   396   in term end;
       
   397 
       
   398 fun instantiate_type_same instT =
       
   399   if ZTVars.is_empty instT then Same.same
       
   400   else ZTypes.unsynchronized_cache (subst_type_same (Same.function (ZTVars.lookup instT)));
       
   401 
       
   402 fun instantiate_term_same typ inst =
       
   403   subst_term_same typ (Same.function (ZVars.lookup inst));
   396 
   404 
   397 
   405 
   398 (* map types/terms within proof *)
   406 (* map types/terms within proof *)
   399 
   407 
   400 fun map_insts_same typ term (instT, inst) =
   408 fun map_insts_same typ term (instT, inst) =
   571     fn t =>
   579     fn t =>
   572       if Envir.is_empty envir andalso not (could_beta_contract t)
   580       if Envir.is_empty envir andalso not (could_beta_contract t)
   573       then raise Same.SAME else norm t
   581       then raise Same.SAME else norm t
   574   end;
   582   end;
   575 
   583 
   576 fun norm_proof_same (cache as {ztyp, ...}) (envir as Envir.Envir {tyenv, ...}) =
   584 fun norm_proof_cache (cache as {ztyp, ...}) (envir as Envir.Envir {tyenv, ...}) =
   577   let
   585   let
   578     val typ = norm_type_same ztyp tyenv;
   586     val norm_tvar = ZTVar #> Same.commit (norm_type_same ztyp tyenv);
   579     val term = norm_term_same cache envir;
   587     val norm_var = ZVar #> Same.commit (norm_term_same cache envir);
   580   in
   588   in
   581     fn prf =>
   589     fn visible => fn prf =>
   582       if Envir.is_empty envir andalso not (exists_proof_terms could_beta_contract prf)
   590       if (Envir.is_empty envir orelse null visible)
   583       then raise Same.SAME else map_proof_same typ term prf
   591         andalso not (exists_proof_terms could_beta_contract prf)
       
   592       then prf
       
   593       else
       
   594         let
       
   595           fun add_tvar v tab =
       
   596             if ZTVars.defined tab v then tab else ZTVars.update (v, norm_tvar v) tab;
       
   597 
       
   598           val inst_typ =
       
   599             if Vartab.is_empty tyenv then Same.same
       
   600             else
       
   601               ZTVars.build (visible |> (fold o Term.fold_types o Term.fold_atyps)
       
   602                 (fn TVar v => add_tvar v | _ => I))
       
   603               |> instantiate_type_same;
       
   604 
       
   605           fun add_var (a, T) tab =
       
   606             let val v = (a, Same.commit inst_typ (ztyp T))
       
   607             in if ZVars.defined tab v then tab else ZVars.update (v, norm_var v) tab end;
       
   608 
       
   609           val inst_term =
       
   610             ZVars.build (visible |> (fold o Term.fold_aterms) (fn Var v => add_var v | _ => I))
       
   611             |> instantiate_term_same inst_typ;
       
   612         in Same.commit (map_proof_same inst_typ inst_term) prf end
   584   end;
   613   end;
   585 
   614 
   586 fun norm_cache thy =
   615 fun norm_cache thy =
   587   let
   616   let
   588     val {ztyp, zterm} = zterm_cache thy;
   617     val {ztyp, zterm} = zterm_cache thy;
   589     val typ = typ_cache ();
   618     val typ = typ_cache ();
   590   in {ztyp = ztyp, zterm = zterm, typ = typ} end;
   619   in {ztyp = ztyp, zterm = zterm, typ = typ} end;
   591 
   620 
   592 fun norm_type tyenv = Same.commit (norm_type_same (ztyp_cache ()) tyenv);
   621 fun norm_type tyenv = Same.commit (norm_type_same (ztyp_cache ()) tyenv);
   593 fun norm_term thy envir = Same.commit (norm_term_same (norm_cache thy) envir);
   622 fun norm_term thy envir = Same.commit (norm_term_same (norm_cache thy) envir);
   594 fun norm_proof thy envir = Same.commit (norm_proof_same (norm_cache thy) envir);
   623 fun norm_proof thy envir = norm_proof_cache (norm_cache thy) envir;
   595 
   624 
   596 
   625 
   597 
   626 
   598 (** proof construction **)
   627 (** proof construction **)
   599 
   628 
   850 fun instantiate_proof thy (Ts, ts) prf =
   879 fun instantiate_proof thy (Ts, ts) prf =
   851   let
   880   let
   852     val {ztyp, zterm} = zterm_cache thy;
   881     val {ztyp, zterm} = zterm_cache thy;
   853     val instT = ZTVars.build (Ts |> fold (fn (v, T) => ZTVars.add (v, ztyp T)));
   882     val instT = ZTVars.build (Ts |> fold (fn (v, T) => ZTVars.add (v, ztyp T)));
   854     val inst = ZVars.build (ts |> fold (fn ((v, T), t) => ZVars.add ((v, ztyp T), zterm t)));
   883     val inst = ZVars.build (ts |> fold (fn ((v, T), t) => ZVars.add ((v, ztyp T), zterm t)));
   855     val typ =
   884     val typ = instantiate_type_same instT;
   856       if ZTVars.is_empty instT then Same.same
   885     val term = instantiate_term_same typ inst;
   857       else ZTypes.unsynchronized_cache (subst_type_same (Same.function (ZTVars.lookup instT)));
       
   858     val term = subst_term_same typ (Same.function (ZVars.lookup inst));
       
   859   in Same.commit (map_proof_same typ term) prf end;
   886   in Same.commit (map_proof_same typ term) prf end;
   860 
   887 
   861 fun varifyT_proof names prf =
   888 fun varifyT_proof names prf =
   862   if null names then prf
   889   if null names then prf
   863   else
   890   else
   989       | imp _ i _ = ZBoundp i;
  1016       | imp _ i _ = ZBoundp i;
   990     fun all (ZApp (ZConst1 ("Pure.all", _), ZAbs (a, T, t))) = ZAbst (a, T, all t)
  1017     fun all (ZApp (ZConst1 ("Pure.all", _), ZAbs (a, T, t))) = ZAbst (a, T, all t)
   991       | all t = imp t (~ i) m
  1018       | all t = imp t (~ i) m
   992   in all C end;
  1019   in all C end;
   993 
  1020 
   994 fun assumption_proof thy envir Bs Bi n prf =
  1021 fun assumption_proof thy envir Bs Bi n visible prf =
   995   let
  1022   let
   996     val cache as {zterm, ...} = norm_cache thy;
  1023     val cache as {zterm, ...} = norm_cache thy;
   997     val normt = zterm #> Same.commit (norm_term_same cache envir);
  1024     val normt = zterm #> Same.commit (norm_term_same cache envir);
   998   in
  1025   in
   999     ZAbsps (map normt Bs)
  1026     ZAbsps (map normt Bs)
  1000       (ZAppps (prf, map ZBoundp (length Bs - 1 downto 0) @ [mk_asm_prf (normt Bi) n ~1]))
  1027       (ZAppps (prf, map ZBoundp (length Bs - 1 downto 0) @ [mk_asm_prf (normt Bi) n ~1]))
  1001     |> Same.commit (norm_proof_same cache envir)
  1028     |> norm_proof_cache cache envir visible
  1002   end;
  1029   end;
  1003 
  1030 
  1004 fun flatten_params_proof i j n (ZApp (ZApp (ZConst0 "Pure.imp", A), B), k) =
  1031 fun flatten_params_proof i j n (ZApp (ZApp (ZConst0 "Pure.imp", A), B), k) =
  1005       ZAbsp ("H", A, flatten_params_proof (i + 1) j n (B, k))
  1032       ZAbsp ("H", A, flatten_params_proof (i + 1) j n (B, k))
  1006   | flatten_params_proof i j n (ZApp (ZConst1 ("Pure.all", _), ZAbs (a, T, t)), k) =
  1033   | flatten_params_proof i j n (ZApp (ZConst1 ("Pure.all", _), ZAbs (a, T, t)), k) =
  1007       ZAbst (a, T, flatten_params_proof i (j + 1) n (t, k))
  1034       ZAbst (a, T, flatten_params_proof i (j + 1) n (t, k))
  1008   | flatten_params_proof i j n (_, k) =
  1035   | flatten_params_proof i j n (_, k) =
  1009       ZAppps (ZAppts (ZBoundp (k + i),
  1036       ZAppps (ZAppts (ZBoundp (k + i),
  1010         map ZBound (j - 1 downto 0)), map ZBoundp (remove (op =) (i - n) (i - 1 downto 0)));
  1037         map ZBound (j - 1 downto 0)), map ZBoundp (remove (op =) (i - n) (i - 1 downto 0)));
  1011 
  1038 
  1012 fun bicompose_proof thy env smax flatten Bs As A oldAs n m rprf sprf =
  1039 fun bicompose_proof thy env smax flatten Bs As A oldAs n m visible rprf sprf =
  1013   let
  1040   let
  1014     val cache as {zterm, ...} = norm_cache thy;
  1041     val cache as {zterm, ...} = norm_cache thy;
  1015     val normt = zterm #> Same.commit (norm_term_same cache env);
  1042     val normt = zterm #> Same.commit (norm_term_same cache env);
  1016     val normp = Same.commit (norm_proof_same cache env);
  1043     val normp = norm_proof_cache cache env visible;
  1017 
  1044 
  1018     val lb = length Bs;
  1045     val lb = length Bs;
  1019     val la = length As;
  1046     val la = length As;
  1020 
  1047 
  1021     fun proof p q =
  1048     fun proof p q =