src/HOLCF/Tools/pcpodef.ML
author huffman
Mon Mar 22 15:53:25 2010 -0700 (2010-03-22)
changeset 35912 b0e300bd3a2c
parent 35902 81608655c69e
child 35994 9cc3df9a606e
permissions -rw-r--r--
error -> raise Fail
haftmann@31738
     1
(*  Title:      HOLCF/Tools/pcpodef.ML
wenzelm@23152
     2
    Author:     Brian Huffman
wenzelm@23152
     3
wenzelm@23152
     4
Primitive domain definitions for HOLCF, similar to Gordon/HOL-style
haftmann@31738
     5
typedef (see also ~~/src/HOL/Tools/typedef.ML).
wenzelm@23152
     6
*)
wenzelm@23152
     7
haftmann@31738
     8
signature PCPODEF =
wenzelm@23152
     9
sig
huffman@33646
    10
  type cpo_info =
huffman@33646
    11
    { below_def: thm, adm: thm, cont_Rep: thm, cont_Abs: thm,
huffman@33646
    12
      lub: thm, thelub: thm, compact: thm }
huffman@33646
    13
  type pcpo_info =
huffman@33646
    14
    { Rep_strict: thm, Abs_strict: thm, Rep_strict_iff: thm, Abs_strict_iff: thm,
huffman@33646
    15
      Rep_defined: thm, Abs_defined: thm }
huffman@33646
    16
wenzelm@35840
    17
  val add_podef: bool -> binding option -> binding * (string * sort) list * mixfix ->
huffman@33646
    18
    term -> (binding * binding) option -> tactic -> theory ->
huffman@33646
    19
    (Typedef.info * thm) * theory
wenzelm@35840
    20
  val add_cpodef: bool -> binding option -> binding * (string * sort) list * mixfix ->
huffman@33646
    21
    term -> (binding * binding) option -> tactic * tactic -> theory ->
huffman@33646
    22
    (Typedef.info * cpo_info) * theory
wenzelm@35840
    23
  val add_pcpodef: bool -> binding option -> binding * (string * sort) list * mixfix ->
huffman@33646
    24
    term -> (binding * binding) option -> tactic * tactic -> theory ->
huffman@33646
    25
    (Typedef.info * cpo_info * pcpo_info) * theory
huffman@33646
    26
wenzelm@35840
    27
  val cpodef_proof: (bool * binding)
wenzelm@35840
    28
    * (binding * (string * sort) list * mixfix) * term
huffman@33646
    29
    * (binding * binding) option -> theory -> Proof.state
wenzelm@35840
    30
  val cpodef_proof_cmd: (bool * binding)
wenzelm@35840
    31
    * (binding * (string * string option) list * mixfix) * string
huffman@33646
    32
    * (binding * binding) option -> theory -> Proof.state
wenzelm@35840
    33
  val pcpodef_proof: (bool * binding)
wenzelm@35840
    34
    * (binding * (string * sort) list * mixfix) * term
wenzelm@30345
    35
    * (binding * binding) option -> theory -> Proof.state
wenzelm@35840
    36
  val pcpodef_proof_cmd: (bool * binding)
wenzelm@35840
    37
    * (binding * (string * string option) list * mixfix) * string
wenzelm@30345
    38
    * (binding * binding) option -> theory -> Proof.state
wenzelm@23152
    39
end;
wenzelm@23152
    40
haftmann@31738
    41
structure Pcpodef :> PCPODEF =
wenzelm@23152
    42
struct
wenzelm@23152
    43
wenzelm@23152
    44
(** type definitions **)
wenzelm@23152
    45
huffman@33646
    46
type cpo_info =
huffman@33646
    47
  { below_def: thm, adm: thm, cont_Rep: thm, cont_Abs: thm,
huffman@33646
    48
    lub: thm, thelub: thm, compact: thm }
wenzelm@23152
    49
huffman@33646
    50
type pcpo_info =
huffman@33646
    51
  { Rep_strict: thm, Abs_strict: thm, Rep_strict_iff: thm, Abs_strict_iff: thm,
huffman@33646
    52
    Rep_defined: thm, Abs_defined: thm }
huffman@33646
    53
huffman@33646
    54
(* building terms *)
wenzelm@23152
    55
wenzelm@29060
    56
fun adm_const T = Const (@{const_name adm}, (T --> HOLogic.boolT) --> HOLogic.boolT);
wenzelm@23152
    57
fun mk_adm (x, T, P) = adm_const T $ absfree (x, T, P);
wenzelm@23152
    58
huffman@33646
    59
fun below_const T = Const (@{const_name below}, T --> T --> HOLogic.boolT);
huffman@33646
    60
huffman@33646
    61
(* manipulating theorems *)
huffman@33646
    62
huffman@33646
    63
fun fold_adm_mem thm NONE = thm
huffman@33646
    64
  | fold_adm_mem thm (SOME set_def) =
huffman@33646
    65
    let val rule = @{lemma "A == B ==> adm (%x. x : B) ==> adm (%x. x : A)" by simp}
huffman@33646
    66
    in rule OF [set_def, thm] end;
huffman@33646
    67
huffman@33646
    68
fun fold_UU_mem thm NONE = thm
huffman@33646
    69
  | fold_UU_mem thm (SOME set_def) =
huffman@33646
    70
    let val rule = @{lemma "A == B ==> UU : B ==> UU : A" by simp}
huffman@33646
    71
    in rule OF [set_def, thm] end;
huffman@33646
    72
huffman@33646
    73
(* proving class instances *)
huffman@33646
    74
huffman@33646
    75
fun prove_cpo
huffman@33646
    76
      (name: binding)
huffman@33646
    77
      (newT: typ)
huffman@33646
    78
      (Rep_name: binding, Abs_name: binding)
huffman@33646
    79
      (type_definition: thm)  (* type_definition Rep Abs A *)
huffman@33646
    80
      (set_def: thm option)   (* A == set *)
huffman@33646
    81
      (below_def: thm)        (* op << == %x y. Rep x << Rep y *)
huffman@33646
    82
      (admissible: thm)       (* adm (%x. x : set) *)
huffman@33646
    83
      (thy: theory)
huffman@33646
    84
    =
huffman@33646
    85
  let
huffman@33646
    86
    val admissible' = fold_adm_mem admissible set_def;
huffman@33646
    87
    val cpo_thms = map (Thm.transfer thy) [type_definition, below_def, admissible'];
huffman@33646
    88
    val (full_tname, Ts) = dest_Type newT;
huffman@33646
    89
    val lhs_sorts = map (snd o dest_TFree) Ts;
huffman@35902
    90
    val tac = Tactic.rtac (@{thm typedef_cpo} OF cpo_thms) 1;
huffman@35902
    91
    val thy = AxClass.prove_arity (full_tname, lhs_sorts, @{sort cpo}) tac thy;
huffman@33678
    92
    (* transfer thms so that they will know about the new cpo instance *)
huffman@35902
    93
    val cpo_thms' = map (Thm.transfer thy) cpo_thms;
wenzelm@35021
    94
    fun make thm = Drule.export_without_context (thm OF cpo_thms');
huffman@35902
    95
    val cont_Rep = make @{thm typedef_cont_Rep};
huffman@35902
    96
    val cont_Abs = make @{thm typedef_cont_Abs};
huffman@35902
    97
    val lub = make @{thm typedef_lub};
huffman@35902
    98
    val thelub = make @{thm typedef_thelub};
huffman@35902
    99
    val compact = make @{thm typedef_compact};
huffman@35902
   100
    val (_, thy) =
huffman@35902
   101
      thy
huffman@33646
   102
      |> Sign.add_path (Binding.name_of name)
huffman@33646
   103
      |> PureThy.add_thms
huffman@35902
   104
        ([((Binding.prefix_name "adm_"      name, admissible'), []),
huffman@35902
   105
          ((Binding.prefix_name "cont_" Rep_name, cont_Rep   ), []),
huffman@35902
   106
          ((Binding.prefix_name "cont_" Abs_name, cont_Abs   ), []),
huffman@35902
   107
          ((Binding.prefix_name "lub_"      name, lub        ), []),
huffman@35902
   108
          ((Binding.prefix_name "thelub_"   name, thelub     ), []),
huffman@35902
   109
          ((Binding.prefix_name "compact_"  name, compact    ), [])])
huffman@35902
   110
      ||> Sign.parent_path;
huffman@33646
   111
    val cpo_info : cpo_info =
huffman@33646
   112
      { below_def = below_def, adm = admissible', cont_Rep = cont_Rep,
huffman@33646
   113
        cont_Abs = cont_Abs, lub = lub, thelub = thelub, compact = compact };
huffman@33646
   114
  in
huffman@35902
   115
    (cpo_info, thy)
huffman@33646
   116
  end;
huffman@33646
   117
huffman@33646
   118
fun prove_pcpo
huffman@33646
   119
      (name: binding)
huffman@33646
   120
      (newT: typ)
huffman@33646
   121
      (Rep_name: binding, Abs_name: binding)
huffman@33646
   122
      (type_definition: thm)  (* type_definition Rep Abs A *)
huffman@33646
   123
      (set_def: thm option)   (* A == set *)
huffman@33646
   124
      (below_def: thm)        (* op << == %x y. Rep x << Rep y *)
huffman@33646
   125
      (UU_mem: thm)           (* UU : set *)
huffman@33646
   126
      (thy: theory)
huffman@33646
   127
    =
huffman@33646
   128
  let
huffman@33646
   129
    val UU_mem' = fold_UU_mem UU_mem set_def;
huffman@33646
   130
    val pcpo_thms = map (Thm.transfer thy) [type_definition, below_def, UU_mem'];
huffman@33646
   131
    val (full_tname, Ts) = dest_Type newT;
huffman@33646
   132
    val lhs_sorts = map (snd o dest_TFree) Ts;
huffman@35902
   133
    val tac = Tactic.rtac (@{thm typedef_pcpo} OF pcpo_thms) 1;
huffman@35902
   134
    val thy = AxClass.prove_arity (full_tname, lhs_sorts, @{sort pcpo}) tac thy;
huffman@35902
   135
    val pcpo_thms' = map (Thm.transfer thy) pcpo_thms;
wenzelm@35021
   136
    fun make thm = Drule.export_without_context (thm OF pcpo_thms');
huffman@35902
   137
    val Rep_strict = make @{thm typedef_Rep_strict};
huffman@35902
   138
    val Abs_strict = make @{thm typedef_Abs_strict};
huffman@35902
   139
    val Rep_strict_iff = make @{thm typedef_Rep_strict_iff};
huffman@35902
   140
    val Abs_strict_iff = make @{thm typedef_Abs_strict_iff};
huffman@35902
   141
    val Rep_defined = make @{thm typedef_Rep_defined};
huffman@35902
   142
    val Abs_defined = make @{thm typedef_Abs_defined};
huffman@35902
   143
    val (_, thy) =
huffman@35902
   144
      thy
huffman@33646
   145
      |> Sign.add_path (Binding.name_of name)
huffman@33646
   146
      |> PureThy.add_thms
huffman@35902
   147
        ([((Binding.suffix_name "_strict"     Rep_name, Rep_strict), []),
huffman@35902
   148
          ((Binding.suffix_name "_strict"     Abs_name, Abs_strict), []),
huffman@35902
   149
          ((Binding.suffix_name "_strict_iff" Rep_name, Rep_strict_iff), []),
huffman@35902
   150
          ((Binding.suffix_name "_strict_iff" Abs_name, Abs_strict_iff), []),
huffman@35902
   151
          ((Binding.suffix_name "_defined"    Rep_name, Rep_defined), []),
huffman@35902
   152
          ((Binding.suffix_name "_defined"    Abs_name, Abs_defined), [])])
huffman@35902
   153
      ||> Sign.parent_path;
huffman@33646
   154
    val pcpo_info =
huffman@33646
   155
      { Rep_strict = Rep_strict, Abs_strict = Abs_strict,
huffman@33646
   156
        Rep_strict_iff = Rep_strict_iff, Abs_strict_iff = Abs_strict_iff,
huffman@33646
   157
        Rep_defined = Rep_defined, Abs_defined = Abs_defined };
huffman@33646
   158
  in
huffman@35902
   159
    (pcpo_info, thy)
huffman@33646
   160
  end;
huffman@33646
   161
huffman@33646
   162
(* prepare_cpodef *)
huffman@33646
   163
huffman@33646
   164
fun declare_type_name a =
huffman@33646
   165
  Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS)));
huffman@33646
   166
wenzelm@35840
   167
fun prepare prep_term name (tname, raw_args, mx) raw_set opt_morphs thy =
wenzelm@23152
   168
  let
wenzelm@29060
   169
    val _ = Theory.requires thy "Pcpodef" "pcpodefs";
wenzelm@30345
   170
wenzelm@23152
   171
    (*rhs*)
wenzelm@35840
   172
    val (_, tmp_lthy) =
wenzelm@35840
   173
      thy |> Theory.copy |> Theory_Target.init NONE
wenzelm@35840
   174
      |> Typedecl.predeclare_constraints (tname, raw_args, mx);
wenzelm@35840
   175
    val set = prep_term tmp_lthy raw_set;
wenzelm@35840
   176
    val tmp_lthy' = tmp_lthy |> Variable.declare_constraints set;
wenzelm@35840
   177
wenzelm@23152
   178
    val setT = Term.fastype_of set;
wenzelm@23152
   179
    val oldT = HOLogic.dest_setT setT handle TYPE _ =>
wenzelm@35840
   180
      error ("Not a set type: " ^ quote (Syntax.string_of_typ tmp_lthy setT));
wenzelm@29060
   181
huffman@33646
   182
    (*lhs*)
wenzelm@35840
   183
    val lhs_tfrees = map (fn (a, _) => (a, ProofContext.default_sort tmp_lthy' (a, ~1))) raw_args;
huffman@33646
   184
    val full_tname = Sign.full_name thy tname;
huffman@33646
   185
    val newT = Type (full_tname, map TFree lhs_tfrees);
huffman@33646
   186
huffman@33646
   187
    val morphs = opt_morphs
huffman@33646
   188
      |> the_default (Binding.prefix_name "Rep_" name, Binding.prefix_name "Abs_" name);
huffman@33646
   189
  in
huffman@33678
   190
    (newT, oldT, set, morphs)
huffman@33646
   191
  end
huffman@33646
   192
huffman@33646
   193
fun add_podef def opt_name typ set opt_morphs tac thy =
huffman@33646
   194
  let
huffman@33646
   195
    val name = the_default (#1 typ) opt_name;
huffman@33646
   196
    val ((full_tname, info as {type_definition, set_def, Rep_name, ...}), thy2) = thy
wenzelm@35742
   197
      |> Typedef.add_typedef_global def opt_name typ set opt_morphs tac;
huffman@33646
   198
    val oldT = #rep_type info;
huffman@33646
   199
    val newT = #abs_type info;
huffman@33646
   200
    val lhs_tfrees = map dest_TFree (snd (dest_Type newT));
huffman@33646
   201
huffman@33646
   202
    val RepC = Const (Rep_name, newT --> oldT);
huffman@33678
   203
    val below_eqn = Logic.mk_equals (below_const newT,
huffman@33646
   204
      Abs ("x", newT, Abs ("y", newT, below_const oldT $ (RepC $ Bound 1) $ (RepC $ Bound 0))));
huffman@33646
   205
    val lthy3 = thy2
huffman@33646
   206
      |> Theory_Target.instantiation ([full_tname], lhs_tfrees, @{sort po});
huffman@33678
   207
    val ((_, (_, below_ldef)), lthy4) = lthy3
huffman@33646
   208
      |> Specification.definition (NONE,
huffman@33678
   209
          ((Binding.prefix_name "below_" (Binding.suffix_name "_def" name), []), below_eqn));
huffman@33646
   210
    val ctxt_thy = ProofContext.init (ProofContext.theory_of lthy4);
huffman@33678
   211
    val below_def = singleton (ProofContext.export lthy4 ctxt_thy) below_ldef;
huffman@33646
   212
    val thy5 = lthy4
huffman@33646
   213
      |> Class.prove_instantiation_instance
huffman@33678
   214
          (K (Tactic.rtac (@{thm typedef_po} OF [type_definition, below_def]) 1))
wenzelm@33671
   215
      |> Local_Theory.exit_global;
huffman@33678
   216
  in ((info, below_def), thy5) end;
huffman@33646
   217
huffman@33646
   218
fun prepare_cpodef
huffman@33646
   219
      (prep_term: Proof.context -> 'a -> term)
huffman@33646
   220
      (def: bool)
huffman@33646
   221
      (name: binding)
wenzelm@35840
   222
      (typ: binding * (string * sort) list * mixfix)
huffman@33646
   223
      (raw_set: 'a)
huffman@33646
   224
      (opt_morphs: (binding * binding) option)
huffman@33646
   225
      (thy: theory)
huffman@33646
   226
    : term * term * (thm -> thm -> theory -> (Typedef.info * cpo_info) * theory) =
huffman@33646
   227
  let
huffman@33678
   228
    val (newT, oldT, set, morphs as (Rep_name, Abs_name)) =
huffman@33678
   229
      prepare prep_term name typ raw_set opt_morphs thy;
huffman@33646
   230
wenzelm@29063
   231
    val goal_nonempty =
wenzelm@29063
   232
      HOLogic.mk_Trueprop (HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), set)));
wenzelm@29063
   233
    val goal_admissible =
wenzelm@29063
   234
      HOLogic.mk_Trueprop (mk_adm ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), set)));
wenzelm@23152
   235
huffman@33646
   236
    fun cpodef_result nonempty admissible thy =
haftmann@28073
   237
      let
huffman@33646
   238
        val ((info as {type_definition, set_def, ...}, below_def), thy2) = thy
huffman@33646
   239
          |> add_podef def (SOME name) typ set opt_morphs (Tactic.rtac nonempty 1);
huffman@33646
   240
        val (cpo_info, thy3) = thy2
huffman@33646
   241
          |> prove_cpo name newT morphs type_definition set_def below_def admissible;
wenzelm@23152
   242
      in
huffman@33646
   243
        ((info, cpo_info), thy3)
wenzelm@23152
   244
      end;
wenzelm@29063
   245
  in
huffman@33646
   246
    (goal_nonempty, goal_admissible, cpodef_result)
wenzelm@29063
   247
  end
wenzelm@30345
   248
  handle ERROR msg =>
wenzelm@30345
   249
    cat_error msg ("The error(s) above occurred in cpodef " ^ quote (Binding.str_of name));
wenzelm@23152
   250
huffman@33646
   251
fun prepare_pcpodef
huffman@33646
   252
      (prep_term: Proof.context -> 'a -> term)
huffman@33646
   253
      (def: bool)
huffman@33646
   254
      (name: binding)
wenzelm@35840
   255
      (typ: binding * (string * sort) list * mixfix)
huffman@33646
   256
      (raw_set: 'a)
huffman@33646
   257
      (opt_morphs: (binding * binding) option)
huffman@33646
   258
      (thy: theory)
huffman@33646
   259
    : term * term * (thm -> thm -> theory -> (Typedef.info * cpo_info * pcpo_info) * theory) =
huffman@33646
   260
  let
huffman@33678
   261
    val (newT, oldT, set, morphs as (Rep_name, Abs_name)) =
huffman@33678
   262
      prepare prep_term name typ raw_set opt_morphs thy;
huffman@33646
   263
huffman@33646
   264
    val goal_UU_mem =
huffman@33646
   265
      HOLogic.mk_Trueprop (HOLogic.mk_mem (Const (@{const_name UU}, oldT), set));
huffman@33646
   266
huffman@33646
   267
    val goal_admissible =
huffman@33646
   268
      HOLogic.mk_Trueprop (mk_adm ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), set)));
huffman@33646
   269
huffman@33646
   270
    fun pcpodef_result UU_mem admissible thy =
huffman@33646
   271
      let
huffman@33646
   272
        val tac = Tactic.rtac exI 1 THEN Tactic.rtac UU_mem 1;
huffman@33646
   273
        val ((info as {type_definition, set_def, ...}, below_def), thy2) = thy
huffman@33646
   274
          |> add_podef def (SOME name) typ set opt_morphs tac;
huffman@33646
   275
        val (cpo_info, thy3) = thy2
huffman@33646
   276
          |> prove_cpo name newT morphs type_definition set_def below_def admissible;
huffman@33646
   277
        val (pcpo_info, thy4) = thy3
huffman@33646
   278
          |> prove_pcpo name newT morphs type_definition set_def below_def UU_mem;
huffman@33646
   279
      in
huffman@33646
   280
        ((info, cpo_info, pcpo_info), thy4)
huffman@33646
   281
      end;
huffman@33646
   282
  in
huffman@33646
   283
    (goal_UU_mem, goal_admissible, pcpodef_result)
huffman@33646
   284
  end
huffman@33646
   285
  handle ERROR msg =>
huffman@33646
   286
    cat_error msg ("The error(s) above occurred in pcpodef " ^ quote (Binding.str_of name));
huffman@33646
   287
wenzelm@23152
   288
huffman@33645
   289
(* tactic interface *)
huffman@33645
   290
huffman@33646
   291
fun add_cpodef def opt_name typ set opt_morphs (tac1, tac2) thy =
huffman@33646
   292
  let
huffman@33646
   293
    val name = the_default (#1 typ) opt_name;
huffman@33646
   294
    val (goal1, goal2, cpodef_result) =
huffman@33646
   295
      prepare_cpodef Syntax.check_term def name typ set opt_morphs thy;
huffman@33646
   296
    val thm1 = Goal.prove_global thy [] [] goal1 (K tac1)
huffman@33646
   297
      handle ERROR msg => cat_error msg
huffman@33646
   298
        ("Failed to prove non-emptiness of " ^ quote (Syntax.string_of_term_global thy set));
huffman@33646
   299
    val thm2 = Goal.prove_global thy [] [] goal2 (K tac2)
huffman@33646
   300
      handle ERROR msg => cat_error msg
huffman@33646
   301
        ("Failed to prove admissibility of " ^ quote (Syntax.string_of_term_global thy set));
huffman@33646
   302
  in cpodef_result thm1 thm2 thy end;
huffman@33645
   303
huffman@33646
   304
fun add_pcpodef def opt_name typ set opt_morphs (tac1, tac2) thy =
huffman@33645
   305
  let
huffman@33645
   306
    val name = the_default (#1 typ) opt_name;
huffman@33645
   307
    val (goal1, goal2, pcpodef_result) =
huffman@33646
   308
      prepare_pcpodef Syntax.check_term def name typ set opt_morphs thy;
huffman@33646
   309
    val thm1 = Goal.prove_global thy [] [] goal1 (K tac1)
huffman@33645
   310
      handle ERROR msg => cat_error msg
huffman@33645
   311
        ("Failed to prove non-emptiness of " ^ quote (Syntax.string_of_term_global thy set));
huffman@33646
   312
    val thm2 = Goal.prove_global thy [] [] goal2 (K tac2)
huffman@33645
   313
      handle ERROR msg => cat_error msg
huffman@33645
   314
        ("Failed to prove admissibility of " ^ quote (Syntax.string_of_term_global thy set));
huffman@33645
   315
  in pcpodef_result thm1 thm2 thy end;
huffman@33645
   316
huffman@33645
   317
wenzelm@29060
   318
(* proof interface *)
wenzelm@29060
   319
wenzelm@29060
   320
local
wenzelm@23152
   321
wenzelm@35840
   322
fun gen_cpodef_proof prep_term prep_constraint
wenzelm@35840
   323
    ((def, name), (b, raw_args, mx), set, opt_morphs) thy =
wenzelm@23152
   324
  let
wenzelm@35840
   325
    val ctxt = ProofContext.init thy;
wenzelm@35840
   326
    val args = map (apsnd (prep_constraint ctxt)) raw_args;
wenzelm@29063
   327
    val (goal1, goal2, make_result) =
wenzelm@35840
   328
      prepare_cpodef prep_term def name (b, args, mx) set opt_morphs thy;
huffman@35902
   329
    fun after_qed [[th1, th2]] = ProofContext.theory (snd o make_result th1 th2)
huffman@35912
   330
      | after_qed _ = raise Fail "cpodef_proof";
wenzelm@35840
   331
  in Proof.theorem_i NONE after_qed [[(goal1, []), (goal2, [])]] ctxt end;
huffman@33646
   332
wenzelm@35840
   333
fun gen_pcpodef_proof prep_term prep_constraint
wenzelm@35840
   334
    ((def, name), (b, raw_args, mx), set, opt_morphs) thy =
huffman@33646
   335
  let
wenzelm@35840
   336
    val ctxt = ProofContext.init thy;
wenzelm@35840
   337
    val args = map (apsnd (prep_constraint ctxt)) raw_args;
huffman@33646
   338
    val (goal1, goal2, make_result) =
wenzelm@35840
   339
      prepare_pcpodef prep_term def name (b, args, mx) set opt_morphs thy;
huffman@35902
   340
    fun after_qed [[th1, th2]] = ProofContext.theory (snd o make_result th1 th2)
huffman@35912
   341
      | after_qed _ = raise Fail "pcpodef_proof";
wenzelm@35840
   342
  in Proof.theorem_i NONE after_qed [[(goal1, []), (goal2, [])]] ctxt end;
wenzelm@23152
   343
wenzelm@29060
   344
in
wenzelm@29060
   345
wenzelm@35840
   346
fun cpodef_proof x = gen_cpodef_proof Syntax.check_term (K I) x;
wenzelm@35840
   347
fun cpodef_proof_cmd x = gen_cpodef_proof Syntax.read_term Typedecl.read_constraint x;
wenzelm@23152
   348
wenzelm@35840
   349
fun pcpodef_proof x = gen_pcpodef_proof Syntax.check_term (K I) x;
wenzelm@35840
   350
fun pcpodef_proof_cmd x = gen_pcpodef_proof Syntax.read_term Typedecl.read_constraint x;
wenzelm@29060
   351
wenzelm@29060
   352
end;
wenzelm@29060
   353
wenzelm@23152
   354
wenzelm@23152
   355
wenzelm@23152
   356
(** outer syntax **)
wenzelm@23152
   357
wenzelm@23152
   358
local structure P = OuterParse and K = OuterKeyword in
wenzelm@23152
   359
wenzelm@23152
   360
val typedef_proof_decl =
wenzelm@23152
   361
  Scan.optional (P.$$$ "(" |--
wenzelm@30345
   362
      ((P.$$$ "open" >> K false) -- Scan.option P.binding || P.binding >> (fn s => (true, SOME s)))
wenzelm@23152
   363
        --| P.$$$ ")") (true, NONE) --
wenzelm@35840
   364
    (P.type_args_constrained -- P.binding) -- P.opt_mixfix -- (P.$$$ "=" |-- P.term) --
wenzelm@30345
   365
    Scan.option (P.$$$ "morphisms" |-- P.!!! (P.binding -- P.binding));
wenzelm@23152
   366
wenzelm@35840
   367
fun mk_pcpodef_proof pcpo ((((((def, opt_name), (args, t)), mx), A), morphs)) =
wenzelm@29060
   368
  (if pcpo then pcpodef_proof_cmd else cpodef_proof_cmd)
wenzelm@35840
   369
    ((def, the_default t opt_name), (t, args, mx), A, morphs);
wenzelm@23152
   370
wenzelm@24867
   371
val _ =
wenzelm@23152
   372
  OuterSyntax.command "pcpodef" "HOLCF type definition (requires admissibility proof)" K.thy_goal
wenzelm@23152
   373
    (typedef_proof_decl >>
wenzelm@23152
   374
      (Toplevel.print oo (Toplevel.theory_to_proof o mk_pcpodef_proof true)));
wenzelm@23152
   375
wenzelm@24867
   376
val _ =
wenzelm@23152
   377
  OuterSyntax.command "cpodef" "HOLCF type definition (requires admissibility proof)" K.thy_goal
wenzelm@23152
   378
    (typedef_proof_decl >>
wenzelm@23152
   379
      (Toplevel.print oo (Toplevel.theory_to_proof o mk_pcpodef_proof false)));
wenzelm@23152
   380
wenzelm@23152
   381
end;
wenzelm@23152
   382
wenzelm@23152
   383
end;