src/Pure/Tools/class_package.ML
changeset 22473 753123c89d72
parent 22423 c1836b14c63a
child 22507 3572bc633d9a
equal deleted inserted replaced
22472:bfd9c0fd70b1 22473:753123c89d72
   415 
   415 
   416 val fully_qualified = ref false;
   416 val fully_qualified = ref false;
   417 
   417 
   418 local
   418 local
   419 
   419 
   420 fun certify_class thy class =
   420 fun gen_class add_locale prep_class prep_param bname
   421   tap (the_class_data thy) (Sign.certify_class thy class);
   421     raw_supclasses raw_elems raw_other_consts thy =
   422 
       
   423 fun read_class thy =
       
   424   certify_class thy o Sign.intern_class thy;
       
   425 
       
   426 fun gen_class add_locale prep_class prep_param bname raw_supclasses raw_elems raw_other_consts thy =
       
   427   let
   422   let
   428     (*FIXME need proper concept for reading locale statements*)
   423     (*FIXME need proper concept for reading locale statements*)
   429     fun subst_classtyvar (_, _) =
   424     fun subst_classtyvar (_, _) =
   430           TFree (AxClass.param_tyvarname, [])
   425           TFree (AxClass.param_tyvarname, [])
   431       | subst_classtyvar (v, sort) =
   426       | subst_classtyvar (v, sort) =
   434       typ = Term.map_type_tfree subst_classtyvar, fact = I, attrib = I};*)
   429       typ = Term.map_type_tfree subst_classtyvar, fact = I, attrib = I};*)
   435     val other_consts = map (prep_param thy) raw_other_consts;
   430     val other_consts = map (prep_param thy) raw_other_consts;
   436     val (elems, includes) = fold_rev (fn Locale.Elem e => apfst (cons e)
   431     val (elems, includes) = fold_rev (fn Locale.Elem e => apfst (cons e)
   437       | Locale.Expr i => apsnd (cons i)) raw_elems ([], []);
   432       | Locale.Expr i => apsnd (cons i)) raw_elems ([], []);
   438     val supclasses = map (prep_class thy) raw_supclasses;
   433     val supclasses = map (prep_class thy) raw_supclasses;
   439     val supsort =
   434     val sups = filter (is_some o lookup_class_data thy) supclasses;
   440       supclasses
   435     val supsort = Sign.certify_sort thy supclasses;
   441       |> Sign.certify_sort thy
   436     val suplocales = map (Locale.Locale o #locale o the_class_data thy) sups;
   442       |> (fn [] => Sign.defaultS thy | S => S); (*FIXME Why syntax defaultS?*)
       
   443     val suplocales = map (Locale.Locale o #locale o the_class_data thy) supclasses;
       
   444     val supexpr = Locale.Merge (suplocales @ includes);
   437     val supexpr = Locale.Merge (suplocales @ includes);
   445     val supparams = (map fst o Locale.parameters_of_expr thy)
   438     val supparams = (map fst o Locale.parameters_of_expr thy)
   446       (Locale.Merge suplocales);
   439       (Locale.Merge suplocales);
   447     val supconsts = AList.make
   440     val supconsts = AList.make (the o AList.lookup (op =) (param_map thy sups))
   448       (the o AList.lookup (op =) (param_map thy supclasses)) (map fst supparams);
   441       (map fst supparams);
   449     (*val elems_constrains = map
   442     (*val elems_constrains = map
   450       (Element.Constrains o apsnd (Term.map_type_tfree subst_classtyvar)) supparams;*)
   443       (Element.Constrains o apsnd (Term.map_type_tfree subst_classtyvar)) supparams;*)
       
   444     fun mk_tyvar (_, sort) = TFree (AxClass.param_tyvarname,
       
   445       if Sign.subsort thy (supsort, sort) then sort else error
       
   446         ("Sort " ^ Sign.string_of_sort thy sort
       
   447           ^ " is less general than permitted least general sort "
       
   448           ^ Sign.string_of_sort thy supsort));
   451     fun extract_params thy name_locale =
   449     fun extract_params thy name_locale =
   452       let   
   450       let
   453         val params = Locale.parameters_of thy name_locale;
   451         val params = Locale.parameters_of thy name_locale;
   454       in
   452       in
   455         (map (fst o fst) params, params
   453         (map (fst o fst) params, params
   456         |> (map o apfst o apsnd o Term.map_type_tfree)
   454         |> (map o apfst o apsnd o Term.map_type_tfree) mk_tyvar
   457              (K (TFree (AxClass.param_tyvarname, Sign.defaultS thy)))
       
   458         |> (map o apsnd) (fork_mixfix true NONE #> fst)
   455         |> (map o apsnd) (fork_mixfix true NONE #> fst)
   459         |> chop (length supconsts)
   456         |> chop (length supconsts)
   460         |> snd)
   457         |> snd)
   461       end;
   458       end;
   462     fun extract_assumes name_locale params thy cs =
   459     fun extract_assumes name_locale params thy cs =
   491       #-> (fn (param_names, params) =>
   488       #-> (fn (param_names, params) =>
   492         axclass_params (bname, supsort) params (extract_assumes name_locale params) other_consts
   489         axclass_params (bname, supsort) params (extract_assumes name_locale params) other_consts
   493       #-> (fn (name_axclass, ((_, (ax_terms, ax_axioms)), consts)) =>
   490       #-> (fn (name_axclass, ((_, (ax_terms, ax_axioms)), consts)) =>
   494         `(fn thy => extract_axiom_names thy name_locale)
   491         `(fn thy => extract_axiom_names thy name_locale)
   495       #-> (fn axiom_names =>
   492       #-> (fn axiom_names =>
   496         add_class_data ((name_axclass, supclasses),
   493         add_class_data ((name_axclass, sups),
   497           (name_locale, map (fst o fst) params ~~ map fst consts,
   494           (name_locale, map (fst o fst) params ~~ map fst consts,
   498             map2 make_witness ax_terms ax_axioms, axiom_names))
   495             map2 make_witness ax_terms ax_axioms, axiom_names))
   499       #> prove_interpretation ((ALLGOALS o ProofContext.fact_tac) ax_axioms)
   496       #> prove_interpretation ((ALLGOALS o ProofContext.fact_tac) ax_axioms)
   500           ((!fully_qualified, Logic.const_of_class bname), []) (Locale.Locale name_locale)
   497           ((!fully_qualified, Logic.const_of_class bname), []) (Locale.Locale name_locale)
   501           (mk_instT name_axclass, mk_inst name_axclass param_names (map snd supconsts @ consts))
   498           (mk_instT name_axclass, mk_inst name_axclass param_names (map snd supconsts @ consts))
   503       )))))
   500       )))))
   504   end;
   501   end;
   505 
   502 
   506 in
   503 in
   507 
   504 
   508 val class_cmd = gen_class Locale.add_locale read_class AxClass.read_param;
   505 val class_cmd = gen_class Locale.add_locale Sign.intern_class AxClass.read_param;
   509 val class = gen_class Locale.add_locale_i certify_class (K I);
   506 val class = gen_class Locale.add_locale_i Sign.certify_class (K I);
   510 
   507 
   511 end; (*local*)
   508 end; (*local*)
   512 
   509 
   513 local
   510 local
   514 
   511