src/Pure/Tools/class_package.ML
changeset 17157 c5fb1fb537c0
parent 17156 e50f95ccde99
child 17158 d68bf267cbba
     1.1 --- a/src/Pure/Tools/class_package.ML	Sun Aug 28 10:05:03 2005 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,569 +0,0 @@
     1.4 -(*  Title:      Pure/hugsclass.ML
     1.5 -    ID:         $Id$
     1.6 -    Author:     Florian Haftmann, TU Muenchen
     1.7 -
     1.8 -Haskell98-like type classes, logically simulated by locales ("hugsclass")
     1.9 -*)
    1.10 -
    1.11 -(*!!! for now, only experimental scratch code !!!*)
    1.12 -
    1.13 -signature CLASS_PACKAGE =
    1.14 -sig
    1.15 -(*   val add_class: (bstring * xstring list * Locale.element list) -> theory -> theory  *)
    1.16 -(*   val add_class_i: bstring -> string list -> Locale.element list -> theory -> theory  *)
    1.17 -
    1.18 -  val get_locale_for_class: theory -> string -> string
    1.19 -  val get_axclass_for_class: theory -> string -> class
    1.20 -  val add_members_x: xstring * xstring list -> theory -> theory
    1.21 -  val add_members: string * string list -> theory -> theory
    1.22 -  val add_tycons_x: xstring * xstring list -> theory -> theory
    1.23 -  val add_tycons: string * string list -> theory -> theory
    1.24 -  val the_members: theory -> class -> string list
    1.25 -  val the_tycons: theory -> class -> string list
    1.26 -
    1.27 -  val is_class: theory -> class -> bool
    1.28 -  val get_arities: theory -> sort -> string -> sort list
    1.29 -  val get_superclasses: theory -> class -> class list
    1.30 -  type sortcontext = (string * (string * sort)) list
    1.31 -                     (* [(identifier, (varname, sort))] *)
    1.32 -  datatype lookup = Instance of class * string * lookup list list
    1.33 -                  | Context of (string * int) * (int * class list)
    1.34 -  val derive_sortctxt: theory -> string -> typ -> sortcontext
    1.35 -  val derive_lookup: theory -> sortcontext -> typ * typ -> lookup list list
    1.36 -end;
    1.37 -
    1.38 -structure ClassPackage : CLASS_PACKAGE =
    1.39 -struct
    1.40 -
    1.41 -
    1.42 -
    1.43 -(** data kind 'Pure/classes' **)
    1.44 -
    1.45 -type class_info = {
    1.46 -  locale_name: string,
    1.47 -  axclass_name: string,
    1.48 -  members: string list,
    1.49 -  tycons: string list
    1.50 -};
    1.51 -
    1.52 -structure ClassesData = TheoryDataFun (
    1.53 -  struct
    1.54 -    val name = "Pure/classes";
    1.55 -    type T = class_info Symtab.table;
    1.56 -    val empty = Symtab.empty;
    1.57 -    val copy = I;
    1.58 -    val extend = I;
    1.59 -    fun merge _ = Symtab.merge (K true);
    1.60 -    fun print _ tab = (Pretty.writeln o Pretty.chunks) (map Pretty.str (Symtab.keys tab));
    1.61 -  end
    1.62 -);
    1.63 -
    1.64 -val _ = Context.add_setup [ClassesData.init];
    1.65 -val print_classes = ClassesData.print;
    1.66 -
    1.67 -fun lookup_class_info thy class = Symtab.lookup (ClassesData.get thy, class);
    1.68 -
    1.69 -fun get_class_info thy class =
    1.70 -  case lookup_class_info thy class
    1.71 -    of NONE => error ("undeclared class " ^ quote class)
    1.72 -     | SOME info => info;
    1.73 -
    1.74 -fun put_class_info class info thy =
    1.75 -  thy
    1.76 -  |> ClassesData.put (Symtab.update ((class, info), ClassesData.get thy));
    1.77 -
    1.78 -
    1.79 -(* name mangling *)
    1.80 -
    1.81 -fun get_locale_for_class thy class =
    1.82 -  #locale_name (get_class_info thy class)
    1.83 -
    1.84 -fun get_axclass_for_class thy class =
    1.85 -  #axclass_name (get_class_info thy class)
    1.86 -
    1.87 -
    1.88 -(* assign members to type classes *)
    1.89 -
    1.90 -local
    1.91 -
    1.92 -fun gen_add_members prep_class prep_member (raw_class, raw_members_new) thy =
    1.93 -  let
    1.94 -    val class = prep_class thy raw_class
    1.95 -    val members_new = map (prep_member thy) raw_members_new
    1.96 -    val {locale_name, axclass_name, members, tycons} =
    1.97 -      get_class_info thy class
    1.98 -  in
    1.99 -    thy
   1.100 -    |> put_class_info class {
   1.101 -         locale_name = locale_name,
   1.102 -         axclass_name = axclass_name,
   1.103 -         members = members @ members_new,
   1.104 -         tycons = tycons
   1.105 -       }
   1.106 -  end
   1.107 -
   1.108 -in
   1.109 -
   1.110 -val add_members_x = gen_add_members Sign.intern_class Sign.intern_const
   1.111 -val add_members = gen_add_members (K I) (K I)
   1.112 -
   1.113 -end (*local*)
   1.114 -
   1.115 -
   1.116 -(* assign type constructors to type classes *)
   1.117 -
   1.118 -local
   1.119 -
   1.120 -fun gen_add_tycons prep_class prep_type (raw_class, raw_tycons_new) thy =
   1.121 -  let
   1.122 -    val class = prep_class thy raw_class
   1.123 -    val tycons_new = map (prep_type thy) raw_tycons_new
   1.124 -    val {locale_name, axclass_name, members, tycons} =
   1.125 -      get_class_info thy class
   1.126 -  in
   1.127 -    thy
   1.128 -    |> put_class_info class {
   1.129 -         locale_name = locale_name,
   1.130 -         axclass_name = axclass_name,
   1.131 -         members = members,
   1.132 -         tycons = tycons @ tycons_new
   1.133 -       }
   1.134 -  end;
   1.135 -
   1.136 -in
   1.137 -
   1.138 -val add_tycons_x = gen_add_tycons Sign.intern_class Sign.intern_type
   1.139 -val add_tycons = gen_add_tycons (K I) (K I)
   1.140 -
   1.141 -end (*local*)
   1.142 -
   1.143 -
   1.144 -(* retrieve members *)
   1.145 -
   1.146 -val the_members = (#members oo get_class_info)
   1.147 -
   1.148 -
   1.149 -(* retrieve type constructor associations *)
   1.150 -
   1.151 -val the_tycons = (#tycons oo get_class_info)
   1.152 -
   1.153 -
   1.154 -
   1.155 -(** generic & useful... !!! move this somewhere else ??? **)
   1.156 -
   1.157 -fun subst_clsvar thy class subst typ = map_type_tfree (fn (tvar, sort) =>
   1.158 -    if Sorts.sort_eq (Sign.classes_of thy) ([class], sort)
   1.159 -    then subst (tvar, sort)
   1.160 -    else TFree (tvar, sort)
   1.161 -  ) typ
   1.162 -
   1.163 -
   1.164 -
   1.165 -(** class declaration **)
   1.166 -
   1.167 -local
   1.168 -
   1.169 -fun gen_add_class prep_class prep_loc
   1.170 -  (bname, raw_superclss, raw_locales, raw_locelems) int thy =
   1.171 -  let
   1.172 -    val name_class = Sign.full_name thy bname
   1.173 -    val name_loc = Sign.full_name thy bname
   1.174 -    val name_intro = name_loc ^ ".intro" (*!!!*)
   1.175 -    val superclss = map (prep_class thy) raw_superclss
   1.176 -    val _ = map (get_class_info thy) superclss
   1.177 -    val defaultS = Sign.defaultS thy
   1.178 -    val axsuperclss = case superclss of [] => defaultS
   1.179 -                                     | _ => superclss
   1.180 -    val _ = writeln ("superclasses: " ^ commas axsuperclss)
   1.181 -    val superlocales = 
   1.182 -      superclss
   1.183 -      |> map (get_locale_for_class thy)
   1.184 -      |> append (map (prep_loc thy) raw_locales)
   1.185 -    val locexpr =
   1.186 -      superlocales
   1.187 -      |> map (Locale.Locale)
   1.188 -      |> Locale.Merge
   1.189 -    fun get_loc_intro thy locname name_intro =
   1.190 -      let
   1.191 -        val ctxt = ProofContext.init thy
   1.192 -                   |> Locale.read_context_statement (SOME locname) [] []
   1.193 -                   |> #4
   1.194 -      in
   1.195 -        case ProofContext.assumptions_of ctxt
   1.196 -        of [] => NONE
   1.197 -         | _ => SOME (PureThy.get_thm thy (Name name_intro))
   1.198 -      end
   1.199 -    fun constify thy (mname, mtyp, _) = Const (Sign.intern_const thy mname, mtyp)
   1.200 -    fun axiom_for loc members thy = case Sign.const_constraint thy loc
   1.201 -      of SOME pred_type => if is_funtype pred_type
   1.202 -           then [((bname, 
   1.203 -             (ObjectLogic.assert_propT thy
   1.204 -             (list_comb (Const (loc, Type.unvarifyT pred_type), map (constify thy) members)))), [])
   1.205 -           ]
   1.206 -           else []
   1.207 -       | NONE => []
   1.208 -    fun get_members thy locname =
   1.209 -      let
   1.210 -        val ctxt = ProofContext.init thy
   1.211 -                   |> Locale.read_context_statement (SOME locname) [] []
   1.212 -                   |> #4
   1.213 -        val fixed = ProofContext.fixed_names_of ctxt
   1.214 -        fun mk_member fixed = (fixed, (the o ProofContext.default_type ctxt) fixed, NoSyn)
   1.215 -      in map mk_member fixed end;
   1.216 -    fun remove_supercls_members thy =
   1.217 -      superlocales
   1.218 -      |> map (get_members thy)
   1.219 -      |> fold (fn fxs => fn rem => gen_rems (fn ((x, _, _), (y, _, _)) => x = y) (rem, fxs))
   1.220 -    fun get_members_only thy locname =
   1.221 -      get_members thy locname
   1.222 -      |> remove_supercls_members thy
   1.223 -    fun add_constraint class (mname, mtyp, _) thy =
   1.224 -      let
   1.225 -        val tfree = case (typ_tfrees o Type.unvarifyT) mtyp
   1.226 -                    of [(tfree, sort)] => if Sorts.sort_eq (Sign.classes_of thy) (sort, defaultS)
   1.227 -                                          then tfree
   1.228 -                                          else error ("sort constraint not permitted in member " ^ quote mname)
   1.229 -                     | _ => error ("no or more than one type variable in declaration for member " ^ quote mname)
   1.230 -        fun constrain_tfree (tfree, _) = TFree (tfree, [class])
   1.231 -      in Sign.add_const_constraint_i
   1.232 -        (mname, map_type_tfree constrain_tfree mtyp) thy
   1.233 -      end
   1.234 -    fun setup_interpretation membernames = IsarThy.register_globally (*!!!*)
   1.235 -      (((bname ^ "_interp", []), Locale.Locale name_loc), membernames)
   1.236 -  in
   1.237 -    thy
   1.238 -    |> Locale.add_locale_context true bname locexpr raw_locelems
   1.239 -       |-> (fn (loccontext, elems) =>
   1.240 -       tap (fn _ => writeln "(1) added locale")
   1.241 -    #> `(fn thy => get_members_only thy name_loc)
   1.242 -       #-> (fn members =>
   1.243 -       Sign.add_consts_i members
   1.244 -    #> `(fn thy => map (fn (n, t, m) => (Sign.intern_const thy n, t, m)) members)
   1.245 -       #-> (fn intern_members =>
   1.246 -       tap (fn _ => writeln "(2) added members")
   1.247 -    #> `(fn thy => get_loc_intro thy name_loc name_intro)
   1.248 -       #-> (fn intro =>
   1.249 -       tap (fn _ => writeln "(3) axiom prep")
   1.250 -    #> `(axiom_for name_loc intern_members)
   1.251 -       #-> (fn axiom =>
   1.252 -(*       `(Sign.restore_naming)  *)
   1.253 -(*        #-> (fn restore_naming =>  *)
   1.254 -(*        Sign.add_path "class"  *)
   1.255 -       tap (fn thy => writeln ("(4) axiom: " ^
   1.256 -         commas (map (Sign.string_of_term thy o snd o fst) axiom)))
   1.257 -    #> AxClass.add_axclass_i (bname, axsuperclss) axiom #> fst
   1.258 -    #> tap (fn _ => writeln "(5) added axclass")
   1.259 -    #> (if is_some intro then PureThy.add_thms [Thm.no_attributes (bname ^ "_intro", the intro)] #> fst else I)
   1.260 -    #> tap (fn _ => writeln "(6) re-introduced intro")
   1.261 -    #> fold (add_constraint name_class) intern_members
   1.262 -(*     #> restore_naming  *)
   1.263 -    #> tap (fn _ => writeln "(7) constrained members")
   1.264 -    #> put_class_info name_class {
   1.265 -         locale_name = name_loc,
   1.266 -         axclass_name = name_class,
   1.267 -         members = map #1 intern_members,
   1.268 -         tycons = []
   1.269 -       }
   1.270 -    #> tap (fn _ => writeln "(8) added class data/members")
   1.271 -    #> setup_interpretation (map (SOME o #1) intern_members) int
   1.272 -    )))))
   1.273 -  end
   1.274 -
   1.275 -in
   1.276 -
   1.277 -val add_class = gen_add_class (Sign.intern_class) (Locale.intern);
   1.278 -
   1.279 -end (*local*)
   1.280 -
   1.281 -
   1.282 -
   1.283 -(** instance definition **)
   1.284 -
   1.285 -local
   1.286 -
   1.287 -fun read_check_memberdefs thy class tycon arity raw_defs = 
   1.288 -  let
   1.289 -    val pp = Sign.pp thy
   1.290 -    val thy_temp =
   1.291 -      thy
   1.292 -      |> Theory.copy
   1.293 -      |> Sign.add_arities_i [(tycon, arity, [class])]
   1.294 -      |> `(fn thy => Sorts.mg_domain (Sign.classes_arities_of thy) tycon [class])
   1.295 -      |> snd
   1.296 -    val _ = writeln "temp"
   1.297 -    fun read_check_memberdef ((raw_bname, raw_term), attr) members = 
   1.298 -      let
   1.299 -        val _ = writeln ("reading " ^ class ^ " - " ^ tycon)
   1.300 -        val _ = (writeln o commas o map (Sign.string_of_sort thy_temp)) (Sorts.mg_domain (Sign.classes_arities_of thy_temp) tycon [class])
   1.301 -        val _ = writeln (raw_bname ^ ": " ^ raw_term)
   1.302 -        val term = Sign.read_prop thy_temp raw_term
   1.303 -        val _ = writeln "read"
   1.304 -        val ((const, typ_def), rhs) = Theory.dest_def pp term handle TERM (msg, _) => error msg
   1.305 -        val _ = if member (op =) members const
   1.306 -                then {}
   1.307 -                else error ("superfluos definition: " ^ quote const)
   1.308 -        val typ_decl = Sign.the_const_constraint thy_temp const
   1.309 -        val typ_arity = Type (tycon, map (fn sort => TFree ("", sort)) arity)
   1.310 -        val typ_inst = subst_clsvar thy_temp class (fn _ => typ_arity) typ_decl
   1.311 -        val _ = if Sign.typ_instance thy_temp (typ_inst, typ_def)
   1.312 -                andalso Sign.typ_instance thy_temp (typ_def, typ_inst)
   1.313 -                then {}
   1.314 -                else error ("member definition " ^ quote const
   1.315 -                  ^ " does not have required type signature: expected "
   1.316 -                  ^ (quote o Sign.string_of_typ thy_temp) typ_inst ^ ", got "
   1.317 -                  ^ (quote o Sign.string_of_typ thy_temp) typ_decl)
   1.318 -        val bname = case raw_bname
   1.319 -                    of "" => const ^ "_" ^ tycon
   1.320 -                     | bname => bname
   1.321 -        val members' = remove (op =) const members
   1.322 -        val _ = (writeln o commas o map (Sign.string_of_sort thy)) (Sorts.mg_domain (Sign.classes_arities_of thy) tycon [class])
   1.323 -      in (((bname, term), attr), members') end
   1.324 -  in
   1.325 -    the_members thy_temp class
   1.326 -    |> fold_map read_check_memberdef raw_defs
   1.327 -    ||> (fn [] => I
   1.328 -          | members => error ("missing class member definitions: " ^ (commas o map quote) members))
   1.329 -    |> fst
   1.330 -  end
   1.331 -
   1.332 -in
   1.333 -
   1.334 -fun instance_arity_proof_ext (raw_ty, raw_arity, raw_class, raw_defs) int thy =
   1.335 -  let
   1.336 -    val class = Sign.intern_class thy raw_class
   1.337 -    val tycon = Sign.intern_type thy raw_ty
   1.338 -    val arity = map (Sign.read_sort thy) raw_arity
   1.339 -    val _ = writeln ("(1) mangled " ^ class ^ " - " ^ tycon)
   1.340 -  in
   1.341 -    thy
   1.342 -    |> `(fn thy => read_check_memberdefs thy class tycon arity raw_defs)
   1.343 -       |-> (fn defs =>
   1.344 -       `(fn _ => writeln ("(2) checked")) #> snd
   1.345 -(*!!! attributes     #> IsarThy.add_defs_i (true, defs)  *)
   1.346 -    #> Theory.add_defs_i true (map fst defs)
   1.347 -    #> `(fn _ => writeln ("(3) added")) #> snd
   1.348 -    #> AxClass.instance_arity_proof (raw_ty, raw_arity, raw_class)
   1.349 -         (add_tycons (class, [tycon])) int
   1.350 -    )
   1.351 -  end
   1.352 -
   1.353 -end (*local*)
   1.354 -
   1.355 -
   1.356 -
   1.357 -(** code generation suppport (dictionaries) *)
   1.358 -
   1.359 -(* filtering non-hugs classes *)
   1.360 -
   1.361 -fun is_class thy = is_some o lookup_class_info thy
   1.362 -
   1.363 -fun filter_class thy = filter (is_class thy)
   1.364 -
   1.365 -fun assert_class thy class =
   1.366 -  if is_class thy class then class
   1.367 -  else error ("not a class: " ^ quote class)
   1.368 -
   1.369 -fun get_arities thy sort tycon =
   1.370 -  Sorts.mg_domain (Sign.classes_arities_of thy) tycon sort
   1.371 -  |> (map o map) (assert_class thy)
   1.372 -
   1.373 -fun get_superclasses thy class =
   1.374 -  Sorts.superclasses (Sign.classes_of thy) class
   1.375 -  |> filter_class thy
   1.376 -
   1.377 -
   1.378 -(* sortcontext handling *)
   1.379 -
   1.380 -type sortcontext = (string * (string * sort)) list
   1.381 -
   1.382 -fun derive_sortctxt_proto thy typ =
   1.383 -  typ
   1.384 -  |> Type.unvarifyT
   1.385 -  |> typ_tfrees
   1.386 -  |> map (fn (v, s) => (v, filter_class thy s))
   1.387 -  |> filter (fn (_, []) => false | _ => true)
   1.388 -
   1.389 -fun derive_sortctxt thy sctxtparm typ =
   1.390 -  typ
   1.391 -  |> derive_sortctxt_proto thy
   1.392 -  |> map (pair sctxtparm)
   1.393 -
   1.394 -datatype lookup = Instance of class * string * lookup list list
   1.395 -                | Context of (string * int) * (int * class list)
   1.396 -
   1.397 -fun derive_lookup thy sctxt_use (raw_typ_def, raw_typ_use) =
   1.398 -  let
   1.399 -    val typ_def = Type.varifyT raw_typ_def
   1.400 -    val typ_use = Type.varifyT raw_typ_use
   1.401 -    val sanatize_sort = filter_class thy
   1.402 -    val match_tab = Sign.typ_match thy (typ_def, typ_use) Vartab.empty
   1.403 -    fun tab_lookup vname = (the o Vartab.lookup) (match_tab, (vname, 0))
   1.404 -    fun get_supercls_derivation (subclasses, superclass) =
   1.405 -      (the oo get_first) (fn subclass =>
   1.406 -        Sorts.class_le_path (Sign.classes_of thy) (subclass, superclass)
   1.407 -      ) subclasses
   1.408 -    fun mk_cls_deriv thy subclasses superclass =
   1.409 -      case get_supercls_derivation (subclasses, superclass)
   1.410 -      of (subclass::deriv) => (find_index_eq subclass subclasses, deriv)
   1.411 -    fun mk_lookup (sort_def, (Type (tycon, tyargs))) =
   1.412 -        let
   1.413 -          val arity_lookup = map2 mk_lookup
   1.414 -            (map sanatize_sort (Sorts.mg_domain (Sign.classes_arities_of thy) tycon sort_def), tyargs)
   1.415 -        in
   1.416 -          map (fn class => Instance (class, tycon, arity_lookup)) sort_def
   1.417 -        end
   1.418 -      | mk_lookup (sort_def, TVar ((vname, _), sort_use)) =
   1.419 -        let
   1.420 -          val vidx = find_index_eq vname (map (fst o snd) sctxt_use)
   1.421 -          val sctxtparm = #1 (List.nth (sctxt_use, vidx))
   1.422 -        in
   1.423 -          map (fn (class) => Context ((sctxtparm, vidx), mk_cls_deriv thy sort_use class)) sort_def
   1.424 -        end
   1.425 -  in
   1.426 -    derive_sortctxt_proto thy raw_typ_def
   1.427 -    |> map #1
   1.428 -    |> map tab_lookup
   1.429 -    |> map (apfst sanatize_sort)
   1.430 -    |> filter (fn ([], _) => false | _ => true)
   1.431 -    |> map mk_lookup
   1.432 -  end
   1.433 -
   1.434 -
   1.435 -
   1.436 -(** outer syntax **)
   1.437 -
   1.438 -local
   1.439 -
   1.440 -structure P = OuterParse
   1.441 -and K = OuterKeyword
   1.442 -
   1.443 -in
   1.444 -
   1.445 -val (classK, extendsK, importingK) = ("Xclass", "Xextends", "Ximporting")
   1.446 -
   1.447 -val classP =
   1.448 -(*   OuterSyntax.command classK "define class" K.thy_decl (  *)
   1.449 -  OuterSyntax.command classK "define class" K.thy_goal (
   1.450 -    P.name
   1.451 -    -- Scan.optional (P.$$$ extendsK |-- P.list1 P.xname) []
   1.452 -    -- Scan.optional (P.$$$ importingK |-- P.list1 P.xname) []
   1.453 -    -- Scan.repeat1 P.locale_element
   1.454 -    >> (fn (((name, superclasses), locales), locelems)
   1.455 -(*         => Toplevel.theory (add_class (name, superclss, locelems)))  *)
   1.456 -         => (Toplevel.print oo Toplevel.theory_to_proof)
   1.457 -              (add_class (name, superclasses, locales, locelems)))
   1.458 -  )
   1.459 -
   1.460 -val (instanceK, whereK) = ("Xinst", "Xwhere")
   1.461 -
   1.462 -val instanceP =
   1.463 -  OuterSyntax.command instanceK "prove type arity" K.thy_goal ((
   1.464 -    Scan.optional (P.$$$ "(" |-- P.!!! (P.list1 (P.group "sort" P.xname) --| P.$$$ ")")) []
   1.465 -    -- P.xname --| P.$$$ "::"
   1.466 -    -- P.group "class" P.xname
   1.467 -    -- Scan.optional (P.$$$ whereK |-- Scan.repeat1 P.spec_opt_name) [])
   1.468 -    >> (fn (((arity, ty), class), defs)
   1.469 -        => (Toplevel.print oo Toplevel.theory_to_proof)
   1.470 -             (instance_arity_proof_ext (ty, arity, class, defs))
   1.471 -       )
   1.472 -  )
   1.473 -
   1.474 -val _ = OuterSyntax.add_parsers [classP, instanceP]
   1.475 -
   1.476 -val _ = OuterSyntax.add_keywords [extendsK, whereK]
   1.477 -
   1.478 -val classcgK = "codegen_class"
   1.479 -
   1.480 -fun classcg raw_class raw_members raw_tycons thy =
   1.481 -  let
   1.482 -    val class = Sign.intern_class thy raw_class
   1.483 -  in
   1.484 -    thy
   1.485 -    |> put_class_info class {
   1.486 -         locale_name = "",
   1.487 -         axclass_name = class,
   1.488 -         members = [],
   1.489 -         tycons = []
   1.490 -       }
   1.491 -    |> add_members_x (class, raw_members)
   1.492 -    |> add_tycons_x (class, raw_tycons)
   1.493 -  end
   1.494 -
   1.495 -val classcgP =
   1.496 -  OuterSyntax.command classcgK "codegen data for classes" K.thy_decl (
   1.497 -    P.xname
   1.498 -    -- ((P.$$$ "\\<Rightarrow>" || P.$$$ "=>") |-- (P.list1 P.name))
   1.499 -    -- (Scan.optional ((P.$$$ "\\<Rightarrow>" || P.$$$ "=>") |-- (P.list1 P.name)) [])
   1.500 -    >> (fn ((name, tycons), members) => (Toplevel.theory (classcg name members tycons)))
   1.501 -  )
   1.502 -
   1.503 -val _ = OuterSyntax.add_parsers [classcgP];
   1.504 -
   1.505 -end (*local*)
   1.506 -
   1.507 -end
   1.508 -
   1.509 -(*
   1.510 -
   1.511 -
   1.512 -
   1.513 -isomorphism
   1.514 -
   1.515 -locale sg = fixes prod assumes assoc: ...
   1.516 -
   1.517 -consts prod :: "'a::type => 'a => 'a"
   1.518 -
   1.519 -axclass sg < type
   1.520 -  sg: "sg prod"
   1.521 -
   1.522 -constraints prod :: "'a::sg => 'a => 'a"
   1.523 -
   1.524 -
   1.525 -theory Scratch imports Main begin
   1.526 -
   1.527 -(* class definition *)
   1.528 -
   1.529 -locale sg =
   1.530 -  fixes f :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infix "\<degree>" 60)
   1.531 -  assumes assoc: "(x \<degree> y) \<degree> z = x \<degree> (y \<degree> z)"
   1.532 -
   1.533 -classes sg
   1.534 -consts prod :: "'a::sg \<Rightarrow> 'a \<Rightarrow> 'a"  (infix "\<degree>\<degree>" 60)
   1.535 -classrel sg < type
   1.536 -axioms sg: "sg prod"
   1.537 -
   1.538 -interpretation sg: sg [prod] by (rule sg)
   1.539 -
   1.540 -
   1.541 -(* abstract reasoning *)
   1.542 -
   1.543 -lemma (in sg)
   1.544 -  bar: "x \<degree> y = x \<degree> y" ..
   1.545 -
   1.546 -lemma baz:
   1.547 -  fixes x :: "'a::sg"
   1.548 -  shows "x \<degree>\<degree> y = x \<degree>\<degree> y" ..
   1.549 -
   1.550 -(* class instantiation *)
   1.551 -
   1.552 -interpretation sg_list: sg ["op @"]
   1.553 -  by (rule sg.intro) (simp only: append_assoc)
   1.554 -arities list :: (type) sg
   1.555 -defs (overloaded) prod_list_def: "prod == op @"
   1.556 -
   1.557 -interpretation sg_prod: sg ["%p q. (fst p \<degree>\<degree> fst q, snd p \<degree>\<degree> snd q)"]
   1.558 -  by (rule sg.intro) (simp add: sg.assoc)
   1.559 -
   1.560 -arities * :: (sg, sg) sg
   1.561 -
   1.562 -    (* fun inferT_axm pre_tm =
   1.563 -      let
   1.564 -        val pp = Sign.pp thy;
   1.565 -        val (t, _) = Sign.infer_types pp thy (K NONE) (K NONE) [] true ([pre_tm], TFree ("a", []));
   1.566 -      in t end
   1.567 -    fun axiom_for members = (bname, inferT_axm
   1.568 -      (ObjectLogic.assert_propT thy (list_comb (Const (name, dummyT), map (fn (mname, mtyp, _) => Const (mname, mtyp)) members)))) *)
   1.569 -
   1.570 -
   1.571 -*)
   1.572 -