src/HOL/Tools/Nitpick/nitpick_nut.ML
author wenzelm
Tue Sep 26 20:54:40 2017 +0200 (24 months ago)
changeset 66695 91500c024c7f
parent 61324 d4ec7594f558
child 69593 3dda49e08b9d
permissions -rw-r--r--
tuned;
blanchet@33982
     1
(*  Title:      HOL/Tools/Nitpick/nitpick_nut.ML
blanchet@33192
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@34982
     3
    Copyright   2008, 2009, 2010
blanchet@33192
     4
blanchet@33192
     5
Nitpick underlying terms (nuts).
blanchet@33192
     6
*)
blanchet@33192
     7
blanchet@33192
     8
signature NITPICK_NUT =
blanchet@33192
     9
sig
blanchet@35070
    10
  type hol_context = Nitpick_HOL.hol_context
blanchet@33232
    11
  type scope = Nitpick_Scope.scope
blanchet@33232
    12
  type name_pool = Nitpick_Peephole.name_pool
blanchet@33232
    13
  type rep = Nitpick_Rep.rep
blanchet@33192
    14
blanchet@33192
    15
  datatype cst =
blanchet@33192
    16
    False |
blanchet@33192
    17
    True |
blanchet@33192
    18
    Iden |
blanchet@33192
    19
    Num of int |
blanchet@33192
    20
    Unknown |
blanchet@33192
    21
    Unrep |
blanchet@33192
    22
    Suc |
blanchet@33192
    23
    Add |
blanchet@33192
    24
    Subtract |
blanchet@33192
    25
    Multiply |
blanchet@33192
    26
    Divide |
blanchet@33192
    27
    Gcd |
blanchet@33192
    28
    Lcm |
blanchet@33192
    29
    Fracs |
blanchet@33192
    30
    NormFrac |
blanchet@33192
    31
    NatToInt |
blanchet@33192
    32
    IntToNat
blanchet@33192
    33
blanchet@33192
    34
  datatype op1 =
blanchet@33192
    35
    Not |
blanchet@33192
    36
    Finite |
blanchet@33192
    37
    Converse |
blanchet@33192
    38
    Closure |
blanchet@33192
    39
    SingletonSet |
blanchet@34936
    40
    IsUnknown |
blanchet@35671
    41
    SafeThe |
blanchet@33192
    42
    First |
blanchet@33192
    43
    Second |
blanchet@33192
    44
    Cast
blanchet@33192
    45
blanchet@33192
    46
  datatype op2 =
blanchet@33192
    47
    All |
blanchet@33192
    48
    Exist |
blanchet@33192
    49
    Or |
blanchet@33192
    50
    And |
blanchet@33192
    51
    Less |
blanchet@33192
    52
    DefEq |
blanchet@33192
    53
    Eq |
blanchet@33192
    54
    Triad |
blanchet@33192
    55
    Composition |
blanchet@33192
    56
    Apply |
blanchet@33192
    57
    Lambda
blanchet@33192
    58
blanchet@33192
    59
  datatype op3 =
blanchet@33192
    60
    Let |
blanchet@33192
    61
    If
blanchet@33192
    62
blanchet@33192
    63
  datatype nut =
blanchet@33192
    64
    Cst of cst * typ * rep |
blanchet@33192
    65
    Op1 of op1 * typ * rep * nut |
blanchet@33192
    66
    Op2 of op2 * typ * rep * nut * nut |
blanchet@33192
    67
    Op3 of op3 * typ * rep * nut * nut * nut |
blanchet@33192
    68
    Tuple of typ * rep * nut list |
blanchet@33192
    69
    Construct of nut list * typ * rep * nut list |
blanchet@33192
    70
    BoundName of int * typ * rep * string |
blanchet@33192
    71
    FreeName of string * typ * rep |
blanchet@33192
    72
    ConstName of string * typ * rep |
blanchet@33192
    73
    BoundRel of Kodkod.n_ary_index * typ * rep * string |
blanchet@33192
    74
    FreeRel of Kodkod.n_ary_index * typ * rep * string |
blanchet@33192
    75
    RelReg of int * typ * rep |
blanchet@33192
    76
    FormulaReg of int * typ * rep
blanchet@33192
    77
blanchet@33192
    78
  structure NameTable : TABLE
blanchet@33192
    79
blanchet@33192
    80
  exception NUT of string * nut list
blanchet@33192
    81
blanchet@33192
    82
  val string_for_nut : Proof.context -> nut -> string
blanchet@33192
    83
  val inline_nut : nut -> bool
blanchet@33192
    84
  val type_of : nut -> typ
blanchet@33192
    85
  val rep_of : nut -> rep
blanchet@33192
    86
  val nickname_of : nut -> string
blanchet@33192
    87
  val is_skolem_name : nut -> bool
blanchet@33192
    88
  val is_eval_name : nut -> bool
blanchet@33192
    89
  val is_Cst : cst -> nut -> bool
blanchet@33192
    90
  val fold_nut : (nut -> 'a -> 'a) -> nut -> 'a -> 'a
blanchet@33192
    91
  val map_nut : (nut -> nut) -> nut -> nut
blanchet@33192
    92
  val untuple : (nut -> 'a) -> nut -> 'a list
blanchet@33192
    93
  val add_free_and_const_names :
blanchet@33192
    94
    nut -> nut list * nut list -> nut list * nut list
blanchet@33192
    95
  val name_ord : (nut * nut) -> order
blanchet@33192
    96
  val the_name : 'a NameTable.table -> nut -> 'a
blanchet@33192
    97
  val the_rel : nut NameTable.table -> nut -> Kodkod.n_ary_index
blanchet@35070
    98
  val nut_from_term : hol_context -> op2 -> term -> nut
blanchet@37262
    99
  val is_fully_representable_set : nut -> bool
blanchet@33192
   100
  val choose_reps_for_free_vars :
blanchet@55889
   101
    scope -> (string * typ) list -> nut list -> rep NameTable.table
blanchet@38170
   102
    -> nut list * rep NameTable.table
blanchet@33192
   103
  val choose_reps_for_consts :
blanchet@33192
   104
    scope -> bool -> nut list -> rep NameTable.table
blanchet@33192
   105
    -> nut list * rep NameTable.table
blanchet@33192
   106
  val choose_reps_for_all_sels :
blanchet@33192
   107
    scope -> rep NameTable.table -> nut list * rep NameTable.table
blanchet@33192
   108
  val choose_reps_in_nut :
blanchet@33192
   109
    scope -> bool -> rep NameTable.table -> bool -> nut -> nut
blanchet@33192
   110
  val rename_free_vars :
blanchet@33192
   111
    nut list -> name_pool -> nut NameTable.table
blanchet@33192
   112
    -> nut list * name_pool * nut NameTable.table
blanchet@33192
   113
  val rename_vars_in_nut : name_pool -> nut NameTable.table -> nut -> nut
blanchet@33192
   114
end;
blanchet@33192
   115
blanchet@33232
   116
structure Nitpick_Nut : NITPICK_NUT =
blanchet@33192
   117
struct
blanchet@33192
   118
blanchet@33232
   119
open Nitpick_Util
blanchet@33232
   120
open Nitpick_HOL
blanchet@33232
   121
open Nitpick_Scope
blanchet@33232
   122
open Nitpick_Peephole
blanchet@33232
   123
open Nitpick_Rep
blanchet@33192
   124
blanchet@34126
   125
structure KK = Kodkod
blanchet@34126
   126
blanchet@33192
   127
datatype cst =
blanchet@33192
   128
  False |
blanchet@33192
   129
  True |
blanchet@33192
   130
  Iden |
blanchet@33192
   131
  Num of int |
blanchet@33192
   132
  Unknown |
blanchet@33192
   133
  Unrep |
blanchet@33192
   134
  Suc |
blanchet@33192
   135
  Add |
blanchet@33192
   136
  Subtract |
blanchet@33192
   137
  Multiply |
blanchet@33192
   138
  Divide |
blanchet@33192
   139
  Gcd |
blanchet@33192
   140
  Lcm |
blanchet@33192
   141
  Fracs |
blanchet@33192
   142
  NormFrac |
blanchet@33192
   143
  NatToInt |
blanchet@33192
   144
  IntToNat
blanchet@33192
   145
blanchet@33192
   146
datatype op1 =
blanchet@33192
   147
  Not |
blanchet@33192
   148
  Finite |
blanchet@33192
   149
  Converse |
blanchet@33192
   150
  Closure |
blanchet@33192
   151
  SingletonSet |
blanchet@34936
   152
  IsUnknown |
blanchet@35671
   153
  SafeThe |
blanchet@33192
   154
  First |
blanchet@33192
   155
  Second |
blanchet@33192
   156
  Cast
blanchet@33192
   157
blanchet@33192
   158
datatype op2 =
blanchet@33192
   159
  All |
blanchet@33192
   160
  Exist |
blanchet@33192
   161
  Or |
blanchet@33192
   162
  And |
blanchet@33192
   163
  Less |
blanchet@33192
   164
  DefEq |
blanchet@33192
   165
  Eq |
blanchet@33192
   166
  Triad |
blanchet@33192
   167
  Composition |
blanchet@33192
   168
  Apply |
blanchet@33192
   169
  Lambda
blanchet@33192
   170
blanchet@33192
   171
datatype op3 =
blanchet@33192
   172
  Let |
blanchet@33192
   173
  If
blanchet@33192
   174
blanchet@33192
   175
datatype nut =
blanchet@33192
   176
  Cst of cst * typ * rep |
blanchet@33192
   177
  Op1 of op1 * typ * rep * nut |
blanchet@33192
   178
  Op2 of op2 * typ * rep * nut * nut |
blanchet@33192
   179
  Op3 of op3 * typ * rep * nut * nut * nut |
blanchet@33192
   180
  Tuple of typ * rep * nut list |
blanchet@33192
   181
  Construct of nut list * typ * rep * nut list |
blanchet@33192
   182
  BoundName of int * typ * rep * string |
blanchet@33192
   183
  FreeName of string * typ * rep |
blanchet@33192
   184
  ConstName of string * typ * rep |
blanchet@34126
   185
  BoundRel of KK.n_ary_index * typ * rep * string |
blanchet@34126
   186
  FreeRel of KK.n_ary_index * typ * rep * string |
blanchet@33192
   187
  RelReg of int * typ * rep |
blanchet@33192
   188
  FormulaReg of int * typ * rep
blanchet@33192
   189
blanchet@33192
   190
exception NUT of string * nut list
blanchet@33192
   191
blanchet@38190
   192
fun string_for_cst False = "False"
blanchet@33192
   193
  | string_for_cst True = "True"
blanchet@33192
   194
  | string_for_cst Iden = "Iden"
blanchet@33192
   195
  | string_for_cst (Num j) = "Num " ^ signed_string_of_int j
blanchet@33192
   196
  | string_for_cst Unknown = "Unknown"
blanchet@33192
   197
  | string_for_cst Unrep = "Unrep"
blanchet@33192
   198
  | string_for_cst Suc = "Suc"
blanchet@33192
   199
  | string_for_cst Add = "Add"
blanchet@33192
   200
  | string_for_cst Subtract = "Subtract"
blanchet@33192
   201
  | string_for_cst Multiply = "Multiply"
blanchet@33192
   202
  | string_for_cst Divide = "Divide"
blanchet@33192
   203
  | string_for_cst Gcd = "Gcd"
blanchet@33192
   204
  | string_for_cst Lcm = "Lcm"
blanchet@33192
   205
  | string_for_cst Fracs = "Fracs"
blanchet@33192
   206
  | string_for_cst NormFrac = "NormFrac"
blanchet@33192
   207
  | string_for_cst NatToInt = "NatToInt"
blanchet@33192
   208
  | string_for_cst IntToNat = "IntToNat"
blanchet@33192
   209
blanchet@33192
   210
fun string_for_op1 Not = "Not"
blanchet@33192
   211
  | string_for_op1 Finite = "Finite"
blanchet@33192
   212
  | string_for_op1 Converse = "Converse"
blanchet@33192
   213
  | string_for_op1 Closure = "Closure"
blanchet@33192
   214
  | string_for_op1 SingletonSet = "SingletonSet"
blanchet@34936
   215
  | string_for_op1 IsUnknown = "IsUnknown"
blanchet@35671
   216
  | string_for_op1 SafeThe = "SafeThe"
blanchet@33192
   217
  | string_for_op1 First = "First"
blanchet@33192
   218
  | string_for_op1 Second = "Second"
blanchet@33192
   219
  | string_for_op1 Cast = "Cast"
blanchet@33192
   220
blanchet@33192
   221
fun string_for_op2 All = "All"
blanchet@33192
   222
  | string_for_op2 Exist = "Exist"
blanchet@33192
   223
  | string_for_op2 Or = "Or"
blanchet@33192
   224
  | string_for_op2 And = "And"
blanchet@33192
   225
  | string_for_op2 Less = "Less"
blanchet@33192
   226
  | string_for_op2 DefEq = "DefEq"
blanchet@33192
   227
  | string_for_op2 Eq = "Eq"
blanchet@33192
   228
  | string_for_op2 Triad = "Triad"
blanchet@33192
   229
  | string_for_op2 Composition = "Composition"
blanchet@33192
   230
  | string_for_op2 Apply = "Apply"
blanchet@33192
   231
  | string_for_op2 Lambda = "Lambda"
blanchet@33192
   232
blanchet@33192
   233
fun string_for_op3 Let = "Let"
blanchet@33192
   234
  | string_for_op3 If = "If"
blanchet@33192
   235
blanchet@33192
   236
fun basic_string_for_nut indent ctxt u =
blanchet@33192
   237
  let
blanchet@33192
   238
    val sub = basic_string_for_nut (indent + 1) ctxt
blanchet@33192
   239
  in
blanchet@33192
   240
    (if indent = 0 then "" else "\n" ^ implode (replicate (2 * indent) " ")) ^
blanchet@33192
   241
    "(" ^
blanchet@33192
   242
    (case u of
blanchet@33192
   243
       Cst (c, T, R) =>
blanchet@33192
   244
       "Cst " ^ string_for_cst c ^ " " ^ Syntax.string_of_typ ctxt T ^ " " ^
blanchet@33192
   245
       string_for_rep R
blanchet@33192
   246
     | Op1 (oper, T, R, u1) =>
blanchet@33192
   247
       "Op1 " ^ string_for_op1 oper ^ " " ^ Syntax.string_of_typ ctxt T ^ " " ^
blanchet@33192
   248
       string_for_rep R ^ " " ^ sub u1
blanchet@33192
   249
     | Op2 (oper, T, R, u1, u2) =>
blanchet@33192
   250
       "Op2 " ^ string_for_op2 oper ^ " " ^ Syntax.string_of_typ ctxt T ^ " " ^
blanchet@33192
   251
       string_for_rep R ^ " " ^ sub u1 ^ " " ^ sub u2
blanchet@33192
   252
     | Op3 (oper, T, R, u1, u2, u3) =>
blanchet@33192
   253
       "Op3 " ^ string_for_op3 oper ^ " " ^ Syntax.string_of_typ ctxt T ^ " " ^
blanchet@33192
   254
       string_for_rep R ^ " " ^ sub u1 ^ " " ^ sub u2 ^ " " ^ sub u3
blanchet@33192
   255
     | Tuple (T, R, us) =>
blanchet@33192
   256
       "Tuple " ^ Syntax.string_of_typ ctxt T ^ " " ^ string_for_rep R ^
blanchet@33192
   257
       implode (map sub us)
blanchet@33192
   258
     | Construct (us', T, R, us) =>
blanchet@35665
   259
       "Construct " ^ implode (map sub us') ^ " " ^
blanchet@35665
   260
       Syntax.string_of_typ ctxt T ^ " " ^ string_for_rep R ^ " " ^
blanchet@35665
   261
       implode (map sub us)
blanchet@33192
   262
     | BoundName (j, T, R, nick) =>
blanchet@33192
   263
       "BoundName " ^ signed_string_of_int j ^ " " ^
blanchet@33192
   264
       Syntax.string_of_typ ctxt T ^ " " ^ string_for_rep R ^ " " ^ nick
blanchet@33192
   265
     | FreeName (s, T, R) =>
blanchet@33192
   266
       "FreeName " ^ s ^ " " ^ Syntax.string_of_typ ctxt T ^ " " ^
blanchet@33192
   267
       string_for_rep R
blanchet@33192
   268
     | ConstName (s, T, R) =>
blanchet@33192
   269
       "ConstName " ^ s ^ " " ^ Syntax.string_of_typ ctxt T ^ " " ^
blanchet@33192
   270
       string_for_rep R
blanchet@33192
   271
     | BoundRel ((n, j), T, R, nick) =>
blanchet@33192
   272
       "BoundRel " ^ string_of_int n ^ "." ^ signed_string_of_int j ^ " " ^
blanchet@33192
   273
       Syntax.string_of_typ ctxt T ^ " " ^ string_for_rep R ^ " " ^ nick
blanchet@33192
   274
     | FreeRel ((n, j), T, R, nick) =>
blanchet@33192
   275
       "FreeRel " ^ string_of_int n ^ "." ^ signed_string_of_int j ^ " " ^
blanchet@33192
   276
       Syntax.string_of_typ ctxt T ^ " " ^ string_for_rep R ^ " " ^ nick
blanchet@33192
   277
     | RelReg (j, T, R) =>
blanchet@33192
   278
       "RelReg " ^ signed_string_of_int j ^ " " ^ Syntax.string_of_typ ctxt T ^
blanchet@33192
   279
       " " ^ string_for_rep R
blanchet@33192
   280
     | FormulaReg (j, T, R) =>
blanchet@33192
   281
       "FormulaReg " ^ signed_string_of_int j ^ " " ^
blanchet@33192
   282
       Syntax.string_of_typ ctxt T ^ " " ^ string_for_rep R) ^
blanchet@33192
   283
    ")"
blanchet@33192
   284
  end
blanchet@33192
   285
val string_for_nut = basic_string_for_nut 0
blanchet@33192
   286
blanchet@33192
   287
fun inline_nut (Op1 _) = false
blanchet@33192
   288
  | inline_nut (Op2 _) = false
blanchet@33192
   289
  | inline_nut (Op3 _) = false
blanchet@33192
   290
  | inline_nut (Tuple (_, _, us)) = forall inline_nut us
blanchet@33192
   291
  | inline_nut _ = true
blanchet@33192
   292
blanchet@33192
   293
fun type_of (Cst (_, T, _)) = T
blanchet@33192
   294
  | type_of (Op1 (_, T, _, _)) = T
blanchet@33192
   295
  | type_of (Op2 (_, T, _, _, _)) = T
blanchet@33192
   296
  | type_of (Op3 (_, T, _, _, _, _)) = T
blanchet@33192
   297
  | type_of (Tuple (T, _, _)) = T
blanchet@33192
   298
  | type_of (Construct (_, T, _, _)) = T
blanchet@33192
   299
  | type_of (BoundName (_, T, _, _)) = T
blanchet@33192
   300
  | type_of (FreeName (_, T, _)) = T
blanchet@33192
   301
  | type_of (ConstName (_, T, _)) = T
blanchet@33192
   302
  | type_of (BoundRel (_, T, _, _)) = T
blanchet@33192
   303
  | type_of (FreeRel (_, T, _, _)) = T
blanchet@33192
   304
  | type_of (RelReg (_, T, _)) = T
blanchet@33192
   305
  | type_of (FormulaReg (_, T, _)) = T
blanchet@33192
   306
blanchet@33192
   307
fun rep_of (Cst (_, _, R)) = R
blanchet@33192
   308
  | rep_of (Op1 (_, _, R, _)) = R
blanchet@33192
   309
  | rep_of (Op2 (_, _, R, _, _)) = R
blanchet@33192
   310
  | rep_of (Op3 (_, _, R, _, _, _)) = R
blanchet@33192
   311
  | rep_of (Tuple (_, R, _)) = R
blanchet@33192
   312
  | rep_of (Construct (_, _, R, _)) = R
blanchet@33192
   313
  | rep_of (BoundName (_, _, R, _)) = R
blanchet@33192
   314
  | rep_of (FreeName (_, _, R)) = R
blanchet@33192
   315
  | rep_of (ConstName (_, _, R)) = R
blanchet@33192
   316
  | rep_of (BoundRel (_, _, R, _)) = R
blanchet@33192
   317
  | rep_of (FreeRel (_, _, R, _)) = R
blanchet@33192
   318
  | rep_of (RelReg (_, _, R)) = R
blanchet@33192
   319
  | rep_of (FormulaReg (_, _, R)) = R
blanchet@33192
   320
blanchet@33192
   321
fun nickname_of (BoundName (_, _, _, nick)) = nick
blanchet@33192
   322
  | nickname_of (FreeName (s, _, _)) = s
blanchet@33192
   323
  | nickname_of (ConstName (s, _, _)) = s
blanchet@33192
   324
  | nickname_of (BoundRel (_, _, _, nick)) = nick
blanchet@33192
   325
  | nickname_of (FreeRel (_, _, _, nick)) = nick
blanchet@33232
   326
  | nickname_of u = raise NUT ("Nitpick_Nut.nickname_of", [u])
blanchet@33192
   327
blanchet@33192
   328
fun is_skolem_name u =
blanchet@33192
   329
  space_explode name_sep (nickname_of u)
blanchet@33192
   330
  |> exists (String.isPrefix skolem_prefix)
blanchet@33232
   331
  handle NUT ("Nitpick_Nut.nickname_of", _) => false
blanchet@55889
   332
blanchet@33192
   333
fun is_eval_name u =
blanchet@33192
   334
  String.isPrefix eval_prefix (nickname_of u)
blanchet@33232
   335
  handle NUT ("Nitpick_Nut.nickname_of", _) => false
blanchet@55889
   336
blanchet@33192
   337
fun is_Cst cst (Cst (cst', _, _)) = (cst = cst')
blanchet@33192
   338
  | is_Cst _ _ = false
blanchet@33192
   339
blanchet@33192
   340
fun fold_nut f u =
blanchet@33192
   341
  case u of
blanchet@33192
   342
    Op1 (_, _, _, u1) => fold_nut f u1
blanchet@33192
   343
  | Op2 (_, _, _, u1, u2) => fold_nut f u1 #> fold_nut f u2
blanchet@33192
   344
  | Op3 (_, _, _, u1, u2, u3) => fold_nut f u1 #> fold_nut f u2 #> fold_nut f u3
blanchet@33192
   345
  | Tuple (_, _, us) => fold (fold_nut f) us
blanchet@33192
   346
  | Construct (us', _, _, us) => fold (fold_nut f) us #> fold (fold_nut f) us'
blanchet@33192
   347
  | _ => f u
blanchet@55889
   348
blanchet@33192
   349
fun map_nut f u =
blanchet@33192
   350
  case u of
blanchet@33192
   351
    Op1 (oper, T, R, u1) => Op1 (oper, T, R, map_nut f u1)
blanchet@33192
   352
  | Op2 (oper, T, R, u1, u2) => Op2 (oper, T, R, map_nut f u1, map_nut f u2)
blanchet@33192
   353
  | Op3 (oper, T, R, u1, u2, u3) =>
blanchet@33192
   354
    Op3 (oper, T, R, map_nut f u1, map_nut f u2, map_nut f u3)
blanchet@33192
   355
  | Tuple (T, R, us) => Tuple (T, R, map (map_nut f) us)
blanchet@33192
   356
  | Construct (us', T, R, us) =>
blanchet@33192
   357
    Construct (map (map_nut f) us', T, R, map (map_nut f) us)
blanchet@33192
   358
  | _ => f u
blanchet@33192
   359
blanchet@33192
   360
fun name_ord (BoundName (j1, _, _, _), BoundName (j2, _, _, _)) =
blanchet@33192
   361
    int_ord (j1, j2)
blanchet@33192
   362
  | name_ord (BoundName _, _) = LESS
blanchet@33192
   363
  | name_ord (_, BoundName _) = GREATER
blanchet@33192
   364
  | name_ord (FreeName (s1, T1, _), FreeName (s2, T2, _)) =
blanchet@33192
   365
    (case fast_string_ord (s1, s2) of
wenzelm@35408
   366
       EQUAL => Term_Ord.typ_ord (T1, T2)
blanchet@33192
   367
     | ord => ord)
blanchet@33192
   368
  | name_ord (FreeName _, _) = LESS
blanchet@33192
   369
  | name_ord (_, FreeName _) = GREATER
blanchet@33192
   370
  | name_ord (ConstName (s1, T1, _), ConstName (s2, T2, _)) =
blanchet@33192
   371
    (case fast_string_ord (s1, s2) of
wenzelm@35408
   372
       EQUAL => Term_Ord.typ_ord (T1, T2)
blanchet@33192
   373
     | ord => ord)
blanchet@33232
   374
  | name_ord (u1, u2) = raise NUT ("Nitpick_Nut.name_ord", [u1, u2])
blanchet@33192
   375
blanchet@36913
   376
fun num_occurrences_in_nut needle_u stack_u =
blanchet@33192
   377
  fold_nut (fn u => if u = needle_u then Integer.add 1 else I) stack_u 0
blanchet@36913
   378
val is_subnut_of = not_equal 0 oo num_occurrences_in_nut
blanchet@33192
   379
blanchet@33192
   380
fun substitute_in_nut needle_u needle_u' =
blanchet@33192
   381
  map_nut (fn u => if u = needle_u then needle_u' else u)
blanchet@33192
   382
blanchet@33192
   383
val add_free_and_const_names =
blanchet@33192
   384
  fold_nut (fn u => case u of
blanchet@33192
   385
                      FreeName _ => apfst (insert (op =) u)
blanchet@33192
   386
                    | ConstName _ => apsnd (insert (op =) u)
blanchet@33192
   387
                    | _ => I)
blanchet@33192
   388
blanchet@33192
   389
fun modify_name_rep (BoundName (j, T, _, nick)) R = BoundName (j, T, R, nick)
blanchet@33192
   390
  | modify_name_rep (FreeName (s, T, _)) R = FreeName (s, T, R)
blanchet@33192
   391
  | modify_name_rep (ConstName (s, T, _)) R = ConstName (s, T, R)
blanchet@33232
   392
  | modify_name_rep u _ = raise NUT ("Nitpick_Nut.modify_name_rep", [u])
blanchet@33192
   393
blanchet@33192
   394
structure NameTable = Table(type key = nut val ord = name_ord)
blanchet@33192
   395
blanchet@33192
   396
fun the_name table name =
blanchet@33192
   397
  case NameTable.lookup table name of
blanchet@33192
   398
    SOME u => u
blanchet@33232
   399
  | NONE => raise NUT ("Nitpick_Nut.the_name", [name])
blanchet@55889
   400
blanchet@33192
   401
fun the_rel table name =
blanchet@33192
   402
  case the_name table name of
blanchet@33192
   403
    FreeRel (x, _, _, _) => x
blanchet@33232
   404
  | u => raise NUT ("Nitpick_Nut.the_rel", [u])
blanchet@33192
   405
blanchet@33192
   406
fun mk_fst (_, Const (@{const_name Pair}, T) $ t1 $ _) = (domain_type T, t1)
blanchet@33192
   407
  | mk_fst (T, t) =
blanchet@33192
   408
    let val res_T = fst (HOLogic.dest_prodT T) in
blanchet@33192
   409
      (res_T, Const (@{const_name fst}, T --> res_T) $ t)
blanchet@33192
   410
    end
blanchet@55889
   411
blanchet@33192
   412
fun mk_snd (_, Const (@{const_name Pair}, T) $ _ $ t2) =
blanchet@33192
   413
    (domain_type (range_type T), t2)
blanchet@33192
   414
  | mk_snd (T, t) =
blanchet@33192
   415
    let val res_T = snd (HOLogic.dest_prodT T) in
blanchet@33192
   416
      (res_T, Const (@{const_name snd}, T --> res_T) $ t)
blanchet@33192
   417
    end
blanchet@55889
   418
blanchet@38190
   419
fun factorize (z as (Type (@{type_name prod}, _), _)) =
blanchet@35665
   420
    maps factorize [mk_fst z, mk_snd z]
blanchet@33192
   421
  | factorize z = [z]
blanchet@33192
   422
blanchet@55888
   423
fun nut_from_term (hol_ctxt as {ctxt, ...}) eq =
blanchet@33192
   424
  let
blanchet@33192
   425
    fun aux eq ss Ts t =
blanchet@33192
   426
      let
blanchet@33192
   427
        val sub = aux Eq ss Ts
blanchet@33192
   428
        val sub' = aux eq ss Ts
blanchet@33192
   429
        fun sub_abs s T = aux eq (s :: ss) (T :: Ts)
blanchet@33192
   430
        fun sub_equals T t1 t2 =
blanchet@33192
   431
          let
blanchet@33192
   432
            val (binder_Ts, body_T) = strip_type (domain_type T)
blanchet@33192
   433
            val n = length binder_Ts
blanchet@33192
   434
          in
blanchet@33192
   435
            if eq = Eq andalso n > 0 then
blanchet@33192
   436
              let
blanchet@33192
   437
                val t1 = incr_boundvars n t1
blanchet@33192
   438
                val t2 = incr_boundvars n t2
blanchet@33192
   439
                val xs = map Bound (n - 1 downto 0)
haftmann@38864
   440
                val equation = Const (@{const_name HOL.eq},
blanchet@33192
   441
                                      body_T --> body_T --> bool_T)
blanchet@33192
   442
                                   $ betapplys (t1, xs) $ betapplys (t2, xs)
blanchet@33192
   443
                val t =
blanchet@33192
   444
                  fold_rev (fn T => fn (t, j) =>
blanchet@33192
   445
                               (Const (@{const_name All}, T --> bool_T)
blanchet@33192
   446
                                $ Abs ("x" ^ nat_subscript j, T, t), j - 1))
blanchet@33192
   447
                           binder_Ts (equation, n) |> fst
blanchet@33192
   448
              in sub' t end
blanchet@33192
   449
            else
blanchet@33192
   450
              Op2 (eq, bool_T, Any, aux Eq ss Ts t1, aux Eq ss Ts t2)
blanchet@33192
   451
          end
blanchet@33192
   452
        fun do_quantifier quant s T t1 =
blanchet@33192
   453
          let
blanchet@33192
   454
            val bound_u = BoundName (length Ts, T, Any, s)
blanchet@33192
   455
            val body_u = sub_abs s T t1
blanchet@36913
   456
          in Op2 (quant, bool_T, Any, bound_u, body_u) end
blanchet@33192
   457
        fun do_apply t0 ts =
blanchet@33192
   458
          let
blanchet@33192
   459
            val (ts', t2) = split_last ts
blanchet@33192
   460
            val t1 = list_comb (t0, ts')
blanchet@33192
   461
            val T1 = fastype_of1 (Ts, t1)
blanchet@46083
   462
          in Op2 (Apply, pseudo_range_type T1, Any, sub t1, sub t2) end
blanchet@35671
   463
        fun do_construct (x as (_, T)) ts =
blanchet@35280
   464
          case num_binder_types T - length ts of
blanchet@35280
   465
            0 => Construct (map ((fn (s', T') => ConstName (s', T', Any))
blanchet@35280
   466
                                  o nth_sel_for_constr x)
blanchet@35280
   467
                                (~1 upto num_sels_for_constr_type T - 1),
blanchet@35280
   468
                            body_type T, Any,
blanchet@35280
   469
                            ts |> map (`(curry fastype_of1 Ts))
blanchet@35280
   470
                               |> maps factorize |> map (sub o snd))
blanchet@35280
   471
          | k => sub (eta_expand Ts t k)
blanchet@33192
   472
      in
blanchet@33192
   473
        case strip_comb t of
wenzelm@56245
   474
          (Const (@{const_name Pure.all}, _), [Abs (s, T, t1)]) =>
blanchet@33192
   475
          do_quantifier All s T t1
wenzelm@56245
   476
        | (t0 as Const (@{const_name Pure.all}, _), [t1]) =>
blanchet@33192
   477
          sub' (t0 $ eta_expand Ts t1 1)
wenzelm@56245
   478
        | (Const (@{const_name Pure.eq}, T), [t1, t2]) => sub_equals T t1 t2
wenzelm@56245
   479
        | (Const (@{const_name Pure.imp}, _), [t1, t2]) =>
blanchet@33192
   480
          Op2 (Or, prop_T, Any, Op1 (Not, prop_T, Any, sub t1), sub' t2)
blanchet@33192
   481
        | (Const (@{const_name Pure.conjunction}, _), [t1, t2]) =>
blanchet@33192
   482
          Op2 (And, prop_T, Any, sub' t1, sub' t2)
blanchet@33192
   483
        | (Const (@{const_name Trueprop}, _), [t1]) => sub' t1
blanchet@33192
   484
        | (Const (@{const_name Not}, _), [t1]) =>
blanchet@33192
   485
          (case sub t1 of
blanchet@33192
   486
             Op1 (Not, _, _, u11) => u11
blanchet@33192
   487
           | u1 => Op1 (Not, bool_T, Any, u1))
blanchet@33192
   488
        | (Const (@{const_name False}, T), []) => Cst (False, T, Any)
blanchet@33192
   489
        | (Const (@{const_name True}, T), []) => Cst (True, T, Any)
blanchet@33192
   490
        | (Const (@{const_name All}, _), [Abs (s, T, t1)]) =>
blanchet@33192
   491
          do_quantifier All s T t1
blanchet@35280
   492
        | (t0 as Const (@{const_name All}, _), [t1]) =>
blanchet@33192
   493
          sub' (t0 $ eta_expand Ts t1 1)
blanchet@33192
   494
        | (Const (@{const_name Ex}, _), [Abs (s, T, t1)]) =>
blanchet@33192
   495
          do_quantifier Exist s T t1
blanchet@35280
   496
        | (t0 as Const (@{const_name Ex}, _), [t1]) =>
blanchet@33192
   497
          sub' (t0 $ eta_expand Ts t1 1)
haftmann@38864
   498
        | (Const (@{const_name HOL.eq}, T), [t1]) =>
blanchet@37476
   499
          Op1 (SingletonSet, range_type T, Any, sub t1)
haftmann@38864
   500
        | (Const (@{const_name HOL.eq}, T), [t1, t2]) => sub_equals T t1 t2
haftmann@38795
   501
        | (Const (@{const_name HOL.conj}, _), [t1, t2]) =>
blanchet@33192
   502
          Op2 (And, bool_T, Any, sub' t1, sub' t2)
haftmann@38795
   503
        | (Const (@{const_name HOL.disj}, _), [t1, t2]) =>
blanchet@33192
   504
          Op2 (Or, bool_T, Any, sub t1, sub t2)
haftmann@38786
   505
        | (Const (@{const_name HOL.implies}, _), [t1, t2]) =>
blanchet@33192
   506
          Op2 (Or, bool_T, Any, Op1 (Not, bool_T, Any, sub t1), sub' t2)
blanchet@33192
   507
        | (Const (@{const_name If}, T), [t1, t2, t3]) =>
blanchet@33192
   508
          Op3 (If, nth_range_type 3 T, Any, sub t1, sub t2, sub t3)
blanchet@33192
   509
        | (Const (@{const_name Let}, T), [t1, Abs (s, T', t2)]) =>
blanchet@33192
   510
          Op3 (Let, nth_range_type 2 T, Any, BoundName (length Ts, T', Any, s),
blanchet@33192
   511
               sub t1, sub_abs s T' t2)
blanchet@35280
   512
        | (t0 as Const (@{const_name Let}, _), [t1, t2]) =>
blanchet@33192
   513
          sub (t0 $ t1 $ eta_expand Ts t2 1)
blanchet@33192
   514
        | (Const (@{const_name Pair}, T), [t1, t2]) =>
blanchet@33192
   515
          Tuple (nth_range_type 2 T, Any, map sub [t1, t2])
blanchet@33192
   516
        | (Const (@{const_name fst}, T), [t1]) =>
blanchet@33192
   517
          Op1 (First, range_type T, Any, sub t1)
blanchet@33192
   518
        | (Const (@{const_name snd}, T), [t1]) =>
blanchet@33192
   519
          Op1 (Second, range_type T, Any, sub t1)
blanchet@46083
   520
        | (Const (@{const_name Set.member}, _), [t1, t2]) => do_apply t2 [t1]
blanchet@46083
   521
        | (Const (@{const_name Collect}, _), [t1]) => sub t1
blanchet@33192
   522
        | (Const (@{const_name Id}, T), []) => Cst (Iden, T, Any)
blanchet@33192
   523
        | (Const (@{const_name converse}, T), [t1]) =>
blanchet@33192
   524
          Op1 (Converse, range_type T, Any, sub t1)
blanchet@33192
   525
        | (Const (@{const_name trancl}, T), [t1]) =>
blanchet@33192
   526
          Op1 (Closure, range_type T, Any, sub t1)
griff@47433
   527
        | (Const (@{const_name relcomp}, T), [t1, t2]) =>
blanchet@33192
   528
          Op2 (Composition, nth_range_type 2 T, Any, sub t1, sub t2)
blanchet@35280
   529
        | (Const (x as (s as @{const_name Suc}, T)), []) =>
blanchet@55888
   530
          if is_built_in_const x then Cst (Suc, T, Any)
blanchet@55888
   531
          else if is_constr ctxt x then do_construct x []
blanchet@35280
   532
          else ConstName (s, T, Any)
blanchet@33192
   533
        | (Const (@{const_name finite}, T), [t1]) =>
blanchet@35070
   534
          (if is_finite_type hol_ctxt (domain_type T) then
blanchet@33877
   535
             Cst (True, bool_T, Any)
blanchet@33877
   536
           else case t1 of
blanchet@33877
   537
             Const (@{const_name top}, _) => Cst (False, bool_T, Any)
blanchet@33877
   538
           | _ => Op1 (Finite, bool_T, Any, sub t1))
blanchet@33192
   539
        | (Const (@{const_name nat}, T), []) => Cst (IntToNat, T, Any)
blanchet@35220
   540
        | (Const (x as (s as @{const_name zero_class.zero}, T)), []) =>
blanchet@55888
   541
          if is_built_in_const x then Cst (Num 0, T, Any)
blanchet@55888
   542
          else if is_constr ctxt x then do_construct x []
blanchet@35280
   543
          else ConstName (s, T, Any)
blanchet@35220
   544
        | (Const (x as (s as @{const_name one_class.one}, T)), []) =>
blanchet@55888
   545
          if is_built_in_const x then Cst (Num 1, T, Any)
blanchet@35220
   546
          else ConstName (s, T, Any)
blanchet@35220
   547
        | (Const (x as (s as @{const_name plus_class.plus}, T)), []) =>
blanchet@55888
   548
          if is_built_in_const x then Cst (Add, T, Any)
blanchet@35220
   549
          else ConstName (s, T, Any)
blanchet@35220
   550
        | (Const (x as (s as @{const_name minus_class.minus}, T)), []) =>
blanchet@55888
   551
          if is_built_in_const x then Cst (Subtract, T, Any)
blanchet@35220
   552
          else ConstName (s, T, Any)
blanchet@35220
   553
        | (Const (x as (s as @{const_name times_class.times}, T)), []) =>
blanchet@55888
   554
          if is_built_in_const x then Cst (Multiply, T, Any)
blanchet@35220
   555
          else ConstName (s, T, Any)
haftmann@60352
   556
        | (Const (x as (s as @{const_name Rings.divide}, T)), []) =>
blanchet@55888
   557
          if is_built_in_const x then Cst (Divide, T, Any)
blanchet@35220
   558
          else ConstName (s, T, Any)
blanchet@35280
   559
        | (t0 as Const (x as (@{const_name ord_class.less}, _)),
blanchet@35220
   560
           ts as [t1, t2]) =>
blanchet@55888
   561
          if is_built_in_const x then
blanchet@35220
   562
            Op2 (Less, bool_T, Any, sub t1, sub t2)
blanchet@35220
   563
          else
blanchet@35220
   564
            do_apply t0 ts
blanchet@46083
   565
        | (t0 as Const (x as (@{const_name ord_class.less_eq}, T)),
blanchet@35220
   566
           ts as [t1, t2]) =>
blanchet@59038
   567
          if is_built_in_const x then
blanchet@46083
   568
            (* FIXME: find out if this case is necessary *)
blanchet@35220
   569
            Op1 (Not, bool_T, Any, Op2 (Less, bool_T, Any, sub t2, sub t1))
blanchet@35220
   570
          else
blanchet@35220
   571
            do_apply t0 ts
blanchet@61324
   572
        | (Const (@{const_name nat_gcd}, T), []) => Cst (Gcd, T, Any)
blanchet@61324
   573
        | (Const (@{const_name nat_lcm}, T), []) => Cst (Lcm, T, Any)
blanchet@35220
   574
        | (Const (x as (s as @{const_name uminus_class.uminus}, T)), []) =>
blanchet@55888
   575
          if is_built_in_const x then
blanchet@35220
   576
            let val num_T = domain_type T in
blanchet@35220
   577
              Op2 (Apply, num_T --> num_T, Any,
blanchet@35220
   578
                   Cst (Subtract, num_T --> num_T --> num_T, Any),
blanchet@35220
   579
                   Cst (Num 0, num_T, Any))
blanchet@35220
   580
            end
blanchet@35220
   581
          else
blanchet@35220
   582
            ConstName (s, T, Any)
blanchet@34936
   583
        | (Const (@{const_name unknown}, T), []) => Cst (Unknown, T, Any)
blanchet@35280
   584
        | (Const (@{const_name is_unknown}, _), [t1]) =>
blanchet@34936
   585
          Op1 (IsUnknown, bool_T, Any, sub t1)
blanchet@35671
   586
        | (Const (@{const_name safe_The},
blanchet@35671
   587
                  Type (@{type_name fun}, [_, T2])), [t1]) =>
blanchet@35671
   588
          Op1 (SafeThe, T2, Any, sub t1)
blanchet@61324
   589
        | (Const (@{const_name Frac}, T), []) => Cst (Fracs, T, Any)
blanchet@61324
   590
        | (Const (@{const_name norm_frac}, T), []) =>
blanchet@47909
   591
          Cst (NormFrac, T, Any)
blanchet@33192
   592
        | (Const (@{const_name of_nat}, T as @{typ "nat => int"}), []) =>
blanchet@33192
   593
          Cst (NatToInt, T, Any)
blanchet@34124
   594
        | (Const (@{const_name of_nat},
blanchet@34124
   595
                  T as @{typ "unsigned_bit word => signed_bit word"}), []) =>
blanchet@34124
   596
          Cst (NatToInt, T, Any)
blanchet@33192
   597
        | (t0 as Const (x as (s, T)), ts) =>
blanchet@55888
   598
          if is_constr ctxt x then
blanchet@35671
   599
            do_construct x ts
blanchet@33192
   600
          else if String.isPrefix numeral_prefix s then
blanchet@33192
   601
            Cst (Num (the (Int.fromString (unprefix numeral_prefix s))), T, Any)
blanchet@33192
   602
          else
blanchet@55888
   603
            (case arity_of_built_in_const x of
blanchet@33192
   604
               SOME n =>
blanchet@33192
   605
               (case n - length ts of
blanchet@33232
   606
                  0 => raise TERM ("Nitpick_Nut.nut_from_term.aux", [t])
blanchet@33192
   607
                | k => if k > 0 then sub (eta_expand Ts t k)
blanchet@33192
   608
                       else do_apply t0 ts)
blanchet@33192
   609
             | NONE => if null ts then ConstName (s, T, Any)
blanchet@33192
   610
                       else do_apply t0 ts)
blanchet@33192
   611
        | (Free (s, T), []) => FreeName (s, T, Any)
blanchet@33877
   612
        | (Var _, []) => raise TERM ("Nitpick_Nut.nut_from_term.aux", [t])
blanchet@33192
   613
        | (Bound j, []) =>
blanchet@33192
   614
          BoundName (length Ts - j - 1, nth Ts j, Any, nth ss j)
blanchet@33192
   615
        | (Abs (s, T, t1), []) =>
blanchet@33192
   616
          Op2 (Lambda, T --> fastype_of1 (T :: Ts, t1), Any,
blanchet@33192
   617
               BoundName (length Ts, T, Any, s), sub_abs s T t1)
blanchet@33192
   618
        | (t0, ts) => do_apply t0 ts
blanchet@33192
   619
      end
blanchet@33192
   620
  in aux eq [] [] end
blanchet@33192
   621
blanchet@37262
   622
fun is_fully_representable_set u =
blanchet@37262
   623
  not (is_opt_rep (rep_of u)) andalso
blanchet@37262
   624
  case u of
blanchet@37262
   625
    FreeName _ => true
blanchet@37262
   626
  | Op1 (SingletonSet, _, _, _) => true
blanchet@37262
   627
  | Op1 (Converse, _, _, u1) => is_fully_representable_set u1
blanchet@39343
   628
  | Op2 (Lambda, _, _, _, Cst (False, _, _)) => true
blanchet@39343
   629
  | Op2 (oper, _, _, u1, _) =>
blanchet@37476
   630
    if oper = Apply then
blanchet@37262
   631
      case u1 of
blanchet@37266
   632
        ConstName (s, _, _) =>
blanchet@37266
   633
        is_sel_like_and_no_discr s orelse s = @{const_name set}
blanchet@37262
   634
      | _ => false
blanchet@37262
   635
    else
blanchet@37262
   636
      false
blanchet@37262
   637
  | _ => false
blanchet@37262
   638
blanchet@33192
   639
fun rep_for_abs_fun scope T =
blanchet@33192
   640
  let val (R1, R2) = best_non_opt_symmetric_reps_for_fun_type scope T in
blanchet@33192
   641
    Func (R1, (card_of_rep R1 <> card_of_rep R2 ? Opt) R2)
blanchet@33192
   642
  end
blanchet@33192
   643
blanchet@38170
   644
fun choose_rep_for_free_var scope pseudo_frees v (vs, table) =
blanchet@33192
   645
  let
blanchet@38170
   646
    val R = (if exists (curry (op =) (nickname_of v) o fst) pseudo_frees then
blanchet@38170
   647
               best_opt_set_rep_for_type
blanchet@38170
   648
             else
blanchet@38170
   649
               best_non_opt_set_rep_for_type) scope (type_of v)
blanchet@33192
   650
    val v = modify_name_rep v R
blanchet@33192
   651
  in (v :: vs, NameTable.update (v, R) table) end
blanchet@55889
   652
blanchet@41856
   653
fun choose_rep_for_const (scope as {hol_ctxt = {ctxt, ...}, ...}) total_consts v
blanchet@35280
   654
                         (vs, table) =
blanchet@33192
   655
  let
blanchet@33192
   656
    val x as (s, T) = (nickname_of v, type_of v)
blanchet@37256
   657
    val R = (if is_abs_fun ctxt x then
blanchet@33192
   658
               rep_for_abs_fun
blanchet@37256
   659
             else if is_rep_fun ctxt x then
blanchet@33192
   660
               Func oo best_non_opt_symmetric_reps_for_fun_type
blanchet@41856
   661
             else if total_consts orelse is_skolem_name v orelse
blanchet@39360
   662
                     member (op =) [@{const_name bisim},
blanchet@39360
   663
                                    @{const_name bisim_iterator_max}]
blanchet@39360
   664
                            (original_name s) then
blanchet@33192
   665
               best_non_opt_set_rep_for_type
blanchet@34121
   666
             else if member (op =) [@{const_name set}, @{const_name distinct},
blanchet@34121
   667
                                    @{const_name ord_class.less},
blanchet@34121
   668
                                    @{const_name ord_class.less_eq}]
blanchet@39360
   669
                            (original_name s) then
blanchet@33192
   670
               best_set_rep_for_type
blanchet@33192
   671
             else
blanchet@33192
   672
               best_opt_set_rep_for_type) scope T
blanchet@33192
   673
    val v = modify_name_rep v R
blanchet@33192
   674
  in (v :: vs, NameTable.update (v, R) table) end
blanchet@33192
   675
blanchet@38170
   676
fun choose_reps_for_free_vars scope pseudo_frees vs table =
blanchet@38170
   677
  fold (choose_rep_for_free_var scope pseudo_frees) vs ([], table)
blanchet@55889
   678
blanchet@41856
   679
fun choose_reps_for_consts scope total_consts vs table =
blanchet@41856
   680
  fold (choose_rep_for_const scope total_consts) vs ([], table)
blanchet@33192
   681
blanchet@35190
   682
fun choose_rep_for_nth_sel_for_constr (scope as {hol_ctxt, binarize, ...})
blanchet@35190
   683
                                      (x as (_, T)) n (vs, table) =
blanchet@33192
   684
  let
blanchet@35190
   685
    val (s', T') = binarized_and_boxed_nth_sel_for_constr hol_ctxt binarize x n
blanchet@34936
   686
    val R' = if n = ~1 orelse is_word_type (body_type T) orelse
blanchet@34936
   687
                (is_fun_type (range_type T') andalso
blanchet@46115
   688
                 is_boolean_type (body_type T')) orelse
blanchet@46115
   689
                (is_set_type (body_type T')) then
blanchet@34124
   690
               best_non_opt_set_rep_for_type scope T'
blanchet@34124
   691
             else
blanchet@34124
   692
               best_opt_set_rep_for_type scope T' |> unopt_rep
blanchet@33192
   693
    val v = ConstName (s', T', R')
blanchet@33192
   694
  in (v :: vs, NameTable.update (v, R') table) end
blanchet@55889
   695
blanchet@33192
   696
fun choose_rep_for_sels_for_constr scope (x as (_, T)) =
blanchet@33192
   697
  fold_rev (choose_rep_for_nth_sel_for_constr scope x)
blanchet@33192
   698
           (~1 upto num_sels_for_constr_type T - 1)
blanchet@55889
   699
blanchet@55890
   700
fun choose_rep_for_sels_of_data_type _ ({deep = false, ...} : data_type_spec) = I
blanchet@55890
   701
  | choose_rep_for_sels_of_data_type scope {constrs, ...} =
blanchet@33558
   702
    fold_rev (choose_rep_for_sels_for_constr scope o #const) constrs
blanchet@55889
   703
blanchet@55890
   704
fun choose_reps_for_all_sels (scope as {data_types, ...}) =
blanchet@55890
   705
  fold (choose_rep_for_sels_of_data_type scope) data_types o pair []
blanchet@33192
   706
blanchet@42412
   707
val min_univ_card = 2
blanchet@42412
   708
blanchet@42412
   709
fun choose_rep_for_bound_var scope v =
blanchet@42412
   710
  let
blanchet@42412
   711
    val R = best_one_rep_for_type scope (type_of v)
blanchet@42412
   712
    val arity = arity_of_rep R
blanchet@42412
   713
  in
blanchet@42412
   714
    if arity > KK.max_arity min_univ_card then
blanchet@42412
   715
      raise TOO_LARGE ("Nitpick_Nut.choose_rep_for_bound_var",
blanchet@42412
   716
                       "arity " ^ string_of_int arity ^ " of bound variable \
blanchet@42412
   717
                       \too large")
blanchet@42412
   718
    else
blanchet@42412
   719
      NameTable.update (v, R)
blanchet@33192
   720
  end
blanchet@33192
   721
blanchet@33192
   722
(* A nut is said to be constructive if whenever it evaluates to unknown in our
blanchet@36913
   723
   three-valued logic, it would evaluate to an unrepresentable value ("Unrep")
blanchet@33631
   724
   according to the HOL semantics. For example, "Suc n" is constructive if "n"
blanchet@35312
   725
   is representable or "Unrep", because unknown implies "Unrep". *)
blanchet@33192
   726
fun is_constructive u =
blanchet@33192
   727
  is_Cst Unrep u orelse
blanchet@46115
   728
  (not (is_fun_or_set_type (type_of u)) andalso
blanchet@46115
   729
   not (is_opt_rep (rep_of u))) orelse
blanchet@33192
   730
  case u of
blanchet@33192
   731
    Cst (Num _, _, _) => true
blanchet@38171
   732
  | Cst (cst, T, _) => body_type T = nat_T andalso (cst = Suc orelse cst = Add)
blanchet@33192
   733
  | Op2 (Apply, _, _, u1, u2) => forall is_constructive [u1, u2]
blanchet@33192
   734
  | Op3 (If, _, _, u1, u2, u3) =>
blanchet@33192
   735
    not (is_opt_rep (rep_of u1)) andalso forall is_constructive [u2, u3]
blanchet@33192
   736
  | Tuple (_, _, us) => forall is_constructive us
blanchet@33192
   737
  | Construct (_, _, _, us) => forall is_constructive us
blanchet@33192
   738
  | _ => false
blanchet@33192
   739
blanchet@33192
   740
fun unknown_boolean T R =
blanchet@34936
   741
  Cst (case R of Formula Pos => False | Formula Neg => True | _ => Unknown,
blanchet@34936
   742
       T, R)
blanchet@33192
   743
blanchet@33192
   744
fun s_op1 oper T R u1 =
blanchet@33192
   745
  ((if oper = Not then
blanchet@33192
   746
      if is_Cst True u1 then Cst (False, T, R)
blanchet@33192
   747
      else if is_Cst False u1 then Cst (True, T, R)
blanchet@33192
   748
      else raise SAME ()
blanchet@33192
   749
    else
blanchet@33192
   750
      raise SAME ())
blanchet@33192
   751
   handle SAME () => Op1 (oper, T, R, u1))
blanchet@55889
   752
blanchet@33192
   753
fun s_op2 oper T R u1 u2 =
blanchet@33192
   754
  ((case oper of
blanchet@36913
   755
      All => if is_subnut_of u1 u2 then Op2 (All, T, R, u1, u2) else u2
blanchet@36913
   756
    | Exist => if is_subnut_of u1 u2 then Op2 (Exist, T, R, u1, u2) else u2
blanchet@36913
   757
    | Or =>
blanchet@33192
   758
      if exists (is_Cst True) [u1, u2] then Cst (True, T, unopt_rep R)
blanchet@33192
   759
      else if is_Cst False u1 then u2
blanchet@33192
   760
      else if is_Cst False u2 then u1
blanchet@33192
   761
      else raise SAME ()
blanchet@33192
   762
    | And =>
blanchet@33192
   763
      if exists (is_Cst False) [u1, u2] then Cst (False, T, unopt_rep R)
blanchet@33192
   764
      else if is_Cst True u1 then u2
blanchet@33192
   765
      else if is_Cst True u2 then u1
blanchet@33192
   766
      else raise SAME ()
blanchet@33192
   767
    | Eq =>
wenzelm@59058
   768
      (case apply2 (is_Cst Unrep) (u1, u2) of
blanchet@33192
   769
         (true, true) => unknown_boolean T R
blanchet@33192
   770
       | (false, false) => raise SAME ()
blanchet@33192
   771
       | _ => if forall (is_opt_rep o rep_of) [u1, u2] then raise SAME ()
blanchet@33192
   772
              else Cst (False, T, Formula Neut))
blanchet@33192
   773
    | Triad =>
blanchet@33192
   774
      if is_Cst True u1 then u1
blanchet@33192
   775
      else if is_Cst False u2 then u2
blanchet@33192
   776
      else raise SAME ()
blanchet@33192
   777
    | Apply =>
blanchet@33192
   778
      if is_Cst Unrep u1 then
blanchet@33192
   779
        Cst (Unrep, T, R)
blanchet@33192
   780
      else if is_Cst Unrep u2 then
blanchet@36913
   781
        if is_boolean_type T then
blanchet@35312
   782
          if is_fully_representable_set u1 then Cst (False, T, Formula Neut)
blanchet@33631
   783
          else unknown_boolean T R
blanchet@36913
   784
        else if is_constructive u1 then
blanchet@36913
   785
          Cst (Unrep, T, R)
blanchet@33192
   786
        else case u1 of
blanchet@33192
   787
          Op2 (Apply, _, _, ConstName (@{const_name List.append}, _, _), _) =>
blanchet@33192
   788
          Cst (Unrep, T, R)
blanchet@33192
   789
        | _ => raise SAME ()
blanchet@33192
   790
      else
blanchet@33192
   791
        raise SAME ()
blanchet@33192
   792
    | _ => raise SAME ())
blanchet@33192
   793
   handle SAME () => Op2 (oper, T, R, u1, u2))
blanchet@55889
   794
blanchet@33192
   795
fun s_op3 oper T R u1 u2 u3 =
blanchet@33192
   796
  ((case oper of
blanchet@33192
   797
      Let =>
blanchet@36913
   798
      if inline_nut u2 orelse num_occurrences_in_nut u1 u3 < 2 then
blanchet@33192
   799
        substitute_in_nut u1 u2 u3
blanchet@33192
   800
      else
blanchet@33192
   801
        raise SAME ()
blanchet@33192
   802
    | _ => raise SAME ())
blanchet@33192
   803
   handle SAME () => Op3 (oper, T, R, u1, u2, u3))
blanchet@55889
   804
blanchet@33192
   805
fun s_tuple T R us =
blanchet@38190
   806
  if exists (is_Cst Unrep) us then Cst (Unrep, T, R) else Tuple (T, R, us)
blanchet@33192
   807
blanchet@33192
   808
fun untuple f (Tuple (_, _, us)) = maps (untuple f) us
blanchet@38190
   809
  | untuple f u = [f u]
blanchet@33192
   810
blanchet@55890
   811
fun choose_reps_in_nut (scope as {card_assigns, bits, data_types, ofs, ...})
blanchet@35185
   812
                       unsound table def =
blanchet@33192
   813
  let
blanchet@33192
   814
    val bool_atom_R = Atom (2, offset_of_type ofs bool_T)
blanchet@33192
   815
    fun bool_rep polar opt =
blanchet@33192
   816
      if polar = Neut andalso opt then Opt bool_atom_R else Formula polar
blanchet@33192
   817
    fun triad u1 u2 = s_op2 Triad (type_of u1) (Opt bool_atom_R) u1 u2
blanchet@33192
   818
    fun triad_fn f = triad (f Pos) (f Neg)
blanchet@33192
   819
    fun unrepify_nut_in_nut table def polar needle_u =
blanchet@33192
   820
      let val needle_T = type_of needle_u in
blanchet@46115
   821
        substitute_in_nut needle_u
blanchet@46115
   822
            (Cst (if is_fun_or_set_type needle_T then Unknown
blanchet@46115
   823
                  else Unrep, needle_T, Any))
blanchet@33192
   824
        #> aux table def polar
blanchet@33192
   825
      end
blanchet@33192
   826
    and aux table def polar u =
blanchet@33192
   827
      let
blanchet@33192
   828
        val gsub = aux table
blanchet@33192
   829
        val sub = gsub false Neut
blanchet@33192
   830
      in
blanchet@33192
   831
        case u of
blanchet@33192
   832
          Cst (False, T, _) => Cst (False, T, Formula Neut)
blanchet@33192
   833
        | Cst (True, T, _) => Cst (True, T, Formula Neut)
blanchet@33192
   834
        | Cst (Num j, T, _) =>
blanchet@34124
   835
          if is_word_type T then
blanchet@34126
   836
            Cst (if is_twos_complement_representable bits j then Num j
blanchet@34126
   837
                 else Unrep, T, best_opt_set_rep_for_type scope T)
blanchet@34124
   838
          else
blanchet@38190
   839
             let
blanchet@38190
   840
               val (k, j0) = spec_of_type scope T
blanchet@38190
   841
               val ok = (if T = int_T then atom_for_int (k, j0) j <> ~1
blanchet@38190
   842
                         else j < k)
blanchet@38190
   843
             in
blanchet@38190
   844
               if ok then Cst (Num j, T, Atom (k, j0))
blanchet@38190
   845
               else Cst (Unrep, T, Opt (Atom (k, j0)))
blanchet@38190
   846
             end
blanchet@35665
   847
        | Cst (Suc, T as Type (@{type_name fun}, [T1, _]), _) =>
blanchet@33192
   848
          let val R = Atom (spec_of_type scope T1) in
blanchet@33192
   849
            Cst (Suc, T, Func (R, Opt R))
blanchet@33192
   850
          end
blanchet@33192
   851
        | Cst (Fracs, T, _) =>
blanchet@33192
   852
          Cst (Fracs, T, best_non_opt_set_rep_for_type scope T)
blanchet@33192
   853
        | Cst (NormFrac, T, _) =>
blanchet@33192
   854
          let val R1 = Atom (spec_of_type scope (domain_type T)) in
blanchet@33192
   855
            Cst (NormFrac, T, Func (R1, Func (R1, Opt (Struct [R1, R1]))))
blanchet@33192
   856
          end
blanchet@33192
   857
        | Cst (cst, T, _) =>
blanchet@34121
   858
          if cst = Unknown orelse cst = Unrep then
blanchet@37483
   859
            case (is_boolean_type T, polar |> unsound ? flip_polarity) of
blanchet@33192
   860
              (true, Pos) => Cst (False, T, Formula Pos)
blanchet@33192
   861
            | (true, Neg) => Cst (True, T, Formula Neg)
blanchet@33192
   862
            | _ => Cst (cst, T, best_opt_set_rep_for_type scope T)
blanchet@34124
   863
          else if member (op =) [Add, Subtract, Multiply, Divide, Gcd, Lcm]
blanchet@34124
   864
                         cst then
blanchet@33192
   865
            let
blanchet@33192
   866
              val T1 = domain_type T
blanchet@33192
   867
              val R1 = Atom (spec_of_type scope T1)
blanchet@34936
   868
              val total = T1 = nat_T andalso
blanchet@34936
   869
                          (cst = Subtract orelse cst = Divide orelse cst = Gcd)
blanchet@33192
   870
            in Cst (cst, T, Func (R1, Func (R1, (not total ? Opt) R1))) end
blanchet@34121
   871
          else if cst = NatToInt orelse cst = IntToNat then
blanchet@33192
   872
            let
blanchet@34124
   873
              val (dom_card, dom_j0) = spec_of_type scope (domain_type T)
blanchet@34124
   874
              val (ran_card, ran_j0) = spec_of_type scope (range_type T)
blanchet@34936
   875
              val total = not (is_word_type (domain_type T)) andalso
blanchet@34936
   876
                          (if cst = NatToInt then
blanchet@34936
   877
                             max_int_for_card ran_card >= dom_card + 1
blanchet@34936
   878
                           else
blanchet@34936
   879
                             max_int_for_card dom_card < ran_card)
blanchet@33192
   880
            in
blanchet@34124
   881
              Cst (cst, T, Func (Atom (dom_card, dom_j0),
blanchet@34124
   882
                                 Atom (ran_card, ran_j0) |> not total ? Opt))
blanchet@33192
   883
            end
blanchet@33192
   884
          else
blanchet@33192
   885
            Cst (cst, T, best_set_rep_for_type scope T)
blanchet@33192
   886
        | Op1 (Not, T, _, u1) =>
blanchet@33192
   887
          (case gsub def (flip_polarity polar) u1 of
blanchet@35280
   888
             Op2 (Triad, T, _, u11, u12) =>
blanchet@33192
   889
             triad (s_op1 Not T (Formula Pos) u12)
blanchet@33192
   890
                   (s_op1 Not T (Formula Neg) u11)
blanchet@33192
   891
           | u1' => s_op1 Not T (flip_rep_polarity (rep_of u1')) u1')
blanchet@34936
   892
        | Op1 (IsUnknown, T, _, u1) =>
blanchet@34936
   893
          let val u1 = sub u1 in
blanchet@34936
   894
            if is_opt_rep (rep_of u1) then Op1 (IsUnknown, T, Formula Neut, u1)
blanchet@34936
   895
            else Cst (False, T, Formula Neut)
blanchet@34936
   896
          end
blanchet@33192
   897
        | Op1 (oper, T, _, u1) =>
blanchet@33192
   898
          let
blanchet@33192
   899
            val u1 = sub u1
blanchet@33192
   900
            val R1 = rep_of u1
blanchet@33192
   901
            val R = case oper of
blanchet@33192
   902
                      Finite => bool_rep polar (is_opt_rep R1)
blanchet@33192
   903
                    | _ => (if is_opt_rep R1 then best_opt_set_rep_for_type
blanchet@33192
   904
                            else best_non_opt_set_rep_for_type) scope T
blanchet@33192
   905
          in s_op1 oper T R u1 end
blanchet@33192
   906
        | Op2 (Less, T, _, u1, u2) =>
blanchet@33192
   907
          let
blanchet@33192
   908
            val u1 = sub u1
blanchet@33192
   909
            val u2 = sub u2
blanchet@33192
   910
            val R = bool_rep polar (exists (is_opt_rep o rep_of) [u1, u2])
blanchet@33192
   911
          in s_op2 Less T R u1 u2 end
blanchet@33192
   912
        | Op2 (DefEq, T, _, u1, u2) =>
blanchet@33192
   913
          s_op2 DefEq T (Formula Neut) (sub u1) (sub u2)
blanchet@33192
   914
        | Op2 (Eq, T, _, u1, u2) =>
blanchet@33192
   915
          let
blanchet@33192
   916
            val u1' = sub u1
blanchet@33192
   917
            val u2' = sub u2
blanchet@33192
   918
            fun non_opt_case () = s_op2 Eq T (Formula polar) u1' u2'
blanchet@33192
   919
            fun opt_opt_case () =
blanchet@33192
   920
              if polar = Neut then
blanchet@33192
   921
                triad_fn (fn polar => s_op2 Eq T (Formula polar) u1' u2')
blanchet@33192
   922
              else
blanchet@33192
   923
                non_opt_case ()
blanchet@33192
   924
            fun hybrid_case u =
blanchet@33192
   925
              (* hackish optimization *)
blanchet@33192
   926
              if is_constructive u then s_op2 Eq T (Formula Neut) u1' u2'
blanchet@33192
   927
              else opt_opt_case ()
blanchet@33192
   928
          in
blanchet@35185
   929
            if unsound orelse polar = Neg orelse
blanchet@55890
   930
               is_concrete_type data_types true (type_of u1) then
blanchet@33192
   931
              case (is_opt_rep (rep_of u1'), is_opt_rep (rep_of u2')) of
blanchet@33192
   932
                (true, true) => opt_opt_case ()
blanchet@33192
   933
              | (true, false) => hybrid_case u1'
blanchet@33192
   934
              | (false, true) => hybrid_case u2'
blanchet@33192
   935
              | (false, false) => non_opt_case ()
blanchet@33192
   936
            else
blanchet@33192
   937
              Cst (False, T, Formula Pos)
blanchet@33192
   938
              |> polar = Neut ? (fn pos_u => triad pos_u (gsub def Neg u))
blanchet@33192
   939
          end
blanchet@33192
   940
        | Op2 (Apply, T, _, u1, u2) =>
blanchet@33192
   941
          let
blanchet@33192
   942
            val u1 = sub u1
blanchet@33192
   943
            val u2 = sub u2
blanchet@33192
   944
            val T1 = type_of u1
blanchet@33192
   945
            val R1 = rep_of u1
blanchet@33192
   946
            val R2 = rep_of u2
blanchet@33192
   947
            val opt =
blanchet@33192
   948
              case (u1, is_opt_rep R2) of
blanchet@33192
   949
                (ConstName (@{const_name set}, _, _), false) => false
blanchet@33192
   950
              | _ => exists is_opt_rep [R1, R2]
blanchet@33192
   951
            val ran_R =
blanchet@33192
   952
              if is_boolean_type T then
blanchet@33192
   953
                bool_rep polar opt
blanchet@33192
   954
              else
blanchet@36128
   955
                lazy_range_rep ofs T1 (fn () => card_of_type card_assigns T)
blanchet@36128
   956
                               (unopt_rep R1)
blanchet@33192
   957
                |> opt ? opt_rep ofs T
blanchet@33192
   958
          in s_op2 Apply T ran_R u1 u2 end
blanchet@33192
   959
        | Op2 (Lambda, T, _, u1, u2) =>
blanchet@33192
   960
          (case best_set_rep_for_type scope T of
blanchet@38190
   961
             R as Func (R1, _) =>
blanchet@33192
   962
             let
blanchet@33192
   963
               val table' = NameTable.update (u1, R1) table
blanchet@33192
   964
               val u1' = aux table' false Neut u1
blanchet@33192
   965
               val u2' = aux table' false Neut u2
blanchet@33192
   966
               val R =
blanchet@47753
   967
                 if is_opt_rep (rep_of u2') then opt_rep ofs T R
blanchet@47753
   968
                 else unopt_rep R
blanchet@33192
   969
             in s_op2 Lambda T R u1' u2' end
blanchet@36913
   970
           | _ => raise NUT ("Nitpick_Nut.choose_reps_in_nut.aux", [u]))
blanchet@33192
   971
        | Op2 (oper, T, _, u1, u2) =>
blanchet@34121
   972
          if oper = All orelse oper = Exist then
blanchet@33192
   973
            let
blanchet@33192
   974
              val table' = fold (choose_rep_for_bound_var scope) (untuple I u1)
blanchet@33192
   975
                                table
blanchet@33192
   976
              val u1' = aux table' def polar u1
blanchet@33192
   977
              val u2' = aux table' def polar u2
blanchet@33192
   978
            in
blanchet@33192
   979
              if polar = Neut andalso is_opt_rep (rep_of u2') then
blanchet@33192
   980
                triad_fn (fn polar => gsub def polar u)
blanchet@33192
   981
              else
blanchet@33192
   982
                let val quant_u = s_op2 oper T (Formula polar) u1' u2' in
blanchet@34936
   983
                  if def orelse
blanchet@35185
   984
                     (unsound andalso (polar = Pos) = (oper = All)) orelse
blanchet@55890
   985
                     is_complete_type data_types true (type_of u1) then
blanchet@33192
   986
                    quant_u
blanchet@33192
   987
                  else
blanchet@33192
   988
                    let
blanchet@33192
   989
                      val connective = if oper = All then And else Or
blanchet@33192
   990
                      val unrepified_u = unrepify_nut_in_nut table def polar
blanchet@33192
   991
                                                             u1 u2
blanchet@33192
   992
                    in
blanchet@33192
   993
                      s_op2 connective T
blanchet@33192
   994
                            (min_rep (rep_of quant_u) (rep_of unrepified_u))
blanchet@33192
   995
                            quant_u unrepified_u
blanchet@33192
   996
                    end
blanchet@33192
   997
                end
blanchet@33192
   998
            end
blanchet@34121
   999
          else if oper = Or orelse oper = And then
blanchet@33192
  1000
            let
blanchet@33192
  1001
              val u1' = gsub def polar u1
blanchet@33192
  1002
              val u2' = gsub def polar u2
blanchet@33192
  1003
            in
blanchet@33192
  1004
              (if polar = Neut then
blanchet@33192
  1005
                 case (is_opt_rep (rep_of u1'), is_opt_rep (rep_of u2')) of
blanchet@33192
  1006
                   (true, true) => triad_fn (fn polar => gsub def polar u)
blanchet@33192
  1007
                 | (true, false) =>
blanchet@33192
  1008
                   s_op2 oper T (Opt bool_atom_R)
blanchet@33192
  1009
                         (triad_fn (fn polar => gsub def polar u1)) u2'
blanchet@33192
  1010
                 | (false, true) =>
blanchet@33192
  1011
                   s_op2 oper T (Opt bool_atom_R)
blanchet@33192
  1012
                         u1' (triad_fn (fn polar => gsub def polar u2))
blanchet@33192
  1013
                 | (false, false) => raise SAME ()
blanchet@33192
  1014
               else
blanchet@33192
  1015
                 raise SAME ())
blanchet@33192
  1016
              handle SAME () => s_op2 oper T (Formula polar) u1' u2'
blanchet@33192
  1017
            end
blanchet@33192
  1018
          else
blanchet@33192
  1019
            let
blanchet@33192
  1020
              val u1 = sub u1
blanchet@33192
  1021
              val u2 = sub u2
blanchet@33192
  1022
              val R =
blanchet@33192
  1023
                best_non_opt_set_rep_for_type scope T
blanchet@33192
  1024
                |> exists (is_opt_rep o rep_of) [u1, u2] ? opt_rep ofs T
blanchet@33192
  1025
            in s_op2 oper T R u1 u2 end
blanchet@33192
  1026
        | Op3 (Let, T, _, u1, u2, u3) =>
blanchet@33192
  1027
          let
blanchet@33192
  1028
            val u2 = sub u2
blanchet@33192
  1029
            val R2 = rep_of u2
blanchet@33192
  1030
            val table' = NameTable.update (u1, R2) table
blanchet@33192
  1031
            val u1 = modify_name_rep u1 R2
blanchet@33192
  1032
            val u3 = aux table' false polar u3
blanchet@33192
  1033
          in s_op3 Let T (rep_of u3) u1 u2 u3 end
blanchet@33192
  1034
        | Op3 (If, T, _, u1, u2, u3) =>
blanchet@33192
  1035
          let
blanchet@33192
  1036
            val u1 = sub u1
blanchet@33192
  1037
            val u2 = gsub def polar u2
blanchet@33192
  1038
            val u3 = gsub def polar u3
blanchet@33192
  1039
            val min_R = min_rep (rep_of u2) (rep_of u3)
blanchet@33192
  1040
            val R = min_R |> is_opt_rep (rep_of u1) ? opt_rep ofs T
blanchet@33192
  1041
          in s_op3 If T R u1 u2 u3 end
blanchet@33192
  1042
        | Tuple (T, _, us) =>
blanchet@33192
  1043
          let
blanchet@33192
  1044
            val Rs = map (best_one_rep_for_type scope o type_of) us
blanchet@33192
  1045
            val us = map sub us
blanchet@38190
  1046
            val R' = Struct Rs
blanchet@38190
  1047
                     |> exists (is_opt_rep o rep_of) us ? opt_rep ofs T
blanchet@33192
  1048
          in s_tuple T R' us end
blanchet@33192
  1049
        | Construct (us', T, _, us) =>
blanchet@33192
  1050
          let
blanchet@33192
  1051
            val us = map sub us
blanchet@33192
  1052
            val Rs = map rep_of us
blanchet@33192
  1053
            val R = best_one_rep_for_type scope T
blanchet@33192
  1054
            val {total, ...} =
blanchet@55890
  1055
              constr_spec data_types (original_name (nickname_of (hd us')), T)
blanchet@33192
  1056
            val opt = exists is_opt_rep Rs orelse not total
blanchet@33192
  1057
          in Construct (map sub us', T, R |> opt ? Opt, us) end
blanchet@33192
  1058
        | _ =>
blanchet@33192
  1059
          let val u = modify_name_rep u (the_name table u) in
blanchet@34936
  1060
            if polar = Neut orelse not (is_boolean_type (type_of u)) orelse
blanchet@34936
  1061
               not (is_opt_rep (rep_of u)) then
blanchet@33192
  1062
              u
blanchet@33192
  1063
            else
blanchet@33192
  1064
              s_op1 Cast (type_of u) (Formula polar) u
blanchet@33192
  1065
          end
blanchet@33192
  1066
      end
blanchet@33192
  1067
  in aux table def Pos end
blanchet@33192
  1068
blanchet@33192
  1069
fun fresh_n_ary_index n [] ys = (0, (n, 1) :: ys)
blanchet@33192
  1070
  | fresh_n_ary_index n ((m, j) :: xs) ys =
blanchet@33192
  1071
    if m = n then (j, ys @ ((m, j + 1) :: xs))
blanchet@33192
  1072
    else fresh_n_ary_index n xs ((m, j) :: ys)
blanchet@55889
  1073
blanchet@33192
  1074
fun fresh_rel n {rels, vars, formula_reg, rel_reg} =
blanchet@33192
  1075
  let val (j, rels') = fresh_n_ary_index n rels [] in
blanchet@33192
  1076
    (j, {rels = rels', vars = vars, formula_reg = formula_reg,
blanchet@33192
  1077
         rel_reg = rel_reg})
blanchet@33192
  1078
  end
blanchet@55889
  1079
blanchet@33192
  1080
fun fresh_var n {rels, vars, formula_reg, rel_reg} =
blanchet@33192
  1081
  let val (j, vars') = fresh_n_ary_index n vars [] in
blanchet@33192
  1082
    (j, {rels = rels, vars = vars', formula_reg = formula_reg,
blanchet@33192
  1083
         rel_reg = rel_reg})
blanchet@33192
  1084
  end
blanchet@55889
  1085
blanchet@33192
  1086
fun fresh_formula_reg {rels, vars, formula_reg, rel_reg} =
blanchet@33192
  1087
  (formula_reg, {rels = rels, vars = vars, formula_reg = formula_reg + 1,
blanchet@33192
  1088
                 rel_reg = rel_reg})
blanchet@55889
  1089
blanchet@33192
  1090
fun fresh_rel_reg {rels, vars, formula_reg, rel_reg} =
blanchet@33192
  1091
  (rel_reg, {rels = rels, vars = vars, formula_reg = formula_reg,
blanchet@33192
  1092
             rel_reg = rel_reg + 1})
blanchet@33192
  1093
blanchet@33192
  1094
fun rename_plain_var v (ws, pool, table) =
blanchet@33192
  1095
  let
blanchet@33192
  1096
    val is_formula = (rep_of v = Formula Neut)
blanchet@33192
  1097
    val fresh = if is_formula then fresh_formula_reg else fresh_rel_reg
blanchet@33192
  1098
    val (j, pool) = fresh pool
blanchet@33192
  1099
    val constr = if is_formula then FormulaReg else RelReg
blanchet@33192
  1100
    val w = constr (j, type_of v, rep_of v)
blanchet@33192
  1101
  in (w :: ws, pool, NameTable.update (v, w) table) end
blanchet@33192
  1102
blanchet@38190
  1103
fun shape_tuple (T as Type (@{type_name prod}, [T1, T2])) (R as Struct [R1, R2])
blanchet@35665
  1104
                us =
blanchet@33192
  1105
    let val arity1 = arity_of_rep R1 in
blanchet@33192
  1106
      Tuple (T, R, [shape_tuple T1 R1 (List.take (us, arity1)),
blanchet@33192
  1107
                    shape_tuple T2 R2 (List.drop (us, arity1))])
blanchet@33192
  1108
    end
blanchet@46086
  1109
  | shape_tuple T (R as Vect (k, R')) us =
blanchet@46086
  1110
    Tuple (T, R, map (shape_tuple (pseudo_range_type T) R')
blanchet@48323
  1111
                     (chunk_list (length us div k) us))
blanchet@35280
  1112
  | shape_tuple T _ [u] =
blanchet@33232
  1113
    if type_of u = T then u else raise NUT ("Nitpick_Nut.shape_tuple", [u])
blanchet@33232
  1114
  | shape_tuple _ _ us = raise NUT ("Nitpick_Nut.shape_tuple", us)
blanchet@33192
  1115
blanchet@33192
  1116
fun rename_n_ary_var rename_free v (ws, pool, table) =
blanchet@33192
  1117
  let
blanchet@33192
  1118
    val T = type_of v
blanchet@33192
  1119
    val R = rep_of v
blanchet@33192
  1120
    val arity = arity_of_rep R
blanchet@33192
  1121
    val nick = nickname_of v
blanchet@33192
  1122
    val (constr, fresh) = if rename_free then (FreeRel, fresh_rel)
blanchet@33192
  1123
                          else (BoundRel, fresh_var)
blanchet@33192
  1124
  in
blanchet@33192
  1125
    if not rename_free andalso arity > 1 then
blanchet@33192
  1126
      let
blanchet@33192
  1127
        val atom_schema = atom_schema_of_rep R
blanchet@33192
  1128
        val type_schema = type_schema_of_rep T R
blanchet@33192
  1129
        val (js, pool) = funpow arity (fn (js, pool) =>
blanchet@33192
  1130
                                          let val (j, pool) = fresh 1 pool in
blanchet@33192
  1131
                                            (j :: js, pool)
blanchet@33192
  1132
                                          end)
blanchet@33192
  1133
                                ([], pool)
wenzelm@58634
  1134
        val ws' = @{map 3} (fn j => fn x => fn T =>
blanchet@33192
  1135
                           constr ((1, j), T, Atom x,
blanchet@33192
  1136
                                   nick ^ " [" ^ string_of_int j ^ "]"))
blanchet@33192
  1137
                       (rev js) atom_schema type_schema
blanchet@33192
  1138
      in (ws' @ ws, pool, NameTable.update (v, shape_tuple T R ws') table) end
blanchet@33192
  1139
    else
blanchet@33192
  1140
      let
blanchet@34124
  1141
        val (j, pool) =
blanchet@34124
  1142
          case v of
blanchet@34124
  1143
            ConstName _ =>
blanchet@34124
  1144
            if is_sel_like_and_no_discr nick then
blanchet@34124
  1145
              case domain_type T of
blanchet@34124
  1146
                @{typ "unsigned_bit word"} =>
blanchet@34124
  1147
                (snd unsigned_bit_word_sel_rel, pool)
blanchet@34124
  1148
              | @{typ "signed_bit word"} => (snd signed_bit_word_sel_rel, pool)
blanchet@34124
  1149
              | _ => fresh arity pool
blanchet@34124
  1150
            else
blanchet@34124
  1151
              fresh arity pool
blanchet@34124
  1152
          | _ => fresh arity pool
blanchet@33192
  1153
        val w = constr ((arity, j), T, R, nick)
blanchet@33192
  1154
      in (w :: ws, pool, NameTable.update (v, w) table) end
blanchet@33192
  1155
  end
blanchet@33192
  1156
blanchet@33192
  1157
fun rename_free_vars vs pool table =
blanchet@33192
  1158
  let
blanchet@33192
  1159
    val (vs, pool, table) = fold (rename_n_ary_var true) vs ([], pool, table)
blanchet@33192
  1160
  in (rev vs, pool, table) end
blanchet@33192
  1161
blanchet@33192
  1162
fun rename_vars_in_nut pool table u =
blanchet@33192
  1163
  case u of
blanchet@33192
  1164
    Cst _ => u
blanchet@33192
  1165
  | Op1 (oper, T, R, u1) => Op1 (oper, T, R, rename_vars_in_nut pool table u1)
blanchet@33192
  1166
  | Op2 (oper, T, R, u1, u2) =>
blanchet@34121
  1167
    if oper = All orelse oper = Exist orelse oper = Lambda then
blanchet@33192
  1168
      let
blanchet@33192
  1169
        val (_, pool, table) = fold (rename_n_ary_var false) (untuple I u1)
blanchet@33192
  1170
                                    ([], pool, table)
blanchet@33192
  1171
      in
blanchet@33192
  1172
        Op2 (oper, T, R, rename_vars_in_nut pool table u1,
blanchet@33192
  1173
             rename_vars_in_nut pool table u2)
blanchet@33192
  1174
      end
blanchet@33192
  1175
    else
blanchet@33192
  1176
      Op2 (oper, T, R, rename_vars_in_nut pool table u1,
blanchet@33192
  1177
           rename_vars_in_nut pool table u2)
blanchet@33192
  1178
  | Op3 (Let, T, R, u1, u2, u3) =>
blanchet@38190
  1179
    if inline_nut u2 then
blanchet@33192
  1180
      let
blanchet@33192
  1181
        val u2 = rename_vars_in_nut pool table u2
blanchet@33192
  1182
        val table = NameTable.update (u1, u2) table
blanchet@33192
  1183
      in rename_vars_in_nut pool table u3 end
blanchet@33192
  1184
    else
blanchet@33192
  1185
      let
blanchet@33192
  1186
        val bs = untuple I u1
blanchet@33192
  1187
        val (_, pool, table') = fold rename_plain_var bs ([], pool, table)
blanchet@33192
  1188
      in
blanchet@33192
  1189
        Op3 (Let, T, R, rename_vars_in_nut pool table' u1,
blanchet@33192
  1190
             rename_vars_in_nut pool table u2,
blanchet@33192
  1191
             rename_vars_in_nut pool table' u3)
blanchet@33192
  1192
      end
blanchet@33192
  1193
  | Op3 (oper, T, R, u1, u2, u3) =>
blanchet@33192
  1194
    Op3 (oper, T, R, rename_vars_in_nut pool table u1,
blanchet@33192
  1195
         rename_vars_in_nut pool table u2, rename_vars_in_nut pool table u3)
blanchet@33192
  1196
  | Tuple (T, R, us) => Tuple (T, R, map (rename_vars_in_nut pool table) us)
blanchet@33192
  1197
  | Construct (us', T, R, us) =>
blanchet@33192
  1198
    Construct (map (rename_vars_in_nut pool table) us', T, R,
blanchet@33192
  1199
               map (rename_vars_in_nut pool table) us)
blanchet@33192
  1200
  | _ => the_name table u
blanchet@33192
  1201
blanchet@33192
  1202
end;