quotient_info.ML
author Christian Urban <urbanc@in.tum.de>
Fri, 06 Nov 2009 19:26:08 +0100
changeset 297 28b264299590
parent 268 4d58c02289ca
child 306 e7279efbe3dd
permissions -rw-r--r--
updated to new Isabelle version and added a new example file
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
264
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
signature QUOTIENT_INFO =
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
sig
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
  type maps_info = {mapfun: string, relfun: string}
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
  val maps_lookup: theory -> string -> maps_info option
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
  val maps_update_thy: string -> maps_info -> theory -> theory    
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
  val maps_update: string -> maps_info -> Proof.context -> Proof.context     
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
  type quotient_info = {qtyp: typ, rtyp: typ, rel: term, equiv_thm: thm}
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  val print_quotdata: Proof.context -> unit
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  val quotdata_lookup_thy: theory -> quotient_info list
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  val quotdata_lookup: Proof.context -> quotient_info list
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  val quotdata_update_thy: (typ * typ * term * thm) -> theory -> theory
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
  val quotdata_update: (typ * typ * term * thm) -> Proof.context -> Proof.context
268
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
    14
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
    15
  type qenv = (typ * typ) list
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
    16
  val mk_qenv: Proof.context -> qenv
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
    17
  val lookup_qenv: ((typ * typ) -> bool) -> qenv -> typ -> (typ * typ) option
264
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
end;
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
structure Quotient_Info: QUOTIENT_INFO =
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
struct
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
268
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
    23
264
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
(* data containers *)
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
(*******************)
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
(* info about map- and rel-functions *)
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
type maps_info = {mapfun: string, relfun: string}
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
structure MapsData = TheoryDataFun
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  (type T = maps_info Symtab.table
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
   val empty = Symtab.empty
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
   val copy = I
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
   val extend = I
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
   fun merge _ = Symtab.merge (K true))
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
val maps_lookup = Symtab.lookup o MapsData.get
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
fun maps_update_thy k minfo = MapsData.map (Symtab.update (k, minfo))
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
fun maps_update k minfo = ProofContext.theory (maps_update_thy k minfo)
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
fun maps_attribute_aux s minfo = Thm.declaration_attribute 
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
  (fn thm => Context.mapping (maps_update_thy s minfo) (maps_update s minfo))
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
(* attribute to be used in declare statements *)
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
fun maps_attribute (ctxt, (tystr, (mapstr, relstr))) = 
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
let  
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  val thy = ProofContext.theory_of ctxt
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
  val tyname = Sign.intern_type thy tystr
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
  val mapname = Sign.intern_const thy mapstr
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  val relname = Sign.intern_const thy relstr
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
in
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  maps_attribute_aux tyname {mapfun = mapname, relfun = relname}
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
end
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
val maps_attr_parser = 
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
      Args.context -- Scan.lift
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
       ((Args.name --| OuterParse.$$$ "=") -- 
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
         (OuterParse.$$$ "(" |-- Args.name --| OuterParse.$$$ "," -- 
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
           Args.name --| OuterParse.$$$ ")"))
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
val _ = Context.>> (Context.map_theory
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
         (Attrib.setup @{binding "map"} (maps_attr_parser >> maps_attribute) 
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
           "declaration of map information"))
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
(* info about the quotient types *)
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
type quotient_info = {qtyp: typ, rtyp: typ, rel: term, equiv_thm: thm}
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
structure QuotData = TheoryDataFun
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
  (type T = quotient_info list
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
   val empty = []
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
   val copy = I
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
   val extend = I
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
   fun merge _ = (op @)) (* FIXME: is this the correct merging function for the list? *)
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
val quotdata_lookup_thy = QuotData.get
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
val quotdata_lookup = QuotData.get o ProofContext.theory_of
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
fun quotdata_update_thy (qty, rty, rel, equiv_thm) thy = 
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
      QuotData.map (fn ls => {qtyp = qty, rtyp = rty, rel = rel, equiv_thm = equiv_thm}::ls) thy
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
fun quotdata_update (qty, rty, rel, equiv_thm) ctxt = 
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
      ProofContext.theory (quotdata_update_thy (qty, rty, rel, equiv_thm)) ctxt
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
fun print_quotdata ctxt =
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
let
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  fun prt_quot {qtyp, rtyp, rel, equiv_thm} = 
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
      Pretty.block (Library.separate (Pretty.brk 2)
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
          [Pretty.str ("quotient type:"), 
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
           Syntax.pretty_typ ctxt qtyp,
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
           Pretty.str ("raw type:"), 
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
           Syntax.pretty_typ ctxt rtyp,
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
           Pretty.str ("relation:"), 
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
           Syntax.pretty_term ctxt rel,
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
           Pretty.str ("equiv. thm:"), 
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
           Syntax.pretty_term ctxt (prop_of equiv_thm)])
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
in
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
  QuotData.get (ProofContext.theory_of ctxt)
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
  |> map prt_quot
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
  |> Pretty.big_list "quotients:" 
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
  |> Pretty.writeln
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
end
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
val _ = 
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
  OuterSyntax.improper_command "print_quotients" "print out all quotients" 
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
    OuterKeyword.diag (Scan.succeed (Toplevel.keep (print_quotdata o Toplevel.context_of)))
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
268
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   110
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   111
(* environments of quotient and raw types *)
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   112
type qenv = (typ * typ) list
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   113
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   114
fun mk_qenv ctxt =
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   115
let
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   116
  val qinfo = quotdata_lookup ctxt
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   117
in
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   118
  map (fn {qtyp, rtyp, ...} => (qtyp, rtyp)) qinfo
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   119
end
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   120
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   121
fun lookup_qenv _ [] _ = NONE
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   122
  | lookup_qenv eq ((qty, rty)::xs) qty' =
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   123
      if eq (qty', qty) then SOME (qty, rty)
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   124
      else lookup_qenv eq xs qty'
4d58c02289ca simplified the quotient_def code; type of the defined constant must now be given; for-part eliminated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   125
264
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
end; (* structure *)
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
d0581fbc096c split quotient.ML into two files
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
open Quotient_Info