src/HOL/Tools/Nitpick/minipick.ML
author blanchet
Tue, 09 Mar 2010 09:25:23 +0100
changeset 35665 ff2bf50505ab
parent 35625 9c818cab0dd0
child 35699 9ed327529a44
permissions -rw-r--r--
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet@33980
     1
(*  Title:      HOL/Tools/Nitpick/minipick.ML
blanchet@33192
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@34969
     3
    Copyright   2009, 2010
blanchet@33192
     4
blanchet@33192
     5
Finite model generation for HOL formulas using Kodkod, minimalistic version.
blanchet@33192
     6
*)
blanchet@33192
     7
blanchet@33192
     8
signature MINIPICK =
blanchet@33192
     9
sig
blanchet@33980
    10
  datatype rep = SRep | RRep
blanchet@33980
    11
  type styp = Nitpick_Util.styp
blanchet@33980
    12
blanchet@33980
    13
  val vars_for_bound_var :
blanchet@33980
    14
    (typ -> int) -> rep -> typ list -> int -> Kodkod.rel_expr list
blanchet@33980
    15
  val rel_expr_for_bound_var :
blanchet@33980
    16
    (typ -> int) -> rep -> typ list -> int -> Kodkod.rel_expr
blanchet@33980
    17
  val decls_for : rep -> (typ -> int) -> typ list -> typ -> Kodkod.decl list
blanchet@33980
    18
  val false_atom : Kodkod.rel_expr
blanchet@33980
    19
  val true_atom : Kodkod.rel_expr
blanchet@33980
    20
  val formula_from_atom : Kodkod.rel_expr -> Kodkod.formula
blanchet@33980
    21
  val atom_from_formula : Kodkod.formula -> Kodkod.rel_expr
blanchet@35284
    22
  val kodkod_problem_from_term :
blanchet@35284
    23
    Proof.context -> (typ -> int) -> term -> Kodkod.problem
blanchet@35284
    24
  val solve_any_kodkod_problem : theory -> Kodkod.problem list -> string
blanchet@33192
    25
end;
blanchet@33192
    26
blanchet@33192
    27
structure Minipick : MINIPICK =
blanchet@33192
    28
struct
blanchet@33192
    29
blanchet@33192
    30
open Kodkod
blanchet@33224
    31
open Nitpick_Util
blanchet@33224
    32
open Nitpick_HOL
blanchet@33224
    33
open Nitpick_Peephole
blanchet@33224
    34
open Nitpick_Kodkod
blanchet@33192
    35
blanchet@33980
    36
datatype rep = SRep | RRep
blanchet@33980
    37
blanchet@33980
    38
(* Proof.context -> typ -> unit *)
blanchet@35665
    39
fun check_type ctxt (Type (@{type_name fun}, Ts)) =
blanchet@35665
    40
    List.app (check_type ctxt) Ts
blanchet@35665
    41
  | check_type ctxt (Type (@{type_name "*"}, Ts)) =
blanchet@35665
    42
    List.app (check_type ctxt) Ts
blanchet@33192
    43
  | check_type _ @{typ bool} = ()
blanchet@33192
    44
  | check_type _ (TFree (_, @{sort "{}"})) = ()
blanchet@33192
    45
  | check_type _ (TFree (_, @{sort HOL.type})) = ()
blanchet@33192
    46
  | check_type ctxt T =
blanchet@33192
    47
    raise NOT_SUPPORTED ("type " ^ quote (Syntax.string_of_typ ctxt T))
blanchet@33192
    48
blanchet@33980
    49
(* rep -> (typ -> int) -> typ -> int list *)
blanchet@35665
    50
fun atom_schema_of SRep card (Type (@{type_name fun}, [T1, T2])) =
blanchet@33980
    51
    replicate_list (card T1) (atom_schema_of SRep card T2)
blanchet@35665
    52
  | atom_schema_of RRep card (Type (@{type_name fun}, [T1, @{typ bool}])) =
blanchet@33980
    53
    atom_schema_of SRep card T1
blanchet@35665
    54
  | atom_schema_of RRep card (Type (@{type_name fun}, [T1, T2])) =
blanchet@33980
    55
    atom_schema_of SRep card T1 @ atom_schema_of RRep card T2
blanchet@35665
    56
  | atom_schema_of _ card (Type (@{type_name "*"}, Ts)) =
blanchet@35665
    57
    maps (atom_schema_of SRep card) Ts
blanchet@33980
    58
  | atom_schema_of _ card T = [card T]
blanchet@33980
    59
(* rep -> (typ -> int) -> typ -> int *)
blanchet@33980
    60
val arity_of = length ooo atom_schema_of
blanchet@33192
    61
blanchet@33192
    62
(* (typ -> int) -> typ list -> int -> int *)
blanchet@33192
    63
fun index_for_bound_var _ [_] 0 = 0
blanchet@33980
    64
  | index_for_bound_var card (_ :: Ts) 0 =
blanchet@33980
    65
    index_for_bound_var card Ts 0 + arity_of SRep card (hd Ts)
blanchet@33980
    66
  | index_for_bound_var card Ts n = index_for_bound_var card (tl Ts) (n - 1)
blanchet@33980
    67
(* (typ -> int) -> rep -> typ list -> int -> rel_expr list *)
blanchet@33980
    68
fun vars_for_bound_var card R Ts j =
blanchet@33980
    69
  map (curry Var 1) (index_seq (index_for_bound_var card Ts j)
blanchet@33980
    70
                               (arity_of R card (nth Ts j)))
blanchet@33980
    71
(* (typ -> int) -> rep -> typ list -> int -> rel_expr *)
blanchet@33980
    72
val rel_expr_for_bound_var = foldl1 Product oooo vars_for_bound_var
blanchet@33980
    73
(* rep -> (typ -> int) -> typ list -> typ -> decl list *)
blanchet@33980
    74
fun decls_for R card Ts T =
blanchet@33192
    75
  map2 (curry DeclOne o pair 1)
blanchet@33980
    76
       (index_seq (index_for_bound_var card (T :: Ts) 0)
blanchet@33980
    77
                  (arity_of R card (nth (T :: Ts) 0)))
blanchet@33980
    78
       (map (AtomSeq o rpair 0) (atom_schema_of R card T))
blanchet@33192
    79
blanchet@33192
    80
(* int list -> rel_expr *)
blanchet@33192
    81
val atom_product = foldl1 Product o map Atom
blanchet@33192
    82
blanchet@33192
    83
val false_atom = Atom 0
blanchet@33192
    84
val true_atom = Atom 1
blanchet@33192
    85
blanchet@33192
    86
(* rel_expr -> formula *)
blanchet@33192
    87
fun formula_from_atom r = RelEq (r, true_atom)
blanchet@33192
    88
(* formula -> rel_expr *)
blanchet@33192
    89
fun atom_from_formula f = RelIf (f, true_atom, false_atom)
blanchet@33192
    90
blanchet@33192
    91
(* Proof.context -> (typ -> int) -> styp list -> term -> formula *)
blanchet@35185
    92
fun kodkod_formula_from_term ctxt card frees =
blanchet@33192
    93
  let
blanchet@33192
    94
    (* typ -> rel_expr -> rel_expr *)
blanchet@35665
    95
    fun R_rep_from_S_rep (T as Type (@{type_name fun}, [T1, @{typ bool}])) r =
blanchet@33192
    96
        let
blanchet@33980
    97
          val jss = atom_schema_of SRep card T1 |> map (rpair 0)
blanchet@33192
    98
                    |> all_combinations
blanchet@33192
    99
        in
blanchet@33192
   100
          map2 (fn i => fn js =>
blanchet@33980
   101
                   RelIf (formula_from_atom (Project (r, [Num i])),
blanchet@33192
   102
                          atom_product js, empty_n_ary_rel (length js)))
blanchet@33192
   103
               (index_seq 0 (length jss)) jss
blanchet@33192
   104
          |> foldl1 Union
blanchet@33192
   105
        end
blanchet@35665
   106
      | R_rep_from_S_rep (Type (@{type_name fun}, [T1, T2])) r =
blanchet@33192
   107
        let
blanchet@33980
   108
          val jss = atom_schema_of SRep card T1 |> map (rpair 0)
blanchet@33192
   109
                    |> all_combinations
blanchet@33980
   110
          val arity2 = arity_of SRep card T2
blanchet@33192
   111
        in
blanchet@33192
   112
          map2 (fn i => fn js =>
blanchet@33192
   113
                   Product (atom_product js,
blanchet@33192
   114
                            Project (r, num_seq (i * arity2) arity2)
blanchet@33980
   115
                            |> R_rep_from_S_rep T2))
blanchet@33192
   116
               (index_seq 0 (length jss)) jss
blanchet@33192
   117
          |> foldl1 Union
blanchet@33192
   118
        end
blanchet@33980
   119
      | R_rep_from_S_rep _ r = r
blanchet@33192
   120
    (* typ list -> typ -> rel_expr -> rel_expr *)
blanchet@35665
   121
    fun S_rep_from_R_rep Ts (T as Type (@{type_name fun}, _)) r =
blanchet@33980
   122
        Comprehension (decls_for SRep card Ts T,
blanchet@33980
   123
            RelEq (R_rep_from_S_rep T
blanchet@33980
   124
                       (rel_expr_for_bound_var card SRep (T :: Ts) 0), r))
blanchet@33980
   125
      | S_rep_from_R_rep _ _ r = r
blanchet@33192
   126
    (* typ list -> term -> formula *)
blanchet@33980
   127
    fun to_F Ts t =
blanchet@33192
   128
      (case t of
blanchet@33980
   129
         @{const Not} $ t1 => Not (to_F Ts t1)
blanchet@33192
   130
       | @{const False} => False
blanchet@33192
   131
       | @{const True} => True
blanchet@33192
   132
       | Const (@{const_name All}, _) $ Abs (s, T, t') =>
blanchet@33980
   133
         All (decls_for SRep card Ts T, to_F (T :: Ts) t')
blanchet@33192
   134
       | (t0 as Const (@{const_name All}, _)) $ t1 =>
blanchet@33980
   135
         to_F Ts (t0 $ eta_expand Ts t1 1)
blanchet@35280
   136
       | Const (@{const_name Ex}, _) $ Abs (_, T, t') =>
blanchet@33980
   137
         Exist (decls_for SRep card Ts T, to_F (T :: Ts) t')
blanchet@33192
   138
       | (t0 as Const (@{const_name Ex}, _)) $ t1 =>
blanchet@33980
   139
         to_F Ts (t0 $ eta_expand Ts t1 1)
blanchet@33192
   140
       | Const (@{const_name "op ="}, _) $ t1 $ t2 =>
blanchet@33980
   141
         RelEq (to_R_rep Ts t1, to_R_rep Ts t2)
blanchet@33192
   142
       | Const (@{const_name ord_class.less_eq},
blanchet@35665
   143
                Type (@{type_name fun},
blanchet@35665
   144
                      [Type (@{type_name fun}, [_, @{typ bool}]), _]))
blanchet@35665
   145
         $ t1 $ t2 =>
blanchet@33980
   146
         Subset (to_R_rep Ts t1, to_R_rep Ts t2)
blanchet@33980
   147
       | @{const "op &"} $ t1 $ t2 => And (to_F Ts t1, to_F Ts t2)
blanchet@33980
   148
       | @{const "op |"} $ t1 $ t2 => Or (to_F Ts t1, to_F Ts t2)
blanchet@33980
   149
       | @{const "op -->"} $ t1 $ t2 => Implies (to_F Ts t1, to_F Ts t2)
blanchet@33980
   150
       | t1 $ t2 => Subset (to_S_rep Ts t2, to_R_rep Ts t1)
blanchet@33192
   151
       | Free _ => raise SAME ()
blanchet@33192
   152
       | Term.Var _ => raise SAME ()
blanchet@33192
   153
       | Bound _ => raise SAME ()
blanchet@33192
   154
       | Const (s, _) => raise NOT_SUPPORTED ("constant " ^ quote s)
blanchet@35185
   155
       | _ => raise TERM ("Minipick.kodkod_formula_from_term.to_F", [t]))
blanchet@33980
   156
      handle SAME () => formula_from_atom (to_R_rep Ts t)
blanchet@33192
   157
    (* typ list -> term -> rel_expr *)
blanchet@33980
   158
    and to_S_rep Ts t =
blanchet@33192
   159
        case t of
blanchet@33192
   160
          Const (@{const_name Pair}, _) $ t1 $ t2 =>
blanchet@33980
   161
          Product (to_S_rep Ts t1, to_S_rep Ts t2)
blanchet@33980
   162
        | Const (@{const_name Pair}, _) $ _ => to_S_rep Ts (eta_expand Ts t 1)
blanchet@33980
   163
        | Const (@{const_name Pair}, _) => to_S_rep Ts (eta_expand Ts t 2)
blanchet@33192
   164
        | Const (@{const_name fst}, _) $ t1 =>
blanchet@33980
   165
          let val fst_arity = arity_of SRep card (fastype_of1 (Ts, t)) in
blanchet@33980
   166
            Project (to_S_rep Ts t1, num_seq 0 fst_arity)
blanchet@33192
   167
          end
blanchet@33980
   168
        | Const (@{const_name fst}, _) => to_S_rep Ts (eta_expand Ts t 1)
blanchet@33192
   169
        | Const (@{const_name snd}, _) $ t1 =>
blanchet@33192
   170
          let
blanchet@33980
   171
            val pair_arity = arity_of SRep card (fastype_of1 (Ts, t1))
blanchet@33980
   172
            val snd_arity = arity_of SRep card (fastype_of1 (Ts, t))
blanchet@33192
   173
            val fst_arity = pair_arity - snd_arity
blanchet@33980
   174
          in Project (to_S_rep Ts t1, num_seq fst_arity snd_arity) end
blanchet@33980
   175
        | Const (@{const_name snd}, _) => to_S_rep Ts (eta_expand Ts t 1)
blanchet@33980
   176
        | Bound j => rel_expr_for_bound_var card SRep Ts j
blanchet@33980
   177
        | _ => S_rep_from_R_rep Ts (fastype_of1 (Ts, t)) (to_R_rep Ts t)
blanchet@33192
   178
    (* term -> rel_expr *)
blanchet@33980
   179
    and to_R_rep Ts t =
blanchet@33192
   180
      (case t of
blanchet@33980
   181
         @{const Not} => to_R_rep Ts (eta_expand Ts t 1)
blanchet@33980
   182
       | Const (@{const_name All}, _) => to_R_rep Ts (eta_expand Ts t 1)
blanchet@33980
   183
       | Const (@{const_name Ex}, _) => to_R_rep Ts (eta_expand Ts t 1)
blanchet@33980
   184
       | Const (@{const_name "op ="}, _) $ _ => to_R_rep Ts (eta_expand Ts t 1)
blanchet@33980
   185
       | Const (@{const_name "op ="}, _) => to_R_rep Ts (eta_expand Ts t 2)
blanchet@33192
   186
       | Const (@{const_name ord_class.less_eq},
blanchet@35665
   187
                Type (@{type_name fun},
blanchet@35665
   188
                      [Type (@{type_name fun}, [_, @{typ bool}]), _])) $ _ =>
blanchet@33980
   189
         to_R_rep Ts (eta_expand Ts t 1)
blanchet@33192
   190
       | Const (@{const_name ord_class.less_eq}, _) =>
blanchet@33980
   191
         to_R_rep Ts (eta_expand Ts t 2)
blanchet@33980
   192
       | @{const "op &"} $ _ => to_R_rep Ts (eta_expand Ts t 1)
blanchet@33980
   193
       | @{const "op &"} => to_R_rep Ts (eta_expand Ts t 2)
blanchet@33980
   194
       | @{const "op |"} $ _ => to_R_rep Ts (eta_expand Ts t 1)
blanchet@33980
   195
       | @{const "op |"} => to_R_rep Ts (eta_expand Ts t 2)
blanchet@33980
   196
       | @{const "op -->"} $ _ => to_R_rep Ts (eta_expand Ts t 1)
blanchet@33980
   197
       | @{const "op -->"} => to_R_rep Ts (eta_expand Ts t 2)
blanchet@33192
   198
       | Const (@{const_name bot_class.bot},
blanchet@35665
   199
                T as Type (@{type_name fun}, [_, @{typ bool}])) =>
blanchet@33980
   200
         empty_n_ary_rel (arity_of RRep card T)
blanchet@33192
   201
       | Const (@{const_name insert}, _) $ t1 $ t2 =>
blanchet@33980
   202
         Union (to_S_rep Ts t1, to_R_rep Ts t2)
blanchet@33980
   203
       | Const (@{const_name insert}, _) $ _ => to_R_rep Ts (eta_expand Ts t 1)
blanchet@33980
   204
       | Const (@{const_name insert}, _) => to_R_rep Ts (eta_expand Ts t 2)
blanchet@33192
   205
       | Const (@{const_name trancl}, _) $ t1 =>
blanchet@33980
   206
         if arity_of RRep card (fastype_of1 (Ts, t1)) = 2 then
blanchet@33980
   207
           Closure (to_R_rep Ts t1)
blanchet@33192
   208
         else
blanchet@33192
   209
           raise NOT_SUPPORTED "transitive closure for function or pair type"
blanchet@33980
   210
       | Const (@{const_name trancl}, _) => to_R_rep Ts (eta_expand Ts t 1)
haftmann@35028
   211
       | Const (@{const_name semilattice_inf_class.inf},
blanchet@35665
   212
                Type (@{type_name fun},
blanchet@35665
   213
                      [Type (@{type_name fun}, [_, @{typ bool}]), _]))
blanchet@35665
   214
         $ t1 $ t2 =>
blanchet@33980
   215
         Intersect (to_R_rep Ts t1, to_R_rep Ts t2)
haftmann@35028
   216
       | Const (@{const_name semilattice_inf_class.inf}, _) $ _ =>
blanchet@33980
   217
         to_R_rep Ts (eta_expand Ts t 1)
haftmann@35028
   218
       | Const (@{const_name semilattice_inf_class.inf}, _) =>
blanchet@33980
   219
         to_R_rep Ts (eta_expand Ts t 2)
haftmann@35028
   220
       | Const (@{const_name semilattice_sup_class.sup},
blanchet@35665
   221
                Type (@{type_name fun},
blanchet@35665
   222
                      [Type (@{type_name fun}, [_, @{typ bool}]), _]))
blanchet@35665
   223
         $ t1 $ t2 =>
blanchet@33980
   224
         Union (to_R_rep Ts t1, to_R_rep Ts t2)
haftmann@35028
   225
       | Const (@{const_name semilattice_sup_class.sup}, _) $ _ =>
blanchet@33980
   226
         to_R_rep Ts (eta_expand Ts t 1)
haftmann@35028
   227
       | Const (@{const_name semilattice_sup_class.sup}, _) =>
blanchet@33980
   228
         to_R_rep Ts (eta_expand Ts t 2)
blanchet@33192
   229
       | Const (@{const_name minus_class.minus},
blanchet@35665
   230
                Type (@{type_name fun},
blanchet@35665
   231
                      [Type (@{type_name fun}, [_, @{typ bool}]), _]))
blanchet@35665
   232
         $ t1 $ t2 =>
blanchet@33980
   233
         Difference (to_R_rep Ts t1, to_R_rep Ts t2)
blanchet@33192
   234
       | Const (@{const_name minus_class.minus},
blanchet@35665
   235
                Type (@{type_name fun},
blanchet@35665
   236
                      [Type (@{type_name fun}, [_, @{typ bool}]), _])) $ _ =>
blanchet@33980
   237
         to_R_rep Ts (eta_expand Ts t 1)
blanchet@33192
   238
       | Const (@{const_name minus_class.minus},
blanchet@35665
   239
                Type (@{type_name fun},
blanchet@35665
   240
                      [Type (@{type_name fun}, [_, @{typ bool}]), _])) =>
blanchet@33980
   241
         to_R_rep Ts (eta_expand Ts t 2)
blanchet@33192
   242
       | Const (@{const_name Pair}, _) $ _ $ _ => raise SAME ()
blanchet@33192
   243
       | Const (@{const_name Pair}, _) $ _ => raise SAME ()
blanchet@33192
   244
       | Const (@{const_name Pair}, _) => raise SAME ()
blanchet@33192
   245
       | Const (@{const_name fst}, _) $ _ => raise SAME ()
blanchet@33192
   246
       | Const (@{const_name fst}, _) => raise SAME ()
blanchet@33192
   247
       | Const (@{const_name snd}, _) $ _ => raise SAME ()
blanchet@33192
   248
       | Const (@{const_name snd}, _) => raise SAME ()
blanchet@33980
   249
       | Const (_, @{typ bool}) => atom_from_formula (to_F Ts t)
blanchet@33192
   250
       | Free (x as (_, T)) =>
blanchet@34118
   251
         Rel (arity_of RRep card T, find_index (curry (op =) x) frees)
blanchet@33192
   252
       | Term.Var _ => raise NOT_SUPPORTED "schematic variables"
blanchet@35280
   253
       | Bound _ => raise SAME ()
blanchet@33192
   254
       | Abs (_, T, t') =>
blanchet@33192
   255
         (case fastype_of1 (T :: Ts, t') of
blanchet@33980
   256
            @{typ bool} => Comprehension (decls_for SRep card Ts T,
blanchet@33980
   257
                                          to_F (T :: Ts) t')
blanchet@33980
   258
          | T' => Comprehension (decls_for SRep card Ts T @
blanchet@33980
   259
                                 decls_for RRep card (T :: Ts) T',
blanchet@33980
   260
                                 Subset (rel_expr_for_bound_var card RRep
blanchet@33980
   261
                                                              (T' :: T :: Ts) 0,
blanchet@33980
   262
                                         to_R_rep (T :: Ts) t')))
blanchet@33192
   263
       | t1 $ t2 =>
blanchet@33192
   264
         (case fastype_of1 (Ts, t) of
blanchet@33980
   265
            @{typ bool} => atom_from_formula (to_F Ts t)
blanchet@33192
   266
          | T =>
blanchet@33192
   267
            let val T2 = fastype_of1 (Ts, t2) in
blanchet@33980
   268
              case arity_of SRep card T2 of
blanchet@33980
   269
                1 => Join (to_S_rep Ts t2, to_R_rep Ts t1)
blanchet@35280
   270
              | arity2 =>
blanchet@35280
   271
                let val res_arity = arity_of RRep card T in
blanchet@33192
   272
                  Project (Intersect
blanchet@33980
   273
                      (Product (to_S_rep Ts t2,
blanchet@33980
   274
                                atom_schema_of RRep card T
blanchet@33192
   275
                                |> map (AtomSeq o rpair 0) |> foldl1 Product),
blanchet@33980
   276
                       to_R_rep Ts t1),
blanchet@33192
   277
                      num_seq arity2 res_arity)
blanchet@33192
   278
                end
blanchet@33192
   279
            end)
blanchet@33192
   280
       | _ => raise NOT_SUPPORTED ("term " ^
blanchet@33192
   281
                                   quote (Syntax.string_of_term ctxt t)))
blanchet@33980
   282
      handle SAME () => R_rep_from_S_rep (fastype_of1 (Ts, t)) (to_S_rep Ts t)
blanchet@33980
   283
  in to_F [] end
blanchet@33192
   284
blanchet@33192
   285
(* (typ -> int) -> int -> styp -> bound *)
blanchet@33980
   286
fun bound_for_free card i (s, T) =
blanchet@33980
   287
  let val js = atom_schema_of RRep card T in
blanchet@33192
   288
    ([((length js, i), s)],
blanchet@33980
   289
     [TupleSet [], atom_schema_of RRep card T |> map (rpair 0)
blanchet@33192
   290
                   |> tuple_set_from_atom_schema])
blanchet@33192
   291
  end
blanchet@33192
   292
blanchet@33192
   293
(* (typ -> int) -> typ list -> typ -> rel_expr -> formula *)
blanchet@35665
   294
fun declarative_axiom_for_rel_expr card Ts (Type (@{type_name fun}, [T1, T2]))
blanchet@35665
   295
                                   r =
blanchet@33192
   296
    if body_type T2 = bool_T then
blanchet@33192
   297
      True
blanchet@33192
   298
    else
blanchet@33980
   299
      All (decls_for SRep card Ts T1,
blanchet@33980
   300
           declarative_axiom_for_rel_expr card (T1 :: Ts) T2
blanchet@33980
   301
               (List.foldl Join r (vars_for_bound_var card SRep (T1 :: Ts) 0)))
blanchet@33192
   302
  | declarative_axiom_for_rel_expr _ _ _ r = One r
blanchet@33980
   303
(* (typ -> int) -> bool -> int -> styp -> formula *)
blanchet@33980
   304
fun declarative_axiom_for_free card i (_, T) =
blanchet@33980
   305
  declarative_axiom_for_rel_expr card [] T (Rel (arity_of RRep card T, i))
blanchet@33192
   306
blanchet@35284
   307
(* Proof.context -> (typ -> int) -> term -> problem *)
blanchet@35284
   308
fun kodkod_problem_from_term ctxt raw_card t =
blanchet@33192
   309
  let
blanchet@33192
   310
    val thy = ProofContext.theory_of ctxt
blanchet@33192
   311
    (* typ -> int *)
blanchet@35665
   312
    fun card (Type (@{type_name fun}, [T1, T2])) =
blanchet@35665
   313
        reasonable_power (card T2) (card T1)
blanchet@35665
   314
      | card (Type (@{type_name "*"}, [T1, T2])) = card T1 * card T2
blanchet@33980
   315
      | card @{typ bool} = 2
blanchet@33980
   316
      | card T = Int.max (1, raw_card T)
wenzelm@35625
   317
    val neg_t = @{const Not} $ Object_Logic.atomize_term thy t
blanchet@33192
   318
    val _ = fold_types (K o check_type ctxt) neg_t ()
blanchet@33192
   319
    val frees = Term.add_frees neg_t []
blanchet@33980
   320
    val bounds = map2 (bound_for_free card) (index_seq 0 (length frees)) frees
blanchet@33192
   321
    val declarative_axioms =
blanchet@33980
   322
      map2 (declarative_axiom_for_free card) (index_seq 0 (length frees)) frees
blanchet@35185
   323
    val formula = kodkod_formula_from_term ctxt card frees neg_t
blanchet@33192
   324
                  |> fold_rev (curry And) declarative_axioms
blanchet@33192
   325
    val univ_card = univ_card 0 0 0 bounds formula
blanchet@33192
   326
  in
blanchet@35284
   327
    {comment = "", settings = [], univ_card = univ_card, tuple_assigns = [],
blanchet@35284
   328
     bounds = bounds, int_bounds = [], expr_assigns = [], formula = formula}
blanchet@35284
   329
  end
blanchet@35284
   330
blanchet@35284
   331
(* theory -> problem list -> string *)
blanchet@35284
   332
fun solve_any_kodkod_problem thy problems =
blanchet@35284
   333
  let
blanchet@35284
   334
    val {overlord, ...} = Nitpick_Isar.default_params thy []
blanchet@35284
   335
    val max_threads = 1
blanchet@35284
   336
    val max_solutions = 1
blanchet@35284
   337
  in
blanchet@35284
   338
    case solve_any_problem overlord NONE max_threads max_solutions problems of
blanchet@34121
   339
      NotInstalled => "unknown"
blanchet@35333
   340
    | Normal ([], _, _) => "none"
blanchet@33192
   341
    | Normal _ => "genuine"
blanchet@33192
   342
    | TimedOut _ => "unknown"
blanchet@33192
   343
    | Interrupted _ => "unknown"
blanchet@33192
   344
    | Error (s, _) => error ("Kodkod error: " ^ s)
blanchet@33192
   345
  end
blanchet@33980
   346
blanchet@33192
   347
end;