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