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