src/Pure/Tools/codegen_consts.ML
changeset 20600 6d75e02ed285
parent 20456 42be3a46dcd8
child 20704 a56f0743b3ee
equal deleted inserted replaced
20599:65bd267ae23f 20600:6d75e02ed285
    10 sig
    10 sig
    11   type const = string * typ list (*type instantiations*)
    11   type const = string * typ list (*type instantiations*)
    12   val const_ord: const * const -> order
    12   val const_ord: const * const -> order
    13   val eq_const: const * const -> bool
    13   val eq_const: const * const -> bool
    14   structure Consttab: TABLE
    14   structure Consttab: TABLE
    15   val typinst_of_typ: theory -> string * typ -> const
    15   val inst_of_typ: theory -> string * typ -> const
    16   val typ_of_typinst: theory -> const -> string * typ
    16   val typ_of_inst: theory -> const -> string * typ
       
    17   val norm: theory -> const -> const
       
    18   val norm_of_typ: theory -> string * typ -> const
    17   val find_def: theory -> const
    19   val find_def: theory -> const
    18     -> ((string (*theory name*) * string (*definition name*)) * typ list) option
    20     -> ((string (*theory name*) * thm) * typ list) option
    19   val sortinsts: theory -> typ * typ -> (typ * sort) list
    21   val sortinsts: theory -> typ * typ -> (typ * sort) list
    20   val norminst_of_typ: theory -> string * typ -> const
       
    21   val class_of_classop: theory -> const -> class option
    22   val class_of_classop: theory -> const -> class option
    22   val insts_of_classop: theory -> const -> const list
    23   val insts_of_classop: theory -> const -> const list
    23   val typ_of_classop: theory -> const -> typ
    24   val typ_of_classop: theory -> const -> typ
    24   val read_const: theory -> string -> const
    25   val read_const: theory -> string -> const
    25   val read_const_typ: theory -> string -> string * typ
       
    26   val string_of_const: theory -> const -> string
    26   val string_of_const: theory -> const -> string
       
    27   val raw_string_of_const: const -> string
    27   val string_of_const_typ: theory -> string * typ -> string
    28   val string_of_const_typ: theory -> string * typ -> string
    28 end;
    29 end;
    29 
    30 
    30 structure CodegenConsts: CODEGEN_CONSTS =
    31 structure CodegenConsts: CODEGEN_CONSTS =
    31 struct
    32 struct
    44   );
    45   );
    45 
    46 
    46 
    47 
    47 (* type instantiations and overloading *)
    48 (* type instantiations and overloading *)
    48 
    49 
    49 fun typinst_of_typ thy (c_ty as (c, ty)) =
    50 fun inst_of_typ thy (c_ty as (c, ty)) =
    50   (c, Consts.typargs (Sign.consts_of thy) c_ty);
    51   (c, Consts.typargs (Sign.consts_of thy) c_ty);
    51 
    52 
    52 fun typ_of_typinst thy (c_tys as (c, tys)) =
    53 fun typ_of_inst thy (c_tys as (c, tys)) =
    53   (c, Consts.instance (Sign.consts_of thy) c_tys);
    54   (c, Consts.instance (Sign.consts_of thy) c_tys);
    54 
    55 
    55 fun find_def thy (c, tys) =
    56 fun find_def thy (c, tys) =
    56   let
    57   let
    57     val specs = Defs.specifications_of (Theory.defs_of thy) c;
    58     val specs = Defs.specifications_of (Theory.defs_of thy) c;
    59      of SOME _ => let
    60      of SOME _ => let
    60           fun instance_tycos (Type (tyco1, _), Type (tyco2, _)) = tyco1 = tyco2
    61           fun instance_tycos (Type (tyco1, _), Type (tyco2, _)) = tyco1 = tyco2
    61             | instance_tycos (_ , TVar _) = true
    62             | instance_tycos (_ , TVar _) = true
    62             | instance_tycos ty_ty = Sign.typ_instance thy ty_ty;
    63             | instance_tycos ty_ty = Sign.typ_instance thy ty_ty;
    63         in instance_tycos end
    64         in instance_tycos end
    64       | NONE =>  Sign.typ_instance thy
    65       | NONE =>  Sign.typ_instance thy;
       
    66     fun get_def (_, { is_def, thyname, name, lhs, rhs }) =
       
    67       if is_def andalso forall typ_instance (tys ~~ lhs) then
       
    68         case try (Thm.get_axiom_i thy) name
       
    69          of SOME thm => SOME ((thyname, thm), lhs)
       
    70           | NONE => NONE
       
    71       else NONE
    65   in
    72   in
    66     get_first (fn (_, { is_def, thyname, name, lhs, ... }) => if is_def
    73     get_first get_def specs
    67       andalso forall typ_instance (tys ~~ lhs)
       
    68       then SOME ((thyname, name), lhs) else NONE) specs
       
    69   end;
    74   end;
    70 
    75 
    71 fun sortinsts thy (ty, ty_decl) =
    76 fun sortinsts thy (ty, ty_decl) =
    72   Vartab.empty
    77   Vartab.empty
    73   |> Sign.typ_match thy (ty_decl, ty) 
    78   |> Sign.typ_match thy (ty_decl, ty) 
    76 
    81 
    77 fun mk_classop_typinst thy (c, tyco) =
    82 fun mk_classop_typinst thy (c, tyco) =
    78   (c, [Type (tyco, map (fn v => TVar ((v, 0), Sign.defaultS thy (*for monotonicity*)))
    83   (c, [Type (tyco, map (fn v => TVar ((v, 0), Sign.defaultS thy (*for monotonicity*)))
    79     (Name.invents Name.context "'a" (Sign.arity_number thy tyco)))]);
    84     (Name.invents Name.context "'a" (Sign.arity_number thy tyco)))]);
    80 
    85 
    81 fun norminst_of_typ thy (c, ty) =
    86 fun norm thy (c, insts) =
    82   let
    87   let
    83     fun disciplined _ [(Type (tyco, _))] =
    88     fun disciplined _ [(Type (tyco, _))] =
    84           mk_classop_typinst thy (c, tyco)
    89           mk_classop_typinst thy (c, tyco)
    85       | disciplined sort _ =
    90       | disciplined sort _ =
    86           (c, [TVar (("'a", 0), sort)]);
    91           (c, [TVar (("'a", 0), sort)]);
    87     fun ad_hoc c tys =
    92     fun ad_hoc c tys =
    88       case find_def thy (c, tys)
    93       case find_def thy (c, tys)
    89        of SOME (_, tys) => (c, tys)
    94        of SOME (_, tys) => (c, tys)
    90         | NONE => typinst_of_typ thy (c, Sign.the_const_type thy c);
    95         | NONE => inst_of_typ thy (c, Sign.the_const_type thy c);
    91     val tyinsts = Consts.typargs (Sign.consts_of thy) (c, ty);
    96   in case AxClass.class_of_param thy c
    92   in if c = "op =" then disciplined (Sign.defaultS thy) tyinsts
    97      of SOME class => disciplined [class] insts
    93     (*the distinction on op = will finally disappear!*)
    98       | _ => ad_hoc c insts
    94     else case AxClass.class_of_param thy c
       
    95      of SOME class => disciplined [class] tyinsts
       
    96       | _ => ad_hoc c tyinsts
       
    97   end;
    99   end;
       
   100 
       
   101 fun norm_of_typ thy (c, ty) =
       
   102   norm thy (c, Consts.typargs (Sign.consts_of thy) (c, ty));
    98 
   103 
    99 fun class_of_classop thy (c, [TVar _]) = 
   104 fun class_of_classop thy (c, [TVar _]) = 
   100       AxClass.class_of_param thy c
   105       AxClass.class_of_param thy c
   101   | class_of_classop thy (c, [TFree _]) = 
   106   | class_of_classop thy (c, [TFree _]) = 
   102       AxClass.class_of_param thy c
   107       AxClass.class_of_param thy c
   146 
   151 
   147 fun read_const_typ thy raw_t =
   152 fun read_const_typ thy raw_t =
   148   let
   153   let
   149     val t = Sign.read_term thy raw_t
   154     val t = Sign.read_term thy raw_t
   150   in case try dest_Const t
   155   in case try dest_Const t
   151    of SOME c_ty => c_ty
   156    of SOME c_ty => (typ_of_inst thy o norm_of_typ thy) c_ty
   152     | NONE => error ("Not a constant: " ^ Sign.string_of_term thy t)
   157     | NONE => error ("Not a constant: " ^ Sign.string_of_term thy t)
   153   end;
   158   end;
   154 
   159 
   155 fun read_const thy =
   160 fun read_const thy =
   156   typinst_of_typ thy o read_const_typ thy;
   161   norm_of_typ thy o read_const_typ thy;
   157 
   162 
   158 
   163 
   159 (* printing constants *)
   164 (* printing constants *)
   160 
   165 
   161 fun string_of_const thy (c, tys) =
   166 fun string_of_const thy (c, tys) =
   162   space_implode " " (Sign.extern_const thy c
   167   space_implode " " (Sign.extern_const thy c
   163     :: map (enclose "[" "]" o Sign.string_of_typ thy) tys);
   168     :: map (enclose "[" "]" o Sign.string_of_typ thy) tys);
   164 
   169 
       
   170 fun raw_string_of_const (c, tys) =
       
   171   space_implode " " (c
       
   172     :: map (enclose "[" "]" o Display.raw_string_of_typ) tys);
       
   173 
   165 fun string_of_const_typ thy (c, ty) =
   174 fun string_of_const_typ thy (c, ty) =
   166   string_of_const thy (c, Consts.typargs (Sign.consts_of thy) (c, ty));
   175   string_of_const thy (c, Consts.typargs (Sign.consts_of thy) (c, ty));
   167 
   176 
   168 end;
   177 end;