src/HOL/Tools/typedef_package.ML
changeset 25535 4975b7529a14
parent 25513 b7de6e23e143
child 26478 9d1029ce0e13
equal deleted inserted replaced
25534:d0b74fdd6067 25535:4975b7529a14
    22   val typedef: (bool * string) * (bstring * string list * mixfix) * string
    22   val typedef: (bool * string) * (bstring * string list * mixfix) * string
    23     * (string * string) option -> theory -> Proof.state
    23     * (string * string) option -> theory -> Proof.state
    24   val typedef_i: (bool * string) * (bstring * string list * mixfix) * term
    24   val typedef_i: (bool * string) * (bstring * string list * mixfix) * term
    25     * (string * string) option -> theory -> Proof.state
    25     * (string * string) option -> theory -> Proof.state
    26   val interpretation: (string -> theory -> theory) -> theory -> theory
    26   val interpretation: (string -> theory -> theory) -> theory -> theory
       
    27   val setup: theory -> theory
    27 end;
    28 end;
    28 
    29 
    29 structure TypedefPackage: TYPEDEF_PACKAGE =
    30 structure TypedefPackage: TYPEDEF_PACKAGE =
    30 struct
    31 struct
    31 
    32 
    78 
    79 
    79 fun err_in_typedef msg name =
    80 fun err_in_typedef msg name =
    80   cat_error msg ("The error(s) above occurred in typedef " ^ quote name);
    81   cat_error msg ("The error(s) above occurred in typedef " ^ quote name);
    81 
    82 
    82 fun declare_type_name a = Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS)));
    83 fun declare_type_name a = Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS)));
       
    84 
       
    85 structure TypedefInterpretation = InterpretationFun(type T = string val eq = op =);
       
    86 val interpretation = TypedefInterpretation.interpretation;
    83 
    87 
    84 fun prepare_typedef prep_term def name (t, vs, mx) raw_set opt_morphs thy =
    88 fun prepare_typedef prep_term def name (t, vs, mx) raw_set opt_morphs thy =
    85   let
    89   let
    86     val _ = Theory.requires thy "Typedef" "typedefs";
    90     val _ = Theory.requires thy "Typedef" "typedefs";
    87     val ctxt = ProofContext.init thy;
    91     val ctxt = ProofContext.init thy;
   175             Rep_name = full Rep_name, Abs_name = full Abs_name,
   179             Rep_name = full Rep_name, Abs_name = full Abs_name,
   176               type_definition = type_definition, set_def = set_def,
   180               type_definition = type_definition, set_def = set_def,
   177               Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse,
   181               Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse,
   178               Rep_inject = Rep_inject, Abs_inject = Abs_inject, Rep_cases = Rep_cases,
   182               Rep_inject = Rep_inject, Abs_inject = Abs_inject, Rep_cases = Rep_cases,
   179             Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct};
   183             Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct};
   180           val thy3 = thy2 |> put_info full_tname info;
   184         in
   181         in ((full_tname, info), thy3) end);
   185           thy2
       
   186           |> put_info full_tname info
       
   187           |> TypedefInterpretation.data full_tname
       
   188           |> pair (full_tname, info)
       
   189         end);
   182 
   190 
   183 
   191 
   184     (* errors *)
   192     (* errors *)
   185 
   193 
   186     fun show_names pairs = commas_quote (map fst pairs);
   194     fun show_names pairs = commas_quote (map fst pairs);
   212   in (set, goal, goal_pat, typedef_result) end
   220   in (set, goal, goal_pat, typedef_result) end
   213   handle ERROR msg => err_in_typedef msg name;
   221   handle ERROR msg => err_in_typedef msg name;
   214 
   222 
   215 
   223 
   216 (* add_typedef interfaces *)
   224 (* add_typedef interfaces *)
   217 
       
   218 structure TypedefInterpretation = InterpretationFun(type T = string val eq = op =);
       
   219 val interpretation = TypedefInterpretation.interpretation;
       
   220 
   225 
   221 local
   226 local
   222 
   227 
   223 fun gen_typedef prep_term def opt_name typ set opt_morphs tac thy =
   228 fun gen_typedef prep_term def opt_name typ set opt_morphs tac thy =
   224   let
   229   let
   230     val non_empty = Goal.prove_global thy [] [] goal (K tac) handle ERROR msg =>
   235     val non_empty = Goal.prove_global thy [] [] goal (K tac) handle ERROR msg =>
   231       cat_error msg ("Failed to prove non-emptiness of " ^ quote (string_of_term set));
   236       cat_error msg ("Failed to prove non-emptiness of " ^ quote (string_of_term set));
   232   in
   237   in
   233     thy
   238     thy
   234     |> typedef_result non_empty
   239     |> typedef_result non_empty
   235     |-> (fn (a, info) => TypedefInterpretation.data a #> pair (a, info))
       
   236   end;
   240   end;
   237 
   241 
   238 in
   242 in
   239 
   243 
   240 val add_typedef = gen_typedef Syntax.read_term;
   244 val add_typedef = gen_typedef Syntax.read_term;
   281 
   285 
   282 val _ =
   286 val _ =
   283   OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal
   287   OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal
   284     (typedef_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef)));
   288     (typedef_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef)));
   285 
   289 
   286 end;
   290 val setup = TypedefInterpretation.init;
   287 
   291 
   288 end;
   292 end;
       
   293 
       
   294 end;