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