src/HOLCF/Tools/pcpodef_package.ML
changeset 30345 76fd85bbf139
parent 29585 c23295521af5
child 31023 d027411c9a38
equal deleted inserted replaced
30344:10a67c5ddddb 30345:76fd85bbf139
     5 typedef (see also ~~/src/HOL/Tools/typedef_package.ML).
     5 typedef (see also ~~/src/HOL/Tools/typedef_package.ML).
     6 *)
     6 *)
     7 
     7 
     8 signature PCPODEF_PACKAGE =
     8 signature PCPODEF_PACKAGE =
     9 sig
     9 sig
    10   val pcpodef_proof: (bool * string) * (bstring * string list * mixfix) * term
    10   val pcpodef_proof: (bool * binding) * (binding * string list * mixfix) * term
    11     * (string * string) option -> theory -> Proof.state
    11     * (binding * binding) option -> theory -> Proof.state
    12   val pcpodef_proof_cmd: (bool * string) * (bstring * string list * mixfix) * string
    12   val pcpodef_proof_cmd: (bool * binding) * (binding * string list * mixfix) * string
    13     * (string * string) option -> theory -> Proof.state
    13     * (binding * binding) option -> theory -> Proof.state
    14   val cpodef_proof: (bool * string) * (bstring * string list * mixfix) * term
    14   val cpodef_proof: (bool * binding) * (binding * string list * mixfix) * term
    15     * (string * string) option -> theory -> Proof.state
    15     * (binding * binding) option -> theory -> Proof.state
    16   val cpodef_proof_cmd: (bool * string) * (bstring * string list * mixfix) * string
    16   val cpodef_proof_cmd: (bool * binding) * (binding * string list * mixfix) * string
    17     * (string * string) option -> theory -> Proof.state
    17     * (binding * binding) option -> theory -> Proof.state
    18 end;
    18 end;
    19 
    19 
    20 structure PcpodefPackage: PCPODEF_PACKAGE =
    20 structure PcpodefPackage: PCPODEF_PACKAGE =
    21 struct
    21 struct
    22 
    22 
    23 (** type definitions **)
    23 (** type definitions **)
    24 
    24 
    25 (* prepare_cpodef *)
    25 (* prepare_cpodef *)
    26 
       
    27 fun err_in_cpodef msg name =
       
    28   cat_error msg ("The error(s) above occurred in cpodef " ^ quote name);
       
    29 
    26 
    30 fun declare_type_name a = Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS)));
    27 fun declare_type_name a = Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS)));
    31 
    28 
    32 fun adm_const T = Const (@{const_name adm}, (T --> HOLogic.boolT) --> HOLogic.boolT);
    29 fun adm_const T = Const (@{const_name adm}, (T --> HOLogic.boolT) --> HOLogic.boolT);
    33 fun mk_adm (x, T, P) = adm_const T $ absfree (x, T, P);
    30 fun mk_adm (x, T, P) = adm_const T $ absfree (x, T, P);
    34 
    31 
    35 fun prepare_pcpodef prep_term pcpo def name (t, vs, mx) raw_set opt_morphs thy =
    32 fun prepare_pcpodef prep_term pcpo def name (t, vs, mx) raw_set opt_morphs thy =
    36   let
    33   let
    37     val _ = Theory.requires thy "Pcpodef" "pcpodefs";
    34     val _ = Theory.requires thy "Pcpodef" "pcpodefs";
    38     val ctxt = ProofContext.init thy;
    35     val ctxt = ProofContext.init thy;
    39     val full = Sign.full_bname thy;
    36 
       
    37     val full = Sign.full_name thy;
       
    38     val full_name = full name;
       
    39     val bname = Binding.name_of name;
    40 
    40 
    41     (*rhs*)
    41     (*rhs*)
    42     val full_name = full name;
       
    43     val set = prep_term (ctxt |> fold declare_type_name vs) raw_set;
    42     val set = prep_term (ctxt |> fold declare_type_name vs) raw_set;
    44     val setT = Term.fastype_of set;
    43     val setT = Term.fastype_of set;
    45     val rhs_tfrees = Term.add_tfrees set [];
    44     val rhs_tfrees = Term.add_tfrees set [];
    46     val oldT = HOLogic.dest_setT setT handle TYPE _ =>
    45     val oldT = HOLogic.dest_setT setT handle TYPE _ =>
    47       error ("Not a set type: " ^ quote (Syntax.string_of_typ ctxt setT));
    46       error ("Not a set type: " ^ quote (Syntax.string_of_typ ctxt setT));
    56     (*lhs*)
    55     (*lhs*)
    57     val defS = Sign.defaultS thy;
    56     val defS = Sign.defaultS thy;
    58     val lhs_tfrees = map (fn v => (v, the_default defS (AList.lookup (op =) rhs_tfrees v))) vs;
    57     val lhs_tfrees = map (fn v => (v, the_default defS (AList.lookup (op =) rhs_tfrees v))) vs;
    59     val lhs_sorts = map snd lhs_tfrees;
    58     val lhs_sorts = map snd lhs_tfrees;
    60 
    59 
    61     val tname = Syntax.type_name t mx;
    60     val tname = Binding.map_name (Syntax.type_name mx) t;
    62     val full_tname = full tname;
    61     val full_tname = full tname;
    63     val newT = Type (full_tname, map TFree lhs_tfrees);
    62     val newT = Type (full_tname, map TFree lhs_tfrees);
    64 
    63 
    65     val (Rep_name, Abs_name) = the_default ("Rep_" ^ name, "Abs_" ^ name) opt_morphs;
    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);
    66     val RepC = Const (full Rep_name, newT --> oldT);
    68     val RepC = Const (full Rep_name, newT --> oldT);
    67     fun lessC T = Const (@{const_name sq_le}, T --> T --> HOLogic.boolT);
    69     fun lessC T = Const (@{const_name sq_le}, T --> T --> HOLogic.boolT);
    68     val less_def = Logic.mk_equals (lessC newT,
    70     val less_def = Logic.mk_equals (lessC newT,
    69       Abs ("x", newT, Abs ("y", newT, lessC oldT $ (RepC $ Bound 1) $ (RepC $ Bound 0))));
    71       Abs ("x", newT, Abs ("y", newT, lessC oldT $ (RepC $ Bound 1) $ (RepC $ Bound 0))));
    70 
    72 
    74           |> TypedefPackage.add_typedef def (SOME name) (t, vs, mx) set opt_morphs tac;
    76           |> TypedefPackage.add_typedef def (SOME name) (t, vs, mx) set opt_morphs tac;
    75         val lthy3 = thy2
    77         val lthy3 = thy2
    76           |> TheoryTarget.instantiation ([full_tname], lhs_tfrees, @{sort po});
    78           |> TheoryTarget.instantiation ([full_tname], lhs_tfrees, @{sort po});
    77         val less_def' = Syntax.check_term lthy3 less_def;
    79         val less_def' = Syntax.check_term lthy3 less_def;
    78         val ((_, (_, less_definition')), lthy4) = lthy3
    80         val ((_, (_, less_definition')), lthy4) = lthy3
    79           |> Specification.definition (NONE, ((Binding.name ("less_" ^ name ^ "_def"), []), less_def'));
    81           |> Specification.definition (NONE,
       
    82               ((Binding.prefix_name "less_" (Binding.suffix_name "_def" name), []), less_def'));
    80         val ctxt_thy = ProofContext.init (ProofContext.theory_of lthy4);
    83         val ctxt_thy = ProofContext.init (ProofContext.theory_of lthy4);
    81         val less_definition = singleton (ProofContext.export lthy4 ctxt_thy) less_definition';
    84         val less_definition = singleton (ProofContext.export lthy4 ctxt_thy) less_definition';
    82         val thy5 = lthy4
    85         val thy5 = lthy4
    83           |> Class.prove_instantiation_instance
    86           |> Class.prove_instantiation_instance
    84               (K (Tactic.rtac (@{thm typedef_po} OF [type_definition, less_definition]) 1))
    87               (K (Tactic.rtac (@{thm typedef_po} OF [type_definition, less_definition]) 1))
    93           |> AxClass.prove_arity (full_tname, lhs_sorts, @{sort cpo})
    96           |> AxClass.prove_arity (full_tname, lhs_sorts, @{sort cpo})
    94             (Tactic.rtac (@{thm typedef_cpo} OF cpo_thms) 1);
    97             (Tactic.rtac (@{thm typedef_cpo} OF cpo_thms) 1);
    95         val cpo_thms' = map (Thm.transfer theory') cpo_thms;
    98         val cpo_thms' = map (Thm.transfer theory') cpo_thms;
    96       in
    99       in
    97         theory'
   100         theory'
    98         |> Sign.add_path name
   101         |> Sign.add_path (Binding.name_of name)
    99         |> PureThy.add_thms
   102         |> PureThy.add_thms
   100           ([((Binding.name ("adm_" ^ name), admissible'), []),
   103           ([((Binding.prefix_name "adm_" name, admissible'), []),
   101             ((Binding.name ("cont_" ^ Rep_name), @{thm typedef_cont_Rep} OF cpo_thms'), []),
   104             ((Binding.prefix_name "cont_" Rep_name, @{thm typedef_cont_Rep} OF cpo_thms'), []),
   102             ((Binding.name ("cont_" ^ Abs_name), @{thm typedef_cont_Abs} OF cpo_thms'), []),
   105             ((Binding.prefix_name "cont_" Abs_name, @{thm typedef_cont_Abs} OF cpo_thms'), []),
   103             ((Binding.name ("lub_" ^ name), @{thm typedef_lub} OF cpo_thms'), []),
   106             ((Binding.prefix_name "lub_" name, @{thm typedef_lub} OF cpo_thms'), []),
   104             ((Binding.name ("thelub_" ^ name), @{thm typedef_thelub} OF cpo_thms'), []),
   107             ((Binding.prefix_name "thelub_" name, @{thm typedef_thelub} OF cpo_thms'), []),
   105             ((Binding.name ("compact_" ^ name), @{thm typedef_compact} OF cpo_thms'), [])])
   108             ((Binding.prefix_name "compact_" name, @{thm typedef_compact} OF cpo_thms'), [])])
   106         |> snd
   109         |> snd
   107         |> Sign.parent_path
   110         |> Sign.parent_path
   108       end;
   111       end;
   109 
   112 
   110     fun make_pcpo UU_mem (type_def, less_def, set_def) theory =
   113     fun make_pcpo UU_mem (type_def, less_def, set_def) theory =
   115           |> AxClass.prove_arity (full_tname, lhs_sorts, @{sort pcpo})
   118           |> AxClass.prove_arity (full_tname, lhs_sorts, @{sort pcpo})
   116             (Tactic.rtac (@{thm typedef_pcpo} OF pcpo_thms) 1);
   119             (Tactic.rtac (@{thm typedef_pcpo} OF pcpo_thms) 1);
   117         val pcpo_thms' = map (Thm.transfer theory') pcpo_thms;
   120         val pcpo_thms' = map (Thm.transfer theory') pcpo_thms;
   118       in
   121       in
   119         theory'
   122         theory'
   120         |> Sign.add_path name
   123         |> Sign.add_path (Binding.name_of name)
   121         |> PureThy.add_thms
   124         |> PureThy.add_thms
   122             ([((Binding.name (Rep_name ^ "_strict"), @{thm typedef_Rep_strict} OF pcpo_thms'), []),
   125           ([((Binding.suffix_name "_strict" Rep_name, @{thm typedef_Rep_strict} OF pcpo_thms'), []),
   123               ((Binding.name (Abs_name ^ "_strict"), @{thm typedef_Abs_strict} OF pcpo_thms'), []),
   126             ((Binding.suffix_name "_strict" Abs_name, @{thm typedef_Abs_strict} OF pcpo_thms'), []),
   124               ((Binding.name (Rep_name ^ "_strict_iff"), @{thm typedef_Rep_strict_iff} OF pcpo_thms'), []),
   127             ((Binding.suffix_name "_strict_iff" Rep_name, @{thm typedef_Rep_strict_iff} OF pcpo_thms'), []),
   125               ((Binding.name (Abs_name ^ "_strict_iff"), @{thm typedef_Abs_strict_iff} OF pcpo_thms'), []),
   128             ((Binding.suffix_name "_strict_iff" Abs_name, @{thm typedef_Abs_strict_iff} OF pcpo_thms'), []),
   126               ((Binding.name (Rep_name ^ "_defined"), @{thm typedef_Rep_defined} OF pcpo_thms'), []),
   129             ((Binding.suffix_name "_defined" Rep_name, @{thm typedef_Rep_defined} OF pcpo_thms'), []),
   127               ((Binding.name (Abs_name ^ "_defined"), @{thm typedef_Abs_defined} OF pcpo_thms'), [])
   130             ((Binding.suffix_name "_defined" Abs_name, @{thm typedef_Abs_defined} OF pcpo_thms'), [])])
   128               ])
       
   129         |> snd
   131         |> snd
   130         |> Sign.parent_path
   132         |> Sign.parent_path
   131       end;
   133       end;
   132 
   134 
   133     fun pcpodef_result UU_mem admissible =
   135     fun pcpodef_result UU_mem admissible =
   140   in
   142   in
   141     if pcpo
   143     if pcpo
   142     then (goal_UU_mem, goal_admissible, pcpodef_result)
   144     then (goal_UU_mem, goal_admissible, pcpodef_result)
   143     else (goal_nonempty, goal_admissible, cpodef_result)
   145     else (goal_nonempty, goal_admissible, cpodef_result)
   144   end
   146   end
   145   handle ERROR msg => err_in_cpodef msg name;
   147   handle ERROR msg =>
       
   148     cat_error msg ("The error(s) above occurred in cpodef " ^ quote (Binding.str_of name));
   146 
   149 
   147 
   150 
   148 (* proof interface *)
   151 (* proof interface *)
   149 
   152 
   150 local
   153 local
   172 
   175 
   173 local structure P = OuterParse and K = OuterKeyword in
   176 local structure P = OuterParse and K = OuterKeyword in
   174 
   177 
   175 val typedef_proof_decl =
   178 val typedef_proof_decl =
   176   Scan.optional (P.$$$ "(" |--
   179   Scan.optional (P.$$$ "(" |--
   177       ((P.$$$ "open" >> K false) -- Scan.option P.name || P.name >> (fn s => (true, SOME s)))
   180       ((P.$$$ "open" >> K false) -- Scan.option P.binding || P.binding >> (fn s => (true, SOME s)))
   178         --| P.$$$ ")") (true, NONE) --
   181         --| P.$$$ ")") (true, NONE) --
   179     (P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) --
   182     (P.type_args -- P.binding) -- P.opt_infix -- (P.$$$ "=" |-- P.term) --
   180     Scan.option (P.$$$ "morphisms" |-- P.!!! (P.name -- P.name));
   183     Scan.option (P.$$$ "morphisms" |-- P.!!! (P.binding -- P.binding));
   181 
   184 
   182 fun mk_pcpodef_proof pcpo ((((((def, opt_name), (vs, t)), mx), A), morphs)) =
   185 fun mk_pcpodef_proof pcpo ((((((def, opt_name), (vs, t)), mx), A), morphs)) =
   183   (if pcpo then pcpodef_proof_cmd else cpodef_proof_cmd)
   186   (if pcpo then pcpodef_proof_cmd else cpodef_proof_cmd)
   184     ((def, the_default (Syntax.type_name t mx) opt_name), (t, vs, mx), A, morphs);
   187     ((def, the_default (Binding.map_name (Syntax.type_name mx) t) opt_name), (t, vs, mx), A, morphs);
   185 
   188 
   186 val _ =
   189 val _ =
   187   OuterSyntax.command "pcpodef" "HOLCF type definition (requires admissibility proof)" K.thy_goal
   190   OuterSyntax.command "pcpodef" "HOLCF type definition (requires admissibility proof)" K.thy_goal
   188     (typedef_proof_decl >>
   191     (typedef_proof_decl >>
   189       (Toplevel.print oo (Toplevel.theory_to_proof o mk_pcpodef_proof true)));
   192       (Toplevel.print oo (Toplevel.theory_to_proof o mk_pcpodef_proof true)));