src/HOL/Tools/Function/sum_tree.ML
author wenzelm
Tue, 29 Sep 2009 22:48:24 +0200
changeset 32765 3032c0308019
parent 32603 e08fdd615333
child 33961 03f2ab6a4ea6
permissions -rw-r--r--
modernized Balanced_Tree;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31775
2b04504fcb69 uniformly capitialized names for subdirectories
haftmann
parents: 29183
diff changeset
     1
(*  Title:      HOL/Tools/Function/sum_tree.ML
25555
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
     2
    Author:     Alexander Krauss, TU Muenchen
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
     3
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
     4
Some common tools for working with sum types in balanced tree form.
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
     5
*)
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
     6
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
     7
structure SumTree =
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
     8
struct
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
     9
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    10
(* Theory dependencies *)
29183
f1648e009dc1 removed duplicate sum_case used only by function package;
krauss
parents: 25555
diff changeset
    11
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
    12
val sumcase_split_ss = HOL_basic_ss addsimps (@{thm "Product_Type.split"} :: @{thms "sum.cases"})
25555
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    13
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    14
(* top-down access in balanced tree *)
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    15
fun access_top_down {left, right, init} len i =
32765
3032c0308019 modernized Balanced_Tree;
wenzelm
parents: 32603
diff changeset
    16
    Balanced_Tree.access {left = (fn f => f o left), right = (fn f => f o right), init = I} len i init
25555
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    17
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    18
(* Sum types *)
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    19
fun mk_sumT LT RT = Type ("+", [LT, RT])
32603
e08fdd615333 tuned const_name antiquotations
haftmann
parents: 31775
diff changeset
    20
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
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    21
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    22
val App = curry op $
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    23
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    24
fun mk_inj ST n i = 
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    25
    access_top_down 
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    26
    { init = (ST, I : term -> term),
32603
e08fdd615333 tuned const_name antiquotations
haftmann
parents: 31775
diff changeset
    27
      left = (fn (T as Type ("+", [LT, RT]), inj) => (LT, inj o App (Const (@{const_name Inl}, LT --> T)))),
e08fdd615333 tuned const_name antiquotations
haftmann
parents: 31775
diff changeset
    28
      right =(fn (T as Type ("+", [LT, RT]), inj) => (RT, inj o App (Const (@{const_name Inr}, RT --> T))))} n i 
25555
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    29
    |> snd
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    30
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    31
fun mk_proj ST n i = 
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    32
    access_top_down 
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    33
    { init = (ST, I : term -> term),
32603
e08fdd615333 tuned const_name antiquotations
haftmann
parents: 31775
diff changeset
    34
      left = (fn (T as Type ("+", [LT, RT]), proj) => (LT, App (Const (@{const_name Datatype.Projl}, T --> LT)) o proj)),
e08fdd615333 tuned const_name antiquotations
haftmann
parents: 31775
diff changeset
    35
      right =(fn (T as Type ("+", [LT, RT]), proj) => (RT, App (Const (@{const_name Datatype.Projr}, T --> RT)) o proj))} n i
25555
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    36
    |> snd
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    37
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    38
fun mk_sumcases T fs =
32765
3032c0308019 modernized Balanced_Tree;
wenzelm
parents: 32603
diff changeset
    39
      Balanced_Tree.make (fn ((f, fT), (g, gT)) => (mk_sumcase fT gT T f g, mk_sumT fT gT)) 
25555
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    40
                        (map (fn f => (f, domain_type (fastype_of f))) fs)
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    41
      |> fst
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    42
224a40e39457 factored out handling of sum types again
krauss
parents:
diff changeset
    43
end