Quot/quotient_info.ML
changeset 873 f14e41e9b08f
parent 871 4163fe3dbf8c
child 875 cc951743c5e2
--- a/Quot/quotient_info.ML	Thu Jan 14 12:14:35 2010 +0100
+++ b/Quot/quotient_info.ML	Thu Jan 14 12:17:39 2010 +0100
@@ -19,9 +19,9 @@
   type qconsts_info = {qconst: term, rconst: term, def: thm}
   val transform_qconsts: morphism -> qconsts_info -> qconsts_info
   val qconsts_lookup: theory -> term -> qconsts_info     (* raises NotFound *)
-  val qconsts_update_thy: term -> qconsts_info -> theory -> theory
-  val qconsts_update_gen: term -> qconsts_info -> Context.generic -> Context.generic
-  val qconsts_dest: theory -> qconsts_info list
+  val qconsts_update_thy: string -> qconsts_info -> theory -> theory
+  val qconsts_update_gen: string -> qconsts_info -> Context.generic -> Context.generic
+  val qconsts_dest: Proof.context -> qconsts_info list
   val print_qconstinfo: Proof.context -> unit
 
   val equiv_rules_get: Proof.context -> thm list
@@ -179,39 +179,45 @@
 (* info about quotient constants *)
 type qconsts_info = {qconst: term, rconst: term, def: thm}
 
+fun qconsts_info_eq (x : qconsts_info, y : qconsts_info) = #qconst x = #qconst y
+
+(* We need to be able to lookup instances of lifted constants,
+   for example given "nat fset" we need to find "'a fset";
+   but overloaded constants share the same name *)
 structure QConstsData = Theory_Data
-  (type T = qconsts_info Termtab.table
-   val empty = Termtab.empty
+  (type T = (qconsts_info list) Symtab.table
+   val empty = Symtab.empty
    val extend = I
-   val merge = Termtab.merge (K true))
+   val merge = Symtab.merge_list qconsts_info_eq)
 
 fun transform_qconsts phi {qconst, rconst, def} =
     {qconst = Morphism.term phi qconst,
      rconst = Morphism.term phi rconst,
      def = Morphism.thm phi def}
 
-fun qconsts_update_thy const qcinfo = QConstsData.map (Termtab.update (const, qcinfo))
-fun qconsts_update_gen const qcinfo = Context.mapping (qconsts_update_thy const qcinfo) I
+fun qconsts_update_thy name qcinfo = QConstsData.map (Symtab.cons_list (name, qcinfo))
+fun qconsts_update_gen name qcinfo = Context.mapping (qconsts_update_thy name qcinfo) I
 
-fun qconsts_dest thy =
-  map snd (Termtab.dest (QConstsData.get thy))
+fun qconsts_dest lthy =
+  flat (map snd (Symtab.dest (QConstsData.get (ProofContext.theory_of lthy))))
 
-(* FIXME / TODO : better implementation of the lookup datastructure *)
-(* for example symtabs to alist; or tables with string type key     *) 
 fun qconsts_lookup thy t =
   let
-    val smt = Termtab.dest (QConstsData.get thy);
     val (name, qty) = dest_Const t
-    fun matches (_, x) =
+    fun matches x =
       let
         val (name', qty') = dest_Const (#qconst x);
       in
         name = name' andalso Sign.typ_instance thy (qty, qty')
       end
   in
-    case (find_first matches smt) of
-      SOME (_, x) => x
-    | _ => raise NotFound
+    case Symtab.lookup (QConstsData.get thy) name of
+      NONE => raise NotFound
+    | SOME l =>
+      (case (find_first matches l) of
+        SOME x => x
+      | NONE => raise NotFound
+      )
   end
 
 fun print_qconstinfo ctxt =
@@ -225,8 +231,10 @@
            Syntax.pretty_term ctxt (prop_of def)])
 in
   QConstsData.get (ProofContext.theory_of ctxt)
-  |> Termtab.dest
-  |> map (prt_qconst o snd)
+  |> Symtab.dest
+  |> map snd
+  |> flat
+  |> map prt_qconst
   |> Pretty.big_list "quotient constants:" 
   |> Pretty.writeln
 end