src/Pure/Isar/code.ML
changeset 59458 9de8ac92cafa
parent 59058 a78612c67ec0
child 59621 291934bac95e
equal deleted inserted replaced
59457:c4f044389c28 59458:9de8ac92cafa
    40   val add_datatype_cmd: string list -> theory -> theory
    40   val add_datatype_cmd: string list -> theory -> theory
    41   val datatype_interpretation:
    41   val datatype_interpretation:
    42     (string * ((string * sort) list * (string * ((string * sort) list * typ list)) list)
    42     (string * ((string * sort) list * (string * ((string * sort) list * typ list)) list)
    43       -> theory -> theory) -> theory -> theory
    43       -> theory -> theory) -> theory -> theory
    44   val add_abstype: thm -> theory -> theory
    44   val add_abstype: thm -> theory -> theory
       
    45   val add_abstype_default: thm -> theory -> theory
    45   val abstype_interpretation:
    46   val abstype_interpretation:
    46     (string * ((string * sort) list * ((string * ((string * sort) list * typ)) * (string * thm)))
    47     (string * ((string * sort) list * ((string * ((string * sort) list * typ)) * (string * thm)))
    47       -> theory -> theory) -> theory -> theory
    48       -> theory -> theory) -> theory -> theory
    48   val add_eqn: thm -> theory -> theory
    49   val add_eqn: thm -> theory -> theory
    49   val add_nbe_eqn: thm -> theory -> theory
    50   val add_nbe_eqn: thm -> theory -> theory
    54   val add_default_eqn_attribute: attribute
    55   val add_default_eqn_attribute: attribute
    55   val add_default_eqn_attrib: Token.src
    56   val add_default_eqn_attrib: Token.src
    56   val add_nbe_default_eqn: thm -> theory -> theory
    57   val add_nbe_default_eqn: thm -> theory -> theory
    57   val add_nbe_default_eqn_attribute: attribute
    58   val add_nbe_default_eqn_attribute: attribute
    58   val add_nbe_default_eqn_attrib: Token.src
    59   val add_nbe_default_eqn_attrib: Token.src
       
    60   val add_abs_default_eqn: thm -> theory -> theory
       
    61   val add_abs_default_eqn_attribute: attribute
       
    62   val add_abs_default_eqn_attrib: Token.src
    59   val del_eqn: thm -> theory -> theory
    63   val del_eqn: thm -> theory -> theory
    60   val del_eqns: string -> theory -> theory
    64   val del_eqns: string -> theory -> theory
    61   val del_exception: string -> theory -> theory
    65   val del_exception: string -> theory -> theory
    62   val add_case: thm -> theory -> theory
    66   val add_case: thm -> theory -> theory
    63   val add_undefined: string -> theory -> theory
    67   val add_undefined: string -> theory -> theory
   173 fun case_consts_of (Constructors (_, case_consts)) = case_consts
   177 fun case_consts_of (Constructors (_, case_consts)) = case_consts
   174   | case_consts_of (Abstractor _) = [];
   178   | case_consts_of (Abstractor _) = [];
   175 
   179 
   176 (* functions *)
   180 (* functions *)
   177 
   181 
   178 datatype fun_spec = Default of (thm * bool) list * (thm * bool) list lazy
   182 datatype fun_spec = Eqns_Default of (thm * bool) list * (thm * bool) list lazy
   179       (* (cache for default equations, lazy computation of default equations)
   183       (* (cache for default equations, lazy computation of default equations)
   180          -- helps to restore natural order of default equations *)
   184          -- helps to restore natural order of default equations *)
   181   | Eqns of (thm * bool) list
   185   | Eqns of (thm * bool) list
   182   | None
   186   | None
   183   | Proj of term * string
   187   | Proj of (term * string) * bool
   184   | Abstr of thm * string;
   188   | Abstr of (thm * string) * bool;
   185 
   189 
   186 val initial_fun_spec = Default ([], Lazy.value []);
   190 val initial_fun_spec = Eqns_Default ([], Lazy.value []);
   187 
   191 
   188 fun is_default (Default _) = true
   192 fun is_default (Eqns_Default _) = true
       
   193   | is_default (Proj (_, default)) = default
       
   194   | is_default (Abstr (_, default)) = default
   189   | is_default _ = false;
   195   | is_default _ = false;
   190 
   196 
   191 fun associated_abstype (Abstr (_, tyco)) = SOME tyco
   197 fun associated_abstype (Abstr ((_, tyco), _)) = SOME tyco
   192   | associated_abstype _ = NONE;
   198   | associated_abstype _ = NONE;
   193 
   199 
   194 
   200 
   195 (* executable code data *)
   201 (* executable code data *)
   196 
   202 
   933 fun get_cert ctxt functrans c =
   939 fun get_cert ctxt functrans c =
   934   let
   940   let
   935     val thy = Proof_Context.theory_of ctxt;
   941     val thy = Proof_Context.theory_of ctxt;
   936   in
   942   in
   937     case retrieve_raw thy c of
   943     case retrieve_raw thy c of
   938       Default (_, eqns_lazy) => Lazy.force eqns_lazy
   944       Eqns_Default (_, eqns_lazy) => Lazy.force eqns_lazy
   939         |> cert_of_eqns_preprocess ctxt functrans c
   945         |> cert_of_eqns_preprocess ctxt functrans c
   940     | Eqns eqns => eqns
   946     | Eqns eqns => eqns
   941         |> cert_of_eqns_preprocess ctxt functrans c
   947         |> cert_of_eqns_preprocess ctxt functrans c
   942     | None => nothing_cert ctxt c
   948     | None => nothing_cert ctxt c
   943     | Proj (_, tyco) => cert_of_proj thy c tyco
   949     | Proj ((_, tyco), _) => cert_of_proj thy c tyco
   944     | Abstr (abs_thm, tyco) => abs_thm
   950     | Abstr ((abs_thm, tyco), _) => abs_thm
   945        |> preprocess Conv.arg_conv ctxt
   951        |> preprocess Conv.arg_conv ctxt
   946        |> cert_of_abs thy tyco c
   952        |> cert_of_abs thy tyco c
   947   end;
   953   end;
   948 
   954 
   949 
   955 
  1002     val ctxt = Proof_Context.init_global thy;
  1008     val ctxt = Proof_Context.init_global thy;
  1003     val exec = the_exec thy;
  1009     val exec = the_exec thy;
  1004     fun pretty_equations const thms =
  1010     fun pretty_equations const thms =
  1005       (Pretty.block o Pretty.fbreaks)
  1011       (Pretty.block o Pretty.fbreaks)
  1006         (Pretty.str (string_of_const thy const) :: map (Display.pretty_thm_item ctxt) thms);
  1012         (Pretty.str (string_of_const thy const) :: map (Display.pretty_thm_item ctxt) thms);
  1007     fun pretty_function (const, Default (_, eqns_lazy)) =
  1013     fun pretty_function (const, Eqns_Default (_, eqns_lazy)) =
  1008           pretty_equations const (map fst (Lazy.force eqns_lazy))
  1014           pretty_equations const (map fst (Lazy.force eqns_lazy))
  1009       | pretty_function (const, Eqns eqns) = pretty_equations const (map fst eqns)
  1015       | pretty_function (const, Eqns eqns) = pretty_equations const (map fst eqns)
  1010       | pretty_function (const, None) = pretty_equations const []
  1016       | pretty_function (const, None) = pretty_equations const []
  1011       | pretty_function (const, Proj (proj, _)) = Pretty.block
  1017       | pretty_function (const, Proj ((proj, _), _)) = Pretty.block
  1012           [Pretty.str (string_of_const thy const), Pretty.fbrk, Syntax.pretty_term ctxt proj]
  1018           [Pretty.str (string_of_const thy const), Pretty.fbrk, Syntax.pretty_term ctxt proj]
  1013       | pretty_function (const, Abstr (thm, _)) = pretty_equations const [thm];
  1019       | pretty_function (const, Abstr ((thm, _), _)) = pretty_equations const [thm];
  1014     fun pretty_typ (tyco, vs) = Pretty.str
  1020     fun pretty_typ (tyco, vs) = Pretty.str
  1015       (string_of_typ thy (Type (tyco, map TFree vs)));
  1021       (string_of_typ thy (Type (tyco, map TFree vs)));
  1016     fun pretty_typspec (typ, (cos, abstract)) = if null cos
  1022     fun pretty_typspec (typ, (cos, abstract)) = if null cos
  1017       then pretty_typ typ
  1023       then pretty_typ typ
  1018       else (Pretty.block o Pretty.breaks) (
  1024       else (Pretty.block o Pretty.breaks) (
  1092                 Display.string_of_thm_global thy thm') else (); true)
  1098                 Display.string_of_thm_global thy thm') else (); true)
  1093           else false;
  1099           else false;
  1094       in (thm, proper) :: filter_out drop eqns end;
  1100       in (thm, proper) :: filter_out drop eqns end;
  1095     fun natural_order eqns =
  1101     fun natural_order eqns =
  1096       (eqns, Lazy.lazy (fn () => fold (update_subsume false) eqns []))
  1102       (eqns, Lazy.lazy (fn () => fold (update_subsume false) eqns []))
  1097     fun add_eqn' true (Default (eqns, _)) = Default (natural_order ((thm, proper) :: eqns))
  1103     fun add_eqn' true (Eqns_Default (eqns, _)) = Eqns_Default (natural_order ((thm, proper) :: eqns))
  1098           (*this restores the natural order and drops syntactic redundancies*)
  1104           (*this restores the natural order and drops syntactic redundancies*)
  1099       | add_eqn' true None = Default (natural_order [(thm, proper)])
  1105       | add_eqn' true None = Eqns_Default (natural_order [(thm, proper)])
  1100       | add_eqn' true fun_spec = fun_spec
  1106       | add_eqn' true fun_spec = fun_spec
  1101       | add_eqn' false (Eqns eqns) = Eqns (update_subsume true (thm, proper) eqns)
  1107       | add_eqn' false (Eqns eqns) = Eqns (update_subsume true (thm, proper) eqns)
  1102       | add_eqn' false _ = Eqns [(thm, proper)];
  1108       | add_eqn' false _ = Eqns [(thm, proper)];
  1103   in change_fun_spec c (add_eqn' default) thy end;
  1109   in change_fun_spec c (add_eqn' default) thy end;
  1104 
  1110 
  1105 fun gen_add_abs_eqn raw_thm thy =
  1111 fun gen_add_abs_eqn default raw_thm thy =
  1106   let
  1112   let
  1107     val (abs_thm, tyco) = apfst Thm.close_derivation raw_thm;
  1113     val (abs_thm, tyco) = apfst Thm.close_derivation raw_thm;
  1108     val c = const_abs_eqn thy abs_thm;
  1114     val c = const_abs_eqn thy abs_thm;
  1109   in change_fun_spec c (K (Abstr (abs_thm, tyco))) thy end;
  1115   in change_fun_spec c (K (Abstr ((abs_thm, tyco), default))) thy end;
  1110 
  1116 
  1111 fun add_eqn thm thy =
  1117 fun add_eqn thm thy =
  1112   gen_add_eqn false (mk_eqn thy (thm, true)) thy;
  1118   gen_add_eqn false (mk_eqn thy (thm, true)) thy;
  1113 
  1119 
  1114 fun add_nbe_eqn thm thy =
  1120 fun add_nbe_eqn thm thy =
  1128 
  1134 
  1129 val add_nbe_default_eqn_attribute = Thm.declaration_attribute
  1135 val add_nbe_default_eqn_attribute = Thm.declaration_attribute
  1130   (fn thm => Context.mapping (add_nbe_default_eqn thm) I);
  1136   (fn thm => Context.mapping (add_nbe_default_eqn thm) I);
  1131 val add_nbe_default_eqn_attrib = Attrib.internal (K add_nbe_default_eqn_attribute);
  1137 val add_nbe_default_eqn_attrib = Attrib.internal (K add_nbe_default_eqn_attribute);
  1132 
  1138 
  1133 fun add_abs_eqn raw_thm thy = gen_add_abs_eqn (mk_abs_eqn thy raw_thm) thy;
  1139 fun add_abs_eqn raw_thm thy = gen_add_abs_eqn false (mk_abs_eqn thy raw_thm) thy;
       
  1140 fun add_abs_default_eqn raw_thm thy = gen_add_abs_eqn true (mk_abs_eqn thy raw_thm) thy;
  1134 
  1141 
  1135 val add_abs_eqn_attribute = Thm.declaration_attribute
  1142 val add_abs_eqn_attribute = Thm.declaration_attribute
  1136   (fn thm => Context.mapping (add_abs_eqn thm) I);
  1143   (fn thm => Context.mapping (add_abs_eqn thm) I);
       
  1144 val add_abs_default_eqn_attribute = Thm.declaration_attribute
       
  1145   (fn thm => Context.mapping (add_abs_default_eqn thm) I);
       
  1146 
  1137 val add_abs_eqn_attrib = Attrib.internal (K add_abs_eqn_attribute);
  1147 val add_abs_eqn_attrib = Attrib.internal (K add_abs_eqn_attribute);
       
  1148 val add_abs_default_eqn_attrib = Attrib.internal (K add_abs_default_eqn_attribute);
  1138 
  1149 
  1139 fun add_eqn_maybe_abs thm thy =
  1150 fun add_eqn_maybe_abs thm thy =
  1140   case mk_eqn_maybe_abs thy thm
  1151   case mk_eqn_maybe_abs thy thm
  1141    of SOME (eqn, NONE) => gen_add_eqn false eqn thy
  1152    of SOME (eqn, NONE) => gen_add_eqn false eqn thy
  1142     | SOME ((thm, _), SOME tyco) => gen_add_abs_eqn (thm, tyco) thy
  1153     | SOME ((thm, _), SOME tyco) => gen_add_abs_eqn false (thm, tyco) thy
  1143     | NONE => thy;
  1154     | NONE => thy;
  1144 
  1155 
  1145 fun del_eqn thm thy = case mk_eqn_liberal thy thm
  1156 fun del_eqn thm thy = case mk_eqn_liberal thy thm
  1146  of SOME (thm, _) =>
  1157  of SOME (thm, _) =>
  1147       let
  1158       let
  1148         fun del_eqn' (Default _) = initial_fun_spec
  1159         fun del_eqn' (Eqns_Default _) = initial_fun_spec
  1149           | del_eqn' (Eqns eqns) =
  1160           | del_eqn' (Eqns eqns) =
  1150               let
  1161               let
  1151                 val eqns' = filter_out (fn (thm', _) => Thm.eq_thm_prop (thm, thm')) eqns
  1162                 val eqns' = filter_out (fn (thm', _) => Thm.eq_thm_prop (thm, thm')) eqns
  1152               in if null eqns' then None else Eqns eqns' end
  1163               in if null eqns' then None else Eqns eqns' end
  1153           | del_eqn' spec = spec
  1164           | del_eqn' spec = spec
  1270 fun abstype_interpretation f =
  1281 fun abstype_interpretation f =
  1271   Abstype_Plugin.interpretation abstype_plugin
  1282   Abstype_Plugin.interpretation abstype_plugin
  1272     (fn tyco =>
  1283     (fn tyco =>
  1273       Local_Theory.background_theory (fn thy => f (tyco, get_abstype_spec thy tyco) thy));
  1284       Local_Theory.background_theory (fn thy => f (tyco, get_abstype_spec thy tyco) thy));
  1274 
  1285 
  1275 fun add_abstype proto_thm thy =
  1286 fun gen_add_abstype default proto_thm thy =
  1276   let
  1287   let
  1277     val (tyco, (vs, (abs_ty as (abs, (_, ty)), (rep, cert)))) =
  1288     val (tyco, (vs, (abs_ty as (abs, (_, ty)), (rep, cert)))) =
  1278       error_abs_thm (check_abstype_cert thy) thy proto_thm;
  1289       error_abs_thm (check_abstype_cert thy) thy proto_thm;
  1279   in
  1290   in
  1280     thy
  1291     thy
  1281     |> register_type (tyco, (vs, Abstractor (abs_ty, (rep, cert))))
  1292     |> register_type (tyco, (vs, Abstractor (abs_ty, (rep, cert))))
  1282     |> change_fun_spec rep
  1293     |> change_fun_spec rep
  1283       (K (Proj (Logic.varify_types_global (mk_proj tyco vs ty abs rep), tyco)))
  1294       (K (Proj ((Logic.varify_types_global (mk_proj tyco vs ty abs rep), tyco), default)))
  1284     |> Named_Target.theory_map (Abstype_Plugin.data_default tyco)
  1295     |> Named_Target.theory_map (Abstype_Plugin.data_default tyco)
  1285   end;
  1296   end;
       
  1297 
       
  1298 val add_abstype = gen_add_abstype false;
       
  1299 val add_abstype_default = gen_add_abstype true;
  1286 
  1300 
  1287 
  1301 
  1288 (* setup *)
  1302 (* setup *)
  1289 
  1303 
  1290 val _ = Theory.setup
  1304 val _ = Theory.setup