src/HOL/Tools/Predicate_Compile/predicate_compile_aux.ML
author bulwahn
Tue Feb 23 13:36:15 2010 +0100 (2010-02-23)
changeset 35324 c9f428269b38
parent 35224 1c9866c5f6fb
child 35381 5038f36b5ea1
permissions -rw-r--r--
adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
wenzelm@33265
     1
(*  Title:      HOL/Tools/Predicate_Compile/predicate_compile_aux.ML
wenzelm@33265
     2
    Author:     Lukas Bulwahn, TU Muenchen
bulwahn@33250
     3
wenzelm@33265
     4
Auxilary functions for predicate compiler.
bulwahn@33250
     5
*)
bulwahn@33250
     6
wenzelm@33265
     7
(* FIXME proper signature *)
wenzelm@33265
     8
bulwahn@34948
     9
structure TermGraph = Graph(type key = term val ord = TermOrd.fast_term_ord);
bulwahn@34948
    10
bulwahn@33250
    11
structure Predicate_Compile_Aux =
bulwahn@33250
    12
struct
bulwahn@33250
    13
bulwahn@34948
    14
(* general functions *)
bulwahn@34948
    15
bulwahn@34948
    16
fun apfst3 f (x, y, z) = (f x, y, z)
bulwahn@34948
    17
fun apsnd3 f (x, y, z) = (x, f y, z)
bulwahn@34948
    18
fun aptrd3 f (x, y, z) = (x, y, f z)
bulwahn@34948
    19
bulwahn@34948
    20
fun comb_option f (SOME x1, SOME x2) = SOME (f (x1, x2))
bulwahn@34948
    21
  | comb_option f (NONE, SOME x2) = SOME x2
bulwahn@34948
    22
  | comb_option f (SOME x1, NONE) = SOME x1
bulwahn@34948
    23
  | comb_option f (NONE, NONE) = NONE
bulwahn@34948
    24
bulwahn@34948
    25
fun map2_optional f (x :: xs) (y :: ys) = (f x (SOME y)) :: (map2_optional f xs ys)
bulwahn@34948
    26
  | map2_optional f (x :: xs) [] = (f x NONE) :: (map2_optional f xs [])
bulwahn@34948
    27
  | map2_optional f [] [] = []
bulwahn@34948
    28
bulwahn@34948
    29
fun find_indices f xs =
bulwahn@34948
    30
  map_filter (fn (i, true) => SOME i | (i, false) => NONE) (map_index (apsnd f) xs)
bulwahn@33328
    31
bulwahn@35324
    32
fun assert check = if check then () else error "Assertion failed!"
bulwahn@33328
    33
(* mode *)
bulwahn@33328
    34
bulwahn@34948
    35
datatype mode = Bool | Input | Output | Pair of mode * mode | Fun of mode * mode
bulwahn@33619
    36
bulwahn@33623
    37
(* equality of instantiatedness with respect to equivalences:
bulwahn@33623
    38
  Pair Input Input == Input and Pair Output Output == Output *)
bulwahn@34948
    39
fun eq_mode (Fun (m1, m2), Fun (m3, m4)) = eq_mode (m1, m3) andalso eq_mode (m2, m4)
bulwahn@34948
    40
  | eq_mode (Pair (m1, m2), Pair (m3, m4)) = eq_mode (m1, m3) andalso eq_mode (m2, m4)
bulwahn@34948
    41
  | eq_mode (Pair (m1, m2), Input) = eq_mode (m1, Input) andalso eq_mode (m2, Input)
bulwahn@34948
    42
  | eq_mode (Pair (m1, m2), Output) = eq_mode (m1, Output) andalso eq_mode (m2, Output)
bulwahn@34948
    43
  | eq_mode (Input, Pair (m1, m2)) = eq_mode (Input, m1) andalso eq_mode (Input, m2)
bulwahn@34948
    44
  | eq_mode (Output, Pair (m1, m2)) = eq_mode (Output, m1) andalso eq_mode (Output, m2)
bulwahn@34948
    45
  | eq_mode (Input, Input) = true
bulwahn@34948
    46
  | eq_mode (Output, Output) = true
bulwahn@34948
    47
  | eq_mode (Bool, Bool) = true
bulwahn@34948
    48
  | eq_mode _ = false
bulwahn@33623
    49
bulwahn@33619
    50
(* name: binder_modes? *)
bulwahn@33619
    51
fun strip_fun_mode (Fun (mode, mode')) = mode :: strip_fun_mode mode'
bulwahn@33619
    52
  | strip_fun_mode Bool = []
bulwahn@33619
    53
  | strip_fun_mode _ = error "Bad mode for strip_fun_mode"
bulwahn@33619
    54
bulwahn@33619
    55
fun dest_fun_mode (Fun (mode, mode')) = mode :: dest_fun_mode mode'
bulwahn@33619
    56
  | dest_fun_mode mode = [mode]
bulwahn@33619
    57
bulwahn@33619
    58
fun dest_tuple_mode (Pair (mode, mode')) = mode :: dest_tuple_mode mode'
bulwahn@33619
    59
  | dest_tuple_mode _ = []
bulwahn@33619
    60
bulwahn@35324
    61
bulwahn@35324
    62
fun all_modes_of_typ' (T as Type ("fun", _)) = 
bulwahn@35324
    63
  let
bulwahn@35324
    64
    val (S, U) = strip_type T
bulwahn@35324
    65
  in
bulwahn@35324
    66
    if U = HOLogic.boolT then
bulwahn@35324
    67
      fold_rev (fn m1 => fn m2 => map_product (curry Fun) m1 m2)
bulwahn@35324
    68
        (map all_modes_of_typ' S) [Bool]
bulwahn@35324
    69
    else
bulwahn@35324
    70
      [Input, Output]
bulwahn@35324
    71
  end
bulwahn@35324
    72
  | all_modes_of_typ' (Type ("*", [T1, T2])) = 
bulwahn@35324
    73
    map_product (curry Pair) (all_modes_of_typ' T1) (all_modes_of_typ' T2)
bulwahn@35324
    74
  | all_modes_of_typ' _ = [Input, Output]
bulwahn@35324
    75
bulwahn@35324
    76
fun all_modes_of_typ (T as Type ("fun", _)) =
bulwahn@34948
    77
  let
bulwahn@34948
    78
    val (S, U) = strip_type T
bulwahn@34948
    79
  in
bulwahn@34948
    80
    if U = HOLogic.boolT then
bulwahn@34948
    81
      fold_rev (fn m1 => fn m2 => map_product (curry Fun) m1 m2)
bulwahn@35324
    82
        (map all_modes_of_typ' S) [Bool]
bulwahn@34948
    83
    else
bulwahn@34948
    84
      [Input, Output]
bulwahn@34948
    85
  end
bulwahn@34948
    86
  | all_modes_of_typ (Type ("bool", [])) = [Bool]
bulwahn@35324
    87
  | all_modes_of_typ T = all_modes_of_typ' T
bulwahn@34948
    88
bulwahn@35324
    89
fun all_smodes_of_typ (T as Type ("fun", _)) =
bulwahn@35324
    90
  let
bulwahn@35324
    91
    val (S, U) = strip_type T
bulwahn@35324
    92
    fun all_smodes (Type ("*", [T1, T2])) = 
bulwahn@35324
    93
      map_product (curry Pair) (all_smodes T1) (all_smodes T2)
bulwahn@35324
    94
      | all_smodes _ = [Input, Output]
bulwahn@35324
    95
  in
bulwahn@35324
    96
    if U = HOLogic.boolT then
bulwahn@35324
    97
      fold_rev (fn m1 => fn m2 => map_product (curry Fun) m1 m2) (map all_smodes S) [Bool]
bulwahn@35324
    98
    else
bulwahn@35324
    99
      error "all_smodes_of_typ: invalid type for predicate"
bulwahn@35324
   100
  end
bulwahn@35324
   101
(*
bulwahn@34948
   102
fun extract_params arg =
bulwahn@34948
   103
  case fastype_of arg of
bulwahn@34948
   104
    (T as Type ("fun", _)) =>
bulwahn@34948
   105
      (if (body_type T = HOLogic.boolT) then
bulwahn@34948
   106
        (case arg of
bulwahn@34948
   107
          Free _ => [arg] | _ => error "extract_params: Unexpected term")
bulwahn@34948
   108
      else [])
bulwahn@34948
   109
  | (Type ("*", [T1, T2])) =>
bulwahn@34948
   110
      let
bulwahn@34948
   111
        val (t1, t2) = HOLogic.dest_prod arg
bulwahn@34948
   112
      in
bulwahn@34948
   113
        extract_params t1 @ extract_params t2
bulwahn@34948
   114
      end
bulwahn@34948
   115
  | _ => []
bulwahn@35324
   116
*)
bulwahn@34948
   117
fun ho_arg_modes_of mode =
bulwahn@34948
   118
  let
bulwahn@34948
   119
    fun ho_arg_mode (m as Fun _) =  [m]
bulwahn@34948
   120
      | ho_arg_mode (Pair (m1, m2)) = ho_arg_mode m1 @ ho_arg_mode m2
bulwahn@34948
   121
      | ho_arg_mode _ = []
bulwahn@34948
   122
  in
bulwahn@34948
   123
    maps ho_arg_mode (strip_fun_mode mode)
bulwahn@34948
   124
  end
bulwahn@34948
   125
bulwahn@34948
   126
fun ho_args_of mode ts =
bulwahn@34948
   127
  let
bulwahn@34948
   128
    fun ho_arg (Fun _) (SOME t) = [t]
bulwahn@34948
   129
      | ho_arg (Fun _) NONE = error "ho_arg_of"
bulwahn@34948
   130
      | ho_arg (Pair (m1, m2)) (SOME (Const ("Pair", _) $ t1 $ t2)) =
bulwahn@34948
   131
          ho_arg m1 (SOME t1) @ ho_arg m2 (SOME t2)
bulwahn@34948
   132
      | ho_arg (Pair (m1, m2)) NONE = ho_arg m1 NONE @ ho_arg m2 NONE
bulwahn@34948
   133
      | ho_arg _ _ = []
bulwahn@34948
   134
  in
bulwahn@34948
   135
    flat (map2_optional ho_arg (strip_fun_mode mode) ts)
bulwahn@34948
   136
  end
bulwahn@34948
   137
bulwahn@34948
   138
(* temporary function should be replaced by unsplit_input or so? *)
bulwahn@34948
   139
fun replace_ho_args mode hoargs ts =
bulwahn@34948
   140
  let
bulwahn@34948
   141
    fun replace (Fun _, _) (arg' :: hoargs') = (arg', hoargs')
bulwahn@34948
   142
      | replace (Pair (m1, m2), Const ("Pair", T) $ t1 $ t2) hoargs =
bulwahn@34948
   143
        let
bulwahn@34948
   144
          val (t1', hoargs') = replace (m1, t1) hoargs
bulwahn@34948
   145
          val (t2', hoargs'') = replace (m2, t2) hoargs'
bulwahn@34948
   146
        in
bulwahn@34948
   147
          (Const ("Pair", T) $ t1' $ t2', hoargs'')
bulwahn@34948
   148
        end
bulwahn@34948
   149
      | replace (_, t) hoargs = (t, hoargs)
bulwahn@34948
   150
  in
bulwahn@34948
   151
    fst (fold_map replace ((strip_fun_mode mode) ~~ ts) hoargs)
bulwahn@34948
   152
  end
bulwahn@34948
   153
bulwahn@34948
   154
fun ho_argsT_of mode Ts =
bulwahn@34948
   155
  let
bulwahn@34948
   156
    fun ho_arg (Fun _) T = [T]
bulwahn@34948
   157
      | ho_arg (Pair (m1, m2)) (Type ("*", [T1, T2])) = ho_arg m1 T1 @ ho_arg m2 T2
bulwahn@34948
   158
      | ho_arg _ _ = []
bulwahn@34948
   159
  in
bulwahn@34948
   160
    flat (map2 ho_arg (strip_fun_mode mode) Ts)
bulwahn@34948
   161
  end
bulwahn@34948
   162
bulwahn@34948
   163
(* splits mode and maps function to higher-order argument types *)
bulwahn@34948
   164
fun split_map_mode f mode ts =
bulwahn@34948
   165
  let
bulwahn@34948
   166
    fun split_arg_mode' (m as Fun _) t = f m t
bulwahn@34948
   167
      | split_arg_mode' (Pair (m1, m2)) (Const ("Pair", _) $ t1 $ t2) =
bulwahn@34948
   168
        let
bulwahn@34948
   169
          val (i1, o1) = split_arg_mode' m1 t1
bulwahn@34948
   170
          val (i2, o2) = split_arg_mode' m2 t2
bulwahn@34948
   171
        in
bulwahn@34948
   172
          (comb_option HOLogic.mk_prod (i1, i2), comb_option HOLogic.mk_prod (o1, o2))
bulwahn@34948
   173
        end
bulwahn@35324
   174
      | split_arg_mode' m t =
bulwahn@35324
   175
        if eq_mode (m, Input) then (SOME t, NONE)
bulwahn@35324
   176
        else if eq_mode (m, Output) then (NONE,  SOME t)
bulwahn@35324
   177
        else error "split_map_mode: mode and term do not match"
bulwahn@34948
   178
  in
bulwahn@34948
   179
    (pairself (map_filter I) o split_list) (map2 split_arg_mode' (strip_fun_mode mode) ts)
bulwahn@34948
   180
  end
bulwahn@34948
   181
bulwahn@34948
   182
(* splits mode and maps function to higher-order argument types *)
bulwahn@34948
   183
fun split_map_modeT f mode Ts =
bulwahn@34948
   184
  let
bulwahn@34948
   185
    fun split_arg_mode' (m as Fun _) T = f m T
bulwahn@34948
   186
      | split_arg_mode' (Pair (m1, m2)) (Type ("*", [T1, T2])) =
bulwahn@34948
   187
        let
bulwahn@34948
   188
          val (i1, o1) = split_arg_mode' m1 T1
bulwahn@34948
   189
          val (i2, o2) = split_arg_mode' m2 T2
bulwahn@34948
   190
        in
bulwahn@34948
   191
          (comb_option HOLogic.mk_prodT (i1, i2), comb_option HOLogic.mk_prodT (o1, o2))
bulwahn@34948
   192
        end
bulwahn@34948
   193
      | split_arg_mode' Input T = (SOME T, NONE)
bulwahn@34948
   194
      | split_arg_mode' Output T = (NONE,  SOME T)
bulwahn@34948
   195
      | split_arg_mode' _ _ = error "split_modeT': mode and type do not match"
bulwahn@34948
   196
  in
bulwahn@34948
   197
    (pairself (map_filter I) o split_list) (map2 split_arg_mode' (strip_fun_mode mode) Ts)
bulwahn@34948
   198
  end
bulwahn@34948
   199
bulwahn@34948
   200
fun split_mode mode ts = split_map_mode (fn _ => fn _ => (NONE, NONE)) mode ts
bulwahn@34948
   201
bulwahn@34948
   202
fun fold_map_aterms_prodT comb f (Type ("*", [T1, T2])) s =
bulwahn@34948
   203
  let
bulwahn@34948
   204
    val (x1, s') = fold_map_aterms_prodT comb f T1 s
bulwahn@34948
   205
    val (x2, s'') = fold_map_aterms_prodT comb f T2 s'
bulwahn@34948
   206
  in
bulwahn@34948
   207
    (comb x1 x2, s'')
bulwahn@34948
   208
  end
bulwahn@34948
   209
  | fold_map_aterms_prodT comb f T s = f T s
bulwahn@34948
   210
bulwahn@34948
   211
fun map_filter_prod f (Const ("Pair", _) $ t1 $ t2) =
bulwahn@34948
   212
  comb_option HOLogic.mk_prod (map_filter_prod f t1, map_filter_prod f t2)
bulwahn@34948
   213
  | map_filter_prod f t = f t
bulwahn@34948
   214
bulwahn@34948
   215
(* obviously, split_mode' and split_modeT' do not match? where does that cause problems? *)
bulwahn@34948
   216
  
bulwahn@34948
   217
fun split_modeT' mode Ts =
bulwahn@34948
   218
  let
bulwahn@34948
   219
    fun split_arg_mode' (Fun _) T = ([], [])
bulwahn@34948
   220
      | split_arg_mode' (Pair (m1, m2)) (Type ("*", [T1, T2])) =
bulwahn@34948
   221
        let
bulwahn@34948
   222
          val (i1, o1) = split_arg_mode' m1 T1
bulwahn@34948
   223
          val (i2, o2) = split_arg_mode' m2 T2
bulwahn@34948
   224
        in
bulwahn@34948
   225
          (i1 @ i2, o1 @ o2)
bulwahn@34948
   226
        end
bulwahn@34948
   227
      | split_arg_mode' Input T = ([T], [])
bulwahn@34948
   228
      | split_arg_mode' Output T = ([], [T])
bulwahn@34948
   229
      | split_arg_mode' _ _ = error "split_modeT': mode and type do not match"
bulwahn@34948
   230
  in
bulwahn@34948
   231
    (pairself flat o split_list) (map2 split_arg_mode' (strip_fun_mode mode) Ts)
bulwahn@34948
   232
  end
bulwahn@34948
   233
bulwahn@34948
   234
fun string_of_mode mode =
bulwahn@33619
   235
  let
bulwahn@33619
   236
    fun string_of_mode1 Input = "i"
bulwahn@33619
   237
      | string_of_mode1 Output = "o"
bulwahn@33619
   238
      | string_of_mode1 Bool = "bool"
bulwahn@33619
   239
      | string_of_mode1 mode = "(" ^ (string_of_mode3 mode) ^ ")"
bulwahn@33626
   240
    and string_of_mode2 (Pair (m1, m2)) = string_of_mode3 m1 ^ " * " ^  string_of_mode2 m2
bulwahn@33619
   241
      | string_of_mode2 mode = string_of_mode1 mode
bulwahn@33619
   242
    and string_of_mode3 (Fun (m1, m2)) = string_of_mode2 m1 ^ " => " ^ string_of_mode3 m2
bulwahn@33619
   243
      | string_of_mode3 mode = string_of_mode2 mode
bulwahn@34948
   244
  in string_of_mode3 mode end
bulwahn@33619
   245
bulwahn@34948
   246
fun ascii_string_of_mode mode' =
bulwahn@33626
   247
  let
bulwahn@33626
   248
    fun ascii_string_of_mode' Input = "i"
bulwahn@33626
   249
      | ascii_string_of_mode' Output = "o"
bulwahn@33626
   250
      | ascii_string_of_mode' Bool = "b"
bulwahn@33626
   251
      | ascii_string_of_mode' (Pair (m1, m2)) =
bulwahn@33626
   252
          "P" ^ ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Pair m2
bulwahn@33626
   253
      | ascii_string_of_mode' (Fun (m1, m2)) = 
bulwahn@33626
   254
          "F" ^ ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Fun m2 ^ "B"
bulwahn@33626
   255
    and ascii_string_of_mode'_Fun (Fun (m1, m2)) =
bulwahn@33626
   256
          ascii_string_of_mode' m1 ^ (if m2 = Bool then "" else "_" ^ ascii_string_of_mode'_Fun m2)
bulwahn@33626
   257
      | ascii_string_of_mode'_Fun Bool = "B"
bulwahn@33626
   258
      | ascii_string_of_mode'_Fun m = ascii_string_of_mode' m
bulwahn@33626
   259
    and ascii_string_of_mode'_Pair (Pair (m1, m2)) =
bulwahn@33626
   260
          ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Pair m2
bulwahn@33626
   261
      | ascii_string_of_mode'_Pair m = ascii_string_of_mode' m
bulwahn@33626
   262
  in ascii_string_of_mode'_Fun mode' end
bulwahn@33626
   263
bulwahn@34948
   264
(* premises *)
bulwahn@33619
   265
bulwahn@34948
   266
datatype indprem = Prem of term | Negprem of term | Sidecond of term
bulwahn@34948
   267
  | Generator of (string * typ);
bulwahn@33619
   268
bulwahn@33250
   269
(* general syntactic functions *)
bulwahn@33250
   270
bulwahn@33250
   271
(*Like dest_conj, but flattens conjunctions however nested*)
bulwahn@33250
   272
fun conjuncts_aux (Const ("op &", _) $ t $ t') conjs = conjuncts_aux t (conjuncts_aux t' conjs)
bulwahn@33250
   273
  | conjuncts_aux t conjs = t::conjs;
bulwahn@33250
   274
bulwahn@33250
   275
fun conjuncts t = conjuncts_aux t [];
bulwahn@33250
   276
bulwahn@33250
   277
fun is_equationlike_term (Const ("==", _) $ _ $ _) = true
bulwahn@33250
   278
  | is_equationlike_term (Const ("Trueprop", _) $ (Const ("op =", _) $ _ $ _)) = true
bulwahn@33250
   279
  | is_equationlike_term _ = false
bulwahn@33250
   280
  
bulwahn@33250
   281
val is_equationlike = is_equationlike_term o prop_of 
bulwahn@33250
   282
bulwahn@33250
   283
fun is_pred_equation_term (Const ("==", _) $ u $ v) =
bulwahn@33250
   284
  (fastype_of u = @{typ bool}) andalso (fastype_of v = @{typ bool})
bulwahn@33250
   285
  | is_pred_equation_term _ = false
bulwahn@33250
   286
  
bulwahn@33250
   287
val is_pred_equation = is_pred_equation_term o prop_of 
bulwahn@33250
   288
bulwahn@33250
   289
fun is_intro_term constname t =
bulwahn@34948
   290
  the_default false (try (fn t => case fst (strip_comb (HOLogic.dest_Trueprop (Logic.strip_imp_concl t))) of
bulwahn@33250
   291
    Const (c, _) => c = constname
bulwahn@34948
   292
  | _ => false) t)
bulwahn@33250
   293
  
bulwahn@33250
   294
fun is_intro constname t = is_intro_term constname (prop_of t)
bulwahn@33250
   295
bulwahn@33250
   296
fun is_pred thy constname =
bulwahn@33250
   297
  let
bulwahn@33250
   298
    val T = (Sign.the_const_type thy constname)
bulwahn@33250
   299
  in body_type T = @{typ "bool"} end;
bulwahn@33250
   300
bulwahn@33250
   301
fun is_predT (T as Type("fun", [_, _])) = (snd (strip_type T) = HOLogic.boolT)
bulwahn@33250
   302
  | is_predT _ = false
bulwahn@33250
   303
bulwahn@33250
   304
(*** check if a term contains only constructor functions ***)
bulwahn@34948
   305
(* TODO: another copy in the core! *)
bulwahn@33623
   306
(* FIXME: constructor terms are supposed to be seen in the way the code generator
bulwahn@33623
   307
  sees constructors.*)
bulwahn@33250
   308
fun is_constrt thy =
bulwahn@33250
   309
  let
bulwahn@33250
   310
    val cnstrs = flat (maps
bulwahn@33250
   311
      (map (fn (_, (Tname, _, cs)) => map (apsnd (rpair Tname o length)) cs) o #descr o snd)
bulwahn@33250
   312
      (Symtab.dest (Datatype.get_all thy)));
bulwahn@33250
   313
    fun check t = (case strip_comb t of
bulwahn@33250
   314
        (Free _, []) => true
bulwahn@33250
   315
      | (Const (s, T), ts) => (case (AList.lookup (op =) cnstrs s, body_type T) of
bulwahn@33250
   316
            (SOME (i, Tname), Type (Tname', _)) => length ts = i andalso Tname = Tname' andalso forall check ts
bulwahn@33250
   317
          | _ => false)
bulwahn@33250
   318
      | _ => false)
bulwahn@33250
   319
  in check end;  
bulwahn@34948
   320
bulwahn@34948
   321
fun is_funtype (Type ("fun", [_, _])) = true
bulwahn@34948
   322
  | is_funtype _ = false;
bulwahn@34948
   323
bulwahn@34948
   324
fun is_Type (Type _) = true
bulwahn@34948
   325
  | is_Type _ = false
bulwahn@34948
   326
bulwahn@34948
   327
(* returns true if t is an application of an datatype constructor *)
bulwahn@34948
   328
(* which then consequently would be splitted *)
bulwahn@34948
   329
(* else false *)
bulwahn@34948
   330
(*
bulwahn@34948
   331
fun is_constructor thy t =
bulwahn@34948
   332
  if (is_Type (fastype_of t)) then
bulwahn@34948
   333
    (case DatatypePackage.get_datatype thy ((fst o dest_Type o fastype_of) t) of
bulwahn@34948
   334
      NONE => false
bulwahn@34948
   335
    | SOME info => (let
bulwahn@34948
   336
      val constr_consts = maps (fn (_, (_, _, constrs)) => map fst constrs) (#descr info)
bulwahn@34948
   337
      val (c, _) = strip_comb t
bulwahn@34948
   338
      in (case c of
bulwahn@34948
   339
        Const (name, _) => name mem_string constr_consts
bulwahn@34948
   340
        | _ => false) end))
bulwahn@34948
   341
  else false
bulwahn@34948
   342
*)
bulwahn@34948
   343
haftmann@35224
   344
val is_constr = Code.is_constr;
bulwahn@34948
   345
bulwahn@33250
   346
fun strip_ex (Const ("Ex", _) $ Abs (x, T, t)) =
bulwahn@33250
   347
  let
bulwahn@33250
   348
    val (xTs, t') = strip_ex t
bulwahn@33250
   349
  in
bulwahn@33250
   350
    ((x, T) :: xTs, t')
bulwahn@33250
   351
  end
bulwahn@33250
   352
  | strip_ex t = ([], t)
bulwahn@33250
   353
bulwahn@33250
   354
fun focus_ex t nctxt =
bulwahn@33250
   355
  let
bulwahn@33250
   356
    val ((xs, Ts), t') = apfst split_list (strip_ex t) 
bulwahn@33250
   357
    val (xs', nctxt') = Name.variants xs nctxt;
bulwahn@33250
   358
    val ps' = xs' ~~ Ts;
bulwahn@33250
   359
    val vs = map Free ps';
bulwahn@33250
   360
    val t'' = Term.subst_bounds (rev vs, t');
bulwahn@33250
   361
  in ((ps', t''), nctxt') end;
bulwahn@33250
   362
bulwahn@33250
   363
(* introduction rule combinators *)
bulwahn@33250
   364
bulwahn@33250
   365
(* combinators to apply a function to all literals of an introduction rules *)
bulwahn@33250
   366
bulwahn@33250
   367
fun map_atoms f intro = 
bulwahn@33250
   368
  let
bulwahn@33250
   369
    val (literals, head) = Logic.strip_horn intro
bulwahn@33250
   370
    fun appl t = (case t of
bulwahn@33250
   371
        (@{term "Not"} $ t') => HOLogic.mk_not (f t')
bulwahn@33250
   372
      | _ => f t)
bulwahn@33250
   373
  in
bulwahn@33250
   374
    Logic.list_implies
bulwahn@33250
   375
      (map (HOLogic.mk_Trueprop o appl o HOLogic.dest_Trueprop) literals, head)
bulwahn@33250
   376
  end
bulwahn@33250
   377
bulwahn@33250
   378
fun fold_atoms f intro s =
bulwahn@33250
   379
  let
bulwahn@33250
   380
    val (literals, head) = Logic.strip_horn intro
bulwahn@33250
   381
    fun appl t s = (case t of
bulwahn@33250
   382
      (@{term "Not"} $ t') => f t' s
bulwahn@33250
   383
      | _ => f t s)
bulwahn@33250
   384
  in fold appl (map HOLogic.dest_Trueprop literals) s end
bulwahn@33250
   385
bulwahn@33250
   386
fun fold_map_atoms f intro s =
bulwahn@33250
   387
  let
bulwahn@33250
   388
    val (literals, head) = Logic.strip_horn intro
bulwahn@33250
   389
    fun appl t s = (case t of
bulwahn@33250
   390
      (@{term "Not"} $ t') => apfst HOLogic.mk_not (f t' s)
bulwahn@33250
   391
      | _ => f t s)
bulwahn@33250
   392
    val (literals', s') = fold_map appl (map HOLogic.dest_Trueprop literals) s
bulwahn@33250
   393
  in
bulwahn@33250
   394
    (Logic.list_implies (map HOLogic.mk_Trueprop literals', head), s')
bulwahn@33250
   395
  end;
bulwahn@33250
   396
bulwahn@33250
   397
fun maps_premises f intro =
bulwahn@33250
   398
  let
bulwahn@33250
   399
    val (premises, head) = Logic.strip_horn intro
bulwahn@33250
   400
  in
bulwahn@33250
   401
    Logic.list_implies (maps f premises, head)
bulwahn@33250
   402
  end
bulwahn@35324
   403
bulwahn@35324
   404
bulwahn@35324
   405
(* split theorems of case expressions *)
bulwahn@35324
   406
bulwahn@35324
   407
(*
bulwahn@35324
   408
fun has_split_rule_cname @{const_name "nat_case"} = true
bulwahn@35324
   409
  | has_split_rule_cname @{const_name "list_case"} = true
bulwahn@35324
   410
  | has_split_rule_cname _ = false
bulwahn@33250
   411
  
bulwahn@35324
   412
fun has_split_rule_term thy (Const (@{const_name "nat_case"}, _)) = true 
bulwahn@35324
   413
  | has_split_rule_term thy (Const (@{const_name "list_case"}, _)) = true 
bulwahn@35324
   414
  | has_split_rule_term thy _ = false
bulwahn@35324
   415
bulwahn@35324
   416
fun has_split_rule_term' thy (Const (@{const_name "If"}, _)) = true
bulwahn@35324
   417
  | has_split_rule_term' thy (Const (@{const_name "Let"}, _)) = true
bulwahn@35324
   418
  | has_split_rule_term' thy c = has_split_rule_term thy c
bulwahn@35324
   419
bulwahn@35324
   420
*)
bulwahn@35324
   421
fun prepare_split_thm ctxt split_thm =
bulwahn@35324
   422
    (split_thm RS @{thm iffD2})
bulwahn@35324
   423
    |> LocalDefs.unfold ctxt [@{thm atomize_conjL[symmetric]},
bulwahn@35324
   424
      @{thm atomize_all[symmetric]}, @{thm atomize_imp[symmetric]}]
bulwahn@35324
   425
bulwahn@35324
   426
fun find_split_thm thy (Const (name, typ)) =
bulwahn@35324
   427
  let
bulwahn@35324
   428
    fun split_name str =
bulwahn@35324
   429
      case first_field "." str
bulwahn@35324
   430
        of (SOME (field, rest)) => field :: split_name rest
bulwahn@35324
   431
         | NONE => [str]
bulwahn@35324
   432
    val splitted_name = split_name name
bulwahn@35324
   433
  in
bulwahn@35324
   434
    if length splitted_name > 0 andalso
bulwahn@35324
   435
       String.isSuffix "_case" (List.last splitted_name)
bulwahn@35324
   436
    then
bulwahn@35324
   437
      (List.take (splitted_name, length splitted_name - 1)) @ ["split"]
bulwahn@35324
   438
      |> space_implode "."
bulwahn@35324
   439
      |> PureThy.get_thm thy
bulwahn@35324
   440
      |> SOME
bulwahn@35324
   441
      handle ERROR msg => NONE
bulwahn@35324
   442
    else NONE
bulwahn@35324
   443
  end
bulwahn@35324
   444
  | find_split_thm _ _ = NONE
bulwahn@35324
   445
bulwahn@35324
   446
bulwahn@35324
   447
(* TODO: split rules for let and if are different *)
bulwahn@35324
   448
fun find_split_thm' thy (Const (@{const_name "If"}, _)) = SOME @{thm split_if}
bulwahn@35324
   449
  | find_split_thm' thy (Const (@{const_name "Let"}, _)) = SOME @{thm refl} (* TODO *)
bulwahn@35324
   450
  | find_split_thm' thy c = find_split_thm thy c
bulwahn@35324
   451
bulwahn@35324
   452
fun has_split_thm thy t = is_some (find_split_thm thy t)
bulwahn@35324
   453
bulwahn@35324
   454
fun strip_all t = (Term.strip_all_vars t, Term.strip_all_body t)
bulwahn@35324
   455
bulwahn@35324
   456
bulwahn@33250
   457
(* lifting term operations to theorems *)
bulwahn@33250
   458
bulwahn@33250
   459
fun map_term thy f th =
bulwahn@33250
   460
  Skip_Proof.make_thm thy (f (prop_of th))
bulwahn@33250
   461
bulwahn@33250
   462
(*
bulwahn@33250
   463
fun equals_conv lhs_cv rhs_cv ct =
bulwahn@33250
   464
  case Thm.term_of ct of
bulwahn@33250
   465
    Const ("==", _) $ _ $ _ => Conv.arg_conv cv ct  
bulwahn@33250
   466
  | _ => error "equals_conv"  
bulwahn@33250
   467
*)
bulwahn@33250
   468
bulwahn@33250
   469
(* Different options for compiler *)
bulwahn@33250
   470
bulwahn@35324
   471
datatype compilation = Pred | Random | Depth_Limited | DSeq | Annotated
bulwahn@35324
   472
  | Pos_Random_DSeq | Neg_Random_DSeq
bulwahn@35324
   473
bulwahn@35324
   474
bulwahn@35324
   475
fun negative_compilation_of Pos_Random_DSeq = Neg_Random_DSeq
bulwahn@35324
   476
  | negative_compilation_of Neg_Random_DSeq = Pos_Random_DSeq
bulwahn@35324
   477
  | negative_compilation_of c = c
bulwahn@35324
   478
  
bulwahn@35324
   479
fun compilation_for_polarity false Pos_Random_DSeq = Neg_Random_DSeq
bulwahn@35324
   480
  | compilation_for_polarity _ c = c
bulwahn@34948
   481
bulwahn@34948
   482
fun string_of_compilation c = case c of
bulwahn@34948
   483
    Pred => ""
bulwahn@34948
   484
  | Random => "random"
bulwahn@34948
   485
  | Depth_Limited => "depth limited"
bulwahn@34948
   486
  | DSeq => "dseq"
bulwahn@34948
   487
  | Annotated => "annotated"
bulwahn@35324
   488
  | Pos_Random_DSeq => "pos_random dseq"
bulwahn@35324
   489
  | Neg_Random_DSeq => "neg_random_dseq"
bulwahn@35324
   490
  
bulwahn@34948
   491
(*datatype compilation_options =
bulwahn@34948
   492
  Pred | Random of int | Depth_Limited of int | DSeq of int | Annotated*)
bulwahn@34948
   493
bulwahn@33250
   494
datatype options = Options of {  
bulwahn@34948
   495
  expected_modes : (string * mode list) option,
bulwahn@34948
   496
  proposed_modes : (string * mode list) option,
bulwahn@34948
   497
  proposed_names : ((string * mode) * string) list,
bulwahn@33250
   498
  show_steps : bool,
bulwahn@33250
   499
  show_proof_trace : bool,
bulwahn@33250
   500
  show_intermediate_results : bool,
bulwahn@33251
   501
  show_mode_inference : bool,
bulwahn@33251
   502
  show_modes : bool,
bulwahn@33250
   503
  show_compilation : bool,
bulwahn@35324
   504
  show_caught_failures : bool,
bulwahn@33250
   505
  skip_proof : bool,
bulwahn@35324
   506
  no_topmost_reordering : bool,
bulwahn@35324
   507
  function_flattening : bool,
bulwahn@35324
   508
  fail_safe_function_flattening : bool,
bulwahn@35324
   509
  no_higher_order_predicate : string list,
bulwahn@33250
   510
  inductify : bool,
bulwahn@34948
   511
  compilation : compilation
bulwahn@33250
   512
};
bulwahn@33250
   513
bulwahn@33250
   514
fun expected_modes (Options opt) = #expected_modes opt
bulwahn@33752
   515
fun proposed_modes (Options opt) = #proposed_modes opt
bulwahn@34948
   516
fun proposed_names (Options opt) name mode = AList.lookup (eq_pair (op =) eq_mode)
bulwahn@33623
   517
  (#proposed_names opt) (name, mode)
bulwahn@33620
   518
bulwahn@33250
   519
fun show_steps (Options opt) = #show_steps opt
bulwahn@33250
   520
fun show_intermediate_results (Options opt) = #show_intermediate_results opt
bulwahn@33250
   521
fun show_proof_trace (Options opt) = #show_proof_trace opt
bulwahn@33251
   522
fun show_modes (Options opt) = #show_modes opt
bulwahn@33251
   523
fun show_mode_inference (Options opt) = #show_mode_inference opt
bulwahn@33250
   524
fun show_compilation (Options opt) = #show_compilation opt
bulwahn@35324
   525
fun show_caught_failures (Options opt) = #show_caught_failures opt
bulwahn@35324
   526
bulwahn@33250
   527
fun skip_proof (Options opt) = #skip_proof opt
bulwahn@33250
   528
bulwahn@35324
   529
fun function_flattening (Options opt) = #function_flattening opt
bulwahn@35324
   530
fun fail_safe_function_flattening (Options opt) = #fail_safe_function_flattening opt
bulwahn@35324
   531
fun no_topmost_reordering (Options opt) = #no_topmost_reordering opt
bulwahn@35324
   532
fun no_higher_order_predicate (Options opt) = #no_higher_order_predicate opt
bulwahn@35324
   533
bulwahn@33250
   534
fun is_inductify (Options opt) = #inductify opt
bulwahn@34948
   535
bulwahn@34948
   536
fun compilation (Options opt) = #compilation opt
bulwahn@33250
   537
bulwahn@33250
   538
val default_options = Options {
bulwahn@33250
   539
  expected_modes = NONE,
bulwahn@33752
   540
  proposed_modes = NONE,
bulwahn@33623
   541
  proposed_names = [],
bulwahn@33250
   542
  show_steps = false,
bulwahn@33250
   543
  show_intermediate_results = false,
bulwahn@33250
   544
  show_proof_trace = false,
bulwahn@33251
   545
  show_modes = false,
bulwahn@33250
   546
  show_mode_inference = false,
bulwahn@33250
   547
  show_compilation = false,
bulwahn@35324
   548
  show_caught_failures = false,
bulwahn@34948
   549
  skip_proof = true,
bulwahn@35324
   550
  no_topmost_reordering = false,
bulwahn@35324
   551
  function_flattening = false,
bulwahn@35324
   552
  fail_safe_function_flattening = false,
bulwahn@35324
   553
  no_higher_order_predicate = [],
bulwahn@33250
   554
  inductify = false,
bulwahn@34948
   555
  compilation = Pred
bulwahn@33250
   556
}
bulwahn@33250
   557
bulwahn@34948
   558
val bool_options = ["show_steps", "show_intermediate_results", "show_proof_trace", "show_modes",
bulwahn@35324
   559
  "show_mode_inference", "show_compilation", "skip_proof", "inductify", "no_function_flattening"]
bulwahn@34948
   560
bulwahn@34948
   561
val compilation_names = [("pred", Pred),
bulwahn@34948
   562
  (*("random", Random), ("depth_limited", Depth_Limited), ("annotated", Annotated),*)
bulwahn@35324
   563
  ("dseq", DSeq), ("random_dseq", Pos_Random_DSeq)]
bulwahn@33250
   564
bulwahn@33250
   565
fun print_step options s =
bulwahn@33250
   566
  if show_steps options then tracing s else ()
bulwahn@33250
   567
bulwahn@33250
   568
(* tuple processing *)
bulwahn@33250
   569
bulwahn@33250
   570
fun expand_tuples thy intro =
bulwahn@33250
   571
  let
bulwahn@33250
   572
    fun rewrite_args [] (pats, intro_t, ctxt) = (pats, intro_t, ctxt)
bulwahn@33250
   573
      | rewrite_args (arg::args) (pats, intro_t, ctxt) = 
bulwahn@33250
   574
      (case HOLogic.strip_tupleT (fastype_of arg) of
bulwahn@33250
   575
        (Ts as _ :: _ :: _) =>
bulwahn@33250
   576
        let
bulwahn@33250
   577
          fun rewrite_arg' (Const ("Pair", _) $ _ $ t2, Type ("*", [_, T2]))
bulwahn@33250
   578
            (args, (pats, intro_t, ctxt)) = rewrite_arg' (t2, T2) (args, (pats, intro_t, ctxt))
bulwahn@33250
   579
            | rewrite_arg' (t, Type ("*", [T1, T2])) (args, (pats, intro_t, ctxt)) =
bulwahn@33250
   580
              let
bulwahn@33250
   581
                val ([x, y], ctxt') = Variable.variant_fixes ["x", "y"] ctxt
bulwahn@33250
   582
                val pat = (t, HOLogic.mk_prod (Free (x, T1), Free (y, T2)))
bulwahn@33250
   583
                val intro_t' = Pattern.rewrite_term thy [pat] [] intro_t
bulwahn@33250
   584
                val args' = map (Pattern.rewrite_term thy [pat] []) args
bulwahn@33250
   585
              in
bulwahn@33250
   586
                rewrite_arg' (Free (y, T2), T2) (args', (pat::pats, intro_t', ctxt'))
bulwahn@33250
   587
              end
bulwahn@33250
   588
            | rewrite_arg' _ (args, (pats, intro_t, ctxt)) = (args, (pats, intro_t, ctxt))
bulwahn@33250
   589
          val (args', (pats, intro_t', ctxt')) = rewrite_arg' (arg, fastype_of arg)
bulwahn@33250
   590
            (args, (pats, intro_t, ctxt))
bulwahn@33250
   591
        in
bulwahn@33250
   592
          rewrite_args args' (pats, intro_t', ctxt')
bulwahn@33250
   593
        end
bulwahn@33250
   594
      | _ => rewrite_args args (pats, intro_t, ctxt))
bulwahn@33250
   595
    fun rewrite_prem atom =
bulwahn@33250
   596
      let
bulwahn@33250
   597
        val (_, args) = strip_comb atom
bulwahn@33250
   598
      in rewrite_args args end
bulwahn@33250
   599
    val ctxt = ProofContext.init thy
bulwahn@33250
   600
    val (((T_insts, t_insts), [intro']), ctxt1) = Variable.import false [intro] ctxt
bulwahn@33250
   601
    val intro_t = prop_of intro'
bulwahn@33250
   602
    val concl = Logic.strip_imp_concl intro_t
bulwahn@33250
   603
    val (p, args) = strip_comb (HOLogic.dest_Trueprop concl)
bulwahn@33250
   604
    val (pats', intro_t', ctxt2) = rewrite_args args ([], intro_t, ctxt1)
bulwahn@33250
   605
    val (pats', intro_t', ctxt3) = 
bulwahn@33250
   606
      fold_atoms rewrite_prem intro_t' (pats', intro_t', ctxt2)
bulwahn@33250
   607
    fun rewrite_pat (ct1, ct2) =
bulwahn@33250
   608
      (ct1, cterm_of thy (Pattern.rewrite_term thy pats' [] (term_of ct2)))
bulwahn@33250
   609
    val t_insts' = map rewrite_pat t_insts
bulwahn@33250
   610
    val intro'' = Thm.instantiate (T_insts, t_insts') intro
bulwahn@33250
   611
    val [intro'''] = Variable.export ctxt3 ctxt [intro'']
bulwahn@33250
   612
    val intro'''' = Simplifier.full_simplify
bulwahn@33250
   613
      (HOL_basic_ss addsimps [@{thm fst_conv}, @{thm snd_conv}, @{thm Pair_eq}])
bulwahn@33250
   614
      intro'''
bulwahn@33250
   615
    (* splitting conjunctions introduced by Pair_eq*)
bulwahn@33250
   616
    fun split_conj prem =
bulwahn@33250
   617
      map HOLogic.mk_Trueprop (conjuncts (HOLogic.dest_Trueprop prem))
bulwahn@33250
   618
    val intro''''' = map_term thy (maps_premises split_conj) intro''''
bulwahn@33250
   619
  in
bulwahn@33250
   620
    intro'''''
bulwahn@33250
   621
  end
bulwahn@33250
   622
bulwahn@33250
   623
end;