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