src/Tools/subtyping.ML
changeset 40284 c9acf88447e6
parent 40283 805ce1d64af0
child 40285 80136c4240cc
equal deleted inserted replaced
40283:805ce1d64af0 40284:c9acf88447e6
     7 signature SUBTYPING =
     7 signature SUBTYPING =
     8 sig
     8 sig
     9   datatype variance = COVARIANT | CONTRAVARIANT | INVARIANT
     9   datatype variance = COVARIANT | CONTRAVARIANT | INVARIANT
    10   val infer_types: Proof.context -> (string -> typ option) -> (indexname -> typ option) ->
    10   val infer_types: Proof.context -> (string -> typ option) -> (indexname -> typ option) ->
    11     term list -> term list
    11     term list -> term list
       
    12   val add_type_map: term -> Context.generic -> Context.generic
       
    13   val add_coercion: term -> Context.generic -> Context.generic
    12   val setup: theory -> theory
    14   val setup: theory -> theory
    13 end;
    15 end;
    14 
    16 
    15 structure Subtyping: SUBTYPING =
    17 structure Subtyping: SUBTYPING =
    16 struct
    18 struct
   657       in if eq_list (op aconv) (xs, xs') then NONE else SOME (xs', ctxt) end);
   659       in if eq_list (op aconv) (xs, xs') then NONE else SOME (xs', ctxt) end);
   658 
   660 
   659 
   661 
   660 (* declarations *)
   662 (* declarations *)
   661 
   663 
   662 fun add_type_map map_fun context =
   664 fun add_type_map raw_t context =
   663   let
   665   let
   664     val ctxt = Context.proof_of context;
   666     val ctxt = Context.proof_of context;
   665     val t = singleton (Variable.polymorphic ctxt) (Syntax.read_term ctxt map_fun);
   667     val t = singleton (Variable.polymorphic ctxt) raw_t;
   666 
   668 
   667     fun err_str () = "\n\nthe general type signature for a map function is" ^
   669     fun err_str () = "\n\nthe general type signature for a map function is" ^
   668       "\nf1 => f2 => ... => fn => C [x1, ..., xn] => C [x1, ..., xn]" ^
   670       "\nf1 => f2 => ... => fn => C [x1, ..., xn] => C [x1, ..., xn]" ^
   669       "\nwhere C is a constructor and fi is of type (xi => yi) or (yi => xi)";
   671       "\nwhere C is a constructor and fi is of type (xi => yi) or (yi => xi)";
   670 
   672 
   697     val res_av = gen_arg_var (fst res);
   699     val res_av = gen_arg_var (fst res);
   698   in
   700   in
   699     map_tmaps (Symtab.update (snd res, (t, res_av))) context
   701     map_tmaps (Symtab.update (snd res, (t, res_av))) context
   700   end;
   702   end;
   701 
   703 
   702 fun add_coercion coercion context =
   704 fun add_coercion raw_t context =
   703   let
   705   let
   704     val ctxt = Context.proof_of context;
   706     val ctxt = Context.proof_of context;
   705     val t = singleton (Variable.polymorphic ctxt) (Syntax.read_term ctxt coercion);
   707     val t = singleton (Variable.polymorphic ctxt) raw_t;
   706 
   708 
   707     fun err_coercion () = error ("Bad type for coercion " ^
   709     fun err_coercion () = error ("Bad type for coercion " ^
   708         Syntax.string_of_term ctxt t ^ ":\n" ^
   710         Syntax.string_of_term ctxt t ^ ":\n" ^
   709         Syntax.string_of_typ ctxt (fastype_of t));
   711         Syntax.string_of_typ ctxt (fastype_of t));
   710 
   712 
   754 
   756 
   755 (* theory setup *)
   757 (* theory setup *)
   756 
   758 
   757 val setup =
   759 val setup =
   758   Context.theory_map add_term_check #>
   760   Context.theory_map add_term_check #>
   759   Attrib.setup @{binding coercion} (Scan.lift Parse.term >>
   761   Attrib.setup @{binding coercion}
   760     (fn t => fn (context, thm) => (add_coercion t context, thm)))
   762     (Args.term >> (fn t => Thm.declaration_attribute (K (add_coercion t))))
   761     "declaration of new coercions" #>
   763     "declaration of new coercions" #>
   762   Attrib.setup @{binding map_function} (Scan.lift Parse.term >>
   764   Attrib.setup @{binding map_function}
   763     (fn t => fn (context, thm) => (add_type_map t context, thm)))
   765     (Args.term >> (fn t => Thm.declaration_attribute (K (add_type_map t))))
   764     "declaration of new map functions";
   766     "declaration of new map functions";
   765 
   767 
   766 end;
   768 end;