src/HOL/Tools/Predicate_Compile/predicate_compile_aux.ML
author bulwahn
Wed Jan 20 11:56:45 2010 +0100 (2010-01-20)
changeset 34948 2d5f2a9f7601
parent 33752 9aa8e961f850
child 35224 1c9866c5f6fb
permissions -rw-r--r--
refactoring the predicate compiler; adding theories for Sequences; adding retrieval to Spec_Rules; adding timing to Quickcheck
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@33328
    32
(* mode *)
bulwahn@33328
    33
bulwahn@34948
    34
datatype mode = Bool | Input | Output | Pair of mode * mode | Fun of mode * mode
bulwahn@33619
    35
bulwahn@33623
    36
(* equality of instantiatedness with respect to equivalences:
bulwahn@33623
    37
  Pair Input Input == Input and Pair Output Output == Output *)
bulwahn@34948
    38
fun eq_mode (Fun (m1, m2), Fun (m3, m4)) = eq_mode (m1, m3) andalso eq_mode (m2, m4)
bulwahn@34948
    39
  | eq_mode (Pair (m1, m2), Pair (m3, m4)) = eq_mode (m1, m3) andalso eq_mode (m2, m4)
bulwahn@34948
    40
  | eq_mode (Pair (m1, m2), Input) = eq_mode (m1, Input) andalso eq_mode (m2, Input)
bulwahn@34948
    41
  | eq_mode (Pair (m1, m2), Output) = eq_mode (m1, Output) andalso eq_mode (m2, Output)
bulwahn@34948
    42
  | eq_mode (Input, Pair (m1, m2)) = eq_mode (Input, m1) andalso eq_mode (Input, m2)
bulwahn@34948
    43
  | eq_mode (Output, Pair (m1, m2)) = eq_mode (Output, m1) andalso eq_mode (Output, m2)
bulwahn@34948
    44
  | eq_mode (Input, Input) = true
bulwahn@34948
    45
  | eq_mode (Output, Output) = true
bulwahn@34948
    46
  | eq_mode (Bool, Bool) = true
bulwahn@34948
    47
  | eq_mode _ = false
bulwahn@33623
    48
bulwahn@33619
    49
(* name: binder_modes? *)
bulwahn@33619
    50
fun strip_fun_mode (Fun (mode, mode')) = mode :: strip_fun_mode mode'
bulwahn@33619
    51
  | strip_fun_mode Bool = []
bulwahn@33619
    52
  | strip_fun_mode _ = error "Bad mode for strip_fun_mode"
bulwahn@33619
    53
bulwahn@33619
    54
fun dest_fun_mode (Fun (mode, mode')) = mode :: dest_fun_mode mode'
bulwahn@33619
    55
  | dest_fun_mode mode = [mode]
bulwahn@33619
    56
bulwahn@33619
    57
fun dest_tuple_mode (Pair (mode, mode')) = mode :: dest_tuple_mode mode'
bulwahn@33619
    58
  | dest_tuple_mode _ = []
bulwahn@33619
    59
bulwahn@34948
    60
fun all_modes_of_typ (T as Type ("fun", _)) = 
bulwahn@34948
    61
  let
bulwahn@34948
    62
    val (S, U) = strip_type T
bulwahn@34948
    63
  in
bulwahn@34948
    64
    if U = HOLogic.boolT then
bulwahn@34948
    65
      fold_rev (fn m1 => fn m2 => map_product (curry Fun) m1 m2)
bulwahn@34948
    66
        (map all_modes_of_typ S) [Bool]
bulwahn@34948
    67
    else
bulwahn@34948
    68
      [Input, Output]
bulwahn@34948
    69
  end
bulwahn@34948
    70
  | all_modes_of_typ (Type ("*", [T1, T2])) = 
bulwahn@34948
    71
    map_product (curry Pair) (all_modes_of_typ T1) (all_modes_of_typ T2)
bulwahn@34948
    72
  | all_modes_of_typ (Type ("bool", [])) = [Bool]
bulwahn@34948
    73
  | all_modes_of_typ _ = [Input, Output]
bulwahn@34948
    74
bulwahn@34948
    75
fun extract_params arg =
bulwahn@34948
    76
  case fastype_of arg of
bulwahn@34948
    77
    (T as Type ("fun", _)) =>
bulwahn@34948
    78
      (if (body_type T = HOLogic.boolT) then
bulwahn@34948
    79
        (case arg of
bulwahn@34948
    80
          Free _ => [arg] | _ => error "extract_params: Unexpected term")
bulwahn@34948
    81
      else [])
bulwahn@34948
    82
  | (Type ("*", [T1, T2])) =>
bulwahn@34948
    83
      let
bulwahn@34948
    84
        val (t1, t2) = HOLogic.dest_prod arg
bulwahn@34948
    85
      in
bulwahn@34948
    86
        extract_params t1 @ extract_params t2
bulwahn@34948
    87
      end
bulwahn@34948
    88
  | _ => []
bulwahn@34948
    89
bulwahn@34948
    90
fun ho_arg_modes_of mode =
bulwahn@34948
    91
  let
bulwahn@34948
    92
    fun ho_arg_mode (m as Fun _) =  [m]
bulwahn@34948
    93
      | ho_arg_mode (Pair (m1, m2)) = ho_arg_mode m1 @ ho_arg_mode m2
bulwahn@34948
    94
      | ho_arg_mode _ = []
bulwahn@34948
    95
  in
bulwahn@34948
    96
    maps ho_arg_mode (strip_fun_mode mode)
bulwahn@34948
    97
  end
bulwahn@34948
    98
bulwahn@34948
    99
fun ho_args_of mode ts =
bulwahn@34948
   100
  let
bulwahn@34948
   101
    fun ho_arg (Fun _) (SOME t) = [t]
bulwahn@34948
   102
      | ho_arg (Fun _) NONE = error "ho_arg_of"
bulwahn@34948
   103
      | ho_arg (Pair (m1, m2)) (SOME (Const ("Pair", _) $ t1 $ t2)) =
bulwahn@34948
   104
          ho_arg m1 (SOME t1) @ ho_arg m2 (SOME t2)
bulwahn@34948
   105
      | ho_arg (Pair (m1, m2)) NONE = ho_arg m1 NONE @ ho_arg m2 NONE
bulwahn@34948
   106
      | ho_arg _ _ = []
bulwahn@34948
   107
  in
bulwahn@34948
   108
    flat (map2_optional ho_arg (strip_fun_mode mode) ts)
bulwahn@34948
   109
  end
bulwahn@34948
   110
bulwahn@34948
   111
(* temporary function should be replaced by unsplit_input or so? *)
bulwahn@34948
   112
fun replace_ho_args mode hoargs ts =
bulwahn@34948
   113
  let
bulwahn@34948
   114
    fun replace (Fun _, _) (arg' :: hoargs') = (arg', hoargs')
bulwahn@34948
   115
      | replace (Pair (m1, m2), Const ("Pair", T) $ t1 $ t2) hoargs =
bulwahn@34948
   116
        let
bulwahn@34948
   117
          val (t1', hoargs') = replace (m1, t1) hoargs
bulwahn@34948
   118
          val (t2', hoargs'') = replace (m2, t2) hoargs'
bulwahn@34948
   119
        in
bulwahn@34948
   120
          (Const ("Pair", T) $ t1' $ t2', hoargs'')
bulwahn@34948
   121
        end
bulwahn@34948
   122
      | replace (_, t) hoargs = (t, hoargs)
bulwahn@34948
   123
  in
bulwahn@34948
   124
    fst (fold_map replace ((strip_fun_mode mode) ~~ ts) hoargs)
bulwahn@34948
   125
  end
bulwahn@34948
   126
bulwahn@34948
   127
fun ho_argsT_of mode Ts =
bulwahn@34948
   128
  let
bulwahn@34948
   129
    fun ho_arg (Fun _) T = [T]
bulwahn@34948
   130
      | ho_arg (Pair (m1, m2)) (Type ("*", [T1, T2])) = ho_arg m1 T1 @ ho_arg m2 T2
bulwahn@34948
   131
      | ho_arg _ _ = []
bulwahn@34948
   132
  in
bulwahn@34948
   133
    flat (map2 ho_arg (strip_fun_mode mode) Ts)
bulwahn@34948
   134
  end
bulwahn@34948
   135
bulwahn@34948
   136
(* splits mode and maps function to higher-order argument types *)
bulwahn@34948
   137
fun split_map_mode f mode ts =
bulwahn@34948
   138
  let
bulwahn@34948
   139
    fun split_arg_mode' (m as Fun _) t = f m t
bulwahn@34948
   140
      | split_arg_mode' (Pair (m1, m2)) (Const ("Pair", _) $ t1 $ t2) =
bulwahn@34948
   141
        let
bulwahn@34948
   142
          val (i1, o1) = split_arg_mode' m1 t1
bulwahn@34948
   143
          val (i2, o2) = split_arg_mode' m2 t2
bulwahn@34948
   144
        in
bulwahn@34948
   145
          (comb_option HOLogic.mk_prod (i1, i2), comb_option HOLogic.mk_prod (o1, o2))
bulwahn@34948
   146
        end
bulwahn@34948
   147
      | split_arg_mode' Input t = (SOME t, NONE)
bulwahn@34948
   148
      | split_arg_mode' Output t = (NONE,  SOME t)
bulwahn@34948
   149
      | split_arg_mode' _ _ = error "split_map_mode: mode and term do not match"
bulwahn@34948
   150
  in
bulwahn@34948
   151
    (pairself (map_filter I) o split_list) (map2 split_arg_mode' (strip_fun_mode mode) ts)
bulwahn@34948
   152
  end
bulwahn@34948
   153
bulwahn@34948
   154
(* splits mode and maps function to higher-order argument types *)
bulwahn@34948
   155
fun split_map_modeT f mode Ts =
bulwahn@34948
   156
  let
bulwahn@34948
   157
    fun split_arg_mode' (m as Fun _) T = f m T
bulwahn@34948
   158
      | split_arg_mode' (Pair (m1, m2)) (Type ("*", [T1, T2])) =
bulwahn@34948
   159
        let
bulwahn@34948
   160
          val (i1, o1) = split_arg_mode' m1 T1
bulwahn@34948
   161
          val (i2, o2) = split_arg_mode' m2 T2
bulwahn@34948
   162
        in
bulwahn@34948
   163
          (comb_option HOLogic.mk_prodT (i1, i2), comb_option HOLogic.mk_prodT (o1, o2))
bulwahn@34948
   164
        end
bulwahn@34948
   165
      | split_arg_mode' Input T = (SOME T, NONE)
bulwahn@34948
   166
      | split_arg_mode' Output T = (NONE,  SOME T)
bulwahn@34948
   167
      | split_arg_mode' _ _ = error "split_modeT': mode and type do not match"
bulwahn@34948
   168
  in
bulwahn@34948
   169
    (pairself (map_filter I) o split_list) (map2 split_arg_mode' (strip_fun_mode mode) Ts)
bulwahn@34948
   170
  end
bulwahn@34948
   171
bulwahn@34948
   172
fun split_mode mode ts = split_map_mode (fn _ => fn _ => (NONE, NONE)) mode ts
bulwahn@34948
   173
bulwahn@34948
   174
fun fold_map_aterms_prodT comb f (Type ("*", [T1, T2])) s =
bulwahn@34948
   175
  let
bulwahn@34948
   176
    val (x1, s') = fold_map_aterms_prodT comb f T1 s
bulwahn@34948
   177
    val (x2, s'') = fold_map_aterms_prodT comb f T2 s'
bulwahn@34948
   178
  in
bulwahn@34948
   179
    (comb x1 x2, s'')
bulwahn@34948
   180
  end
bulwahn@34948
   181
  | fold_map_aterms_prodT comb f T s = f T s
bulwahn@34948
   182
bulwahn@34948
   183
fun map_filter_prod f (Const ("Pair", _) $ t1 $ t2) =
bulwahn@34948
   184
  comb_option HOLogic.mk_prod (map_filter_prod f t1, map_filter_prod f t2)
bulwahn@34948
   185
  | map_filter_prod f t = f t
bulwahn@34948
   186
bulwahn@34948
   187
(* obviously, split_mode' and split_modeT' do not match? where does that cause problems? *)
bulwahn@34948
   188
  
bulwahn@34948
   189
fun split_modeT' mode Ts =
bulwahn@34948
   190
  let
bulwahn@34948
   191
    fun split_arg_mode' (Fun _) T = ([], [])
bulwahn@34948
   192
      | split_arg_mode' (Pair (m1, m2)) (Type ("*", [T1, T2])) =
bulwahn@34948
   193
        let
bulwahn@34948
   194
          val (i1, o1) = split_arg_mode' m1 T1
bulwahn@34948
   195
          val (i2, o2) = split_arg_mode' m2 T2
bulwahn@34948
   196
        in
bulwahn@34948
   197
          (i1 @ i2, o1 @ o2)
bulwahn@34948
   198
        end
bulwahn@34948
   199
      | split_arg_mode' Input T = ([T], [])
bulwahn@34948
   200
      | split_arg_mode' Output T = ([], [T])
bulwahn@34948
   201
      | split_arg_mode' _ _ = error "split_modeT': mode and type do not match"
bulwahn@34948
   202
  in
bulwahn@34948
   203
    (pairself flat o split_list) (map2 split_arg_mode' (strip_fun_mode mode) Ts)
bulwahn@34948
   204
  end
bulwahn@34948
   205
bulwahn@34948
   206
fun string_of_mode mode =
bulwahn@33619
   207
  let
bulwahn@33619
   208
    fun string_of_mode1 Input = "i"
bulwahn@33619
   209
      | string_of_mode1 Output = "o"
bulwahn@33619
   210
      | string_of_mode1 Bool = "bool"
bulwahn@33619
   211
      | string_of_mode1 mode = "(" ^ (string_of_mode3 mode) ^ ")"
bulwahn@33626
   212
    and string_of_mode2 (Pair (m1, m2)) = string_of_mode3 m1 ^ " * " ^  string_of_mode2 m2
bulwahn@33619
   213
      | string_of_mode2 mode = string_of_mode1 mode
bulwahn@33619
   214
    and string_of_mode3 (Fun (m1, m2)) = string_of_mode2 m1 ^ " => " ^ string_of_mode3 m2
bulwahn@33619
   215
      | string_of_mode3 mode = string_of_mode2 mode
bulwahn@34948
   216
  in string_of_mode3 mode end
bulwahn@33619
   217
bulwahn@34948
   218
fun ascii_string_of_mode mode' =
bulwahn@33626
   219
  let
bulwahn@33626
   220
    fun ascii_string_of_mode' Input = "i"
bulwahn@33626
   221
      | ascii_string_of_mode' Output = "o"
bulwahn@33626
   222
      | ascii_string_of_mode' Bool = "b"
bulwahn@33626
   223
      | ascii_string_of_mode' (Pair (m1, m2)) =
bulwahn@33626
   224
          "P" ^ ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Pair m2
bulwahn@33626
   225
      | ascii_string_of_mode' (Fun (m1, m2)) = 
bulwahn@33626
   226
          "F" ^ ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Fun m2 ^ "B"
bulwahn@33626
   227
    and ascii_string_of_mode'_Fun (Fun (m1, m2)) =
bulwahn@33626
   228
          ascii_string_of_mode' m1 ^ (if m2 = Bool then "" else "_" ^ ascii_string_of_mode'_Fun m2)
bulwahn@33626
   229
      | ascii_string_of_mode'_Fun Bool = "B"
bulwahn@33626
   230
      | ascii_string_of_mode'_Fun m = ascii_string_of_mode' m
bulwahn@33626
   231
    and ascii_string_of_mode'_Pair (Pair (m1, m2)) =
bulwahn@33626
   232
          ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Pair m2
bulwahn@33626
   233
      | ascii_string_of_mode'_Pair m = ascii_string_of_mode' m
bulwahn@33626
   234
  in ascii_string_of_mode'_Fun mode' end
bulwahn@33626
   235
bulwahn@34948
   236
(* premises *)
bulwahn@33619
   237
bulwahn@34948
   238
datatype indprem = Prem of term | Negprem of term | Sidecond of term
bulwahn@34948
   239
  | Generator of (string * typ);
bulwahn@33619
   240
bulwahn@33250
   241
(* general syntactic functions *)
bulwahn@33250
   242
bulwahn@33250
   243
(*Like dest_conj, but flattens conjunctions however nested*)
bulwahn@33250
   244
fun conjuncts_aux (Const ("op &", _) $ t $ t') conjs = conjuncts_aux t (conjuncts_aux t' conjs)
bulwahn@33250
   245
  | conjuncts_aux t conjs = t::conjs;
bulwahn@33250
   246
bulwahn@33250
   247
fun conjuncts t = conjuncts_aux t [];
bulwahn@33250
   248
bulwahn@33250
   249
fun is_equationlike_term (Const ("==", _) $ _ $ _) = true
bulwahn@33250
   250
  | is_equationlike_term (Const ("Trueprop", _) $ (Const ("op =", _) $ _ $ _)) = true
bulwahn@33250
   251
  | is_equationlike_term _ = false
bulwahn@33250
   252
  
bulwahn@33250
   253
val is_equationlike = is_equationlike_term o prop_of 
bulwahn@33250
   254
bulwahn@33250
   255
fun is_pred_equation_term (Const ("==", _) $ u $ v) =
bulwahn@33250
   256
  (fastype_of u = @{typ bool}) andalso (fastype_of v = @{typ bool})
bulwahn@33250
   257
  | is_pred_equation_term _ = false
bulwahn@33250
   258
  
bulwahn@33250
   259
val is_pred_equation = is_pred_equation_term o prop_of 
bulwahn@33250
   260
bulwahn@33250
   261
fun is_intro_term constname t =
bulwahn@34948
   262
  the_default false (try (fn t => case fst (strip_comb (HOLogic.dest_Trueprop (Logic.strip_imp_concl t))) of
bulwahn@33250
   263
    Const (c, _) => c = constname
bulwahn@34948
   264
  | _ => false) t)
bulwahn@33250
   265
  
bulwahn@33250
   266
fun is_intro constname t = is_intro_term constname (prop_of t)
bulwahn@33250
   267
bulwahn@33250
   268
fun is_pred thy constname =
bulwahn@33250
   269
  let
bulwahn@33250
   270
    val T = (Sign.the_const_type thy constname)
bulwahn@33250
   271
  in body_type T = @{typ "bool"} end;
bulwahn@33250
   272
  
bulwahn@33250
   273
bulwahn@33250
   274
fun is_predT (T as Type("fun", [_, _])) = (snd (strip_type T) = HOLogic.boolT)
bulwahn@33250
   275
  | is_predT _ = false
bulwahn@33250
   276
bulwahn@33250
   277
(*** check if a term contains only constructor functions ***)
bulwahn@34948
   278
(* TODO: another copy in the core! *)
bulwahn@33623
   279
(* FIXME: constructor terms are supposed to be seen in the way the code generator
bulwahn@33623
   280
  sees constructors.*)
bulwahn@33250
   281
fun is_constrt thy =
bulwahn@33250
   282
  let
bulwahn@33250
   283
    val cnstrs = flat (maps
bulwahn@33250
   284
      (map (fn (_, (Tname, _, cs)) => map (apsnd (rpair Tname o length)) cs) o #descr o snd)
bulwahn@33250
   285
      (Symtab.dest (Datatype.get_all thy)));
bulwahn@33250
   286
    fun check t = (case strip_comb t of
bulwahn@33250
   287
        (Free _, []) => true
bulwahn@33250
   288
      | (Const (s, T), ts) => (case (AList.lookup (op =) cnstrs s, body_type T) of
bulwahn@33250
   289
            (SOME (i, Tname), Type (Tname', _)) => length ts = i andalso Tname = Tname' andalso forall check ts
bulwahn@33250
   290
          | _ => false)
bulwahn@33250
   291
      | _ => false)
bulwahn@33250
   292
  in check end;  
bulwahn@34948
   293
bulwahn@34948
   294
fun is_funtype (Type ("fun", [_, _])) = true
bulwahn@34948
   295
  | is_funtype _ = false;
bulwahn@34948
   296
bulwahn@34948
   297
fun is_Type (Type _) = true
bulwahn@34948
   298
  | is_Type _ = false
bulwahn@34948
   299
bulwahn@34948
   300
(* returns true if t is an application of an datatype constructor *)
bulwahn@34948
   301
(* which then consequently would be splitted *)
bulwahn@34948
   302
(* else false *)
bulwahn@34948
   303
(*
bulwahn@34948
   304
fun is_constructor thy t =
bulwahn@34948
   305
  if (is_Type (fastype_of t)) then
bulwahn@34948
   306
    (case DatatypePackage.get_datatype thy ((fst o dest_Type o fastype_of) t) of
bulwahn@34948
   307
      NONE => false
bulwahn@34948
   308
    | SOME info => (let
bulwahn@34948
   309
      val constr_consts = maps (fn (_, (_, _, constrs)) => map fst constrs) (#descr info)
bulwahn@34948
   310
      val (c, _) = strip_comb t
bulwahn@34948
   311
      in (case c of
bulwahn@34948
   312
        Const (name, _) => name mem_string constr_consts
bulwahn@34948
   313
        | _ => false) end))
bulwahn@34948
   314
  else false
bulwahn@34948
   315
*)
bulwahn@34948
   316
bulwahn@34948
   317
(* must be exported in code.ML *)
bulwahn@34948
   318
(* TODO: is there copy in the core? *)
bulwahn@34948
   319
fun is_constr thy = is_some o Code.get_datatype_of_constr thy;
bulwahn@34948
   320
bulwahn@33250
   321
fun strip_ex (Const ("Ex", _) $ Abs (x, T, t)) =
bulwahn@33250
   322
  let
bulwahn@33250
   323
    val (xTs, t') = strip_ex t
bulwahn@33250
   324
  in
bulwahn@33250
   325
    ((x, T) :: xTs, t')
bulwahn@33250
   326
  end
bulwahn@33250
   327
  | strip_ex t = ([], t)
bulwahn@33250
   328
bulwahn@33250
   329
fun focus_ex t nctxt =
bulwahn@33250
   330
  let
bulwahn@33250
   331
    val ((xs, Ts), t') = apfst split_list (strip_ex t) 
bulwahn@33250
   332
    val (xs', nctxt') = Name.variants xs nctxt;
bulwahn@33250
   333
    val ps' = xs' ~~ Ts;
bulwahn@33250
   334
    val vs = map Free ps';
bulwahn@33250
   335
    val t'' = Term.subst_bounds (rev vs, t');
bulwahn@33250
   336
  in ((ps', t''), nctxt') end;
bulwahn@33250
   337
bulwahn@33250
   338
(* introduction rule combinators *)
bulwahn@33250
   339
bulwahn@33250
   340
(* combinators to apply a function to all literals of an introduction rules *)
bulwahn@33250
   341
bulwahn@33250
   342
fun map_atoms f intro = 
bulwahn@33250
   343
  let
bulwahn@33250
   344
    val (literals, head) = Logic.strip_horn intro
bulwahn@33250
   345
    fun appl t = (case t of
bulwahn@33250
   346
        (@{term "Not"} $ t') => HOLogic.mk_not (f t')
bulwahn@33250
   347
      | _ => f t)
bulwahn@33250
   348
  in
bulwahn@33250
   349
    Logic.list_implies
bulwahn@33250
   350
      (map (HOLogic.mk_Trueprop o appl o HOLogic.dest_Trueprop) literals, head)
bulwahn@33250
   351
  end
bulwahn@33250
   352
bulwahn@33250
   353
fun fold_atoms f intro s =
bulwahn@33250
   354
  let
bulwahn@33250
   355
    val (literals, head) = Logic.strip_horn intro
bulwahn@33250
   356
    fun appl t s = (case t of
bulwahn@33250
   357
      (@{term "Not"} $ t') => f t' s
bulwahn@33250
   358
      | _ => f t s)
bulwahn@33250
   359
  in fold appl (map HOLogic.dest_Trueprop literals) s end
bulwahn@33250
   360
bulwahn@33250
   361
fun fold_map_atoms f intro s =
bulwahn@33250
   362
  let
bulwahn@33250
   363
    val (literals, head) = Logic.strip_horn intro
bulwahn@33250
   364
    fun appl t s = (case t of
bulwahn@33250
   365
      (@{term "Not"} $ t') => apfst HOLogic.mk_not (f t' s)
bulwahn@33250
   366
      | _ => f t s)
bulwahn@33250
   367
    val (literals', s') = fold_map appl (map HOLogic.dest_Trueprop literals) s
bulwahn@33250
   368
  in
bulwahn@33250
   369
    (Logic.list_implies (map HOLogic.mk_Trueprop literals', head), s')
bulwahn@33250
   370
  end;
bulwahn@33250
   371
bulwahn@33250
   372
fun maps_premises f intro =
bulwahn@33250
   373
  let
bulwahn@33250
   374
    val (premises, head) = Logic.strip_horn intro
bulwahn@33250
   375
  in
bulwahn@33250
   376
    Logic.list_implies (maps f premises, head)
bulwahn@33250
   377
  end
bulwahn@33250
   378
  
bulwahn@33250
   379
(* lifting term operations to theorems *)
bulwahn@33250
   380
bulwahn@33250
   381
fun map_term thy f th =
bulwahn@33250
   382
  Skip_Proof.make_thm thy (f (prop_of th))
bulwahn@33250
   383
bulwahn@33250
   384
(*
bulwahn@33250
   385
fun equals_conv lhs_cv rhs_cv ct =
bulwahn@33250
   386
  case Thm.term_of ct of
bulwahn@33250
   387
    Const ("==", _) $ _ $ _ => Conv.arg_conv cv ct  
bulwahn@33250
   388
  | _ => error "equals_conv"  
bulwahn@33250
   389
*)
bulwahn@33250
   390
bulwahn@33250
   391
(* Different options for compiler *)
bulwahn@33250
   392
bulwahn@34948
   393
datatype compilation = Pred | Random | Depth_Limited | DSeq | Annotated | Random_DSeq
bulwahn@34948
   394
bulwahn@34948
   395
fun string_of_compilation c = case c of
bulwahn@34948
   396
    Pred => ""
bulwahn@34948
   397
  | Random => "random"
bulwahn@34948
   398
  | Depth_Limited => "depth limited"
bulwahn@34948
   399
  | DSeq => "dseq"
bulwahn@34948
   400
  | Annotated => "annotated"
bulwahn@34948
   401
  | Random_DSeq => "random dseq"
bulwahn@34948
   402
bulwahn@34948
   403
(*datatype compilation_options =
bulwahn@34948
   404
  Pred | Random of int | Depth_Limited of int | DSeq of int | Annotated*)
bulwahn@34948
   405
bulwahn@33250
   406
datatype options = Options of {  
bulwahn@34948
   407
  expected_modes : (string * mode list) option,
bulwahn@34948
   408
  proposed_modes : (string * mode list) option,
bulwahn@34948
   409
  proposed_names : ((string * mode) * string) list,
bulwahn@33250
   410
  show_steps : bool,
bulwahn@33250
   411
  show_proof_trace : bool,
bulwahn@33250
   412
  show_intermediate_results : bool,
bulwahn@33251
   413
  show_mode_inference : bool,
bulwahn@33251
   414
  show_modes : bool,
bulwahn@33250
   415
  show_compilation : bool,
bulwahn@33250
   416
  skip_proof : bool,
bulwahn@33250
   417
bulwahn@33250
   418
  inductify : bool,
bulwahn@34948
   419
  compilation : compilation
bulwahn@33250
   420
};
bulwahn@33250
   421
bulwahn@33250
   422
fun expected_modes (Options opt) = #expected_modes opt
bulwahn@33752
   423
fun proposed_modes (Options opt) = #proposed_modes opt
bulwahn@34948
   424
fun proposed_names (Options opt) name mode = AList.lookup (eq_pair (op =) eq_mode)
bulwahn@33623
   425
  (#proposed_names opt) (name, mode)
bulwahn@33620
   426
bulwahn@33250
   427
fun show_steps (Options opt) = #show_steps opt
bulwahn@33250
   428
fun show_intermediate_results (Options opt) = #show_intermediate_results opt
bulwahn@33250
   429
fun show_proof_trace (Options opt) = #show_proof_trace opt
bulwahn@33251
   430
fun show_modes (Options opt) = #show_modes opt
bulwahn@33251
   431
fun show_mode_inference (Options opt) = #show_mode_inference opt
bulwahn@33250
   432
fun show_compilation (Options opt) = #show_compilation opt
bulwahn@33250
   433
fun skip_proof (Options opt) = #skip_proof opt
bulwahn@33250
   434
bulwahn@33250
   435
fun is_inductify (Options opt) = #inductify opt
bulwahn@34948
   436
bulwahn@34948
   437
fun compilation (Options opt) = #compilation opt
bulwahn@33250
   438
bulwahn@33250
   439
val default_options = Options {
bulwahn@33250
   440
  expected_modes = NONE,
bulwahn@33752
   441
  proposed_modes = NONE,
bulwahn@33623
   442
  proposed_names = [],
bulwahn@33250
   443
  show_steps = false,
bulwahn@33250
   444
  show_intermediate_results = false,
bulwahn@33250
   445
  show_proof_trace = false,
bulwahn@33251
   446
  show_modes = false,
bulwahn@33250
   447
  show_mode_inference = false,
bulwahn@33250
   448
  show_compilation = false,
bulwahn@34948
   449
  skip_proof = true,
bulwahn@33250
   450
  
bulwahn@33250
   451
  inductify = false,
bulwahn@34948
   452
  compilation = Pred
bulwahn@33250
   453
}
bulwahn@33250
   454
bulwahn@34948
   455
val bool_options = ["show_steps", "show_intermediate_results", "show_proof_trace", "show_modes",
bulwahn@34948
   456
  "show_mode_inference", "show_compilation", "skip_proof", "inductify"]
bulwahn@34948
   457
bulwahn@34948
   458
val compilation_names = [("pred", Pred),
bulwahn@34948
   459
  (*("random", Random), ("depth_limited", Depth_Limited), ("annotated", Annotated),*)
bulwahn@34948
   460
  ("dseq", DSeq), ("random_dseq", Random_DSeq)]
bulwahn@33250
   461
bulwahn@33250
   462
fun print_step options s =
bulwahn@33250
   463
  if show_steps options then tracing s else ()
bulwahn@33250
   464
bulwahn@33250
   465
(* tuple processing *)
bulwahn@33250
   466
bulwahn@33250
   467
fun expand_tuples thy intro =
bulwahn@33250
   468
  let
bulwahn@33250
   469
    fun rewrite_args [] (pats, intro_t, ctxt) = (pats, intro_t, ctxt)
bulwahn@33250
   470
      | rewrite_args (arg::args) (pats, intro_t, ctxt) = 
bulwahn@33250
   471
      (case HOLogic.strip_tupleT (fastype_of arg) of
bulwahn@33250
   472
        (Ts as _ :: _ :: _) =>
bulwahn@33250
   473
        let
bulwahn@33250
   474
          fun rewrite_arg' (Const ("Pair", _) $ _ $ t2, Type ("*", [_, T2]))
bulwahn@33250
   475
            (args, (pats, intro_t, ctxt)) = rewrite_arg' (t2, T2) (args, (pats, intro_t, ctxt))
bulwahn@33250
   476
            | rewrite_arg' (t, Type ("*", [T1, T2])) (args, (pats, intro_t, ctxt)) =
bulwahn@33250
   477
              let
bulwahn@33250
   478
                val ([x, y], ctxt') = Variable.variant_fixes ["x", "y"] ctxt
bulwahn@33250
   479
                val pat = (t, HOLogic.mk_prod (Free (x, T1), Free (y, T2)))
bulwahn@33250
   480
                val intro_t' = Pattern.rewrite_term thy [pat] [] intro_t
bulwahn@33250
   481
                val args' = map (Pattern.rewrite_term thy [pat] []) args
bulwahn@33250
   482
              in
bulwahn@33250
   483
                rewrite_arg' (Free (y, T2), T2) (args', (pat::pats, intro_t', ctxt'))
bulwahn@33250
   484
              end
bulwahn@33250
   485
            | rewrite_arg' _ (args, (pats, intro_t, ctxt)) = (args, (pats, intro_t, ctxt))
bulwahn@33250
   486
          val (args', (pats, intro_t', ctxt')) = rewrite_arg' (arg, fastype_of arg)
bulwahn@33250
   487
            (args, (pats, intro_t, ctxt))
bulwahn@33250
   488
        in
bulwahn@33250
   489
          rewrite_args args' (pats, intro_t', ctxt')
bulwahn@33250
   490
        end
bulwahn@33250
   491
      | _ => rewrite_args args (pats, intro_t, ctxt))
bulwahn@33250
   492
    fun rewrite_prem atom =
bulwahn@33250
   493
      let
bulwahn@33250
   494
        val (_, args) = strip_comb atom
bulwahn@33250
   495
      in rewrite_args args end
bulwahn@33250
   496
    val ctxt = ProofContext.init thy
bulwahn@33250
   497
    val (((T_insts, t_insts), [intro']), ctxt1) = Variable.import false [intro] ctxt
bulwahn@33250
   498
    val intro_t = prop_of intro'
bulwahn@33250
   499
    val concl = Logic.strip_imp_concl intro_t
bulwahn@33250
   500
    val (p, args) = strip_comb (HOLogic.dest_Trueprop concl)
bulwahn@33250
   501
    val (pats', intro_t', ctxt2) = rewrite_args args ([], intro_t, ctxt1)
bulwahn@33250
   502
    val (pats', intro_t', ctxt3) = 
bulwahn@33250
   503
      fold_atoms rewrite_prem intro_t' (pats', intro_t', ctxt2)
bulwahn@33250
   504
    fun rewrite_pat (ct1, ct2) =
bulwahn@33250
   505
      (ct1, cterm_of thy (Pattern.rewrite_term thy pats' [] (term_of ct2)))
bulwahn@33250
   506
    val t_insts' = map rewrite_pat t_insts
bulwahn@33250
   507
    val intro'' = Thm.instantiate (T_insts, t_insts') intro
bulwahn@33250
   508
    val [intro'''] = Variable.export ctxt3 ctxt [intro'']
bulwahn@33250
   509
    val intro'''' = Simplifier.full_simplify
bulwahn@33250
   510
      (HOL_basic_ss addsimps [@{thm fst_conv}, @{thm snd_conv}, @{thm Pair_eq}])
bulwahn@33250
   511
      intro'''
bulwahn@33250
   512
    (* splitting conjunctions introduced by Pair_eq*)
bulwahn@33250
   513
    fun split_conj prem =
bulwahn@33250
   514
      map HOLogic.mk_Trueprop (conjuncts (HOLogic.dest_Trueprop prem))
bulwahn@33250
   515
    val intro''''' = map_term thy (maps_premises split_conj) intro''''
bulwahn@33250
   516
  in
bulwahn@33250
   517
    intro'''''
bulwahn@33250
   518
  end
bulwahn@33250
   519
bulwahn@33250
   520
end;