src/HOL/Tools/Quotient/quotient_info.ML
author kuncar
Fri, 23 Mar 2012 14:20:09 +0100
changeset 47964 1a7ad2601cb5
parent 47963 0516a6c1ea59
child 47982 f067afe98049
permissions -rw-r--r--
store the relational theorem for every relator
     1 (*  Title:      HOL/Tools/Quotient/quotient_info.ML
     2     Author:     Cezary Kaliszyk and Christian Urban
     3 
     4 Context data for the quotient package.
     5 *)
     6 
     7 signature QUOTIENT_INFO =
     8 sig
     9   type quotmaps = {relmap: string, quot_thm: thm}
    10   val lookup_quotmaps: Proof.context -> string -> quotmaps option
    11   val lookup_quotmaps_global: theory -> string -> quotmaps option
    12   val print_quotmaps: Proof.context -> unit
    13 
    14   type abs_rep = {abs : term, rep : term}
    15   val transform_abs_rep: morphism -> abs_rep -> abs_rep
    16   val lookup_abs_rep: Proof.context -> string -> abs_rep option
    17   val lookup_abs_rep_global: theory -> string -> abs_rep option
    18   val update_abs_rep: string -> abs_rep -> Context.generic -> Context.generic
    19   val print_abs_rep: Proof.context -> unit
    20   
    21   type quotients = {qtyp: typ, rtyp: typ, equiv_rel: term, equiv_thm: thm, quot_thm: thm}
    22   val transform_quotients: morphism -> quotients -> quotients
    23   val lookup_quotients: Proof.context -> string -> quotients option
    24   val lookup_quotients_global: theory -> string -> quotients option
    25   val update_quotients: string -> quotients -> Context.generic -> Context.generic
    26   val dest_quotients: Proof.context -> quotients list
    27   val print_quotients: Proof.context -> unit
    28 
    29   type quotconsts = {qconst: term, rconst: term, def: thm}
    30   val transform_quotconsts: morphism -> quotconsts -> quotconsts
    31   val lookup_quotconsts_global: theory -> term -> quotconsts option
    32   val update_quotconsts: string -> quotconsts -> Context.generic -> Context.generic
    33   val dest_quotconsts_global: theory -> quotconsts list
    34   val dest_quotconsts: Proof.context -> quotconsts list
    35   val print_quotconsts: Proof.context -> unit
    36 
    37   val equiv_rules: Proof.context -> thm list
    38   val equiv_rules_add: attribute
    39   val rsp_rules: Proof.context -> thm list
    40   val rsp_rules_add: attribute
    41   val prs_rules: Proof.context -> thm list
    42   val prs_rules_add: attribute
    43   val id_simps: Proof.context -> thm list
    44   val quotient_rules: Proof.context -> thm list
    45   val quotient_rules_add: attribute
    46   val setup: theory -> theory
    47 end;
    48 
    49 structure Quotient_Info: QUOTIENT_INFO =
    50 struct
    51 
    52 (** data containers **)
    53 
    54 (* FIXME just one data slot (record) per program unit *)
    55 
    56 (* info about map- and rel-functions for a type *)
    57 type quotmaps = {relmap: string, quot_thm: thm}
    58 
    59 structure Quotmaps = Generic_Data
    60 (
    61   type T = quotmaps Symtab.table
    62   val empty = Symtab.empty
    63   val extend = I
    64   fun merge data = Symtab.merge (K true) data
    65 )
    66 
    67 val lookup_quotmaps = Symtab.lookup o Quotmaps.get o Context.Proof
    68 val lookup_quotmaps_global = Symtab.lookup o Quotmaps.get o Context.Theory
    69 
    70 (* FIXME export proper internal update operation!? *)
    71 
    72 val quotmaps_attribute_setup =
    73   Attrib.setup @{binding map}
    74     ((Args.type_name true --| Scan.lift (@{keyword "="})) --
    75       (Scan.lift (@{keyword "("}) |-- Args.const_proper true --| Scan.lift (@{keyword ","}) --
    76         Attrib.thm --| Scan.lift (@{keyword ")"})) >>
    77       (fn (tyname, (relname, qthm)) =>
    78         let val minfo = {relmap = relname, quot_thm = qthm}
    79         in Thm.declaration_attribute (fn _ => Quotmaps.map (Symtab.update (tyname, minfo))) end))
    80     "declaration of map information"
    81 
    82 fun print_quotmaps ctxt =
    83   let
    84     fun prt_map (ty_name, {relmap, quot_thm}) =
    85       Pretty.block (separate (Pretty.brk 2)
    86          [Pretty.str "type:", 
    87           Pretty.str ty_name,
    88           Pretty.str "relation map:", 
    89           Pretty.str relmap,
    90           Pretty.str "quot. theorem:", 
    91           Syntax.pretty_term ctxt (prop_of quot_thm)])
    92   in
    93     map prt_map (Symtab.dest (Quotmaps.get (Context.Proof ctxt)))
    94     |> Pretty.big_list "maps for type constructors:"
    95     |> Pretty.writeln
    96   end
    97 
    98 (* info about abs/rep terms *)
    99 type abs_rep = {abs : term, rep : term}
   100 
   101 structure Abs_Rep = Generic_Data
   102 (
   103   type T = abs_rep Symtab.table
   104   val empty = Symtab.empty
   105   val extend = I
   106   fun merge data = Symtab.merge (K true) data
   107 )
   108 
   109 fun transform_abs_rep phi {abs, rep} = {abs = Morphism.term phi abs, rep = Morphism.term phi rep}
   110 
   111 val lookup_abs_rep = Symtab.lookup o Abs_Rep.get o Context.Proof
   112 val lookup_abs_rep_global = Symtab.lookup o Abs_Rep.get o Context.Theory
   113 
   114 fun update_abs_rep str data = Abs_Rep.map (Symtab.update (str, data))
   115 
   116 fun print_abs_rep ctxt =
   117   let
   118     fun prt_abs_rep (s, {abs, rep}) =
   119       Pretty.block (separate (Pretty.brk 2)
   120        [Pretty.str "type constructor:",
   121         Pretty.str s,
   122         Pretty.str "abs term:",
   123         Syntax.pretty_term ctxt abs,
   124         Pretty.str "rep term:",
   125         Syntax.pretty_term ctxt rep])
   126   in
   127     map prt_abs_rep (Symtab.dest (Abs_Rep.get (Context.Proof ctxt)))
   128     |> Pretty.big_list "abs/rep terms:"
   129     |> Pretty.writeln
   130   end
   131 
   132 (* info about quotient types *)
   133 type quotients = {qtyp: typ, rtyp: typ, equiv_rel: term, equiv_thm: thm, quot_thm: thm}
   134 
   135 structure Quotients = Generic_Data
   136 (
   137   type T = quotients Symtab.table
   138   val empty = Symtab.empty
   139   val extend = I
   140   fun merge data = Symtab.merge (K true) data
   141 )
   142 
   143 fun transform_quotients phi {qtyp, rtyp, equiv_rel, equiv_thm, quot_thm} =
   144   {qtyp = Morphism.typ phi qtyp,
   145    rtyp = Morphism.typ phi rtyp,
   146    equiv_rel = Morphism.term phi equiv_rel,
   147    equiv_thm = Morphism.thm phi equiv_thm,
   148    quot_thm = Morphism.thm phi quot_thm}
   149 
   150 val lookup_quotients = Symtab.lookup o Quotients.get o Context.Proof
   151 val lookup_quotients_global = Symtab.lookup o Quotients.get o Context.Theory
   152 
   153 fun update_quotients str qinfo = Quotients.map (Symtab.update (str, qinfo))
   154 
   155 fun dest_quotients ctxt =  (* FIXME slightly expensive way to retrieve data *)
   156   map snd (Symtab.dest (Quotients.get (Context.Proof ctxt)))
   157 
   158 fun print_quotients ctxt =
   159   let
   160     fun prt_quot {qtyp, rtyp, equiv_rel, equiv_thm, quot_thm} =
   161       Pretty.block (separate (Pretty.brk 2)
   162        [Pretty.str "quotient type:",
   163         Syntax.pretty_typ ctxt qtyp,
   164         Pretty.str "raw type:",
   165         Syntax.pretty_typ ctxt rtyp,
   166         Pretty.str "relation:",
   167         Syntax.pretty_term ctxt equiv_rel,
   168         Pretty.str "equiv. thm:",
   169         Syntax.pretty_term ctxt (prop_of equiv_thm),
   170         Pretty.str "quot. thm:",
   171         Syntax.pretty_term ctxt (prop_of quot_thm)])
   172   in
   173     map (prt_quot o snd) (Symtab.dest (Quotients.get (Context.Proof ctxt)))
   174     |> Pretty.big_list "quotients:"
   175     |> Pretty.writeln
   176   end
   177 
   178 
   179 (* info about quotient constants *)
   180 type quotconsts = {qconst: term, rconst: term, def: thm}
   181 
   182 fun eq_quotconsts (x : quotconsts, y : quotconsts) = #qconst x = #qconst y
   183 
   184 (* We need to be able to lookup instances of lifted constants,
   185    for example given "nat fset" we need to find "'a fset";
   186    but overloaded constants share the same name *)
   187 structure Quotconsts = Generic_Data
   188 (
   189   type T = quotconsts list Symtab.table
   190   val empty = Symtab.empty
   191   val extend = I
   192   val merge = Symtab.merge_list eq_quotconsts
   193 )
   194 
   195 fun transform_quotconsts phi {qconst, rconst, def} =
   196   {qconst = Morphism.term phi qconst,
   197    rconst = Morphism.term phi rconst,
   198    def = Morphism.thm phi def}
   199 
   200 fun update_quotconsts name qcinfo = Quotconsts.map (Symtab.cons_list (name, qcinfo))
   201 
   202 fun dest_quotconsts ctxt =
   203   flat (map snd (Symtab.dest (Quotconsts.get (Context.Proof ctxt))))
   204 
   205 fun dest_quotconsts_global thy =
   206   flat (map snd (Symtab.dest (Quotconsts.get (Context.Theory thy))))
   207 
   208 
   209 
   210 fun lookup_quotconsts_global thy t =
   211   let
   212     val (name, qty) = dest_Const t
   213     fun matches (x: quotconsts) =
   214       let val (name', qty') = dest_Const (#qconst x);
   215       in name = name' andalso Sign.typ_instance thy (qty, qty') end
   216   in
   217     (case Symtab.lookup (Quotconsts.get (Context.Theory thy)) name of
   218       NONE => NONE
   219     | SOME l => find_first matches l)
   220   end
   221 
   222 fun print_quotconsts ctxt =
   223   let
   224     fun prt_qconst {qconst, rconst, def} =
   225       Pretty.block (separate (Pretty.brk 1)
   226        [Syntax.pretty_term ctxt qconst,
   227         Pretty.str ":=",
   228         Syntax.pretty_term ctxt rconst,
   229         Pretty.str "as",
   230         Syntax.pretty_term ctxt (prop_of def)])
   231   in
   232     map prt_qconst (maps snd (Symtab.dest (Quotconsts.get (Context.Proof ctxt))))
   233     |> Pretty.big_list "quotient constants:"
   234     |> Pretty.writeln
   235   end
   236 
   237 (* equivalence relation theorems *)
   238 structure Equiv_Rules = Named_Thms
   239 (
   240   val name = @{binding quot_equiv}
   241   val description = "equivalence relation theorems"
   242 )
   243 
   244 val equiv_rules = Equiv_Rules.get
   245 val equiv_rules_add = Equiv_Rules.add
   246 
   247 (* respectfulness theorems *)
   248 structure Rsp_Rules = Named_Thms
   249 (
   250   val name = @{binding quot_respect}
   251   val description = "respectfulness theorems"
   252 )
   253 
   254 val rsp_rules = Rsp_Rules.get
   255 val rsp_rules_add = Rsp_Rules.add
   256 
   257 (* preservation theorems *)
   258 structure Prs_Rules = Named_Thms
   259 (
   260   val name = @{binding quot_preserve}
   261   val description = "preservation theorems"
   262 )
   263 
   264 val prs_rules = Prs_Rules.get
   265 val prs_rules_add = Prs_Rules.add
   266 
   267 (* id simplification theorems *)
   268 structure Id_Simps = Named_Thms
   269 (
   270   val name = @{binding id_simps}
   271   val description = "identity simp rules for maps"
   272 )
   273 
   274 val id_simps = Id_Simps.get
   275 
   276 (* quotient theorems *)
   277 structure Quotient_Rules = Named_Thms
   278 (
   279   val name = @{binding quot_thm}
   280   val description = "quotient theorems"
   281 )
   282 
   283 val quotient_rules = Quotient_Rules.get
   284 val quotient_rules_add = Quotient_Rules.add
   285 
   286 
   287 (* theory setup *)
   288 
   289 val setup =
   290   quotmaps_attribute_setup #>
   291   Equiv_Rules.setup #>
   292   Rsp_Rules.setup #>
   293   Prs_Rules.setup #>
   294   Id_Simps.setup #>
   295   Quotient_Rules.setup
   296 
   297 
   298 (* outer syntax commands *)
   299 
   300 val _ =
   301   Outer_Syntax.improper_command @{command_spec "print_quotmaps"} "print quotient map functions"
   302     (Scan.succeed (Toplevel.keep (print_quotmaps o Toplevel.context_of)))
   303 
   304 val _ =
   305   Outer_Syntax.improper_command @{command_spec "print_quotients"} "print quotients"
   306     (Scan.succeed (Toplevel.keep (print_quotients o Toplevel.context_of)))
   307 
   308 val _ =
   309   Outer_Syntax.improper_command @{command_spec "print_quotconsts"} "print quotient constants"
   310     (Scan.succeed (Toplevel.keep (print_quotconsts o Toplevel.context_of)))
   311 
   312 end;