src/HOL/Nitpick_Examples/minipick.ML
author blanchet
Fri, 23 Sep 2011 14:25:53 +0200
changeset 45062 9598cada31b3
parent 45035 60d2c03d5c70
child 45128 5af3a3203a76
permissions -rw-r--r--
first step towards extending Minipick with more translations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
     1
(*  Title:      HOL/Nitpick_Examples/minipick.ML
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
     3
    Copyright   2009-2010
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
     4
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
     5
Finite model generation for HOL formulas using Kodkod, minimalistic version.
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
     6
*)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
     7
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
     8
signature MINIPICK =
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
     9
sig
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    10
  val minipick : Proof.context -> int -> term -> string
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    11
  val minipick_expect : Proof.context -> string -> int -> term -> unit
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    12
end;
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    13
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    14
structure Minipick : MINIPICK =
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    15
struct
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    16
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    17
open Kodkod
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    18
open Nitpick_Util
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    19
open Nitpick_HOL
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    20
open Nitpick_Peephole
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    21
open Nitpick_Kodkod
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    22
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    23
datatype rep =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    24
  S_Rep |
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    25
  R_Rep of bool
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    26
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    27
fun check_type ctxt raw_infinite (Type (@{type_name fun}, Ts)) =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    28
    List.app (check_type ctxt raw_infinite) Ts
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    29
  | check_type ctxt raw_infinite (Type (@{type_name prod}, Ts)) =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    30
    List.app (check_type ctxt raw_infinite) Ts
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    31
  | check_type _ _ @{typ bool} = ()
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    32
  | check_type _ _ (TFree (_, @{sort "{}"})) = ()
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    33
  | check_type _ _ (TFree (_, @{sort HOL.type})) = ()
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    34
  | check_type ctxt raw_infinite T =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    35
    if raw_infinite T then ()
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    36
    else raise NOT_SUPPORTED ("type " ^ quote (Syntax.string_of_typ ctxt T))
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    37
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    38
fun atom_schema_of S_Rep card (Type (@{type_name fun}, [T1, T2])) =
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    39
    replicate_list (card T1) (atom_schema_of S_Rep card T2)
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    40
  | atom_schema_of (R_Rep true) card
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    41
                   (Type (@{type_name fun}, [T1, @{typ bool}])) =
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    42
    atom_schema_of S_Rep card T1
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    43
  | atom_schema_of (rep as R_Rep _) card (Type (@{type_name fun}, [T1, T2])) =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    44
    atom_schema_of S_Rep card T1 @ atom_schema_of rep card T2
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    45
  | atom_schema_of _ card (Type (@{type_name prod}, Ts)) =
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    46
    maps (atom_schema_of S_Rep card) Ts
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    47
  | atom_schema_of _ card T = [card T]
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    48
val arity_of = length ooo atom_schema_of
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    49
val atom_seqs_of = map (AtomSeq o rpair 0) ooo atom_schema_of
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    50
val atom_seq_product_of = foldl1 Product ooo atom_seqs_of
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    51
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    52
fun index_for_bound_var _ [_] 0 = 0
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    53
  | index_for_bound_var card (_ :: Ts) 0 =
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    54
    index_for_bound_var card Ts 0 + arity_of S_Rep card (hd Ts)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    55
  | index_for_bound_var card Ts n = index_for_bound_var card (tl Ts) (n - 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    56
fun vars_for_bound_var card R Ts j =
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    57
  map (curry Var 1) (index_seq (index_for_bound_var card Ts j)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    58
                               (arity_of R card (nth Ts j)))
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    59
val rel_expr_for_bound_var = foldl1 Product oooo vars_for_bound_var
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    60
fun decls_for R card Ts T =
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    61
  map2 (curry DeclOne o pair 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    62
       (index_seq (index_for_bound_var card (T :: Ts) 0)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    63
                  (arity_of R card (nth (T :: Ts) 0)))
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    64
       (atom_seqs_of R card T)
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    65
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    66
val atom_product = foldl1 Product o map Atom
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    67
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    68
val false_atom_num = 0
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    69
val true_atom_num = 1
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    70
val false_atom = Atom false_atom_num
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    71
val true_atom = Atom true_atom_num
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    72
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    73
fun kodkod_formula_from_term ctxt total card complete concrete frees =
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
    74
  let
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    75
    fun F_from_S_rep (SOME false) r = Not (RelEq (r, false_atom))
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    76
      | F_from_S_rep _ r = RelEq (r, true_atom)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    77
    fun S_rep_from_F NONE f = RelIf (f, true_atom, false_atom)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    78
      | S_rep_from_F (SOME true) f = RelIf (f, true_atom, None)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    79
      | S_rep_from_F (SOME false) f = RelIf (Not f, false_atom, None)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    80
    fun R_rep_from_S_rep (Type (@{type_name fun}, [T1, T2])) r =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    81
        if total andalso T2 = bool_T then
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    82
          let
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    83
            val jss = atom_schema_of S_Rep card T1 |> map (rpair 0)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    84
                      |> all_combinations
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    85
          in
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    86
            map2 (fn i => fn js =>
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    87
(*
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    88
                     RelIf (F_from_S_rep NONE (Project (r, [Num i])),
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    89
                            atom_product js, empty_n_ary_rel (length js))
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    90
*)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    91
                     Join (Project (r, [Num i]),
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    92
                           atom_product (false_atom_num :: js))
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    93
                 ) (index_seq 0 (length jss)) jss
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    94
            |> foldl1 Union
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    95
          end
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    96
        else
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    97
          let
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    98
            val jss = atom_schema_of S_Rep card T1 |> map (rpair 0)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
    99
                      |> all_combinations
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   100
            val arity2 = arity_of S_Rep card T2
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   101
          in
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   102
            map2 (fn i => fn js =>
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   103
                     Product (atom_product js,
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   104
                              Project (r, num_seq (i * arity2) arity2)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   105
                              |> R_rep_from_S_rep T2))
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   106
                 (index_seq 0 (length jss)) jss
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   107
            |> foldl1 Union
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   108
          end
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   109
      | R_rep_from_S_rep _ r = r
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   110
    fun S_rep_from_R_rep Ts (T as Type (@{type_name fun}, _)) r =
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   111
        Comprehension (decls_for S_Rep card Ts T,
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   112
            RelEq (R_rep_from_S_rep T
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   113
                       (rel_expr_for_bound_var card S_Rep (T :: Ts) 0), r))
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   114
      | S_rep_from_R_rep _ _ r = r
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   115
    fun partial_eq pos Ts (Type (@{type_name fun}, [T1, T2])) t1 t2 =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   116
        HOLogic.mk_all ("x", T1,
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   117
                        HOLogic.eq_const T2 $ (incr_boundvars 1 t1 $ Bound 0)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   118
                        $ (incr_boundvars 1 t2 $ Bound 0))
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   119
        |> to_F (SOME pos) Ts
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   120
      | partial_eq pos Ts T t1 t2 =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   121
        if pos andalso not (concrete T) then
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   122
          False
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   123
        else
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   124
          (t1, t2) |> pairself (to_R_rep Ts)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   125
                   |> (if pos then Some o Intersect else Lone o Union)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   126
    and to_F pos Ts t =
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   127
      (case t of
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   128
         @{const Not} $ t1 => Not (to_F (Option.map not pos) Ts t1)
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   129
       | @{const False} => False
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   130
       | @{const True} => True
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   131
       | Const (@{const_name All}, _) $ Abs (_, T, t') =>
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   132
         if pos = SOME true andalso not (complete T) then False
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   133
         else All (decls_for S_Rep card Ts T, to_F pos (T :: Ts) t')
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   134
       | (t0 as Const (@{const_name All}, _)) $ t1 =>
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   135
         to_F pos Ts (t0 $ eta_expand Ts t1 1)
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   136
       | Const (@{const_name Ex}, _) $ Abs (_, T, t') =>
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   137
         if pos = SOME false andalso not (complete T) then True
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   138
         else Exist (decls_for S_Rep card Ts T, to_F pos (T :: Ts) t')
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   139
       | (t0 as Const (@{const_name Ex}, _)) $ t1 =>
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   140
         to_F pos Ts (t0 $ eta_expand Ts t1 1)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   141
       | Const (@{const_name HOL.eq}, Type (_, [T, _])) $ t1 $ t2 =>
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   142
         (case pos of
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   143
            NONE => RelEq (to_R_rep Ts t1, to_R_rep Ts t2)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   144
          | SOME pos => partial_eq pos Ts T t1 t2)
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   145
       | Const (@{const_name ord_class.less_eq},
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   146
                Type (@{type_name fun},
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   147
                      [Type (@{type_name fun}, [T', @{typ bool}]), _]))
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   148
         $ t1 $ t2 =>
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   149
         (case pos of
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   150
            NONE => Subset (to_R_rep Ts t1, to_R_rep Ts t2)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   151
          | SOME true =>
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   152
            Subset (Difference (atom_seq_product_of S_Rep card T',
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   153
                                Join (to_R_rep Ts t1, false_atom)),
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   154
                    Join (to_R_rep Ts t2, true_atom))
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   155
          | SOME false =>
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   156
            Subset (Join (to_R_rep Ts t1, true_atom),
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   157
                    Difference (atom_seq_product_of S_Rep card T',
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   158
                                Join (to_R_rep Ts t2, false_atom))))
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   159
       | @{const HOL.conj} $ t1 $ t2 => And (to_F pos Ts t1, to_F pos Ts t2)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   160
       | @{const HOL.disj} $ t1 $ t2 => Or (to_F pos Ts t1, to_F pos Ts t2)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   161
       | @{const HOL.implies} $ t1 $ t2 =>
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   162
         Implies (to_F (Option.map not pos) Ts t1, to_F pos Ts t2)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   163
       | t1 $ t2 =>
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   164
         (case pos of
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   165
            NONE => Subset (to_S_rep Ts t2, to_R_rep Ts t1)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   166
          | SOME pos =>
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   167
            let
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   168
              val kt1 = to_R_rep Ts t1
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   169
              val kt2 = to_S_rep Ts t2
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   170
              val kT = atom_seq_product_of S_Rep card (fastype_of1 (Ts, t2))
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   171
            in
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   172
              if pos then
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   173
                Not (Subset (kt2, Difference (kT, Join (kt1, true_atom))))
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   174
              else
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   175
                Subset (kt2, Difference (kT, Join (kt1, false_atom)))
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   176
            end)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   177
       | _ => raise SAME ())
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   178
      handle SAME () => F_from_S_rep pos (to_R_rep Ts t)
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   179
    and to_S_rep Ts t =
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   180
      case t of
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   181
        Const (@{const_name Pair}, _) $ t1 $ t2 =>
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   182
        Product (to_S_rep Ts t1, to_S_rep Ts t2)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   183
      | Const (@{const_name Pair}, _) $ _ => to_S_rep Ts (eta_expand Ts t 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   184
      | Const (@{const_name Pair}, _) => to_S_rep Ts (eta_expand Ts t 2)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   185
      | Const (@{const_name fst}, _) $ t1 =>
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   186
        let val fst_arity = arity_of S_Rep card (fastype_of1 (Ts, t)) in
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   187
          Project (to_S_rep Ts t1, num_seq 0 fst_arity)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   188
        end
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   189
      | Const (@{const_name fst}, _) => to_S_rep Ts (eta_expand Ts t 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   190
      | Const (@{const_name snd}, _) $ t1 =>
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   191
        let
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   192
          val pair_arity = arity_of S_Rep card (fastype_of1 (Ts, t1))
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   193
          val snd_arity = arity_of S_Rep card (fastype_of1 (Ts, t))
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   194
          val fst_arity = pair_arity - snd_arity
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   195
        in Project (to_S_rep Ts t1, num_seq fst_arity snd_arity) end
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   196
      | Const (@{const_name snd}, _) => to_S_rep Ts (eta_expand Ts t 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   197
      | Bound j => rel_expr_for_bound_var card S_Rep Ts j
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   198
      | _ => S_rep_from_R_rep Ts (fastype_of1 (Ts, t)) (to_R_rep Ts t)
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   199
    and partial_set_op swap1 swap2 op1 op2 Ts t1 t2 =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   200
      let
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   201
        val kt1 = to_R_rep Ts t1
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   202
        val kt2 = to_R_rep Ts t2
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   203
        val (a11, a21) = (false_atom, true_atom) |> swap1 ? swap
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   204
        val (a12, a22) = (false_atom, true_atom) |> swap2 ? swap
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   205
      in
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   206
        Union (Product (op1 (Join (kt1, a11), Join (kt2, a12)), true_atom),
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   207
               Product (op2 (Join (kt1, a21), Join (kt2, a22)), false_atom))
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   208
      end
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   209
    and to_R_rep Ts t =
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   210
      (case t of
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   211
         @{const Not} => to_R_rep Ts (eta_expand Ts t 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   212
       | Const (@{const_name All}, _) => to_R_rep Ts (eta_expand Ts t 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   213
       | Const (@{const_name Ex}, _) => to_R_rep Ts (eta_expand Ts t 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   214
       | Const (@{const_name HOL.eq}, _) $ _ => to_R_rep Ts (eta_expand Ts t 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   215
       | Const (@{const_name HOL.eq}, _) => to_R_rep Ts (eta_expand Ts t 2)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   216
       | Const (@{const_name ord_class.less_eq},
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   217
                Type (@{type_name fun},
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   218
                      [Type (@{type_name fun}, [_, @{typ bool}]), _])) $ _ =>
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   219
         to_R_rep Ts (eta_expand Ts t 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   220
       | Const (@{const_name ord_class.less_eq}, _) =>
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   221
         to_R_rep Ts (eta_expand Ts t 2)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   222
       | @{const HOL.conj} $ _ => to_R_rep Ts (eta_expand Ts t 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   223
       | @{const HOL.conj} => to_R_rep Ts (eta_expand Ts t 2)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   224
       | @{const HOL.disj} $ _ => to_R_rep Ts (eta_expand Ts t 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   225
       | @{const HOL.disj} => to_R_rep Ts (eta_expand Ts t 2)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   226
       | @{const HOL.implies} $ _ => to_R_rep Ts (eta_expand Ts t 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   227
       | @{const HOL.implies} => to_R_rep Ts (eta_expand Ts t 2)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   228
       | Const (@{const_name bot_class.bot},
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   229
                T as Type (@{type_name fun}, [T', @{typ bool}])) =>
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   230
         if total then empty_n_ary_rel (arity_of (R_Rep total) card T)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   231
         else Product (atom_seq_product_of (R_Rep total) card T', false_atom)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   232
       | Const (@{const_name top_class.top},
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   233
                T as Type (@{type_name fun}, [T', @{typ bool}])) =>
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   234
         if total then atom_seq_product_of (R_Rep total) card T
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   235
         else Product (atom_seq_product_of (R_Rep total) card T', true_atom)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   236
       | Const (@{const_name insert}, Type (_, [T, _])) $ t1 $ t2 =>
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   237
         if total then
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   238
           Union (to_S_rep Ts t1, to_R_rep Ts t2)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   239
         else
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   240
           let
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   241
             val kt1 = to_S_rep Ts t1
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   242
             val kt2 = to_R_rep Ts t2
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   243
           in
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   244
             RelIf (Some kt1,
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   245
                    if arity_of S_Rep card T = 1 then
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   246
                      Override (kt2, Product (kt1, true_atom))
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   247
                    else
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   248
                      Union (Difference (kt2, Product (kt1, false_atom)),
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   249
                             Product (kt1, true_atom)),
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   250
                    Difference (kt2, Product (atom_seq_product_of S_Rep card T,
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   251
                                              false_atom)))
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   252
           end
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   253
       | Const (@{const_name insert}, _) $ _ => to_R_rep Ts (eta_expand Ts t 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   254
       | Const (@{const_name insert}, _) => to_R_rep Ts (eta_expand Ts t 2)
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   255
       | Const (@{const_name trancl},
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   256
                Type (_, [Type (_, [Type (_, [T', _]), _]), _])) $ t1 =>
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   257
         if arity_of S_Rep card T' = 1 then
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   258
           if total then
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   259
             Closure (to_R_rep Ts t1)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   260
           else
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   261
             let
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   262
               val kt1 = to_R_rep Ts t1
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   263
               val true_core_kt = Closure (Join (kt1, true_atom))
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   264
               val kTx =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   265
                 atom_seq_product_of S_Rep card (HOLogic.mk_prodT (`I T'))
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   266
               val false_mantle_kt =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   267
                 Difference (kTx,
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   268
                     Closure (Difference (kTx, Join (kt1, false_atom))))
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   269
             in
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   270
               Union (Product (Difference (false_mantle_kt, true_core_kt),
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   271
                               false_atom),
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   272
                      Product (true_core_kt, true_atom))
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   273
             end
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   274
         else
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   275
           raise NOT_SUPPORTED "transitive closure for function or pair type"
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   276
       | Const (@{const_name trancl}, _) => to_R_rep Ts (eta_expand Ts t 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   277
       | Const (@{const_name inf_class.inf},
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   278
                Type (@{type_name fun},
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   279
                      [Type (@{type_name fun}, [_, @{typ bool}]), _]))
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   280
         $ t1 $ t2 =>
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   281
         if total then Intersect (to_R_rep Ts t1, to_R_rep Ts t2)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   282
         else partial_set_op true true Intersect Union Ts t1 t2
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   283
       | Const (@{const_name inf_class.inf}, _) $ _ =>
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   284
         to_R_rep Ts (eta_expand Ts t 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   285
       | Const (@{const_name inf_class.inf}, _) =>
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   286
         to_R_rep Ts (eta_expand Ts t 2)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   287
       | Const (@{const_name sup_class.sup},
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   288
                Type (@{type_name fun},
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   289
                      [Type (@{type_name fun}, [_, @{typ bool}]), _]))
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   290
         $ t1 $ t2 =>
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   291
         if total then Union (to_R_rep Ts t1, to_R_rep Ts t2)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   292
         else partial_set_op true true Union Intersect Ts t1 t2
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   293
       | Const (@{const_name sup_class.sup}, _) $ _ =>
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   294
         to_R_rep Ts (eta_expand Ts t 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   295
       | Const (@{const_name sup_class.sup}, _) =>
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   296
         to_R_rep Ts (eta_expand Ts t 2)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   297
       | Const (@{const_name minus_class.minus},
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   298
                Type (@{type_name fun},
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   299
                      [Type (@{type_name fun}, [_, @{typ bool}]), _]))
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   300
         $ t1 $ t2 =>
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   301
         if total then Difference (to_R_rep Ts t1, to_R_rep Ts t2)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   302
         else partial_set_op true false Intersect Union Ts t1 t2
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   303
       | Const (@{const_name minus_class.minus},
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   304
                Type (@{type_name fun},
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   305
                      [Type (@{type_name fun}, [_, @{typ bool}]), _])) $ _ =>
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   306
         to_R_rep Ts (eta_expand Ts t 1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   307
       | Const (@{const_name minus_class.minus},
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   308
                Type (@{type_name fun},
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   309
                      [Type (@{type_name fun}, [_, @{typ bool}]), _])) =>
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   310
         to_R_rep Ts (eta_expand Ts t 2)
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   311
       | Const (@{const_name Pair}, _) $ _ $ _ => to_S_rep Ts t
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   312
       | Const (@{const_name Pair}, _) $ _ => to_S_rep Ts t
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   313
       | Const (@{const_name Pair}, _) => to_S_rep Ts t
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   314
       | Const (@{const_name fst}, _) $ _ => raise SAME ()
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   315
       | Const (@{const_name fst}, _) => raise SAME ()
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   316
       | Const (@{const_name snd}, _) $ _ => raise SAME ()
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   317
       | Const (@{const_name snd}, _) => raise SAME ()
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   318
       | @{const False} => false_atom
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   319
       | @{const True} => true_atom
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   320
       | Free (x as (_, T)) =>
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   321
         Rel (arity_of (R_Rep total) card T, find_index (curry (op =) x) frees)
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   322
       | Term.Var _ => raise NOT_SUPPORTED "schematic variables"
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   323
       | Bound _ => raise SAME ()
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   324
       | Abs (_, T, t') =>
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   325
         (case (total, fastype_of1 (T :: Ts, t')) of
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   326
            (true, @{typ bool}) =>
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   327
            Comprehension (decls_for S_Rep card Ts T, to_F NONE (T :: Ts) t')
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   328
          | (_, T') =>
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   329
            Comprehension (decls_for S_Rep card Ts T @
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   330
                           decls_for (R_Rep total) card (T :: Ts) T',
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   331
                           Subset (rel_expr_for_bound_var card (R_Rep total)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   332
                                                          (T' :: T :: Ts) 0,
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   333
                                   to_R_rep (T :: Ts) t')))
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   334
       | t1 $ t2 =>
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   335
         (case fastype_of1 (Ts, t) of
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   336
            @{typ bool} =>
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   337
            if total then
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   338
              S_rep_from_F NONE (to_F NONE Ts t)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   339
            else
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   340
              RelIf (to_F (SOME true) Ts t, true_atom,
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   341
                     RelIf (Not (to_F (SOME false) Ts t), false_atom,
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   342
                     None))
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   343
          | T =>
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   344
            let val T2 = fastype_of1 (Ts, t2) in
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   345
              case arity_of S_Rep card T2 of
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   346
                1 => Join (to_S_rep Ts t2, to_R_rep Ts t1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   347
              | arity2 =>
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   348
                let val res_arity = arity_of (R_Rep total) card T in
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   349
                  Project (Intersect
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   350
                      (Product (to_S_rep Ts t2,
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   351
                                atom_seq_product_of (R_Rep total) card T),
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   352
                       to_R_rep Ts t1),
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   353
                      num_seq arity2 res_arity)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   354
                end
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   355
            end)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   356
       | _ => raise NOT_SUPPORTED ("term " ^
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   357
                                   quote (Syntax.string_of_term ctxt t)))
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   358
      handle SAME () => R_rep_from_S_rep (fastype_of1 (Ts, t)) (to_S_rep Ts t)
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   359
  in to_F (if total then NONE else SOME true) [] end
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   360
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   361
fun bound_for_free total card i (s, T) =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   362
  let val js = atom_schema_of (R_Rep total) card T in
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   363
    ([((length js, i), s)],
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   364
     [TupleSet [], atom_schema_of (R_Rep total) card T |> map (rpair 0)
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   365
                   |> tuple_set_from_atom_schema])
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   366
  end
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   367
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   368
fun declarative_axiom_for_rel_expr total card Ts
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   369
                                   (Type (@{type_name fun}, [T1, T2])) r =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   370
    if total andalso body_type T2 = bool_T then
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   371
      True
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   372
    else
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   373
      All (decls_for S_Rep card Ts T1,
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   374
           declarative_axiom_for_rel_expr total card (T1 :: Ts) T2
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   375
               (List.foldl Join r (vars_for_bound_var card S_Rep (T1 :: Ts) 0)))
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   376
  | declarative_axiom_for_rel_expr total _ _ _ r =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   377
    (if total then One else Lone) r
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   378
fun declarative_axiom_for_free total card i (_, T) =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   379
  declarative_axiom_for_rel_expr total card [] T
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   380
      (Rel (arity_of (R_Rep total) card T, i))
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   381
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   382
fun kodkod_problem_from_term ctxt total raw_card raw_infinite t =
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   383
  let
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   384
    val thy = ProofContext.theory_of ctxt
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   385
    fun card (Type (@{type_name fun}, [T1, T2])) =
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   386
        reasonable_power (card T2) (card T1)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   387
      | card (Type (@{type_name prod}, [T1, T2])) = card T1 * card T2
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   388
      | card @{typ bool} = 2
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   389
      | card T = Int.max (1, raw_card T)
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   390
    fun complete (Type (@{type_name fun}, [T1, T2])) =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   391
        concrete T1 andalso complete T2
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   392
      | complete (Type (@{type_name prod}, Ts)) = forall complete Ts
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   393
      | complete T = not (raw_infinite T)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   394
    and concrete (Type (@{type_name fun}, [T1, T2])) =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   395
        complete T1 andalso concrete T2
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   396
      | concrete (Type (@{type_name prod}, Ts)) = forall concrete Ts
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   397
      | concrete _ = true
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   398
    val neg_t = @{const Not} $ Object_Logic.atomize_term thy t
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   399
    val _ = fold_types (K o check_type ctxt raw_infinite) neg_t ()
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   400
    val frees = Term.add_frees neg_t []
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   401
    val bounds =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   402
      map2 (bound_for_free total card) (index_seq 0 (length frees)) frees
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   403
    val declarative_axioms =
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   404
      map2 (declarative_axiom_for_free total card)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   405
           (index_seq 0 (length frees)) frees
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   406
    val formula =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   407
      neg_t |> kodkod_formula_from_term ctxt total card complete concrete frees 
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   408
            |> fold_rev (curry And) declarative_axioms
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   409
    val univ_card = univ_card 0 0 0 bounds formula
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   410
  in
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   411
    {comment = "", settings = [], univ_card = univ_card, tuple_assigns = [],
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   412
     bounds = bounds, int_bounds = [], expr_assigns = [], formula = formula}
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   413
  end
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   414
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   415
fun solve_any_kodkod_problem thy problems =
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   416
  let
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   417
    val {debug, overlord, ...} = Nitpick_Isar.default_params thy []
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   418
    val max_threads = 1
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   419
    val max_solutions = 1
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   420
  in
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   421
    case solve_any_problem debug overlord NONE max_threads max_solutions
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   422
                           problems of
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   423
      JavaNotInstalled => "unknown"
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   424
    | JavaTooOld => "unknown"
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   425
    | KodkodiNotInstalled => "unknown"
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   426
    | Normal ([], _, _) => "none"
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   427
    | Normal _ => "genuine"
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   428
    | TimedOut _ => "unknown"
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   429
    | Error (s, _) => error ("Kodkod error: " ^ s)
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   430
  end
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   431
45062
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   432
val default_raw_infinite = member (op =) [@{typ nat}, @{typ int}]
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   433
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   434
fun minipick ctxt n t =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   435
  let
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   436
    val thy = ProofContext.theory_of ctxt
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   437
    val {total_consts, ...} = Nitpick_Isar.default_params thy []
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   438
    val totals =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   439
      total_consts |> Option.map single |> the_default [true, false]
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   440
    fun problem_for (total, k) =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   441
      kodkod_problem_from_term ctxt total (K k) default_raw_infinite t
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   442
  in
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   443
    (totals, 1 upto n)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   444
    |-> map_product pair
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   445
    |> map problem_for
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   446
    |> solve_any_kodkod_problem (Proof_Context.theory_of ctxt)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   447
  end
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   448
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   449
fun minipick_expect ctxt expect n t =
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   450
  if getenv "KODKODI" <> "" then
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   451
    if minipick ctxt n t = expect then ()
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   452
    else error ("\"minipick_expect\" expected " ^ quote expect)
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   453
  else
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   454
    ()
9598cada31b3 first step towards extending Minipick with more translations
blanchet
parents: 45035
diff changeset
   455
45035
60d2c03d5c70 reintroduced Minipick as Nitpick example
blanchet
parents:
diff changeset
   456
end;