src/HOLCF/Tools/pcpodef.ML
changeset 31740 002da20f442e
parent 31738 7b9b9ba532ca
child 33226 9a2911232c1b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOLCF/Tools/pcpodef.ML	Sun Jun 21 15:45:57 2009 +0200
     1.3 @@ -0,0 +1,201 @@
     1.4 +(*  Title:      HOLCF/Tools/pcpodef.ML
     1.5 +    Author:     Brian Huffman
     1.6 +
     1.7 +Primitive domain definitions for HOLCF, similar to Gordon/HOL-style
     1.8 +typedef (see also ~~/src/HOL/Tools/typedef.ML).
     1.9 +*)
    1.10 +
    1.11 +signature PCPODEF =
    1.12 +sig
    1.13 +  val pcpodef_proof: (bool * binding) * (binding * string list * mixfix) * term
    1.14 +    * (binding * binding) option -> theory -> Proof.state
    1.15 +  val pcpodef_proof_cmd: (bool * binding) * (binding * string list * mixfix) * string
    1.16 +    * (binding * binding) option -> theory -> Proof.state
    1.17 +  val cpodef_proof: (bool * binding) * (binding * string list * mixfix) * term
    1.18 +    * (binding * binding) option -> theory -> Proof.state
    1.19 +  val cpodef_proof_cmd: (bool * binding) * (binding * string list * mixfix) * string
    1.20 +    * (binding * binding) option -> theory -> Proof.state
    1.21 +end;
    1.22 +
    1.23 +structure Pcpodef :> PCPODEF =
    1.24 +struct
    1.25 +
    1.26 +(** type definitions **)
    1.27 +
    1.28 +(* prepare_cpodef *)
    1.29 +
    1.30 +fun declare_type_name a = Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS)));
    1.31 +
    1.32 +fun adm_const T = Const (@{const_name adm}, (T --> HOLogic.boolT) --> HOLogic.boolT);
    1.33 +fun mk_adm (x, T, P) = adm_const T $ absfree (x, T, P);
    1.34 +
    1.35 +fun prepare_pcpodef prep_term pcpo def name (t, vs, mx) raw_set opt_morphs thy =
    1.36 +  let
    1.37 +    val _ = Theory.requires thy "Pcpodef" "pcpodefs";
    1.38 +    val ctxt = ProofContext.init thy;
    1.39 +
    1.40 +    val full = Sign.full_name thy;
    1.41 +    val full_name = full name;
    1.42 +    val bname = Binding.name_of name;
    1.43 +
    1.44 +    (*rhs*)
    1.45 +    val set = prep_term (ctxt |> fold declare_type_name vs) raw_set;
    1.46 +    val setT = Term.fastype_of set;
    1.47 +    val rhs_tfrees = Term.add_tfrees set [];
    1.48 +    val oldT = HOLogic.dest_setT setT handle TYPE _ =>
    1.49 +      error ("Not a set type: " ^ quote (Syntax.string_of_typ ctxt setT));
    1.50 +
    1.51 +    (*goal*)
    1.52 +    val goal_UU_mem = HOLogic.mk_Trueprop (HOLogic.mk_mem (Const (@{const_name UU}, oldT), set));
    1.53 +    val goal_nonempty =
    1.54 +      HOLogic.mk_Trueprop (HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), set)));
    1.55 +    val goal_admissible =
    1.56 +      HOLogic.mk_Trueprop (mk_adm ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), set)));
    1.57 +
    1.58 +    (*lhs*)
    1.59 +    val defS = Sign.defaultS thy;
    1.60 +    val lhs_tfrees = map (fn v => (v, the_default defS (AList.lookup (op =) rhs_tfrees v))) vs;
    1.61 +    val lhs_sorts = map snd lhs_tfrees;
    1.62 +
    1.63 +    val tname = Binding.map_name (Syntax.type_name mx) t;
    1.64 +    val full_tname = full tname;
    1.65 +    val newT = Type (full_tname, map TFree lhs_tfrees);
    1.66 +
    1.67 +    val (Rep_name, Abs_name) =
    1.68 +      (case opt_morphs of
    1.69 +        NONE => (Binding.prefix_name "Rep_" name, Binding.prefix_name "Abs_" name)
    1.70 +      | SOME morphs => morphs);
    1.71 +    val RepC = Const (full Rep_name, newT --> oldT);
    1.72 +    fun belowC T = Const (@{const_name below}, T --> T --> HOLogic.boolT);
    1.73 +    val below_def = Logic.mk_equals (belowC newT,
    1.74 +      Abs ("x", newT, Abs ("y", newT, belowC oldT $ (RepC $ Bound 1) $ (RepC $ Bound 0))));
    1.75 +
    1.76 +    fun make_po tac thy1 =
    1.77 +      let
    1.78 +        val ((_, {type_definition, set_def, ...}), thy2) = thy1
    1.79 +          |> Typedef.add_typedef def (SOME name) (t, vs, mx) set opt_morphs tac;
    1.80 +        val lthy3 = thy2
    1.81 +          |> TheoryTarget.instantiation ([full_tname], lhs_tfrees, @{sort po});
    1.82 +        val below_def' = Syntax.check_term lthy3 below_def;
    1.83 +        val ((_, (_, below_definition')), lthy4) = lthy3
    1.84 +          |> Specification.definition (NONE,
    1.85 +              ((Binding.prefix_name "below_" (Binding.suffix_name "_def" name), []), below_def'));
    1.86 +        val ctxt_thy = ProofContext.init (ProofContext.theory_of lthy4);
    1.87 +        val below_definition = singleton (ProofContext.export lthy4 ctxt_thy) below_definition';
    1.88 +        val thy5 = lthy4
    1.89 +          |> Class.prove_instantiation_instance
    1.90 +              (K (Tactic.rtac (@{thm typedef_po} OF [type_definition, below_definition]) 1))
    1.91 +          |> LocalTheory.exit_global;
    1.92 +      in ((type_definition, below_definition, set_def), thy5) end;
    1.93 +
    1.94 +    fun make_cpo admissible (type_def, below_def, set_def) theory =
    1.95 +      let
    1.96 +        val admissible' = fold_rule (the_list set_def) admissible;
    1.97 +        val cpo_thms = map (Thm.transfer theory) [type_def, below_def, admissible'];
    1.98 +        val theory' = theory
    1.99 +          |> AxClass.prove_arity (full_tname, lhs_sorts, @{sort cpo})
   1.100 +            (Tactic.rtac (@{thm typedef_cpo} OF cpo_thms) 1);
   1.101 +        val cpo_thms' = map (Thm.transfer theory') cpo_thms;
   1.102 +      in
   1.103 +        theory'
   1.104 +        |> Sign.add_path (Binding.name_of name)
   1.105 +        |> PureThy.add_thms
   1.106 +          ([((Binding.prefix_name "adm_" name, admissible'), []),
   1.107 +            ((Binding.prefix_name "cont_" Rep_name, @{thm typedef_cont_Rep} OF cpo_thms'), []),
   1.108 +            ((Binding.prefix_name "cont_" Abs_name, @{thm typedef_cont_Abs} OF cpo_thms'), []),
   1.109 +            ((Binding.prefix_name "lub_" name, @{thm typedef_lub} OF cpo_thms'), []),
   1.110 +            ((Binding.prefix_name "thelub_" name, @{thm typedef_thelub} OF cpo_thms'), []),
   1.111 +            ((Binding.prefix_name "compact_" name, @{thm typedef_compact} OF cpo_thms'), [])])
   1.112 +        |> snd
   1.113 +        |> Sign.parent_path
   1.114 +      end;
   1.115 +
   1.116 +    fun make_pcpo UU_mem (type_def, below_def, set_def) theory =
   1.117 +      let
   1.118 +        val UU_mem' = fold_rule (the_list set_def) UU_mem;
   1.119 +        val pcpo_thms = map (Thm.transfer theory) [type_def, below_def, UU_mem'];
   1.120 +        val theory' = theory
   1.121 +          |> AxClass.prove_arity (full_tname, lhs_sorts, @{sort pcpo})
   1.122 +            (Tactic.rtac (@{thm typedef_pcpo} OF pcpo_thms) 1);
   1.123 +        val pcpo_thms' = map (Thm.transfer theory') pcpo_thms;
   1.124 +      in
   1.125 +        theory'
   1.126 +        |> Sign.add_path (Binding.name_of name)
   1.127 +        |> PureThy.add_thms
   1.128 +          ([((Binding.suffix_name "_strict" Rep_name, @{thm typedef_Rep_strict} OF pcpo_thms'), []),
   1.129 +            ((Binding.suffix_name "_strict" Abs_name, @{thm typedef_Abs_strict} OF pcpo_thms'), []),
   1.130 +            ((Binding.suffix_name "_strict_iff" Rep_name, @{thm typedef_Rep_strict_iff} OF pcpo_thms'), []),
   1.131 +            ((Binding.suffix_name "_strict_iff" Abs_name, @{thm typedef_Abs_strict_iff} OF pcpo_thms'), []),
   1.132 +            ((Binding.suffix_name "_defined" Rep_name, @{thm typedef_Rep_defined} OF pcpo_thms'), []),
   1.133 +            ((Binding.suffix_name "_defined" Abs_name, @{thm typedef_Abs_defined} OF pcpo_thms'), [])])
   1.134 +        |> snd
   1.135 +        |> Sign.parent_path
   1.136 +      end;
   1.137 +
   1.138 +    fun pcpodef_result UU_mem admissible =
   1.139 +      make_po (Tactic.rtac exI 1 THEN Tactic.rtac UU_mem 1)
   1.140 +      #-> (fn defs => make_cpo admissible defs #> make_pcpo UU_mem defs);
   1.141 +
   1.142 +    fun cpodef_result nonempty admissible =
   1.143 +      make_po (Tactic.rtac nonempty 1)
   1.144 +      #-> make_cpo admissible;
   1.145 +  in
   1.146 +    if pcpo
   1.147 +    then (goal_UU_mem, goal_admissible, pcpodef_result)
   1.148 +    else (goal_nonempty, goal_admissible, cpodef_result)
   1.149 +  end
   1.150 +  handle ERROR msg =>
   1.151 +    cat_error msg ("The error(s) above occurred in cpodef " ^ quote (Binding.str_of name));
   1.152 +
   1.153 +
   1.154 +(* proof interface *)
   1.155 +
   1.156 +local
   1.157 +
   1.158 +fun gen_pcpodef_proof prep_term pcpo ((def, name), typ, set, opt_morphs) thy =
   1.159 +  let
   1.160 +    val (goal1, goal2, make_result) =
   1.161 +      prepare_pcpodef prep_term pcpo def name typ set opt_morphs thy;
   1.162 +    fun after_qed [[th1, th2]] = ProofContext.theory (make_result th1 th2);
   1.163 +  in Proof.theorem_i NONE after_qed [[(goal1, []), (goal2, [])]] (ProofContext.init thy) end;
   1.164 +
   1.165 +in
   1.166 +
   1.167 +fun pcpodef_proof x = gen_pcpodef_proof Syntax.check_term true x;
   1.168 +fun pcpodef_proof_cmd x = gen_pcpodef_proof Syntax.read_term true x;
   1.169 +
   1.170 +fun cpodef_proof x = gen_pcpodef_proof Syntax.check_term false x;
   1.171 +fun cpodef_proof_cmd x = gen_pcpodef_proof Syntax.read_term false x;
   1.172 +
   1.173 +end;
   1.174 +
   1.175 +
   1.176 +
   1.177 +(** outer syntax **)
   1.178 +
   1.179 +local structure P = OuterParse and K = OuterKeyword in
   1.180 +
   1.181 +val typedef_proof_decl =
   1.182 +  Scan.optional (P.$$$ "(" |--
   1.183 +      ((P.$$$ "open" >> K false) -- Scan.option P.binding || P.binding >> (fn s => (true, SOME s)))
   1.184 +        --| P.$$$ ")") (true, NONE) --
   1.185 +    (P.type_args -- P.binding) -- P.opt_infix -- (P.$$$ "=" |-- P.term) --
   1.186 +    Scan.option (P.$$$ "morphisms" |-- P.!!! (P.binding -- P.binding));
   1.187 +
   1.188 +fun mk_pcpodef_proof pcpo ((((((def, opt_name), (vs, t)), mx), A), morphs)) =
   1.189 +  (if pcpo then pcpodef_proof_cmd else cpodef_proof_cmd)
   1.190 +    ((def, the_default (Binding.map_name (Syntax.type_name mx) t) opt_name), (t, vs, mx), A, morphs);
   1.191 +
   1.192 +val _ =
   1.193 +  OuterSyntax.command "pcpodef" "HOLCF type definition (requires admissibility proof)" K.thy_goal
   1.194 +    (typedef_proof_decl >>
   1.195 +      (Toplevel.print oo (Toplevel.theory_to_proof o mk_pcpodef_proof true)));
   1.196 +
   1.197 +val _ =
   1.198 +  OuterSyntax.command "cpodef" "HOLCF type definition (requires admissibility proof)" K.thy_goal
   1.199 +    (typedef_proof_decl >>
   1.200 +      (Toplevel.print oo (Toplevel.theory_to_proof o mk_pcpodef_proof false)));
   1.201 +
   1.202 +end;
   1.203 +
   1.204 +end;