src/Pure/Isar/code.ML
changeset 63239 d562c9948dee
parent 63238 7c593d4f1f89
child 63240 f82c0b803bda
equal deleted inserted replaced
63238:7c593d4f1f89 63239:d562c9948dee
    44   val add_abstype: thm -> theory -> theory
    44   val add_abstype: thm -> theory -> theory
    45   val add_abstype_default: thm -> theory -> theory
    45   val add_abstype_default: thm -> theory -> theory
    46   val abstype_interpretation:
    46   val abstype_interpretation:
    47     (string * ((string * sort) list * ((string * ((string * sort) list * typ)) * (string * thm)))
    47     (string * ((string * sort) list * ((string * ((string * sort) list * typ)) * (string * thm)))
    48       -> theory -> theory) -> theory -> theory
    48       -> theory -> theory) -> theory -> theory
    49   val add_eqn: thm -> theory -> theory
    49   type kind
    50   val add_nbe_eqn: thm -> theory -> theory
    50   val Equation: kind
    51   val add_abs_eqn: thm -> theory -> theory
    51   val Nbe: kind
    52   val add_default_eqn: thm -> theory -> theory
    52   val Abstract: kind
    53   val add_default_eqn_attribute: attribute
    53   val add_eqn: kind * bool -> thm -> theory -> theory
    54   val add_default_eqn_attrib: Token.src
    54   val add_default_eqn_attribute: kind -> attribute
    55   val add_nbe_default_eqn: thm -> theory -> theory
    55   val add_default_eqn_attrib: kind -> Token.src
    56   val add_nbe_default_eqn_attribute: attribute
       
    57   val add_nbe_default_eqn_attrib: Token.src
       
    58   val add_abs_default_eqn: thm -> theory -> theory
       
    59   val add_abs_default_eqn_attribute: attribute
       
    60   val add_abs_default_eqn_attrib: Token.src
       
    61   val del_eqn: thm -> theory -> theory
    56   val del_eqn: thm -> theory -> theory
    62   val del_eqns: string -> theory -> theory
    57   val del_eqns: string -> theory -> theory
    63   val del_exception: string -> theory -> theory
    58   val del_exception: string -> theory -> theory
    64   val add_case: thm -> theory -> theory
    59   val add_case: thm -> theory -> theory
    65   val add_undefined: string -> theory -> theory
    60   val add_undefined: string -> theory -> theory
  1071 
  1066 
  1072 (** declaring executable ingredients **)
  1067 (** declaring executable ingredients **)
  1073 
  1068 
  1074 (* code equations *)
  1069 (* code equations *)
  1075 
  1070 
       
  1071 (*
       
  1072   external distinction:
       
  1073     kind * default
       
  1074   processual distinction:
       
  1075     thm * proper  for concrete equations
       
  1076     thm  for abstract equations
       
  1077 *)
       
  1078 
  1076 fun gen_add_eqn default (raw_thm, proper) thy =
  1079 fun gen_add_eqn default (raw_thm, proper) thy =
  1077   let
  1080   let
  1078     val thm = Thm.close_derivation raw_thm;
  1081     val thm = Thm.close_derivation raw_thm;
  1079     val c = const_eqn thy thm;
  1082     val c = const_eqn thy thm;
  1080     fun update_subsume verbose (thm, proper) eqns = 
  1083     fun update_subsume verbose (thm, proper) eqns = 
  1110   let
  1113   let
  1111     val (abs_thm, tyco) = apfst Thm.close_derivation raw_thm;
  1114     val (abs_thm, tyco) = apfst Thm.close_derivation raw_thm;
  1112     val c = const_abs_eqn thy abs_thm;
  1115     val c = const_abs_eqn thy abs_thm;
  1113   in change_fun_spec c (K (Abstr ((abs_thm, tyco), default))) thy end;
  1116   in change_fun_spec c (K (Abstr ((abs_thm, tyco), default))) thy end;
  1114 
  1117 
  1115 fun add_eqn thm thy =
  1118 datatype kind = Equation | Nbe | Abstract;
  1116   gen_add_eqn false (mk_eqn thy (thm, true)) thy;
  1119 
  1117 
  1120 fun add_eqn (kind, default) thm thy =
  1118 fun add_nbe_eqn thm thy =
  1121   case (kind, default) of
  1119   gen_add_eqn false (mk_eqn thy (thm, false)) thy;
  1122     (Equation, true) => (case mk_eqn_liberal thy thm of
  1120 
  1123       SOME eqn => gen_add_eqn true eqn thy
  1121 fun add_default_eqn thm thy =
  1124     | NONE => thy)
  1122   case mk_eqn_liberal thy thm
  1125   | (Equation, false) => gen_add_eqn false (mk_eqn thy (thm, true)) thy
  1123    of SOME eqn => gen_add_eqn true eqn thy
  1126   | (Nbe, _) => gen_add_eqn default (mk_eqn thy (thm, false)) thy
  1124     | NONE => thy;
  1127   | (Abstract, _) => gen_add_abs_eqn default (mk_abs_eqn thy thm) thy;
  1125 
  1128 
  1126 val add_default_eqn_attribute = Thm.declaration_attribute
  1129 fun lift_attribute f = Thm.declaration_attribute
  1127   (fn thm => Context.mapping (add_default_eqn thm) I);
  1130   (fn thm => Context.mapping (f thm) I);
  1128 val add_default_eqn_attrib = Attrib.internal (K add_default_eqn_attribute);
  1131 
  1129 
  1132 fun add_default_eqn_attribute kind =
  1130 fun add_nbe_default_eqn thm thy =
  1133   lift_attribute (add_eqn (kind, true));
  1131   gen_add_eqn true (mk_eqn thy (thm, false)) thy;
  1134 
  1132 
  1135 fun add_default_eqn_attrib kind =
  1133 val add_nbe_default_eqn_attribute = Thm.declaration_attribute
  1136   Attrib.internal (K (add_default_eqn_attribute kind));
  1134   (fn thm => Context.mapping (add_nbe_default_eqn thm) I);
       
  1135 val add_nbe_default_eqn_attrib = Attrib.internal (K add_nbe_default_eqn_attribute);
       
  1136 
       
  1137 fun add_abs_eqn raw_thm thy = gen_add_abs_eqn false (mk_abs_eqn thy raw_thm) thy;
       
  1138 fun add_abs_default_eqn raw_thm thy = gen_add_abs_eqn true (mk_abs_eqn thy raw_thm) thy;
       
  1139 
       
  1140 val add_abs_default_eqn_attribute = Thm.declaration_attribute
       
  1141   (fn thm => Context.mapping (add_abs_default_eqn thm) I);
       
  1142 val add_abs_default_eqn_attrib = Attrib.internal (K add_abs_default_eqn_attribute);
       
  1143 
  1137 
  1144 fun add_eqn_maybe_abs thm thy =
  1138 fun add_eqn_maybe_abs thm thy =
  1145   case mk_eqn_maybe_abs thy thm
  1139   case mk_eqn_maybe_abs thy thm
  1146    of SOME (eqn, NONE) => gen_add_eqn false eqn thy
  1140    of SOME (eqn, NONE) => gen_add_eqn false eqn thy
  1147     | SOME ((thm, _), SOME tyco) => gen_add_abs_eqn false (thm, tyco) thy
  1141     | SOME ((thm, _), SOME tyco) => gen_add_abs_eqn false (thm, tyco) thy
  1295 
  1289 
  1296 (* setup *)
  1290 (* setup *)
  1297 
  1291 
  1298 val _ = Theory.setup
  1292 val _ = Theory.setup
  1299   (let
  1293   (let
  1300     fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
  1294     fun lift_const_attribute f cs =
  1301     fun mk_const_attribute f cs =
  1295       lift_attribute (K (fold (fn c => fn thy => f (read_const thy c) thy) cs));
  1302       mk_attribute (K (fold (fn c => fn thy => f (read_const thy c) thy) cs));
       
  1303     val code_attribute_parser =
  1296     val code_attribute_parser =
  1304       Args.$$$ "equation" |-- Scan.succeed (mk_attribute add_eqn)
  1297       Args.$$$ "equation" |-- Scan.succeed (lift_attribute (add_eqn (Equation, false)))
  1305       || Args.$$$ "nbe" |-- Scan.succeed (mk_attribute add_nbe_eqn)
  1298       || Args.$$$ "nbe" |-- Scan.succeed (lift_attribute (add_eqn (Nbe, false)))
  1306       || Args.$$$ "abstype" |-- Scan.succeed (mk_attribute add_abstype)
  1299       || Args.$$$ "abstract" |-- Scan.succeed (lift_attribute (add_eqn (Abstract, false)))
  1307       || Args.$$$ "abstract" |-- Scan.succeed (mk_attribute add_abs_eqn)
  1300       || Args.$$$ "abstype" |-- Scan.succeed (lift_attribute add_abstype)
  1308       || Args.del |-- Scan.succeed (mk_attribute del_eqn)
  1301       || Args.del |-- Scan.succeed (lift_attribute del_eqn)
  1309       || Args.$$$ "drop" -- Args.colon |-- (Scan.repeat1 Parse.term >> mk_const_attribute del_eqns)
  1302       || Args.$$$ "drop" -- Args.colon |-- (Scan.repeat1 Parse.term >> lift_const_attribute del_eqns)
  1310       || Args.$$$ "abort" -- Args.colon |-- (Scan.repeat1 Parse.term >> mk_const_attribute del_exception)
  1303       || Args.$$$ "abort" -- Args.colon |-- (Scan.repeat1 Parse.term >> lift_const_attribute del_exception)
  1311       || Scan.succeed (mk_attribute add_eqn_maybe_abs);
  1304       || Scan.succeed (lift_attribute add_eqn_maybe_abs);
  1312   in
  1305   in
  1313     Attrib.setup @{binding code} (Scan.lift code_attribute_parser)
  1306     Attrib.setup @{binding code} (Scan.lift code_attribute_parser)
  1314         "declare theorems for code generation"
  1307         "declare theorems for code generation"
  1315   end);
  1308   end);
  1316 
  1309