src/HOL/Tools/type_mapper.ML
changeset 40856 3ad8a5925ba4
parent 40855 149dcaa26728
child 40857 b3489aa6b63f
equal deleted inserted replaced
40855:149dcaa26728 40856:3ad8a5925ba4
     7 signature TYPE_MAPPER =
     7 signature TYPE_MAPPER =
     8 sig
     8 sig
     9   val find_atomic: theory -> typ -> (typ * (bool * bool)) list
     9   val find_atomic: theory -> typ -> (typ * (bool * bool)) list
    10   val construct_mapper: theory -> (string * bool -> term)
    10   val construct_mapper: theory -> (string * bool -> term)
    11     -> bool -> typ -> typ -> term
    11     -> bool -> typ -> typ -> term
    12   val type_mapper: term -> theory -> Proof.state
    12   val type_mapper: string option -> term -> theory -> Proof.state
    13   type entry
    13   type entry
    14   val entries: theory -> entry Symtab.table
    14   val entries: theory -> entry Symtab.table
    15 end;
    15 end;
    16 
    16 
    17 structure Type_Mapper : TYPE_MAPPER =
    17 structure Type_Mapper : TYPE_MAPPER =
   170       end;
   170       end;
   171     val (variances, left_variances) = fold_map check_variance_pair (vs1 ~~ vs2) Ts;
   171     val (variances, left_variances) = fold_map check_variance_pair (vs1 ~~ vs2) Ts;
   172     val _ = if null left_variances then () else bad_typ ();
   172     val _ = if null left_variances then () else bad_typ ();
   173   in variances end;
   173   in variances end;
   174 
   174 
   175 fun gen_type_mapper prep_term raw_t thy =
   175 fun gen_type_mapper prep_term some_prfx raw_t thy =
   176   let
   176   let
   177     val (mapper, T) = case prep_term thy raw_t
   177     val (mapper, T) = case prep_term thy raw_t
   178      of Const cT => cT
   178      of Const cT => cT
   179       | t => error ("No constant: " ^ Syntax.string_of_term_global thy t);
   179       | t => error ("No constant: " ^ Syntax.string_of_term_global thy t);
       
   180     val prfx = the_default (Long_Name.base_name mapper) some_prfx;
   180     val _ = Type.no_tvars T;
   181     val _ = Type.no_tvars T;
   181     fun add_tycos (Type (tyco, Ts)) = insert (op =) tyco #> fold add_tycos Ts
   182     fun add_tycos (Type (tyco, Ts)) = insert (op =) tyco #> fold add_tycos Ts
   182       | add_tycos _ = I;
   183       | add_tycos _ = I;
   183     val tycos = add_tycos T [];
   184     val tycos = add_tycos T [];
   184     val tyco = if tycos = ["fun"] then "fun"
   185     val tyco = if tycos = ["fun"] then "fun"
   188     val variances = analyze_variances thy tyco T;
   189     val variances = analyze_variances thy tyco T;
   189     val compositionality_prop = uncurry (fold_rev Logic.all)
   190     val compositionality_prop = uncurry (fold_rev Logic.all)
   190       (make_compositionality_prop variances (tyco, mapper));
   191       (make_compositionality_prop variances (tyco, mapper));
   191     val identity_prop = uncurry Logic.all
   192     val identity_prop = uncurry Logic.all
   192       (make_identity_prop variances (tyco, mapper));
   193       (make_identity_prop variances (tyco, mapper));
   193     val qualify = Binding.qualify true (Long_Name.base_name mapper) o Binding.name;
   194     val qualify = Binding.qualify true prfx o Binding.name;
   194     fun after_qed [single_compositionality, single_identity] lthy =
   195     fun after_qed [single_compositionality, single_identity] lthy =
   195       lthy
   196       lthy
   196       |> Local_Theory.note ((qualify compositionalityN, []), single_compositionality)
   197       |> Local_Theory.note ((qualify compositionalityN, []), single_compositionality)
   197       ||>> Local_Theory.note ((qualify identityN, []), single_identity)
   198       ||>> Local_Theory.note ((qualify identityN, []), single_identity)
   198       |-> (fn ((_, [compositionality]), (_, [identity])) =>
   199       |-> (fn ((_, [compositionality]), (_, [identity])) =>
   208 val type_mapper = gen_type_mapper Sign.cert_term;
   209 val type_mapper = gen_type_mapper Sign.cert_term;
   209 val type_mapper_cmd = gen_type_mapper Syntax.read_term_global;
   210 val type_mapper_cmd = gen_type_mapper Syntax.read_term_global;
   210 
   211 
   211 val _ =
   212 val _ =
   212   Outer_Syntax.command "type_mapper" "register functorial mapper for type with its properties" Keyword.thy_goal
   213   Outer_Syntax.command "type_mapper" "register functorial mapper for type with its properties" Keyword.thy_goal
   213     (Parse.term >> (fn t => Toplevel.print o (Toplevel.theory_to_proof (type_mapper_cmd t))));
   214     (Scan.option (Parse.name --| Parse.$$$ ":") -- Parse.term
       
   215       >> (fn (prfx, t) => Toplevel.print o (Toplevel.theory_to_proof (type_mapper_cmd prfx t))));
   214 
   216 
   215 end;
   217 end;