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