src/ZF/Datatype_ZF.thy
author wenzelm
Thu Mar 15 22:08:53 2012 +0100 (2012-03-15)
changeset 46950 d0181abdbdac
parent 41777 1f7cbe39d425
child 48891 c0eafbd55de3
permissions -rw-r--r--
declare command keywords via theory header, including strict checking outside Pure;
wenzelm@41777
     1
(*  Title:      ZF/Datatype_ZF.thy
krauss@26056
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
krauss@26056
     3
    Copyright   1997  University of Cambridge
krauss@26056
     4
*)
krauss@26056
     5
krauss@26056
     6
header{*Datatype and CoDatatype Definitions*}
krauss@26056
     7
krauss@26056
     8
theory Datatype_ZF
krauss@26056
     9
imports Inductive_ZF Univ QUniv
wenzelm@46950
    10
keywords "datatype" "codatatype" :: thy_decl
krauss@26056
    11
uses "Tools/datatype_package.ML"
krauss@26056
    12
begin
krauss@26056
    13
wenzelm@26480
    14
ML {*
krauss@26056
    15
(*Typechecking rules for most datatypes involving univ*)
krauss@26056
    16
structure Data_Arg =
krauss@26056
    17
  struct
wenzelm@32432
    18
  val intrs =
krauss@26056
    19
      [@{thm SigmaI}, @{thm InlI}, @{thm InrI},
wenzelm@32432
    20
       @{thm Pair_in_univ}, @{thm Inl_in_univ}, @{thm Inr_in_univ},
krauss@26056
    21
       @{thm zero_in_univ}, @{thm A_into_univ}, @{thm nat_into_univ}, @{thm UnCI}];
krauss@26056
    22
krauss@26056
    23
krauss@26056
    24
  val elims = [make_elim @{thm InlD}, make_elim @{thm InrD},   (*for mutual recursion*)
krauss@26056
    25
               @{thm SigmaE}, @{thm sumE}];                    (*allows * and + in spec*)
krauss@26056
    26
  end;
krauss@26056
    27
krauss@26056
    28
wenzelm@32432
    29
structure Data_Package =
krauss@26056
    30
  Add_datatype_def_Fun
krauss@26056
    31
   (structure Fp=Lfp and Pr=Standard_Prod and CP=Standard_CP
krauss@26056
    32
    and Su=Standard_Sum
krauss@26056
    33
    and Ind_Package = Ind_Package
krauss@26056
    34
    and Datatype_Arg = Data_Arg
krauss@26056
    35
    val coind = false);
krauss@26056
    36
krauss@26056
    37
krauss@26056
    38
(*Typechecking rules for most codatatypes involving quniv*)
krauss@26056
    39
structure CoData_Arg =
krauss@26056
    40
  struct
wenzelm@32432
    41
  val intrs =
krauss@26056
    42
      [@{thm QSigmaI}, @{thm QInlI}, @{thm QInrI},
wenzelm@32432
    43
       @{thm QPair_in_quniv}, @{thm QInl_in_quniv}, @{thm QInr_in_quniv},
krauss@26056
    44
       @{thm zero_in_quniv}, @{thm A_into_quniv}, @{thm nat_into_quniv}, @{thm UnCI}];
krauss@26056
    45
krauss@26056
    46
  val elims = [make_elim @{thm QInlD}, make_elim @{thm QInrD},   (*for mutual recursion*)
krauss@26056
    47
               @{thm QSigmaE}, @{thm qsumE}];                    (*allows * and + in spec*)
krauss@26056
    48
  end;
krauss@26056
    49
wenzelm@32432
    50
structure CoData_Package =
krauss@26056
    51
  Add_datatype_def_Fun
krauss@26056
    52
   (structure Fp=Gfp and Pr=Quine_Prod and CP=Quine_CP
krauss@26056
    53
    and Su=Quine_Sum
krauss@26056
    54
    and Ind_Package = CoInd_Package
krauss@26056
    55
    and Datatype_Arg = CoData_Arg
krauss@26056
    56
    val coind = true);
krauss@26056
    57
krauss@26056
    58
krauss@26056
    59
krauss@26056
    60
(*Simproc for freeness reasoning: compare datatype constructors for equality*)
krauss@26056
    61
structure DataFree =
krauss@26056
    62
struct
wenzelm@32740
    63
  val trace = Unsynchronized.ref false;
krauss@26056
    64
haftmann@38522
    65
  fun mk_new ([],[]) = Const(@{const_name True},FOLogic.oT)
krauss@26056
    66
    | mk_new (largs,rargs) =
wenzelm@32765
    67
        Balanced_Tree.make FOLogic.mk_conj
krauss@26056
    68
                 (map FOLogic.mk_eq (ListPair.zip (largs,rargs)));
krauss@26056
    69
krauss@26056
    70
 val datatype_ss = @{simpset};
krauss@26056
    71
krauss@26056
    72
 fun proc sg ss old =
wenzelm@32432
    73
   let val _ =
wenzelm@32432
    74
         if !trace then writeln ("data_free: OLD = " ^ Syntax.string_of_term_global sg old)
wenzelm@32432
    75
         else ()
krauss@26056
    76
       val (lhs,rhs) = FOLogic.dest_eq old
krauss@26056
    77
       val (lhead, largs) = strip_comb lhs
krauss@26056
    78
       and (rhead, rargs) = strip_comb rhs
krauss@26056
    79
       val lname = #1 (dest_Const lhead) handle TERM _ => raise Match;
krauss@26056
    80
       val rname = #1 (dest_Const rhead) handle TERM _ => raise Match;
krauss@26056
    81
       val lcon_info = the (Symtab.lookup (ConstructorsData.get sg) lname)
krauss@26056
    82
         handle Option => raise Match;
krauss@26056
    83
       val rcon_info = the (Symtab.lookup (ConstructorsData.get sg) rname)
krauss@26056
    84
         handle Option => raise Match;
wenzelm@32432
    85
       val new =
wenzelm@32432
    86
           if #big_rec_name lcon_info = #big_rec_name rcon_info
krauss@26056
    87
               andalso not (null (#free_iffs lcon_info)) then
krauss@26056
    88
               if lname = rname then mk_new (largs, rargs)
haftmann@38522
    89
               else Const(@{const_name False},FOLogic.oT)
krauss@26056
    90
           else raise Match
wenzelm@32432
    91
       val _ =
wenzelm@32432
    92
         if !trace then writeln ("NEW = " ^ Syntax.string_of_term_global sg new)
wenzelm@32432
    93
         else ();
krauss@26056
    94
       val goal = Logic.mk_equals (old, new)
krauss@26056
    95
       val thm = Goal.prove (Simplifier.the_context ss) [] [] goal
wenzelm@35409
    96
         (fn _ => rtac @{thm iff_reflection} 1 THEN
krauss@26056
    97
           simp_tac (Simplifier.inherit_context ss datatype_ss addsimps #free_iffs lcon_info) 1)
krauss@26056
    98
         handle ERROR msg =>
wenzelm@26939
    99
         (warning (msg ^ "\ndata_free simproc:\nfailed to prove " ^ Syntax.string_of_term_global sg goal);
krauss@26056
   100
          raise Match)
krauss@26056
   101
   in SOME thm end
krauss@26056
   102
   handle Match => NONE;
krauss@26056
   103
krauss@26056
   104
wenzelm@38715
   105
 val conv = Simplifier.simproc_global @{theory} "data_free" ["(x::i) = y"] proc;
krauss@26056
   106
krauss@26056
   107
end;
krauss@26056
   108
krauss@26056
   109
krauss@26056
   110
Addsimprocs [DataFree.conv];
krauss@26056
   111
*}
krauss@26056
   112
krauss@26056
   113
end