author | wenzelm |
Wed, 31 Dec 2008 18:53:16 +0100 | |
changeset 29270 | 0eade173f77e |
parent 29183 | f1648e009dc1 |
permissions | -rw-r--r-- |
25555 | 1 |
(* Title: HOL/Tools/function_package/sum_tree.ML |
2 |
ID: $Id$ |
|
3 |
Author: Alexander Krauss, TU Muenchen |
|
4 |
||
5 |
Some common tools for working with sum types in balanced tree form. |
|
6 |
*) |
|
7 |
||
8 |
structure SumTree = |
|
9 |
struct |
|
10 |
||
11 |
(* Theory dependencies *) |
|
29183
f1648e009dc1
removed duplicate sum_case used only by function package;
krauss
parents:
25555
diff
changeset
|
12 |
val proj_in_rules = [@{thm "Datatype.Projl_Inl"}, @{thm "Datatype.Projr_Inr"}] |
f1648e009dc1
removed duplicate sum_case used only by function package;
krauss
parents:
25555
diff
changeset
|
13 |
val sumcase_split_ss = HOL_basic_ss addsimps (@{thm "Product_Type.split"} :: @{thms "sum.cases"}) |
25555 | 14 |
|
15 |
(* top-down access in balanced tree *) |
|
16 |
fun access_top_down {left, right, init} len i = |
|
17 |
BalancedTree.access {left = (fn f => f o left), right = (fn f => f o right), init = I} len i init |
|
18 |
||
19 |
(* Sum types *) |
|
20 |
fun mk_sumT LT RT = Type ("+", [LT, RT]) |
|
29183
f1648e009dc1
removed duplicate sum_case used only by function package;
krauss
parents:
25555
diff
changeset
|
21 |
fun mk_sumcase TL TR T l r = Const (@{const_name "sum.sum_case"}, (TL --> T) --> (TR --> T) --> mk_sumT TL TR --> T) $ l $ r |
25555 | 22 |
|
23 |
val App = curry op $ |
|
24 |
||
25 |
fun mk_inj ST n i = |
|
26 |
access_top_down |
|
27 |
{ init = (ST, I : term -> term), |
|
28 |
left = (fn (T as Type ("+", [LT, RT]), inj) => (LT, inj o App (Const (@{const_name "Inl"}, LT --> T)))), |
|
29 |
right =(fn (T as Type ("+", [LT, RT]), inj) => (RT, inj o App (Const (@{const_name "Inr"}, RT --> T))))} n i |
|
30 |
|> snd |
|
31 |
||
32 |
fun mk_proj ST n i = |
|
33 |
access_top_down |
|
34 |
{ init = (ST, I : term -> term), |
|
29183
f1648e009dc1
removed duplicate sum_case used only by function package;
krauss
parents:
25555
diff
changeset
|
35 |
left = (fn (T as Type ("+", [LT, RT]), proj) => (LT, App (Const (@{const_name "Datatype.Projl"}, T --> LT)) o proj)), |
f1648e009dc1
removed duplicate sum_case used only by function package;
krauss
parents:
25555
diff
changeset
|
36 |
right =(fn (T as Type ("+", [LT, RT]), proj) => (RT, App (Const (@{const_name "Datatype.Projr"}, T --> RT)) o proj))} n i |
25555 | 37 |
|> snd |
38 |
||
39 |
fun mk_sumcases T fs = |
|
40 |
BalancedTree.make (fn ((f, fT), (g, gT)) => (mk_sumcase fT gT T f g, mk_sumT fT gT)) |
|
41 |
(map (fn f => (f, domain_type (fastype_of f))) fs) |
|
42 |
|> fst |
|
43 |
||
44 |
end |