src/HOLCF/Tools/pcpodef.ML
author huffman
Wed Nov 11 10:15:32 2009 -0800 (2009-11-11)
changeset 33645 562635ab559b
parent 33553 35f2b30593a8
child 33646 d2f3104ca3d2
permissions -rw-r--r--
use Drule.standard (following typedef package), add pcpodef tactic interface
     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   val add_pcpodef: bool -> binding option -> binding * string list * mixfix ->
    11     term -> (binding * binding) option -> tactic -> theory -> theory
    12   val add_cpodef: bool -> binding option -> binding * string list * mixfix ->
    13     term -> (binding * binding) option -> tactic -> theory -> theory
    14   val pcpodef_proof: (bool * binding) * (binding * string list * mixfix) * term
    15     * (binding * binding) option -> theory -> Proof.state
    16   val pcpodef_proof_cmd: (bool * binding) * (binding * string list * mixfix) * string
    17     * (binding * binding) option -> theory -> Proof.state
    18   val cpodef_proof: (bool * binding) * (binding * string list * mixfix) * term
    19     * (binding * binding) option -> theory -> Proof.state
    20   val cpodef_proof_cmd: (bool * binding) * (binding * string list * mixfix) * string
    21     * (binding * binding) option -> theory -> Proof.state
    22 end;
    23 
    24 structure Pcpodef :> PCPODEF =
    25 struct
    26 
    27 (** type definitions **)
    28 
    29 (* prepare_cpodef *)
    30 
    31 fun declare_type_name a = Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS)));
    32 
    33 fun adm_const T = Const (@{const_name adm}, (T --> HOLogic.boolT) --> HOLogic.boolT);
    34 fun mk_adm (x, T, P) = adm_const T $ absfree (x, T, P);
    35 
    36 fun prepare_pcpodef prep_term pcpo def name (t, vs, mx) raw_set opt_morphs thy =
    37   let
    38     val _ = Theory.requires thy "Pcpodef" "pcpodefs";
    39     val ctxt = ProofContext.init thy;
    40 
    41     val full = Sign.full_name thy;
    42     val full_name = full name;
    43     val bname = Binding.name_of name;
    44 
    45     (*rhs*)
    46     val set = prep_term (ctxt |> fold declare_type_name vs) raw_set;
    47     val setT = Term.fastype_of set;
    48     val rhs_tfrees = Term.add_tfrees set [];
    49     val oldT = HOLogic.dest_setT setT handle TYPE _ =>
    50       error ("Not a set type: " ^ quote (Syntax.string_of_typ ctxt setT));
    51 
    52     (*goal*)
    53     val goal_UU_mem = HOLogic.mk_Trueprop (HOLogic.mk_mem (Const (@{const_name UU}, oldT), set));
    54     val goal_nonempty =
    55       HOLogic.mk_Trueprop (HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), set)));
    56     val goal_admissible =
    57       HOLogic.mk_Trueprop (mk_adm ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), set)));
    58 
    59     (*lhs*)
    60     val defS = Sign.defaultS thy;
    61     val lhs_tfrees = map (fn v => (v, the_default defS (AList.lookup (op =) rhs_tfrees v))) vs;
    62     val lhs_sorts = map snd lhs_tfrees;
    63 
    64     val tname = Binding.map_name (Syntax.type_name mx) t;
    65     val full_tname = full tname;
    66     val newT = Type (full_tname, map TFree lhs_tfrees);
    67 
    68     val (Rep_name, Abs_name) =
    69       (case opt_morphs of
    70         NONE => (Binding.prefix_name "Rep_" name, Binding.prefix_name "Abs_" name)
    71       | SOME morphs => morphs);
    72     val RepC = Const (full Rep_name, newT --> oldT);
    73     fun belowC T = Const (@{const_name below}, T --> T --> HOLogic.boolT);
    74     val below_def = Logic.mk_equals (belowC newT,
    75       Abs ("x", newT, Abs ("y", newT, belowC oldT $ (RepC $ Bound 1) $ (RepC $ Bound 0))));
    76 
    77     fun make_po tac thy1 =
    78       let
    79         val ((_, {type_definition, set_def, ...}), thy2) = thy1
    80           |> Typedef.add_typedef def (SOME name) (t, vs, mx) set opt_morphs tac;
    81         val lthy3 = thy2
    82           |> Theory_Target.instantiation ([full_tname], lhs_tfrees, @{sort po});
    83         val below_def' = Syntax.check_term lthy3 below_def;
    84         val ((_, (_, below_definition')), lthy4) = lthy3
    85           |> Specification.definition (NONE,
    86               ((Binding.prefix_name "below_" (Binding.suffix_name "_def" name), []), below_def'));
    87         val ctxt_thy = ProofContext.init (ProofContext.theory_of lthy4);
    88         val below_definition = singleton (ProofContext.export lthy4 ctxt_thy) below_definition';
    89         val thy5 = lthy4
    90           |> Class.prove_instantiation_instance
    91               (K (Tactic.rtac (@{thm typedef_po} OF [type_definition, below_definition]) 1))
    92           |> LocalTheory.exit_global;
    93       in ((type_definition, below_definition, set_def), thy5) end;
    94 
    95     fun make_cpo admissible (type_def, below_def, set_def) theory =
    96       let
    97         (* FIXME fold_rule might fold user input inintentionally *)
    98         val admissible' = fold_rule (the_list set_def) admissible;
    99         val cpo_thms = map (Thm.transfer theory) [type_def, below_def, admissible'];
   100         val theory' = theory
   101           |> AxClass.prove_arity (full_tname, lhs_sorts, @{sort cpo})
   102             (Tactic.rtac (@{thm typedef_cpo} OF cpo_thms) 1);
   103         val cpo_thms' = map (Thm.transfer theory') cpo_thms;
   104         fun make thm = Drule.standard (thm OF cpo_thms');
   105       in
   106         theory'
   107         |> Sign.add_path (Binding.name_of name)
   108         |> PureThy.add_thms
   109           ([((Binding.prefix_name "adm_" name, admissible'), []),
   110             ((Binding.prefix_name "cont_" Rep_name, make @{thm typedef_cont_Rep}), []),
   111             ((Binding.prefix_name "cont_" Abs_name, make @{thm typedef_cont_Abs}), []),
   112             ((Binding.prefix_name "lub_" name, make @{thm typedef_lub}), []),
   113             ((Binding.prefix_name "thelub_" name, make @{thm typedef_thelub}), []),
   114             ((Binding.prefix_name "compact_" name, make @{thm typedef_compact}), [])])
   115         |> snd
   116         |> Sign.parent_path
   117       end;
   118 
   119     fun make_pcpo UU_mem (type_def, below_def, set_def) theory =
   120       let
   121         (* FIXME fold_rule might fold user input inintentionally *)
   122         val UU_mem' = fold_rule (the_list set_def) UU_mem;
   123         val pcpo_thms = map (Thm.transfer theory) [type_def, below_def, UU_mem'];
   124         val theory' = theory
   125           |> AxClass.prove_arity (full_tname, lhs_sorts, @{sort pcpo})
   126             (Tactic.rtac (@{thm typedef_pcpo} OF pcpo_thms) 1);
   127         val pcpo_thms' = map (Thm.transfer theory') pcpo_thms;
   128         fun make thm = Drule.standard (thm OF pcpo_thms');
   129       in
   130         theory'
   131         |> Sign.add_path (Binding.name_of name)
   132         |> PureThy.add_thms
   133           ([((Binding.suffix_name "_strict" Rep_name, make @{thm typedef_Rep_strict}), []),
   134             ((Binding.suffix_name "_strict" Abs_name, make @{thm typedef_Abs_strict}), []),
   135             ((Binding.suffix_name "_strict_iff" Rep_name, make @{thm typedef_Rep_strict_iff}), []),
   136             ((Binding.suffix_name "_strict_iff" Abs_name, make @{thm typedef_Abs_strict_iff}), []),
   137             ((Binding.suffix_name "_defined" Rep_name, make @{thm typedef_Rep_defined}), []),
   138             ((Binding.suffix_name "_defined" Abs_name, make @{thm typedef_Abs_defined}), [])])
   139         |> snd
   140         |> Sign.parent_path
   141       end;
   142 
   143     fun pcpodef_result UU_mem admissible =
   144       make_po (Tactic.rtac exI 1 THEN Tactic.rtac UU_mem 1)
   145       #-> (fn defs => make_cpo admissible defs #> make_pcpo UU_mem defs);
   146 
   147     fun cpodef_result nonempty admissible =
   148       make_po (Tactic.rtac nonempty 1)
   149       #-> make_cpo admissible;
   150   in
   151     if pcpo
   152     then (goal_UU_mem, goal_admissible, pcpodef_result)
   153     else (goal_nonempty, goal_admissible, cpodef_result)
   154   end
   155   handle ERROR msg =>
   156     cat_error msg ("The error(s) above occurred in cpodef " ^ quote (Binding.str_of name));
   157 
   158 
   159 (* tactic interface *)
   160 
   161 local
   162 
   163 fun gen_add_pcpodef pcpo def opt_name typ set opt_morphs tac thy =
   164   let
   165     val name = the_default (#1 typ) opt_name;
   166     val (goal1, goal2, pcpodef_result) =
   167       prepare_pcpodef Syntax.check_term pcpo def name typ set opt_morphs thy;
   168     val thm1 = Goal.prove_global thy [] [] goal1 (K tac)
   169       handle ERROR msg => cat_error msg
   170         ("Failed to prove non-emptiness of " ^ quote (Syntax.string_of_term_global thy set));
   171     val thm2 = Goal.prove_global thy [] [] goal2 (K tac)
   172       handle ERROR msg => cat_error msg
   173         ("Failed to prove admissibility of " ^ quote (Syntax.string_of_term_global thy set));
   174   in pcpodef_result thm1 thm2 thy end;
   175 
   176 in
   177 
   178 val add_pcpodef = gen_add_pcpodef true;
   179 val add_cpodef = gen_add_pcpodef false;
   180 
   181 end;
   182 
   183 (* proof interface *)
   184 
   185 local
   186 
   187 fun gen_pcpodef_proof prep_term pcpo ((def, name), typ, set, opt_morphs) thy =
   188   let
   189     val (goal1, goal2, make_result) =
   190       prepare_pcpodef prep_term pcpo def name typ set opt_morphs thy;
   191     fun after_qed [[th1, th2]] = ProofContext.theory (make_result th1 th2);
   192   in Proof.theorem_i NONE after_qed [[(goal1, []), (goal2, [])]] (ProofContext.init thy) end;
   193 
   194 in
   195 
   196 fun pcpodef_proof x = gen_pcpodef_proof Syntax.check_term true x;
   197 fun pcpodef_proof_cmd x = gen_pcpodef_proof Syntax.read_term true x;
   198 
   199 fun cpodef_proof x = gen_pcpodef_proof Syntax.check_term false x;
   200 fun cpodef_proof_cmd x = gen_pcpodef_proof Syntax.read_term false x;
   201 
   202 end;
   203 
   204 
   205 
   206 (** outer syntax **)
   207 
   208 local structure P = OuterParse and K = OuterKeyword in
   209 
   210 val typedef_proof_decl =
   211   Scan.optional (P.$$$ "(" |--
   212       ((P.$$$ "open" >> K false) -- Scan.option P.binding || P.binding >> (fn s => (true, SOME s)))
   213         --| P.$$$ ")") (true, NONE) --
   214     (P.type_args -- P.binding) -- P.opt_infix -- (P.$$$ "=" |-- P.term) --
   215     Scan.option (P.$$$ "morphisms" |-- P.!!! (P.binding -- P.binding));
   216 
   217 fun mk_pcpodef_proof pcpo ((((((def, opt_name), (vs, t)), mx), A), morphs)) =
   218   (if pcpo then pcpodef_proof_cmd else cpodef_proof_cmd)
   219     ((def, the_default (Binding.map_name (Syntax.type_name mx) t) opt_name), (t, vs, mx), A, morphs);
   220 
   221 val _ =
   222   OuterSyntax.command "pcpodef" "HOLCF type definition (requires admissibility proof)" K.thy_goal
   223     (typedef_proof_decl >>
   224       (Toplevel.print oo (Toplevel.theory_to_proof o mk_pcpodef_proof true)));
   225 
   226 val _ =
   227   OuterSyntax.command "cpodef" "HOLCF type definition (requires admissibility proof)" K.thy_goal
   228     (typedef_proof_decl >>
   229       (Toplevel.print oo (Toplevel.theory_to_proof o mk_pcpodef_proof false)));
   230 
   231 end;
   232 
   233 end;