src/Pure/Isar/class_declaration.ML
changeset 58294 7f990b3d5189
parent 58293 1d0343818ec0
child 58319 9228350683d0
     1.1 --- a/src/Pure/Isar/class_declaration.ML	Wed Sep 10 14:58:01 2014 +0200
     1.2 +++ b/src/Pure/Isar/class_declaration.ML	Wed Sep 10 14:58:02 2014 +0200
     1.3 @@ -114,10 +114,11 @@
     1.4      val proto_base_sort =
     1.5        if null sups then Sign.defaultS thy
     1.6        else fold inter_sort (map (Class.base_sort thy) sups) [];
     1.7 +    val is_param = member (op =) (map fst (Class.these_params thy sups));
     1.8      val base_constraints = (map o apsnd)
     1.9        (map_type_tfree (K (TVar ((Name.aT, 0), proto_base_sort))) o fst o snd)
    1.10          (Class.these_operations thy sups);
    1.11 -    fun singleton_fixate Ts =
    1.12 +    fun singleton_fixateT Ts =
    1.13        let
    1.14          val tfrees = fold Term.add_tfreesT Ts [];
    1.15          val inferred_sort =
    1.16 @@ -140,18 +141,20 @@
    1.17          (map o map_atyps)
    1.18            (fn T as TVar (xi, _) => if Type_Infer.is_param xi then fixateT else T | T => T) Ts
    1.19        end;
    1.20 -    fun after_infer_fixate Ts =
    1.21 +    fun singleton_fixate _ ts = burrow_types singleton_fixateT ts;
    1.22 +    fun unify_params ctxt ts =
    1.23        let
    1.24 -        val fixate_sort =
    1.25 -          (fold o fold_atyps)
    1.26 -            (fn TVar (xi, S) => if Type_Infer.is_param xi then inter_sort S else I | _ => I) Ts [];
    1.27 -      in
    1.28 -        (map o map_atyps)
    1.29 -          (fn T as TVar (xi, _) =>
    1.30 -              if Type_Infer.is_param xi then Type_Infer.param 0 (Name.aT, fixate_sort) else T
    1.31 -            | T => T) Ts
    1.32 +        val param_Ts = (fold o fold_aterms)
    1.33 +          (fn Free (v, T) => if is_param v then fold_atyps (insert (op =)) T else I | _ => I) ts [];
    1.34 +        val param_Ts_subst = map_filter (try dest_TVar) param_Ts;
    1.35 +        val param_T = if null param_Ts_subst then NONE
    1.36 +          else SOME (case get_first (try dest_TFree) param_Ts of
    1.37 +            SOME v_sort => TFree v_sort |
    1.38 +            NONE => TVar ((fst o hd) param_Ts_subst, fold (inter_sort o snd) param_Ts_subst []));
    1.39 +      in case param_T of
    1.40 +        NONE => ts |
    1.41 +        SOME T => map (subst_TVars (map (rpair T o fst) param_Ts_subst)) ts
    1.42        end;
    1.43 -    fun lift_check f _ ts = burrow_types f ts;
    1.44  
    1.45      (* preprocessing elements, retrieving base sort from type-checked elements *)
    1.46      val raw_supexpr =
    1.47 @@ -159,10 +162,10 @@
    1.48      val init_class_body =
    1.49        fold (Proof_Context.add_const_constraint o apsnd SOME) base_constraints
    1.50        #> Class.redeclare_operations thy sups
    1.51 -      #> Context.proof_map (Syntax_Phases.term_check 0 "singleton_fixate" (lift_check singleton_fixate));
    1.52 +      #> Context.proof_map (Syntax_Phases.term_check 0 "singleton_fixate" singleton_fixate);
    1.53      val ((raw_supparams, _, raw_inferred_elems, _), _) =
    1.54        Proof_Context.init_global thy
    1.55 -      |> Context.proof_map (Syntax_Phases.term_check 0 "after_infer_fixate" (lift_check after_infer_fixate))
    1.56 +      |> Context.proof_map (Syntax_Phases.term_check 0 "unify_params" unify_params)
    1.57        |> prep_decl raw_supexpr init_class_body raw_elems;
    1.58      fun filter_element (Element.Fixes []) = NONE
    1.59        | filter_element (e as Element.Fixes _) = SOME e