src/Pure/Isar/class_target.ML
changeset 29632 c3d576157244
parent 29610 83d282f12352
child 29969 9dbb046136d0
equal deleted inserted replaced
29631:3aa049e5f156 29632:c3d576157244
    27     -> (string * mixfix) * term -> theory -> theory
    27     -> (string * mixfix) * term -> theory -> theory
    28   val abbrev: class -> Syntax.mode -> Properties.T
    28   val abbrev: class -> Syntax.mode -> Properties.T
    29     -> (string * mixfix) * term -> theory -> theory
    29     -> (string * mixfix) * term -> theory -> theory
    30   val class_prefix: string -> string
    30   val class_prefix: string -> string
    31   val refresh_syntax: class -> Proof.context -> Proof.context
    31   val refresh_syntax: class -> Proof.context -> Proof.context
       
    32   val redeclare_operations: theory -> sort -> Proof.context -> Proof.context
    32 
    33 
    33   (*instances*)
    34   (*instances*)
    34   val init_instantiation: string list * (string * sort) list * sort
    35   val init_instantiation: string list * (string * sort) list * sort
    35     -> theory -> local_theory
    36     -> theory -> local_theory
    36   val instantiation_instance: (local_theory -> local_theory)
    37   val instantiation_instance: (local_theory -> local_theory)
   296 (* class context syntax *)
   297 (* class context syntax *)
   297 
   298 
   298 fun these_unchecks thy =
   299 fun these_unchecks thy =
   299   map (fn (c, (_, (ty, t))) => (t, Const (c, ty))) o these_operations thy;
   300   map (fn (c, (_, (ty, t))) => (t, Const (c, ty))) o these_operations thy;
   300 
   301 
       
   302 fun redeclare_const thy c =
       
   303   let val b = Sign.base_name c
       
   304   in Sign.intern_const thy b = c ? Variable.declare_const (b, c) end;
       
   305 
   301 fun synchronize_class_syntax sort base_sort ctxt =
   306 fun synchronize_class_syntax sort base_sort ctxt =
   302   let
   307   let
   303     val thy = ProofContext.theory_of ctxt;
   308     val thy = ProofContext.theory_of ctxt;
   304     val algebra = Sign.classes_of thy;
   309     val algebra = Sign.classes_of thy;
   305     val operations = these_operations thy sort;
   310     val operations = these_operations thy sort;
   306     fun subst_class_typ sort = map_type_tfree (K (TVar ((Name.aT, 0), sort)));
   311     fun subst_class_typ sort = map_type_tfree (K (TVar ((Name.aT, 0), sort)));
   307     val primary_constraints =
   312     val primary_constraints =
   308       (map o apsnd) (subst_class_typ base_sort o fst o snd) operations;
   313       (map o apsnd) (subst_class_typ base_sort o fst o snd) operations;
   309     val secondary_constraints =
   314     val secondary_constraints =
   310       (map o apsnd) (fn (class, (ty, _)) => subst_class_typ [class] ty) operations;
   315       (map o apsnd) (fn (class, (ty, _)) => subst_class_typ [class] ty) operations;
   311     fun declare_const (c, _) =
       
   312       let val b = Sign.base_name c
       
   313       in Sign.intern_const thy b = c ? Variable.declare_const (b, c) end;
       
   314     fun improve (c, ty) = (case AList.lookup (op =) primary_constraints c
   316     fun improve (c, ty) = (case AList.lookup (op =) primary_constraints c
   315      of SOME ty' => (case try (Type.raw_match (ty', ty)) Vartab.empty
   317      of SOME ty' => (case try (Type.raw_match (ty', ty)) Vartab.empty
   316          of SOME tyenv => (case Vartab.lookup tyenv (Name.aT, 0)
   318          of SOME tyenv => (case Vartab.lookup tyenv (Name.aT, 0)
   317              of SOME (_, ty' as TVar (tvar as (vi, sort))) =>
   319              of SOME (_, ty' as TVar (tvar as (vi, sort))) =>
   318                   if TypeInfer.is_param vi
   320                   if TypeInfer.is_param vi
   324       | NONE => NONE)
   326       | NONE => NONE)
   325     fun subst (c, ty) = Option.map snd (AList.lookup (op =) operations c);
   327     fun subst (c, ty) = Option.map snd (AList.lookup (op =) operations c);
   326     val unchecks = these_unchecks thy sort;
   328     val unchecks = these_unchecks thy sort;
   327   in
   329   in
   328     ctxt
   330     ctxt
   329     |> fold declare_const primary_constraints
   331     |> fold (redeclare_const thy o fst) primary_constraints
   330     |> Overloading.map_improvable_syntax (K (((primary_constraints, secondary_constraints),
   332     |> Overloading.map_improvable_syntax (K (((primary_constraints, secondary_constraints),
   331         (((improve, subst), true), unchecks)), false))
   333         (((improve, subst), true), unchecks)), false))
   332     |> Overloading.set_primary_constraints
   334     |> Overloading.set_primary_constraints
   333   end;
   335   end;
   334 
   336 
   335 fun refresh_syntax class ctxt =
   337 fun refresh_syntax class ctxt =
   336   let
   338   let
   337     val thy = ProofContext.theory_of ctxt;
   339     val thy = ProofContext.theory_of ctxt;
   338     val base_sort = base_sort thy class;
   340     val base_sort = base_sort thy class;
   339   in synchronize_class_syntax [class] base_sort ctxt end;
   341   in synchronize_class_syntax [class] base_sort ctxt end;
       
   342 
       
   343 fun redeclare_operations thy sort =
       
   344   fold (redeclare_const thy o fst) (these_operations thy sort);
   340 
   345 
   341 fun begin sort base_sort ctxt =
   346 fun begin sort base_sort ctxt =
   342   ctxt
   347   ctxt
   343   |> Variable.declare_term
   348   |> Variable.declare_term
   344       (Logic.mk_type (TFree (Name.aT, base_sort)))
   349       (Logic.mk_type (TFree (Name.aT, base_sort)))
   487 
   492 
   488 fun init_instantiation (tycos, vs, sort) thy =
   493 fun init_instantiation (tycos, vs, sort) thy =
   489   let
   494   let
   490     val _ = if null tycos then error "At least one arity must be given" else ();
   495     val _ = if null tycos then error "At least one arity must be given" else ();
   491     val params = these_params thy sort;
   496     val params = these_params thy sort;
   492     fun get_param tyco (param, (_, (c, ty))) = if can (AxClass.param_of_inst thy) (c, tyco)
   497     fun get_param tyco (param, (_, (c, ty))) =
       
   498       if can (AxClass.param_of_inst thy) (c, tyco)
   493       then NONE else SOME ((c, tyco),
   499       then NONE else SOME ((c, tyco),
   494         (param ^ "_" ^ type_name tyco, map_atyps (K (Type (tyco, map TFree vs))) ty));
   500         (param ^ "_" ^ type_name tyco, map_atyps (K (Type (tyco, map TFree vs))) ty));
   495     val inst_params = map_product get_param tycos params |> map_filter I;
   501     val inst_params = map_product get_param tycos params |> map_filter I;
   496     val primary_constraints = map (apsnd
   502     val primary_constraints = map (apsnd
   497       (map_atyps (K (TVar ((Name.aT, 0), [])))) o snd o snd) params;
   503       (map_atyps (K (TVar ((Name.aT, 0), [])))) o snd o snd) params;