src/Pure/Isar/code.ML
changeset 49534 791e6fc53f73
parent 49533 484ac6cb13e6
child 49535 e016736fbe0a
equal deleted inserted replaced
49533:484ac6cb13e6 49534:791e6fc53f73
   114   end;
   114   end;
   115 
   115 
   116 
   116 
   117 (* constants *)
   117 (* constants *)
   118 
   118 
       
   119 fun const_typ thy = Type.strip_sorts o Sign.the_const_type thy;
       
   120 
       
   121 fun args_number thy = length o binder_types o const_typ thy;
       
   122 
       
   123 fun devarify ty =
       
   124   let
       
   125     val tys = fold_atyps (fn TVar vi_sort => AList.update (op =) vi_sort) ty [];
       
   126     val vs = Name.invent Name.context Name.aT (length tys);
       
   127     val mapping = map2 (fn v => fn (vi, sort) => (vi, TFree (v, sort))) vs tys;
       
   128   in Term.typ_subst_TVars mapping ty end;
       
   129 
       
   130 fun typscheme thy (c, ty) =
       
   131   (map dest_TFree (Sign.const_typargs thy (c, ty)), Type.strip_sorts ty);
       
   132 
       
   133 fun typscheme_equiv (ty1, ty2) =
       
   134   Type.raw_instance (devarify ty1, ty2) andalso Type.raw_instance (devarify ty2, ty1);
       
   135 
   119 fun check_bare_const thy t = case try dest_Const t
   136 fun check_bare_const thy t = case try dest_Const t
   120  of SOME c_ty => c_ty
   137  of SOME c_ty => c_ty
   121   | NONE => error ("Not a constant: " ^ Syntax.string_of_term_global thy t);
   138   | NONE => error ("Not a constant: " ^ Syntax.string_of_term_global thy t);
   122 
   139 
   123 fun check_unoverload thy (c, ty) =
   140 fun check_unoverload thy (c, ty) =
   124   let
   141   let
   125     val c' = AxClass.unoverload_const thy (c, ty);
   142     val c' = AxClass.unoverload_const thy (c, ty);
   126     val ty_decl = Sign.the_const_type thy c';
   143     val ty_decl = Sign.the_const_type thy c';
   127   in
   144   in
   128     if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts (Logic.varifyT_global ty))
   145     if typscheme_equiv (ty_decl, Logic.varifyT_global ty)
   129     then c'
   146     then c'
   130     else
   147     else
   131       error ("Type\n" ^ string_of_typ thy ty ^
   148       error ("Type\n" ^ string_of_typ thy ty ^
   132         "\nof constant " ^ quote c ^
   149         "\nof constant " ^ quote c ^
   133         "\nis too specific compared to declared type\n" ^
   150         "\nis too specific compared to declared type\n" ^
   327 end; (*local*)
   344 end; (*local*)
   328 
   345 
   329 
   346 
   330 (** foundation **)
   347 (** foundation **)
   331 
   348 
   332 (* constants *)
       
   333 
       
   334 fun const_typ thy = Type.strip_sorts o Sign.the_const_type thy;
       
   335 
       
   336 fun args_number thy = length o binder_types o const_typ thy;
       
   337 
       
   338 fun logical_typscheme thy (c, ty) =
       
   339   (map dest_TFree (Sign.const_typargs thy (c, ty)), Type.strip_sorts ty);
       
   340 
       
   341 fun typscheme thy (c, ty) = logical_typscheme thy (c, ty);
       
   342 
       
   343 
       
   344 (* datatypes *)
   349 (* datatypes *)
   345 
   350 
   346 fun no_constr thy s (c, ty) = error ("Not a datatype constructor:\n" ^ string_of_const thy c
   351 fun no_constr thy s (c, ty) = error ("Not a datatype constructor:\n" ^ string_of_const thy c
   347   ^ " :: " ^ string_of_typ thy ty ^ "\n" ^ enclose "(" ")" s);
   352   ^ " :: " ^ string_of_typ thy ty ^ "\n" ^ enclose "(" ")" s);
   348 
   353 
   381       in ((tyco, sorts''), c :: cs) end;
   386       in ((tyco, sorts''), c :: cs) end;
   382     fun inst vs' (c, (vs, ty)) =
   387     fun inst vs' (c, (vs, ty)) =
   383       let
   388       let
   384         val the_v = the o AList.lookup (op =) (vs ~~ vs');
   389         val the_v = the o AList.lookup (op =) (vs ~~ vs');
   385         val ty' = map_type_tfree (fn (v, _) => TFree (the_v v)) ty;
   390         val ty' = map_type_tfree (fn (v, _) => TFree (the_v v)) ty;
   386         val (vs'', _) = logical_typscheme thy (c, ty');
   391         val (vs'', _) = typscheme thy (c, ty');
   387       in (c, (vs'', binder_types ty')) end;
   392       in (c, (vs'', binder_types ty')) end;
   388     val c' :: cs' = map (analyze_constructor thy) cs;
   393     val c' :: cs' = map (analyze_constructor thy) cs;
   389     val ((tyco, sorts), cs'') = fold add cs' (apsnd single c');
   394     val ((tyco, sorts), cs'') = fold add cs' (apsnd single c');
   390     val vs = Name.invent_names Name.context Name.aT sorts;
   395     val vs = Name.invent_names Name.context Name.aT sorts;
   391     val cs''' = map (inst vs) cs'';
   396     val cs''' = map (inst vs) cs'';
   442     (fn Var (v, _) => cons v | _ => I) args [])) end;
   447     (fn Var (v, _) => cons v | _ => I) args [])) end;
   443 
   448 
   444 fun check_decl_ty thy (c, ty) =
   449 fun check_decl_ty thy (c, ty) =
   445   let
   450   let
   446     val ty_decl = Sign.the_const_type thy c;
   451     val ty_decl = Sign.the_const_type thy c;
   447   in if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty) then ()
   452   in if typscheme_equiv (ty_decl, ty) then ()
   448     else bad_thm ("Type\n" ^ string_of_typ thy ty
   453     else bad_thm ("Type\n" ^ string_of_typ thy ty
   449       ^ "\nof constant " ^ quote c
   454       ^ "\nof constant " ^ quote c
   450       ^ "\nis too specific compared to declared type\n"
   455       ^ "\nis too specific compared to declared type\n"
   451       ^ string_of_typ thy ty_decl)
   456       ^ string_of_typ thy ty_decl)
   452   end; 
   457   end; 
   645       handle TERM _ => bad "Not an abstype certificate";
   650       handle TERM _ => bad "Not an abstype certificate";
   646     val _ = if param = rhs then () else bad "Not an abstype certificate";
   651     val _ = if param = rhs then () else bad "Not an abstype certificate";
   647     val ((tyco, sorts), (abs, (vs, ty'))) =
   652     val ((tyco, sorts), (abs, (vs, ty'))) =
   648       analyze_constructor thy (abs, Logic.unvarifyT_global raw_ty);
   653       analyze_constructor thy (abs, Logic.unvarifyT_global raw_ty);
   649     val ty = domain_type ty';
   654     val ty = domain_type ty';
   650     val (vs', _) = logical_typscheme thy (abs, ty');
   655     val (vs', _) = typscheme thy (abs, ty');
   651   in (tyco, (vs ~~ sorts, ((abs, (vs', ty)), (rep, thm)))) end;
   656   in (tyco, (vs ~~ sorts, ((abs, (vs', ty)), (rep, thm)))) end;
   652 
   657 
   653 
   658 
   654 (* code equation certificates *)
   659 (* code equation certificates *)
   655 
   660 
   709 with
   714 with
   710 
   715 
   711 fun empty_cert thy c = 
   716 fun empty_cert thy c = 
   712   let
   717   let
   713     val raw_ty = Logic.unvarifyT_global (const_typ thy c);
   718     val raw_ty = Logic.unvarifyT_global (const_typ thy c);
   714     val (vs, _) = logical_typscheme thy (c, raw_ty);
   719     val (vs, _) = typscheme thy (c, raw_ty);
   715     val sortargs = case AxClass.class_of_param thy c
   720     val sortargs = case AxClass.class_of_param thy c
   716      of SOME class => [[class]]
   721      of SOME class => [[class]]
   717       | NONE => (case get_type_of_constr_or_abstr thy c
   722       | NONE => (case get_type_of_constr_or_abstr thy c
   718          of SOME (tyco, _) => (map snd o fst o the)
   723          of SOME (tyco, _) => (map snd o fst o the)
   719               (AList.lookup (op =) ((snd o fst o get_type thy) tyco) c)
   724               (AList.lookup (op =) ((snd o fst o get_type thy) tyco) c)