src/HOL/Tools/Ctr_Sugar/ctr_sugar.ML
changeset 57830 2d0f0d6fdf3e
parent 57633 4ff8c090d580
child 57882 38bf4de248a6
     1.1 --- a/src/HOL/Tools/Ctr_Sugar/ctr_sugar.ML	Wed Jul 30 16:44:54 2014 +0200
     1.2 +++ b/src/HOL/Tools/Ctr_Sugar/ctr_sugar.ML	Wed Jul 30 09:40:28 2014 +0200
     1.3 @@ -62,6 +62,7 @@
     1.4    val ctr_of_ctr_spec: ('c, 'a) ctr_spec -> 'c
     1.5    val args_of_ctr_spec: ('c, 'a) ctr_spec -> 'a list
     1.6  
     1.7 +  val fake_local_theory_for_sel_defaults: (binding * typ) list -> Proof.context -> Proof.context
     1.8    val free_constructors: ({prems: thm list, context: Proof.context} -> tactic) list list ->
     1.9      (((bool * bool) * binding) * (term, binding) ctr_spec list) * term list -> local_theory ->
    1.10      ctr_sugar * local_theory
    1.11 @@ -334,6 +335,12 @@
    1.12        malformed ()
    1.13    end;
    1.14  
    1.15 +(* Ideally, we would enrich the context with constants rather than free variables. *)
    1.16 +fun fake_local_theory_for_sel_defaults sel_bTs =
    1.17 +  Proof_Context.allow_dummies
    1.18 +  #> Proof_Context.add_fixes (map (fn (b, T) => (b, SOME T, NoSyn)) sel_bTs)
    1.19 +  #> snd;
    1.20 +
    1.21  type ('c, 'a) ctr_spec = (binding * 'c) * 'a list;
    1.22  
    1.23  fun disc_of_ctr_spec ((disc, _), _) = disc;
    1.24 @@ -507,30 +514,35 @@
    1.25          (true, [], [], [], [], [], lthy')
    1.26        else
    1.27          let
    1.28 -          val sel_bindings = flat sel_bindingss;
    1.29 -          val uniq_sel_bindings = distinct Binding.eq_name sel_bindings;
    1.30 -          val all_sels_distinct = (length uniq_sel_bindings = length sel_bindings);
    1.31 +          val all_sel_bindings = flat sel_bindingss;
    1.32 +          val num_all_sel_bindings = length all_sel_bindings;
    1.33 +          val uniq_sel_bindings = distinct Binding.eq_name all_sel_bindings;
    1.34 +          val all_sels_distinct = (length uniq_sel_bindings = num_all_sel_bindings);
    1.35  
    1.36            val sel_binding_index =
    1.37 -            if all_sels_distinct then 1 upto length sel_bindings
    1.38 -            else map (fn b => find_index (curry Binding.eq_name b) uniq_sel_bindings) sel_bindings;
    1.39 +            if all_sels_distinct then
    1.40 +              1 upto num_all_sel_bindings
    1.41 +            else
    1.42 +              map (fn b => find_index (curry Binding.eq_name b) uniq_sel_bindings) all_sel_bindings;
    1.43  
    1.44 -          val all_proto_sels = flat (map3 (fn k => fn xs => map (fn x => (k, (xs, x)))) ks xss xss);
    1.45 +          val all_proto_sels = flat (map3 (fn k => fn xs => map (pair k o pair xs)) ks xss xss);
    1.46            val sel_infos =
    1.47              AList.group (op =) (sel_binding_index ~~ all_proto_sels)
    1.48              |> sort (int_ord o pairself fst)
    1.49              |> map snd |> curry (op ~~) uniq_sel_bindings;
    1.50            val sel_bindings = map fst sel_infos;
    1.51 -          val sel_Ts = map (curry (op -->) fcT o fastype_of o snd o snd o hd o snd) sel_infos;
    1.52 -
    1.53 -          val sel_default_lthy = no_defs_lthy
    1.54 -            |> Proof_Context.allow_dummies
    1.55 -            |> Proof_Context.add_fixes
    1.56 -              (map2 (fn b => fn T => (b, SOME T, NoSyn)) sel_bindings sel_Ts)
    1.57 -            |> snd;
    1.58  
    1.59            val sel_defaults =
    1.60 -            map (extract_sel_default sel_default_lthy o prep_term sel_default_lthy) sel_default_eqs;
    1.61 +            if null sel_default_eqs then
    1.62 +              []
    1.63 +            else
    1.64 +              let
    1.65 +                val sel_Ts = map (curry (op -->) fcT o fastype_of o snd o snd o hd o snd) sel_infos;
    1.66 +                val fake_lthy =
    1.67 +                  fake_local_theory_for_sel_defaults (sel_bindings ~~ sel_Ts) no_defs_lthy;
    1.68 +              in
    1.69 +                map (extract_sel_default fake_lthy o prep_term fake_lthy) sel_default_eqs
    1.70 +              end;
    1.71  
    1.72            fun disc_free b = Free (Binding.name_of b, mk_pred1T fcT);
    1.73