src/HOLCF/pcpodef_package.ML
author wenzelm
Sat Jan 21 23:02:14 2006 +0100 (2006-01-21)
changeset 18728 6790126ab5f6
parent 18678 dd0c569fa43d
child 18799 f137c5e971f5
permissions -rw-r--r--
simplified type attribute;
     1 (*  Title:      HOLCF/pcpodef_package.ML
     2     ID:         $Id$
     3     Author:     Brian Huffman
     4 
     5 Gordon/HOL-style type definitions for HOLCF.
     6 *)
     7 
     8 signature PCPODEF_PACKAGE =
     9 sig
    10   val quiet_mode: bool ref
    11   val pcpodef_proof: (bool * string) * (bstring * string list * mixfix) * string
    12     * (string * string) option -> theory -> Proof.state
    13   val pcpodef_proof_i: (bool * string) * (bstring * string list * mixfix) * term
    14     * (string * string) option -> theory -> Proof.state
    15   val cpodef_proof: (bool * string) * (bstring * string list * mixfix) * string
    16     * (string * string) option -> theory -> Proof.state
    17   val cpodef_proof_i: (bool * string) * (bstring * string list * mixfix) * term
    18     * (string * string) option -> theory -> Proof.state
    19 end;
    20 
    21 structure PcpodefPackage: PCPODEF_PACKAGE =
    22 struct
    23 
    24 
    25 (** theory context references **)
    26 
    27 val typedef_po = thm "typedef_po";
    28 val typedef_cpo = thm "typedef_cpo";
    29 val typedef_pcpo = thm "typedef_pcpo";
    30 val typedef_lub = thm "typedef_lub";
    31 val typedef_thelub = thm "typedef_thelub";
    32 val typedef_compact = thm "typedef_compact";
    33 val cont_Rep = thm "typedef_cont_Rep";
    34 val cont_Abs = thm "typedef_cont_Abs";
    35 val Rep_strict = thm "typedef_Rep_strict";
    36 val Abs_strict = thm "typedef_Abs_strict";
    37 val Rep_defined = thm "typedef_Rep_defined";
    38 val Abs_defined = thm "typedef_Abs_defined";
    39 
    40 
    41 (** type definitions **)
    42 
    43 (* messages *)
    44 
    45 val quiet_mode = ref false;
    46 fun message s = if ! quiet_mode then () else writeln s;
    47 
    48 
    49 (* prepare_cpodef *)
    50 
    51 fun read_term thy used s =
    52   #1 (Thm.read_def_cterm (thy, K NONE, K NONE) used true (s, HOLogic.typeT));
    53 
    54 fun cert_term thy _ t = Thm.cterm_of thy t handle TERM (msg, _) => error msg;
    55 
    56 fun err_in_cpodef msg name =
    57   cat_error msg ("The error(s) above occurred in cpodef " ^ quote name);
    58 
    59 fun adm_const T = Const ("Adm.adm", (T --> HOLogic.boolT) --> HOLogic.boolT);
    60 fun mk_adm (x, T, P) = adm_const T $ absfree (x, T, P);
    61 
    62 fun gen_prepare_pcpodef prep_term pcpo def name (t, vs, mx) raw_set opt_morphs thy =
    63   let
    64     val full = Sign.full_name thy;
    65 
    66     (*rhs*)
    67     val full_name = full name;
    68     val cset = prep_term thy vs raw_set;
    69     val {T = setT, t = set, ...} = Thm.rep_cterm cset;
    70     val rhs_tfrees = term_tfrees set;
    71     val oldT = HOLogic.dest_setT setT handle TYPE _ =>
    72       error ("Not a set type: " ^ quote (Sign.string_of_typ thy setT));
    73     fun mk_nonempty A =
    74       HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), A));
    75     fun mk_admissible A =
    76       mk_adm ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), A));
    77     fun mk_UU_mem A = HOLogic.mk_mem (Const ("Pcpo.UU", oldT), A);
    78     val goal = if pcpo
    79       then HOLogic.mk_Trueprop (HOLogic.mk_conj (mk_UU_mem set, mk_admissible set))
    80       else HOLogic.mk_Trueprop (HOLogic.mk_conj (mk_nonempty set, mk_admissible set));
    81   
    82     (*lhs*)
    83     val lhs_tfrees = map (fn v => (v, AList.lookup (op =) rhs_tfrees v |> the_default HOLogic.typeS)) vs;
    84     val lhs_sorts = map snd lhs_tfrees;
    85     val tname = Syntax.type_name t mx;
    86     val full_tname = full tname;
    87     val newT = Type (full_tname, map TFree lhs_tfrees);
    88 
    89     val (Rep_name, Abs_name) = getOpt (opt_morphs, ("Rep_" ^ name, "Abs_" ^ name));
    90     val RepC = Const (full Rep_name, newT --> oldT);
    91     fun lessC T = Const ("Porder.<<", T --> T --> HOLogic.boolT);
    92     val less_def = ("less_" ^ name ^ "_def", Logic.mk_equals (lessC newT,
    93       Abs ("x", newT, Abs ("y", newT, lessC oldT $ (RepC $ Bound 1) $ (RepC $ Bound 0)))));
    94 
    95     fun option_fold_rule NONE = I
    96       | option_fold_rule (SOME def) = fold_rule [def];
    97     
    98     fun make_po tac thy =
    99       thy
   100       |> TypedefPackage.add_typedef_i def (SOME name) (t, vs, mx) set opt_morphs tac
   101       |>> AxClass.add_inst_arity_i (full_tname, lhs_sorts, ["Porder.sq_ord"])
   102            (AxClass.intro_classes_tac [])
   103       |>>> (PureThy.add_defs_i true [Thm.no_attributes less_def] #> Library.swap)
   104       |> (fn (thy', ({type_definition, set_def, ...}, [less_definition])) =>
   105            thy'
   106            |> AxClass.add_inst_arity_i (full_tname, lhs_sorts, ["Porder.po"])
   107              (Tactic.rtac (typedef_po OF [type_definition, less_definition]) 1)
   108            |> rpair (type_definition, less_definition, set_def));
   109     
   110     fun make_cpo admissible (theory, defs as (type_def, less_def, set_def)) =
   111       let
   112         val admissible' = option_fold_rule set_def admissible;
   113         val cpo_thms = [type_def, less_def, admissible'];
   114         val (_, theory') =
   115           theory
   116           |> AxClass.add_inst_arity_i (full_tname, lhs_sorts, ["Pcpo.cpo"])
   117             (Tactic.rtac (typedef_cpo OF cpo_thms) 1)
   118           |> Theory.add_path name
   119           |> PureThy.add_thms
   120             ([(("adm_" ^ name, admissible'), []),
   121               (("cont_" ^ Rep_name, cont_Rep OF cpo_thms), []),
   122               (("cont_" ^ Abs_name, cont_Abs OF cpo_thms), []),
   123               (("lub_"     ^ name, typedef_lub     OF cpo_thms), []),
   124               (("thelub_"  ^ name, typedef_thelub  OF cpo_thms), []),
   125               (("compact_" ^ name, typedef_compact OF cpo_thms), [])])
   126           ||> Theory.parent_path;
   127       in (theory', defs) end;
   128 
   129     fun make_pcpo UUmem (theory, defs as (type_def, less_def, set_def)) =
   130       let
   131         val UUmem' = option_fold_rule set_def UUmem;
   132         val pcpo_thms = [type_def, less_def, UUmem'];
   133         val (_, theory') =
   134           theory
   135           |> AxClass.add_inst_arity_i (full_tname, lhs_sorts, ["Pcpo.pcpo"])
   136             (Tactic.rtac (typedef_pcpo OF pcpo_thms) 1)
   137           |> Theory.add_path name
   138           |> PureThy.add_thms
   139             ([((Rep_name ^ "_strict", Rep_strict OF pcpo_thms), []),
   140               ((Abs_name ^ "_strict", Abs_strict OF pcpo_thms), []),
   141               ((Rep_name ^ "_defined", Rep_defined OF pcpo_thms), []),
   142               ((Abs_name ^ "_defined", Abs_defined OF pcpo_thms), [])
   143               ])
   144           ||> Theory.parent_path;
   145       in (theory', defs) end;
   146     
   147     fun pcpodef_result (context, UUmem_admissible) =
   148       let
   149         val theory = Context.the_theory context;
   150         val UUmem = UUmem_admissible RS conjunct1;
   151         val admissible = UUmem_admissible RS conjunct2;
   152       in
   153         theory
   154         |> make_po (Tactic.rtac exI 1 THEN Tactic.rtac UUmem 1)
   155         |> make_cpo admissible
   156         |> make_pcpo UUmem
   157         |> (fn (theory', (type_def, _, _)) => (Context.Theory theory', type_def))
   158       end;
   159   
   160     fun cpodef_result (context, nonempty_admissible) =
   161       let
   162         val theory = Context.the_theory context;
   163         val nonempty = nonempty_admissible RS conjunct1;
   164         val admissible = nonempty_admissible RS conjunct2;
   165       in
   166         theory
   167         |> make_po (Tactic.rtac nonempty 1)
   168         |> make_cpo admissible
   169         |> (fn (theory', (type_def, _, _)) => (Context.Theory theory', type_def))
   170       end;
   171   
   172   in (goal, if pcpo then pcpodef_result else cpodef_result) end
   173   handle ERROR msg => err_in_cpodef msg name;
   174 
   175 (* cpodef_proof interface *)
   176 
   177 fun gen_pcpodef_proof prep_term pcpo ((def, name), typ, set, opt_morphs) thy =
   178   let
   179     val (goal, att) =
   180       gen_prepare_pcpodef prep_term pcpo def name typ set opt_morphs thy;
   181   in IsarThy.theorem_i Drule.internalK ("", [att]) (goal, ([], [])) thy end;
   182 
   183 fun pcpodef_proof x = gen_pcpodef_proof read_term true x;
   184 fun pcpodef_proof_i x = gen_pcpodef_proof cert_term true x;
   185 
   186 fun cpodef_proof x = gen_pcpodef_proof read_term false x;
   187 fun cpodef_proof_i x = gen_pcpodef_proof cert_term false x;
   188 
   189 
   190 (** outer syntax **)
   191 
   192 local structure P = OuterParse and K = OuterKeyword in
   193 
   194 (* copied from HOL/Tools/typedef_package.ML *)
   195 val typedef_proof_decl =
   196   Scan.optional (P.$$$ "(" |--
   197       ((P.$$$ "open" >> K false) -- Scan.option P.name || P.name >> (fn s => (true, SOME s)))
   198         --| P.$$$ ")") (true, NONE) --
   199     (P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) --
   200     Scan.option (P.$$$ "morphisms" |-- P.!!! (P.name -- P.name));
   201 
   202 fun mk_pcpodef_proof pcpo ((((((def, opt_name), (vs, t)), mx), A), morphs)) =
   203   (if pcpo then pcpodef_proof else cpodef_proof)
   204     ((def, getOpt (opt_name, Syntax.type_name t mx)), (t, vs, mx), A, morphs);
   205 
   206 val pcpodefP =
   207   OuterSyntax.command "pcpodef" "HOLCF type definition (requires admissibility proof)" K.thy_goal
   208     (typedef_proof_decl >>
   209       (Toplevel.print oo (Toplevel.theory_to_proof o mk_pcpodef_proof true)));
   210 
   211 val cpodefP =
   212   OuterSyntax.command "cpodef" "HOLCF type definition (requires admissibility proof)" K.thy_goal
   213     (typedef_proof_decl >>
   214       (Toplevel.print oo (Toplevel.theory_to_proof o mk_pcpodef_proof false)));
   215 
   216 val _ = OuterSyntax.add_parsers [pcpodefP, cpodefP];
   217 
   218 end;
   219 
   220 end;