src/Pure/Isar/code.ML
changeset 40726 16dcfedc4eb7
parent 40564 6827505e96e1
child 40758 4f2c3e842ef8
equal deleted inserted replaced
40717:88f2955a111e 40726:16dcfedc4eb7
    19   val subst_signatures: theory -> term -> term
    19   val subst_signatures: theory -> term -> term
    20   val args_number: theory -> string -> int
    20   val args_number: theory -> string -> int
    21 
    21 
    22   (*constructor sets*)
    22   (*constructor sets*)
    23   val constrset_of_consts: theory -> (string * typ) list
    23   val constrset_of_consts: theory -> (string * typ) list
    24     -> string * ((string * sort) list * (string * typ list) list)
    24     -> string * ((string * sort) list * (string * ((string * sort) list * typ list)) list)
    25 
    25 
    26   (*code equations and certificates*)
    26   (*code equations and certificates*)
    27   val mk_eqn: theory -> thm * bool -> thm * bool
    27   val mk_eqn: theory -> thm * bool -> thm * bool
    28   val mk_eqn_warning: theory -> thm -> (thm * bool) option
    28   val mk_eqn_warning: theory -> thm -> (thm * bool) option
    29   val mk_eqn_liberal: theory -> thm -> (thm * bool) option
    29   val mk_eqn_liberal: theory -> thm -> (thm * bool) option
    46   val add_signature: string * typ -> theory -> theory
    46   val add_signature: string * typ -> theory -> theory
    47   val add_signature_cmd: string * string -> theory -> theory
    47   val add_signature_cmd: string * string -> theory -> theory
    48   val add_datatype: (string * typ) list -> theory -> theory
    48   val add_datatype: (string * typ) list -> theory -> theory
    49   val add_datatype_cmd: string list -> theory -> theory
    49   val add_datatype_cmd: string list -> theory -> theory
    50   val datatype_interpretation:
    50   val datatype_interpretation:
    51     (string * ((string * sort) list * (string * typ list) list)
    51     (string * ((string * sort) list * (string * ((string * sort) list * typ list)) list)
    52       -> theory -> theory) -> theory -> theory
    52       -> theory -> theory) -> theory -> theory
    53   val add_abstype: thm -> theory -> theory
    53   val add_abstype: thm -> theory -> theory
    54   val abstype_interpretation:
    54   val abstype_interpretation:
    55     (string * ((string * sort) list * ((string * typ) * (string * thm)))
    55     (string * ((string * sort) list * ((string * ((string * sort) list * typ)) * (string * thm)))
    56       -> theory -> theory) -> theory -> theory
    56       -> theory -> theory) -> theory -> theory
    57   val add_eqn: thm -> theory -> theory
    57   val add_eqn: thm -> theory -> theory
    58   val add_nbe_eqn: thm -> theory -> theory
    58   val add_nbe_eqn: thm -> theory -> theory
    59   val add_default_eqn: thm -> theory -> theory
    59   val add_default_eqn: thm -> theory -> theory
    60   val add_default_eqn_attribute: attribute
    60   val add_default_eqn_attribute: attribute
    64   val add_nbe_default_eqn_attrib: Attrib.src
    64   val add_nbe_default_eqn_attrib: Attrib.src
    65   val del_eqn: thm -> theory -> theory
    65   val del_eqn: thm -> theory -> theory
    66   val del_eqns: string -> theory -> theory
    66   val del_eqns: string -> theory -> theory
    67   val add_case: thm -> theory -> theory
    67   val add_case: thm -> theory -> theory
    68   val add_undefined: string -> theory -> theory
    68   val add_undefined: string -> theory -> theory
    69   val get_type: theory -> string -> ((string * sort) list * ((string * string list) * typ list) list)
    69   val get_type: theory -> string
       
    70     -> ((string * sort) list * (string * ((string * sort) list * typ list)) list) * bool
    70   val get_type_of_constr_or_abstr: theory -> string -> (string * bool) option
    71   val get_type_of_constr_or_abstr: theory -> string -> (string * bool) option
    71   val is_constr: theory -> string -> bool
    72   val is_constr: theory -> string -> bool
    72   val is_abstr: theory -> string -> bool
    73   val is_abstr: theory -> string -> bool
    73   val get_cert: theory -> ((thm * bool) list -> (thm * bool) list) -> string -> cert
    74   val get_cert: theory -> ((thm * bool) list -> (thm * bool) list) -> string -> cert
    74   val get_case_scheme: theory -> string -> (int * (int * string list)) option
    75   val get_case_scheme: theory -> string -> (int * (int * string list)) option
   145 
   146 
   146 (** data store **)
   147 (** data store **)
   147 
   148 
   148 (* datatypes *)
   149 (* datatypes *)
   149 
   150 
   150 datatype typ_spec = Constructors of (string * typ list) list
   151 datatype typ_spec = Constructors of (string * ((string * sort) list * typ list)) list
   151   | Abstractor of (string * typ) * (string * thm);
   152   | Abstractor of (string * ((string * sort) list * typ)) * (string * thm);
   152 
   153 
   153 fun constructors_of (Constructors cos) = (cos, false)
   154 fun constructors_of (Constructors cos) = (cos, false)
   154   | constructors_of (Abstractor ((co, ty), _)) = ([(co, [ty])], true);
   155   | constructors_of (Abstractor ((co, (vs, ty)), _)) = ([(co, (vs, [ty]))], true);
   155 
   156 
   156 
   157 
   157 (* functions *)
   158 (* functions *)
   158 
   159 
   159 datatype fun_spec = Default of (thm * bool) list * (thm * bool) list lazy
   160 datatype fun_spec = Default of (thm * bool) list * (thm * bool) list lazy
   410       in ((tyco, sorts), c :: cs) end;
   411       in ((tyco, sorts), c :: cs) end;
   411     fun inst vs' (c, (vs, ty)) =
   412     fun inst vs' (c, (vs, ty)) =
   412       let
   413       let
   413         val the_v = the o AList.lookup (op =) (vs ~~ vs');
   414         val the_v = the o AList.lookup (op =) (vs ~~ vs');
   414         val ty' = map_atyps (fn TFree (v, _) => TFree (the_v v)) ty;
   415         val ty' = map_atyps (fn TFree (v, _) => TFree (the_v v)) ty;
   415       in (c, (fst o strip_type) ty') end;
   416         val vs'' = map dest_TFree (Sign.const_typargs thy (c, ty'));
       
   417       in (c, (vs'', (fst o strip_type) ty')) end;
   416     val c' :: cs' = map (ty_sorts thy) cs;
   418     val c' :: cs' = map (ty_sorts thy) cs;
   417     val ((tyco, sorts), cs'') = fold add cs' (apsnd single c');
   419     val ((tyco, sorts), cs'') = fold add cs' (apsnd single c');
   418     val vs = Name.names Name.context Name.aT sorts;
   420     val vs = Name.names Name.context Name.aT sorts;
   419     val cs''' = map (inst vs) cs'';
   421     val cs''' = map (inst vs) cs'';
   420   in (tyco, (vs, rev cs''')) end;
   422   in (tyco, (vs, rev cs''')) end;
   421 
   423 
   422 fun get_type_entry thy tyco = case these (Symtab.lookup ((the_types o the_exec) thy) tyco)
   424 fun get_type_entry thy tyco = case these (Symtab.lookup ((the_types o the_exec) thy) tyco)
   423  of (_, entry) :: _ => SOME entry
   425  of (_, entry) :: _ => SOME entry
   424   | _ => NONE;
   426   | _ => NONE;
   425 
   427 
   426 fun get_type_spec thy tyco = case get_type_entry thy tyco
   428 fun get_type thy tyco = case get_type_entry thy tyco
   427  of SOME (vs, spec) => apfst (pair vs) (constructors_of spec)
   429  of SOME (vs, spec) => apfst (pair vs) (constructors_of spec)
   428   | NONE => arity_number thy tyco
   430   | NONE => arity_number thy tyco
   429       |> Name.invents Name.context Name.aT
   431       |> Name.invents Name.context Name.aT
   430       |> map (rpair [])
   432       |> map (rpair [])
   431       |> rpair []
   433       |> rpair []
   433 
   435 
   434 fun get_abstype_spec thy tyco = case get_type_entry thy tyco
   436 fun get_abstype_spec thy tyco = case get_type_entry thy tyco
   435  of SOME (vs, Abstractor spec) => (vs, spec)
   437  of SOME (vs, Abstractor spec) => (vs, spec)
   436   | _ => error ("Not an abstract type: " ^ tyco);
   438   | _ => error ("Not an abstract type: " ^ tyco);
   437  
   439  
   438 fun get_type thy tyco =
       
   439   let
       
   440     val ((vs, cos), _) = get_type_spec thy tyco;
       
   441     fun args_of c tys = map (fst o dest_TFree)
       
   442       (Sign.const_typargs thy (c, tys ---> Type (tyco, map TFree vs)));
       
   443     fun add_typargs (c, tys) = ((c, args_of c tys), tys);
       
   444   in (vs, map add_typargs cos) end;
       
   445 
       
   446 fun get_type_of_constr_or_abstr thy c =
   440 fun get_type_of_constr_or_abstr thy c =
   447   case (snd o strip_type o const_typ thy) c
   441   case (snd o strip_type o const_typ thy) c
   448    of Type (tyco, _) => let val ((vs, cos), abstract) = get_type_spec thy tyco
   442    of Type (tyco, _) => let val ((vs, cos), abstract) = get_type thy tyco
   449         in if member (op =) (map fst cos) c then SOME (tyco, abstract) else NONE end
   443         in if member (op =) (map fst cos) c then SOME (tyco, abstract) else NONE end
   450     | _ => NONE;
   444     | _ => NONE;
   451 
   445 
   452 fun is_constr thy c = case get_type_of_constr_or_abstr thy c
   446 fun is_constr thy c = case get_type_of_constr_or_abstr thy c
   453  of SOME (_, false) => true
   447  of SOME (_, false) => true
   681     val var = (fst o dest_Var) param
   675     val var = (fst o dest_Var) param
   682       handle TERM _ => bad "Not an abstype certificate";
   676       handle TERM _ => bad "Not an abstype certificate";
   683     val _ = if param = rhs then () else bad "Not an abstype certificate";
   677     val _ = if param = rhs then () else bad "Not an abstype certificate";
   684     val ((tyco, sorts), (abs, (vs, ty'))) = ty_sorts thy (abs, Logic.unvarifyT_global raw_ty);
   678     val ((tyco, sorts), (abs, (vs, ty'))) = ty_sorts thy (abs, Logic.unvarifyT_global raw_ty);
   685     val ty = domain_type ty';
   679     val ty = domain_type ty';
       
   680     val vs' = map dest_TFree (Sign.const_typargs thy (abs, ty'));
   686     val ty_abs = range_type ty';
   681     val ty_abs = range_type ty';
   687   in (tyco, (vs ~~ sorts, ((abs, ty), (rep, thm)))) end;
   682   in (tyco, (vs ~~ sorts, ((abs, (vs', ty)), (rep, thm)))) end;
   688 
   683 
   689 
   684 
   690 (* code equation certificates *)
   685 (* code equation certificates *)
   691 
   686 
   692 fun build_head thy (c, ty) =
   687 fun build_head thy (c, ty) =
   782         val cert_thm = Conjunction.intr_balanced (map rewrite_head thms);
   777         val cert_thm = Conjunction.intr_balanced (map rewrite_head thms);
   783       in Equations (cert_thm, propers) end;
   778       in Equations (cert_thm, propers) end;
   784 
   779 
   785 fun cert_of_proj thy c tyco =
   780 fun cert_of_proj thy c tyco =
   786   let
   781   let
   787     val (vs, ((abs, ty), (rep, cert))) = get_abstype_spec thy tyco;
   782     val (vs, ((abs, (_, ty)), (rep, cert))) = get_abstype_spec thy tyco;
   788     val _ = if c = rep then () else
   783     val _ = if c = rep then () else
   789       error ("Wrong head of projection,\nexpected constant " ^ string_of_const thy rep);
   784       error ("Wrong head of projection,\nexpected constant " ^ string_of_const thy rep);
   790   in Projection (mk_proj tyco vs ty abs rep, tyco) end;
   785   in Projection (mk_proj tyco vs ty abs rep, tyco) end;
   791 
   786 
   792 fun cert_of_abs thy tyco c raw_abs_thm =
   787 fun cert_of_abs thy tyco c raw_abs_thm =
   977       then pretty_typ typ
   972       then pretty_typ typ
   978       else (Pretty.block o Pretty.breaks) (
   973       else (Pretty.block o Pretty.breaks) (
   979         pretty_typ typ
   974         pretty_typ typ
   980         :: Pretty.str "="
   975         :: Pretty.str "="
   981         :: (if abstract then [Pretty.str "(abstract)"] else [])
   976         :: (if abstract then [Pretty.str "(abstract)"] else [])
   982         @ separate (Pretty.str "|") (map (fn (c, []) => Pretty.str (string_of_const thy c)
   977         @ separate (Pretty.str "|") (map (fn (c, (_, [])) => Pretty.str (string_of_const thy c)
   983              | (c, tys) =>
   978              | (c, (_, tys)) =>
   984                  (Pretty.block o Pretty.breaks)
   979                  (Pretty.block o Pretty.breaks)
   985                     (Pretty.str (string_of_const thy c)
   980                     (Pretty.str (string_of_const thy c)
   986                       :: Pretty.str "of"
   981                       :: Pretty.str "of"
   987                       :: map (Pretty.quote o Syntax.pretty_typ_global thy) tys)) cos)
   982                       :: map (Pretty.quote o Syntax.pretty_typ_global thy) tys)) cos)
   988       );
   983       );
  1200 
  1195 
  1201 structure Datatype_Interpretation =
  1196 structure Datatype_Interpretation =
  1202   Interpretation(type T = string * serial val eq = eq_snd (op =) : T * T -> bool);
  1197   Interpretation(type T = string * serial val eq = eq_snd (op =) : T * T -> bool);
  1203 
  1198 
  1204 fun datatype_interpretation f = Datatype_Interpretation.interpretation
  1199 fun datatype_interpretation f = Datatype_Interpretation.interpretation
  1205   (fn (tyco, _) => fn thy => f (tyco, fst (get_type_spec thy tyco)) thy);
  1200   (fn (tyco, _) => fn thy => f (tyco, fst (get_type thy tyco)) thy);
  1206 
  1201 
  1207 fun add_datatype proto_constrs thy =
  1202 fun add_datatype proto_constrs thy =
  1208   let
  1203   let
  1209     val constrs = map (unoverload_const_typ thy) proto_constrs;
  1204     val constrs = map (unoverload_const_typ thy) proto_constrs;
  1210     val (tyco, (vs, cos)) = constrset_of_consts thy constrs;
  1205     val (tyco, (vs, cos)) = constrset_of_consts thy constrs;
  1224 fun abstype_interpretation f = Abstype_Interpretation.interpretation
  1219 fun abstype_interpretation f = Abstype_Interpretation.interpretation
  1225   (fn (tyco, _) => fn thy => f (tyco, get_abstype_spec thy tyco) thy);
  1220   (fn (tyco, _) => fn thy => f (tyco, get_abstype_spec thy tyco) thy);
  1226 
  1221 
  1227 fun add_abstype proto_thm thy =
  1222 fun add_abstype proto_thm thy =
  1228   let
  1223   let
  1229     val (tyco, (vs, (abs_ty as (abs, ty), (rep, cert)))) =
  1224     val (tyco, (vs, (abs_ty as (abs, (_, ty)), (rep, cert)))) =
  1230       error_thm (check_abstype_cert thy) proto_thm;
  1225       error_thm (check_abstype_cert thy) proto_thm;
  1231   in
  1226   in
  1232     thy
  1227     thy
  1233     |> del_eqns abs
  1228     |> del_eqns abs
  1234     |> register_type (tyco, (vs, Abstractor (abs_ty, (rep, cert))))
  1229     |> register_type (tyco, (vs, Abstractor (abs_ty, (rep, cert))))