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