src/Pure/Isar/code.ML
changeset 27582 367aff8d7ffd
parent 27557 151731493264
child 27609 b23c9ad0fe7d
equal deleted inserted replaced
27581:db431936de07 27582:367aff8d7ffd
   178 fun melt _ ([], []) = (false, [])
   178 fun melt _ ([], []) = (false, [])
   179   | melt _ ([], ys) = (true, ys)
   179   | melt _ ([], ys) = (true, ys)
   180   | melt eq (xs, ys) = fold_rev
   180   | melt eq (xs, ys) = fold_rev
   181       (fn y => fn (t, xs) => (t orelse not (member eq xs y), insert eq y xs)) ys (false, xs);
   181       (fn y => fn (t, xs) => (t orelse not (member eq xs y), insert eq y xs)) ys (false, xs);
   182 
   182 
   183 fun melt_alist eq_key eq (xys as (xs, ys)) =
       
   184   if eq_list (eq_pair eq_key eq) (xs, ys)
       
   185   then (false, xs)
       
   186   else (true, AList.merge eq_key eq xys);
       
   187 
       
   188 val melt_thms = melt Thm.eq_thm_prop;
   183 val melt_thms = melt Thm.eq_thm_prop;
   189 
   184 
   190 fun melt_lthms (r1, r2) =
   185 fun melt_lthms (r1, r2) =
   191   if Susp.same (r1, r2)
   186   if Susp.same (r1, r2)
   192     then (false, r1)
   187     then (false, r1)
   212   end;
   207   end;
   213 
   208 
   214 
   209 
   215 (* specification data *)
   210 (* specification data *)
   216 
   211 
   217 fun melt_funcs tabs =
   212 val merge_funcs = Symtab.join (fn _ => fn ((_, a), (_, b)) => melt_sdthms (a, b));
   218   let
       
   219     val tab' = Symtab.join (fn _ => fn ((_, a), (_, b)) => melt_sdthms (a, b)) tabs;
       
   220     val touched = Symtab.fold (fn (c, (true, _)) => insert (op =) c | _ => I) tab' [];
       
   221   in (touched, tab') end;
       
   222 
   213 
   223 val eq_string = op = : string * string -> bool;
   214 val eq_string = op = : string * string -> bool;
   224 fun eq_dtyp ((vs1, cs1), (vs2, cs2)) = 
   215 fun eq_dtyp ((vs1, cs1), (vs2, cs2)) = 
   225   gen_eq_set (eq_pair eq_string (gen_eq_set eq_string)) (vs1, vs2)
   216   gen_eq_set (eq_pair eq_string (gen_eq_set eq_string)) (vs1, vs2)
   226     andalso gen_eq_set (eq_fst eq_string) (cs1, cs2);
   217     andalso gen_eq_set (eq_fst eq_string) (cs1, cs2);
   227 fun melt_dtyps (tabs as (tab1, tab2)) =
   218 fun merge_dtyps (tabs as (tab1, tab2)) =
   228   let
   219   let
   229     val tycos1 = Symtab.keys tab1;
   220     fun join _ (cos as (_, cos2)) = if eq_dtyp cos then raise Symtab.SAME else cos2;
   230     val tycos2 = Symtab.keys tab2;
   221   in Symtab.join join tabs end;
   231     val tycos' = filter (member eq_string tycos2) tycos1;
   222 
   232     val touched = not (gen_eq_set (op =) (tycos1, tycos2)
   223 fun merge_cases ((cases1, undefs1), (cases2, undefs2)) =
   233       andalso gen_eq_set (eq_pair (op =) eq_dtyp)
   224   (Symtab.merge (K true) (cases1, cases2), Symtab.merge (K true) (undefs1, undefs2));
   234       (AList.make (the o Symtab.lookup tab1) tycos',
       
   235        AList.make (the o Symtab.lookup tab2) tycos'));
       
   236     fun join _ (cos as (_, cos2)) = if eq_dtyp cos
       
   237       then raise Symtab.SAME else cos2;
       
   238   in (touched, Symtab.join join tabs) end;
       
   239 
       
   240 fun melt_cases ((cases1, undefs1), (cases2, undefs2)) =
       
   241   let
       
   242     val touched1 = subtract (op =) (Symtab.keys cases1) (Symtab.keys cases2)
       
   243       @ subtract (op =) (Symtab.keys cases2) (Symtab.keys cases1);
       
   244     val touched2 = subtract (op =) (Symtab.keys undefs1) (Symtab.keys undefs2)
       
   245       @ subtract (op =) (Symtab.keys undefs2) (Symtab.keys undefs1);
       
   246     val touched = fold (insert (op =)) touched1 touched2;
       
   247   in
       
   248     (touched, (Symtab.merge (K true) (cases1, cases2),
       
   249       Symtab.merge (K true) (undefs1, undefs2)))
       
   250   end;
       
   251 
   225 
   252 datatype spec = Spec of {
   226 datatype spec = Spec of {
   253   funcs: (bool * sdthms) Symtab.table,
   227   funcs: (bool * sdthms) Symtab.table,
   254   dtyps: ((string * sort) list * (string * typ list) list) Symtab.table,
   228   dtyps: ((string * sort) list * (string * typ list) list) Symtab.table,
   255   cases: (int * string list) Symtab.table * unit Symtab.table
   229   cases: (int * string list) Symtab.table * unit Symtab.table
   260 fun map_spec f (Spec { funcs = funcs, dtyps = dtyps, cases = cases }) =
   234 fun map_spec f (Spec { funcs = funcs, dtyps = dtyps, cases = cases }) =
   261   mk_spec (f (funcs, (dtyps, cases)));
   235   mk_spec (f (funcs, (dtyps, cases)));
   262 fun merge_spec (Spec { funcs = funcs1, dtyps = dtyps1, cases = cases1 },
   236 fun merge_spec (Spec { funcs = funcs1, dtyps = dtyps1, cases = cases1 },
   263   Spec { funcs = funcs2, dtyps = dtyps2, cases = cases2 }) =
   237   Spec { funcs = funcs2, dtyps = dtyps2, cases = cases2 }) =
   264   let
   238   let
   265     val (_, funcs) = melt_funcs (funcs1, funcs2);
   239     val funcs = merge_funcs (funcs1, funcs2);
   266     val (_, dtyps) = melt_dtyps (dtyps1, dtyps2);
   240     val dtyps = merge_dtyps (dtyps1, dtyps2);
   267     val (_, cases) = melt_cases (cases1, cases2);
   241     val cases = merge_cases (cases1, cases2);
   268   in mk_spec (funcs, (dtyps, cases)) end;
   242   in mk_spec (funcs, (dtyps, cases)) end;
   269 
   243 
   270 
   244 
   271 (* pre- and postprocessor *)
   245 (* pre- and postprocessor *)
   272 
   246 
   887   #> snd;
   861   #> snd;
   888 
   862 
   889 in
   863 in
   890 
   864 
   891 fun preprocess thy thms =
   865 fun preprocess thy thms =
   892   thms
   866   let
   893   |> fold (fn (_, (_, f)) => apply_functrans thy f) ((#functrans o the_thmproc o the_exec) thy)
   867     val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy;
   894   |> map (CodeUnit.rewrite_func ((#pre o the_thmproc o the_exec) thy))
   868   in
   895   (*FIXME - must check gere: rewrite rule, defining equation, proper constant *)
   869     thms
   896   |> map (AxClass.unoverload thy)
   870     |> fold (fn (_, (_, f)) => apply_functrans thy f) ((#functrans o the_thmproc o the_exec) thy)
   897   |> common_typ_funcs;
   871     |> map (CodeUnit.rewrite_func pre)
       
   872     (*FIXME - must check gere: rewrite rule, defining equation, proper constant *)
       
   873     |> map (AxClass.unoverload thy)
       
   874     |> common_typ_funcs
       
   875   end;
   898 
   876 
   899 
   877 
   900 fun preprocess_conv ct =
   878 fun preprocess_conv ct =
   901   let
   879   let
   902     val thy = Thm.theory_of_cterm ct;
   880     val thy = Thm.theory_of_cterm ct;
       
   881     val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy;
   903   in
   882   in
   904     ct
   883     ct
   905     |> MetaSimplifier.rewrite' (ProofContext.init thy) false ((#pre o the_thmproc o the_exec) thy)
   884     |> Simplifier.rewrite pre
   906     |> rhs_conv (AxClass.unoverload_conv thy)
   885     |> rhs_conv (AxClass.unoverload_conv thy)
   907   end;
   886   end;
   908 
   887 
   909 fun preprocess_term thy = term_of_conv thy preprocess_conv;
   888 fun preprocess_term thy = term_of_conv thy preprocess_conv;
   910 
   889 
   911 fun postprocess_conv ct =
   890 fun postprocess_conv ct =
   912   let
   891   let
   913     val thy = Thm.theory_of_cterm ct;
   892     val thy = Thm.theory_of_cterm ct;
       
   893     val post = (Simplifier.theory_context thy o #post o the_thmproc o the_exec) thy;
   914   in
   894   in
   915     ct
   895     ct
   916     |> AxClass.overload_conv thy
   896     |> AxClass.overload_conv thy
   917     |> rhs_conv (MetaSimplifier.rewrite' (ProofContext.init thy) false 
   897     |> rhs_conv (Simplifier.rewrite post)
   918           ((#post o the_thmproc o the_exec) thy))
       
   919   end;
   898   end;
   920 
   899 
   921 fun postprocess_term thy = term_of_conv thy postprocess_conv;
   900 fun postprocess_term thy = term_of_conv thy postprocess_conv;
   922 
   901 
   923 end; (*local*)
   902 end; (*local*)