src/HOL/Tools/inductive_codegen.ML
author berghofe
Fri Apr 13 15:43:25 2007 +0200 (2007-04-13)
changeset 22661 f3ba63a2663e
parent 22642 bfdb29f11eb4
child 22791 992222f3d2eb
permissions -rw-r--r--
Removed erroneous application of rev in get_clauses that caused
introduction rules taken from the InductivePackage database to
be in the wrong order.
berghofe@12453
     1
(*  Title:      HOL/inductive_codegen.ML
berghofe@11537
     2
    ID:         $Id$
wenzelm@11539
     3
    Author:     Stefan Berghofer, TU Muenchen
berghofe@11537
     4
wenzelm@11539
     5
Code generator for inductive predicates.
berghofe@11537
     6
*)
berghofe@11537
     7
berghofe@11537
     8
signature INDUCTIVE_CODEGEN =
berghofe@11537
     9
sig
berghofe@22271
    10
  val add : string option -> int option -> attribute
wenzelm@18708
    11
  val setup : theory -> theory
berghofe@11537
    12
end;
berghofe@11537
    13
berghofe@11537
    14
structure InductiveCodegen : INDUCTIVE_CODEGEN =
berghofe@11537
    15
struct
berghofe@11537
    16
berghofe@11537
    17
open Codegen;
berghofe@11537
    18
berghofe@22271
    19
(* read off parameters of inductive predicate from raw induction rule *)
berghofe@22271
    20
fun params_of induct =
berghofe@22271
    21
  let
berghofe@22271
    22
    val (_ $ t $ u :: _) =
berghofe@22271
    23
      HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of induct));
berghofe@22271
    24
    val (_, ts) = strip_comb t;
berghofe@22271
    25
    val (_, us) = strip_comb u
berghofe@22271
    26
  in
berghofe@22271
    27
    List.take (ts, length ts - length us)
berghofe@22271
    28
  end;
berghofe@22271
    29
berghofe@12557
    30
(**** theory data ****)
berghofe@12557
    31
wenzelm@18930
    32
fun merge_rules tabs =
haftmann@22642
    33
  Symtab.join (fn _ => AList.merge (Thm.eq_thm_prop) (K true)) tabs;
wenzelm@18930
    34
wenzelm@16424
    35
structure CodegenData = TheoryDataFun
wenzelm@16424
    36
(struct
berghofe@12557
    37
  val name = "HOL/inductive_codegen";
berghofe@15031
    38
  type T =
berghofe@22271
    39
    {intros : (thm * (string * int)) list Symtab.table,
berghofe@15031
    40
     graph : unit Graph.T,
berghofe@16645
    41
     eqns : (thm * string) list Symtab.table};
berghofe@15031
    42
  val empty =
berghofe@15031
    43
    {intros = Symtab.empty, graph = Graph.empty, eqns = Symtab.empty};
berghofe@12557
    44
  val copy = I;
wenzelm@16424
    45
  val extend = I;
wenzelm@16424
    46
  fun merge _ ({intros=intros1, graph=graph1, eqns=eqns1},
berghofe@15031
    47
    {intros=intros2, graph=graph2, eqns=eqns2}) =
wenzelm@18930
    48
    {intros = merge_rules (intros1, intros2),
berghofe@15031
    49
     graph = Graph.merge (K true) (graph1, graph2),
wenzelm@18930
    50
     eqns = merge_rules (eqns1, eqns2)};
berghofe@12557
    51
  fun print _ _ = ();
wenzelm@16424
    52
end);
berghofe@12557
    53
berghofe@12557
    54
berghofe@12557
    55
fun warn thm = warning ("InductiveCodegen: Not a proper clause:\n" ^
berghofe@12557
    56
  string_of_thm thm);
berghofe@11537
    57
berghofe@14162
    58
fun add_node (g, x) = Graph.new_node (x, ()) g handle Graph.DUP _ => g;
berghofe@14162
    59
berghofe@22271
    60
fun add optmod optnparms = Thm.declaration_attribute (fn thm => Context.mapping (fn thy =>
berghofe@16645
    61
  let
berghofe@16645
    62
    val {intros, graph, eqns} = CodegenData.get thy;
berghofe@16645
    63
    fun thyname_of s = (case optmod of
berghofe@16645
    64
      NONE => thyname_of_const s thy | SOME s => s);
berghofe@22271
    65
  in (case Option.map strip_comb (try HOLogic.dest_Trueprop (concl_of thm)) of
berghofe@22271
    66
      SOME (Const ("op =", _), [t, _]) => (case head_of t of
berghofe@15031
    67
        Const (s, _) =>
wenzelm@18728
    68
          CodegenData.put {intros = intros, graph = graph,
haftmann@22642
    69
             eqns = eqns |> Symtab.map_default (s, [])
haftmann@22642
    70
               (AList.update Thm.eq_thm_prop (thm, thyname_of s))} thy
wenzelm@18728
    71
      | _ => (warn thm; thy))
berghofe@22271
    72
    | SOME (Const (s, _), _) =>
berghofe@22271
    73
        let
berghofe@22271
    74
          val cs = foldr add_term_consts [] (prems_of thm);
berghofe@22271
    75
          val rules = Symtab.lookup_list intros s;
berghofe@22271
    76
          val nparms = (case optnparms of
berghofe@22271
    77
            SOME k => k
berghofe@22271
    78
          | NONE => (case rules of
berghofe@22271
    79
             [] => (case try (InductivePackage.the_inductive (ProofContext.init thy)) s of
berghofe@22271
    80
                 SOME (_, {raw_induct, ...}) => length (params_of raw_induct)
berghofe@22271
    81
               | NONE => 0)
berghofe@22271
    82
            | xs => snd (snd (snd (split_last xs)))))
berghofe@22271
    83
        in CodegenData.put
berghofe@22271
    84
          {intros = intros |>
haftmann@22642
    85
           Symtab.update (s, (AList.update Thm.eq_thm_prop
haftmann@22642
    86
             (thm, (thyname_of s, nparms)) rules)),
berghofe@22271
    87
           graph = foldr (uncurry (Graph.add_edge o pair s))
berghofe@22271
    88
             (Library.foldl add_node (graph, s :: cs)) cs,
berghofe@22271
    89
           eqns = eqns} thy
berghofe@22271
    90
        end
wenzelm@18728
    91
    | _ => (warn thm; thy))
wenzelm@20897
    92
  end) I);
berghofe@12557
    93
berghofe@12557
    94
fun get_clauses thy s =
berghofe@15031
    95
  let val {intros, graph, ...} = CodegenData.get thy
wenzelm@17412
    96
  in case Symtab.lookup intros s of
berghofe@22271
    97
      NONE => (case try (InductivePackage.the_inductive (ProofContext.init thy)) s of
skalberg@15531
    98
        NONE => NONE
berghofe@22271
    99
      | SOME ({names, ...}, {intrs, raw_induct, ...}) =>
berghofe@22271
   100
          SOME (names, thyname_of_const s thy, length (params_of raw_induct),
berghofe@22661
   101
            preprocess thy intrs))
skalberg@15531
   102
    | SOME _ =>
berghofe@16645
   103
        let
berghofe@16645
   104
          val SOME names = find_first
haftmann@22642
   105
            (fn xs => member (op =) xs s) (Graph.strong_conn graph);
haftmann@22642
   106
          val intrs as (_, (thyname, nparms)) :: _ =
haftmann@22642
   107
            maps (the o Symtab.lookup intros) names;
haftmann@22642
   108
        in SOME (names, thyname, nparms, preprocess thy (map fst (rev intrs))) end
berghofe@14162
   109
  end;
berghofe@12557
   110
berghofe@12557
   111
berghofe@11537
   112
(**** check if a term contains only constructor functions ****)
berghofe@11537
   113
berghofe@11537
   114
fun is_constrt thy =
berghofe@11537
   115
  let
skalberg@15570
   116
    val cnstrs = List.concat (List.concat (map
berghofe@11537
   117
      (map (fn (_, (_, _, cs)) => map (apsnd length) cs) o #descr o snd)
berghofe@11537
   118
      (Symtab.dest (DatatypePackage.get_datatypes thy))));
berghofe@11537
   119
    fun check t = (case strip_comb t of
berghofe@11537
   120
        (Var _, []) => true
haftmann@17521
   121
      | (Const (s, _), ts) => (case AList.lookup (op =) cnstrs s of
skalberg@15531
   122
            NONE => false
skalberg@15531
   123
          | SOME i => length ts = i andalso forall check ts)
berghofe@11537
   124
      | _ => false)
berghofe@11537
   125
  in check end;
berghofe@11537
   126
berghofe@11537
   127
(**** check if a type is an equality type (i.e. doesn't contain fun) ****)
berghofe@11537
   128
berghofe@11537
   129
fun is_eqT (Type (s, Ts)) = s <> "fun" andalso forall is_eqT Ts
berghofe@11537
   130
  | is_eqT _ = true;
berghofe@11537
   131
berghofe@11537
   132
(**** mode inference ****)
berghofe@11537
   133
berghofe@15204
   134
fun string_of_mode (iss, is) = space_implode " -> " (map
skalberg@15531
   135
  (fn NONE => "X"
skalberg@15531
   136
    | SOME js => enclose "[" "]" (commas (map string_of_int js)))
skalberg@15531
   137
       (iss @ [SOME is]));
berghofe@15204
   138
berghofe@15204
   139
fun print_modes modes = message ("Inferred modes:\n" ^
berghofe@15204
   140
  space_implode "\n" (map (fn (s, ms) => s ^ ": " ^ commas (map
berghofe@15204
   141
    string_of_mode ms)) modes));
berghofe@15204
   142
berghofe@11537
   143
val term_vs = map (fst o fst o dest_Var) o term_vars;
wenzelm@19046
   144
val terms_vs = distinct (op =) o List.concat o (map term_vs);
berghofe@11537
   145
berghofe@11537
   146
(** collect all Vars in a term (with duplicates!) **)
wenzelm@16861
   147
fun term_vTs tm =
wenzelm@16861
   148
  fold_aterms (fn Var ((x, _), T) => cons (x, T) | _ => I) tm [];
berghofe@11537
   149
berghofe@11537
   150
fun get_args _ _ [] = ([], [])
berghofe@11537
   151
  | get_args is i (x::xs) = (if i mem is then apfst else apsnd) (cons x)
berghofe@11537
   152
      (get_args is (i+1) xs);
berghofe@11537
   153
berghofe@11537
   154
fun merge xs [] = xs
berghofe@11537
   155
  | merge [] ys = ys
berghofe@11537
   156
  | merge (x::xs) (y::ys) = if length x >= length y then x::merge xs (y::ys)
berghofe@11537
   157
      else y::merge (x::xs) ys;
berghofe@11537
   158
berghofe@11537
   159
fun subsets i j = if i <= j then
berghofe@11537
   160
       let val is = subsets (i+1) j
berghofe@11537
   161
       in merge (map (fn ks => i::ks) is) is end
berghofe@11537
   162
     else [[]];
berghofe@11537
   163
berghofe@12557
   164
fun cprod ([], ys) = []
berghofe@12557
   165
  | cprod (x :: xs, ys) = map (pair x) ys @ cprod (xs, ys);
berghofe@12557
   166
skalberg@15574
   167
fun cprods xss = foldr (map op :: o cprod) [[]] xss;
berghofe@12557
   168
berghofe@22271
   169
datatype mode = Mode of (int list option list * int list) * int list * mode option list;
berghofe@12557
   170
berghofe@12557
   171
fun modes_of modes t =
berghofe@12557
   172
  let
berghofe@22271
   173
    val ks = 1 upto length (binder_types (fastype_of t));
berghofe@22271
   174
    val default = [Mode (([], ks), ks, [])];
berghofe@22271
   175
    fun mk_modes name args = Option.map (List.concat o
berghofe@22271
   176
      map (fn (m as (iss, is)) =>
berghofe@22271
   177
        let
berghofe@22271
   178
          val (args1, args2) =
berghofe@22271
   179
            if length args < length iss then
berghofe@22271
   180
              error ("Too few arguments for inductive predicate " ^ name)
berghofe@22271
   181
            else chop (length iss) args;
berghofe@22271
   182
          val k = length args2;
berghofe@22271
   183
          val prfx = 1 upto k
berghofe@22271
   184
        in
berghofe@22271
   185
          if not (is_prefix op = prfx is) then [] else
berghofe@22271
   186
          let val is' = map (fn i => i - k) (List.drop (is, k))
berghofe@22271
   187
          in map (fn x => Mode (m, is', x)) (cprods (map
berghofe@22271
   188
            (fn (NONE, _) => [NONE]
berghofe@22271
   189
              | (SOME js, arg) => map SOME (List.filter
berghofe@22271
   190
                  (fn Mode (_, js', _) => js=js') (modes_of modes arg)))
berghofe@22271
   191
                    (iss ~~ args1)))
berghofe@22271
   192
          end
berghofe@22271
   193
        end)) (AList.lookup op = modes name)
berghofe@12557
   194
berghofe@12557
   195
  in (case strip_comb t of
berghofe@14163
   196
      (Const ("op =", Type (_, [T, _])), _) =>
berghofe@22271
   197
        [Mode (([], [1]), [1], []), Mode (([], [2]), [2], [])] @
berghofe@22271
   198
        (if is_eqT T then [Mode (([], [1, 2]), [1, 2], [])] else [])
berghofe@22271
   199
    | (Const (name, _), args) => the_default default (mk_modes name args)
berghofe@22271
   200
    | (Var ((name, _), _), args) => the (mk_modes name args)
berghofe@22271
   201
    | (Free (name, _), args) => the (mk_modes name args)
berghofe@22271
   202
    | _ => default)
berghofe@12557
   203
  end;
berghofe@12557
   204
berghofe@12557
   205
datatype indprem = Prem of term list * term | Sidecond of term;
berghofe@12557
   206
berghofe@11537
   207
fun select_mode_prem thy modes vs ps =
wenzelm@19861
   208
  find_first (is_some o snd) (ps ~~ map
berghofe@22271
   209
    (fn Prem (us, t) => find_first (fn Mode (_, is, _) =>
berghofe@11537
   210
          let
berghofe@15031
   211
            val (in_ts, out_ts) = get_args is 1 us;
skalberg@15570
   212
            val (out_ts', in_ts') = List.partition (is_constrt thy) out_ts;
skalberg@15570
   213
            val vTs = List.concat (map term_vTs out_ts');
wenzelm@18964
   214
            val dupTs = map snd (duplicates (op =) vTs) @
haftmann@17521
   215
              List.mapPartial (AList.lookup (op =) vTs) vs;
berghofe@11537
   216
          in
berghofe@15031
   217
            terms_vs (in_ts @ in_ts') subset vs andalso
berghofe@15031
   218
            forall (is_eqT o fastype_of) in_ts' andalso
berghofe@12557
   219
            term_vs t subset vs andalso
berghofe@11537
   220
            forall is_eqT dupTs
berghofe@11537
   221
          end)
berghofe@22271
   222
            (modes_of modes t handle Option => [Mode (([], []), [], [])])
berghofe@22271
   223
      | Sidecond t => if term_vs t subset vs then SOME (Mode (([], []), [], []))
skalberg@15531
   224
          else NONE) ps);
berghofe@11537
   225
berghofe@12557
   226
fun check_mode_clause thy arg_vs modes (iss, is) (ts, ps) =
berghofe@11537
   227
  let
skalberg@15570
   228
    val modes' = modes @ List.mapPartial
skalberg@15531
   229
      (fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)]))
berghofe@12557
   230
        (arg_vs ~~ iss);
skalberg@15531
   231
    fun check_mode_prems vs [] = SOME vs
berghofe@12557
   232
      | check_mode_prems vs ps = (case select_mode_prem thy modes' vs ps of
skalberg@15531
   233
          NONE => NONE
skalberg@15531
   234
        | SOME (x, _) => check_mode_prems
berghofe@12557
   235
            (case x of Prem (us, _) => vs union terms_vs us | _ => vs)
berghofe@11537
   236
            (filter_out (equal x) ps));
skalberg@15570
   237
    val (in_ts, in_ts') = List.partition (is_constrt thy) (fst (get_args is 1 ts));
berghofe@11537
   238
    val in_vs = terms_vs in_ts;
berghofe@11537
   239
    val concl_vs = terms_vs ts
berghofe@11537
   240
  in
wenzelm@18964
   241
    forall is_eqT (map snd (duplicates (op =) (List.concat (map term_vTs in_ts)))) andalso
berghofe@15204
   242
    forall (is_eqT o fastype_of) in_ts' andalso
berghofe@11537
   243
    (case check_mode_prems (arg_vs union in_vs) ps of
skalberg@15531
   244
       NONE => false
skalberg@15531
   245
     | SOME vs => concl_vs subset vs)
berghofe@11537
   246
  end;
berghofe@11537
   247
berghofe@11537
   248
fun check_modes_pred thy arg_vs preds modes (p, ms) =
haftmann@17521
   249
  let val SOME rs = AList.lookup (op =) preds p
skalberg@15570
   250
  in (p, List.filter (fn m => case find_index
berghofe@15204
   251
    (not o check_mode_clause thy arg_vs modes m) rs of
berghofe@15204
   252
      ~1 => true
berghofe@15204
   253
    | i => (message ("Clause " ^ string_of_int (i+1) ^ " of " ^
berghofe@15204
   254
      p ^ " violates mode " ^ string_of_mode m); false)) ms)
berghofe@15204
   255
  end;
berghofe@11537
   256
haftmann@22642
   257
fun fixp f (x : (string * (int list option list * int list) list) list) =
berghofe@11537
   258
  let val y = f x
berghofe@11537
   259
  in if x = y then x else fixp f y end;
berghofe@11537
   260
berghofe@22271
   261
fun infer_modes thy extra_modes arities arg_vs preds = fixp (fn modes =>
berghofe@11537
   262
  map (check_modes_pred thy arg_vs preds (modes @ extra_modes)) modes)
berghofe@22271
   263
    (map (fn (s, (ks, k)) => (s, cprod (cprods (map
skalberg@15531
   264
      (fn NONE => [NONE]
berghofe@22271
   265
        | SOME k' => map SOME (subsets 1 k')) ks),
berghofe@22271
   266
      subsets 1 k))) arities);
berghofe@11537
   267
berghofe@11537
   268
(**** code generation ****)
berghofe@11537
   269
berghofe@11537
   270
fun mk_eq (x::xs) =
berghofe@11537
   271
  let fun mk_eqs _ [] = []
berghofe@11537
   272
        | mk_eqs a (b::cs) = Pretty.str (a ^ " = " ^ b) :: mk_eqs b cs
berghofe@11537
   273
  in mk_eqs x xs end;
berghofe@11537
   274
berghofe@11537
   275
fun mk_tuple xs = Pretty.block (Pretty.str "(" ::
skalberg@15570
   276
  List.concat (separate [Pretty.str ",", Pretty.brk 1] (map single xs)) @
berghofe@11537
   277
  [Pretty.str ")"]);
berghofe@11537
   278
haftmann@17521
   279
fun mk_v ((names, vs), s) = (case AList.lookup (op =) vs s of
skalberg@15531
   280
      NONE => ((names, (s, [s])::vs), s)
wenzelm@20071
   281
    | SOME xs => let val s' = Name.variant names s in
haftmann@17521
   282
        ((s'::names, AList.update (op =) (s, s'::xs) vs), s') end);
berghofe@11537
   283
berghofe@11537
   284
fun distinct_v (nvs, Var ((s, 0), T)) =
berghofe@11537
   285
      apsnd (Var o rpair T o rpair 0) (mk_v (nvs, s))
berghofe@11537
   286
  | distinct_v (nvs, t $ u) =
berghofe@11537
   287
      let
berghofe@11537
   288
        val (nvs', t') = distinct_v (nvs, t);
berghofe@11537
   289
        val (nvs'', u') = distinct_v (nvs', u);
berghofe@11537
   290
      in (nvs'', t' $ u') end
berghofe@11537
   291
  | distinct_v x = x;
berghofe@11537
   292
berghofe@15061
   293
fun is_exhaustive (Var _) = true
berghofe@15061
   294
  | is_exhaustive (Const ("Pair", _) $ t $ u) =
berghofe@15061
   295
      is_exhaustive t andalso is_exhaustive u
berghofe@15061
   296
  | is_exhaustive _ = false;
berghofe@15061
   297
berghofe@15061
   298
fun compile_match nvs eq_ps out_ps success_p can_fail =
skalberg@15570
   299
  let val eqs = List.concat (separate [Pretty.str " andalso", Pretty.brk 1]
skalberg@15570
   300
    (map single (List.concat (map (mk_eq o snd) nvs) @ eq_ps)));
berghofe@11537
   301
  in
berghofe@11537
   302
    Pretty.block
berghofe@11537
   303
     ([Pretty.str "(fn ", mk_tuple out_ps, Pretty.str " =>", Pretty.brk 1] @
berghofe@11537
   304
      (Pretty.block ((if eqs=[] then [] else Pretty.str "if " ::
berghofe@11537
   305
         [Pretty.block eqs, Pretty.brk 1, Pretty.str "then "]) @
berghofe@11537
   306
         (success_p ::
berghofe@15061
   307
          (if eqs=[] then [] else [Pretty.brk 1, Pretty.str "else Seq.empty"]))) ::
berghofe@15061
   308
       (if can_fail then
berghofe@15061
   309
          [Pretty.brk 1, Pretty.str "| _ => Seq.empty)"]
berghofe@15061
   310
        else [Pretty.str ")"])))
berghofe@11537
   311
  end;
berghofe@11537
   312
berghofe@17144
   313
fun modename module s (iss, is) gr =
berghofe@17144
   314
  let val (gr', id) = if s = "op =" then (gr, ("", "equal"))
berghofe@17144
   315
    else mk_const_id module s gr
berghofe@17144
   316
  in (gr', space_implode "__"
berghofe@17144
   317
    (mk_qual_id module id ::
berghofe@17144
   318
      map (space_implode "_" o map string_of_int) (List.mapPartial I iss @ [is])))
berghofe@17144
   319
  end;
berghofe@11537
   320
berghofe@22271
   321
fun mk_funcomp brack s k p = (if brack then parens else I)
berghofe@22271
   322
  (Pretty.block [Pretty.block ((if k = 0 then [] else [Pretty.str "("]) @
berghofe@22271
   323
    separate (Pretty.brk 1) (Pretty.str s :: replicate k (Pretty.str "|> ???")) @
berghofe@22271
   324
    (if k = 0 then [] else [Pretty.str ")"])), Pretty.brk 1, p]);
berghofe@22271
   325
berghofe@22271
   326
fun compile_expr thy defs dep module brack modes (gr, (NONE, t)) =
berghofe@17144
   327
      apsnd single (invoke_codegen thy defs dep module brack (gr, t))
berghofe@22271
   328
  | compile_expr _ _ _ _ _ _ (gr, (SOME _, Var ((name, _), _))) =
berghofe@12557
   329
      (gr, [Pretty.str name])
berghofe@22271
   330
  | compile_expr thy defs dep module brack modes (gr, (SOME (Mode (mode, _, ms)), t)) =
berghofe@22271
   331
      (case strip_comb t of
berghofe@22271
   332
         (Const (name, _), args) =>
berghofe@22271
   333
           if name = "op =" orelse AList.defined op = modes name then
berghofe@22271
   334
             let
berghofe@22271
   335
               val (args1, args2) = chop (length ms) args;
berghofe@22271
   336
               val (gr', (ps, mode_id)) = foldl_map
berghofe@22271
   337
                   (compile_expr thy defs dep module true modes) (gr, ms ~~ args1) |>>>
berghofe@22271
   338
                 modename module name mode;
berghofe@22271
   339
               val (gr'', ps') = foldl_map
berghofe@22271
   340
                 (invoke_codegen thy defs dep module true) (gr', args2)
berghofe@22271
   341
             in (gr', (if brack andalso not (null ps andalso null ps') then
berghofe@22271
   342
               single o parens o Pretty.block else I)
berghofe@22271
   343
                 (List.concat (separate [Pretty.brk 1]
berghofe@22271
   344
                   ([Pretty.str mode_id] :: ps @ map single ps'))))
berghofe@22271
   345
             end
berghofe@22271
   346
           else apsnd (single o mk_funcomp brack "??" (length (binder_types (fastype_of t))))
berghofe@22271
   347
             (invoke_codegen thy defs dep module true (gr, t))
berghofe@22271
   348
       | _ => apsnd (single o mk_funcomp brack "??" (length (binder_types (fastype_of t))))
berghofe@22271
   349
           (invoke_codegen thy defs dep module true (gr, t)));
berghofe@12557
   350
berghofe@22271
   351
fun compile_clause thy defs gr dep module all_vs arg_vs modes (iss, is) (ts, ps) inp =
berghofe@11537
   352
  let
skalberg@15570
   353
    val modes' = modes @ List.mapPartial
skalberg@15531
   354
      (fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)]))
berghofe@12557
   355
        (arg_vs ~~ iss);
berghofe@12557
   356
berghofe@11537
   357
    fun check_constrt ((names, eqs), t) =
berghofe@11537
   358
      if is_constrt thy t then ((names, eqs), t) else
wenzelm@20071
   359
        let val s = Name.variant names "x";
berghofe@11537
   360
        in ((s::names, (s, t)::eqs), Var ((s, 0), fastype_of t)) end;
berghofe@11537
   361
berghofe@15031
   362
    fun compile_eq (gr, (s, t)) =
berghofe@15031
   363
      apsnd (Pretty.block o cons (Pretty.str (s ^ " = ")) o single)
berghofe@17144
   364
        (invoke_codegen thy defs dep module false (gr, t));
berghofe@15031
   365
berghofe@12557
   366
    val (in_ts, out_ts) = get_args is 1 ts;
berghofe@11537
   367
    val ((all_vs', eqs), in_ts') =
berghofe@11537
   368
      foldl_map check_constrt ((all_vs, []), in_ts);
berghofe@11537
   369
berghofe@15031
   370
    fun is_ind t = (case head_of t of
haftmann@17521
   371
          Const (s, _) => s = "op =" orelse AList.defined (op =) modes s
berghofe@15031
   372
        | Var ((s, _), _) => s mem arg_vs);
berghofe@15031
   373
berghofe@11537
   374
    fun compile_prems out_ts' vs names gr [] =
berghofe@11537
   375
          let
berghofe@12453
   376
            val (gr2, out_ps) = foldl_map
berghofe@17144
   377
              (invoke_codegen thy defs dep module false) (gr, out_ts);
berghofe@15031
   378
            val (gr3, eq_ps) = foldl_map compile_eq (gr2, eqs);
berghofe@15031
   379
            val ((names', eqs'), out_ts'') =
berghofe@15031
   380
              foldl_map check_constrt ((names, []), out_ts');
berghofe@15031
   381
            val (nvs, out_ts''') = foldl_map distinct_v
berghofe@15031
   382
              ((names', map (fn x => (x, [x])) vs), out_ts'');
berghofe@12453
   383
            val (gr4, out_ps') = foldl_map
berghofe@17144
   384
              (invoke_codegen thy defs dep module false) (gr3, out_ts''');
berghofe@15031
   385
            val (gr5, eq_ps') = foldl_map compile_eq (gr4, eqs')
berghofe@11537
   386
          in
berghofe@15031
   387
            (gr5, compile_match (snd nvs) (eq_ps @ eq_ps') out_ps'
berghofe@11537
   388
              (Pretty.block [Pretty.str "Seq.single", Pretty.brk 1, mk_tuple out_ps])
berghofe@15061
   389
              (exists (not o is_exhaustive) out_ts'''))
berghofe@11537
   390
          end
berghofe@11537
   391
      | compile_prems out_ts vs names gr ps =
berghofe@11537
   392
          let
wenzelm@19046
   393
            val vs' = distinct (op =) (List.concat (vs :: map term_vs out_ts));
berghofe@22271
   394
            val SOME (p, mode as SOME (Mode (_, js, _))) =
berghofe@15126
   395
              select_mode_prem thy modes' vs' ps;
berghofe@11537
   396
            val ps' = filter_out (equal p) ps;
berghofe@15031
   397
            val ((names', eqs), out_ts') =
berghofe@15031
   398
              foldl_map check_constrt ((names, []), out_ts);
berghofe@15031
   399
            val (nvs, out_ts'') = foldl_map distinct_v
berghofe@15031
   400
              ((names', map (fn x => (x, [x])) vs), out_ts');
berghofe@15031
   401
            val (gr0, out_ps) = foldl_map
berghofe@17144
   402
              (invoke_codegen thy defs dep module false) (gr, out_ts'');
berghofe@15031
   403
            val (gr1, eq_ps) = foldl_map compile_eq (gr0, eqs)
berghofe@11537
   404
          in
berghofe@11537
   405
            (case p of
berghofe@12557
   406
               Prem (us, t) =>
berghofe@11537
   407
                 let
berghofe@15031
   408
                   val (in_ts, out_ts''') = get_args js 1 us;
berghofe@15031
   409
                   val (gr2, in_ps) = foldl_map
berghofe@22271
   410
                     (invoke_codegen thy defs dep module true) (gr1, in_ts);
berghofe@15031
   411
                   val (gr3, ps) = if is_ind t then
berghofe@22271
   412
                       apsnd (fn ps => ps @ Pretty.brk 1 ::
berghofe@22271
   413
                           separate (Pretty.brk 1) in_ps)
berghofe@22271
   414
                         (compile_expr thy defs dep module false modes
berghofe@16645
   415
                           (gr2, (mode, t)))
berghofe@15031
   416
                     else
berghofe@22271
   417
                       apsnd (fn p => [Pretty.str "Seq.of_list", Pretty.brk 1, p])
berghofe@17144
   418
                           (invoke_codegen thy defs dep module true (gr2, t));
berghofe@15031
   419
                   val (gr4, rest) = compile_prems out_ts''' vs' (fst nvs) gr3 ps';
berghofe@11537
   420
                 in
berghofe@15031
   421
                   (gr4, compile_match (snd nvs) eq_ps out_ps
berghofe@12557
   422
                      (Pretty.block (ps @
berghofe@15031
   423
                         [Pretty.str " :->", Pretty.brk 1, rest]))
berghofe@15061
   424
                      (exists (not o is_exhaustive) out_ts''))
berghofe@11537
   425
                 end
berghofe@11537
   426
             | Sidecond t =>
berghofe@11537
   427
                 let
berghofe@17144
   428
                   val (gr2, side_p) = invoke_codegen thy defs dep module true (gr1, t);
berghofe@11537
   429
                   val (gr3, rest) = compile_prems [] vs' (fst nvs) gr2 ps';
berghofe@11537
   430
                 in
berghofe@15031
   431
                   (gr3, compile_match (snd nvs) eq_ps out_ps
berghofe@11537
   432
                      (Pretty.block [Pretty.str "?? ", side_p,
berghofe@11537
   433
                        Pretty.str " :->", Pretty.brk 1, rest])
berghofe@15061
   434
                      (exists (not o is_exhaustive) out_ts''))
berghofe@11537
   435
                 end)
berghofe@11537
   436
          end;
berghofe@11537
   437
berghofe@15126
   438
    val (gr', prem_p) = compile_prems in_ts' arg_vs all_vs' gr ps;
berghofe@11537
   439
  in
berghofe@22271
   440
    (gr', Pretty.block [Pretty.str "Seq.single", Pretty.brk 1, inp,
berghofe@22271
   441
       Pretty.str " :->", Pretty.brk 1, prem_p])
berghofe@11537
   442
  end;
berghofe@11537
   443
berghofe@17144
   444
fun compile_pred thy defs gr dep module prfx all_vs arg_vs modes s cls mode =
berghofe@22271
   445
  let
berghofe@22271
   446
    val xs = map Pretty.str (Name.variant_list arg_vs
berghofe@22271
   447
      (map (fn i => "x" ^ string_of_int i) (snd mode)));
berghofe@22271
   448
    val (gr', (cl_ps, mode_id)) =
berghofe@22271
   449
      foldl_map (fn (gr, cl) => compile_clause thy defs
berghofe@22271
   450
        gr dep module all_vs arg_vs modes mode cl (mk_tuple xs)) (gr, cls) |>>>
berghofe@22271
   451
      modename module s mode
berghofe@11537
   452
  in
berghofe@11537
   453
    ((gr', "and "), Pretty.block
berghofe@11537
   454
      ([Pretty.block (separate (Pretty.brk 1)
berghofe@17144
   455
         (Pretty.str (prfx ^ mode_id) ::
berghofe@22271
   456
           map Pretty.str arg_vs @ xs) @
berghofe@22271
   457
         [Pretty.str " ="]),
berghofe@11537
   458
        Pretty.brk 1] @
skalberg@15570
   459
       List.concat (separate [Pretty.str " ++", Pretty.brk 1] (map single cl_ps))))
berghofe@11537
   460
  end;
berghofe@11537
   461
berghofe@17144
   462
fun compile_preds thy defs gr dep module all_vs arg_vs modes preds =
berghofe@11537
   463
  let val ((gr', _), prs) = foldl_map (fn ((gr, prfx), (s, cls)) =>
berghofe@16645
   464
    foldl_map (fn ((gr', prfx'), mode) => compile_pred thy defs gr'
berghofe@17144
   465
      dep module prfx' all_vs arg_vs modes s cls mode)
haftmann@17521
   466
        ((gr, prfx), ((the o AList.lookup (op =) modes) s))) ((gr, "fun "), preds)
berghofe@11537
   467
  in
skalberg@15570
   468
    (gr', space_implode "\n\n" (map Pretty.string_of (List.concat prs)) ^ ";\n\n")
berghofe@11537
   469
  end;
berghofe@11537
   470
berghofe@11537
   471
(**** processing of introduction rules ****)
berghofe@11537
   472
berghofe@12557
   473
exception Modes of
berghofe@12557
   474
  (string * (int list option list * int list) list) list *
berghofe@22271
   475
  (string * (int option list * int)) list;
berghofe@12557
   476
berghofe@17144
   477
fun lookup_modes gr dep = apfst List.concat (apsnd List.concat (ListPair.unzip
berghofe@17144
   478
  (map ((fn (SOME (Modes x), _, _) => x | _ => ([], [])) o get_node gr)
berghofe@17144
   479
    (Graph.all_preds (fst gr) [dep]))));
berghofe@12557
   480
berghofe@22271
   481
fun print_arities arities = message ("Arities:\n" ^
berghofe@22271
   482
  space_implode "\n" (map (fn (s, (ks, k)) => s ^ ": " ^
berghofe@12557
   483
    space_implode " -> " (map
berghofe@22271
   484
      (fn NONE => "X" | SOME k' => string_of_int k')
berghofe@22271
   485
        (ks @ [SOME k]))) arities));
berghofe@11537
   486
berghofe@15031
   487
fun prep_intrs intrs = map (rename_term o #prop o rep_thm o standard) intrs;
berghofe@15031
   488
berghofe@15031
   489
fun constrain cs [] = []
haftmann@22642
   490
  | constrain cs ((s, xs) :: ys) = (s, case AList.lookup (op =) cs (s : string) of
skalberg@15531
   491
      NONE => xs
skalberg@15531
   492
    | SOME xs' => xs inter xs') :: constrain cs ys;
berghofe@15031
   493
berghofe@17144
   494
fun mk_extra_defs thy defs gr dep names module ts =
skalberg@15570
   495
  Library.foldl (fn (gr, name) =>
berghofe@12557
   496
    if name mem names then gr
berghofe@12557
   497
    else (case get_clauses thy name of
skalberg@15531
   498
        NONE => gr
berghofe@22271
   499
      | SOME (names, thyname, nparms, intrs) =>
berghofe@17144
   500
          mk_ind_def thy defs gr dep names (if_library thyname module)
berghofe@22271
   501
            [] (prep_intrs intrs) nparms))
skalberg@15574
   502
            (gr, foldr add_term_consts [] ts)
berghofe@12557
   503
berghofe@22271
   504
and mk_ind_def thy defs gr dep names module modecs intrs nparms =
berghofe@17144
   505
  add_edge (hd names, dep) gr handle Graph.UNDEF _ =>
berghofe@11537
   506
    let
berghofe@22271
   507
      val _ $ u = Logic.strip_imp_concl (hd intrs);
berghofe@22271
   508
      val args = List.take (snd (strip_comb u), nparms);
skalberg@15570
   509
      val arg_vs = List.concat (map term_vs args);
berghofe@15031
   510
berghofe@22271
   511
      fun get_nparms s = if s mem names then SOME nparms else
berghofe@22271
   512
        Option.map #3 (get_clauses thy s);
berghofe@11537
   513
berghofe@22271
   514
      fun dest_prem (_ $ (Const ("op :", _) $ t $ u)) = Prem ([t], u)
berghofe@22271
   515
        | dest_prem (_ $ ((eq as Const ("op =", _)) $ t $ u)) = Prem ([t, u], eq)
berghofe@22271
   516
        | dest_prem (_ $ t) =
berghofe@22271
   517
            (case strip_comb t of
berghofe@22556
   518
               (v as Var _, ts) => if v mem args then Prem (ts, v) else Sidecond t
berghofe@22271
   519
             | (c as Const (s, _), ts) => (case get_nparms s of
berghofe@22271
   520
                 NONE => Sidecond t
berghofe@22271
   521
               | SOME k =>
berghofe@22271
   522
                   let val (ts1, ts2) = chop k ts
berghofe@22271
   523
                   in Prem (ts2, list_comb (c, ts1)) end)
berghofe@22271
   524
             | _ => Sidecond t);
berghofe@22271
   525
berghofe@22271
   526
      fun add_clause intr (clauses, arities) =
berghofe@11537
   527
        let
berghofe@22271
   528
          val _ $ t = Logic.strip_imp_concl intr;
berghofe@22271
   529
          val (Const (name, T), ts) = strip_comb t;
berghofe@22271
   530
          val (ts1, ts2) = chop nparms ts;
berghofe@22271
   531
          val prems = map dest_prem (Logic.strip_imp_prems intr);
berghofe@22271
   532
          val (Ts, Us) = chop nparms (binder_types T)
berghofe@11537
   533
        in
berghofe@22271
   534
          (AList.update op = (name, these (AList.lookup op = clauses name) @
berghofe@22271
   535
             [(ts2, prems)]) clauses,
berghofe@22271
   536
           AList.update op = (name, (map (fn U => (case strip_type U of
berghofe@22271
   537
                 (Rs as _ :: _, Type ("bool", [])) => SOME (length Rs)
berghofe@22271
   538
               | _ => NONE)) Ts,
berghofe@22271
   539
             length Us)) arities)
berghofe@11537
   540
        end;
berghofe@11537
   541
berghofe@16645
   542
      val gr' = mk_extra_defs thy defs
berghofe@17144
   543
        (add_edge (hd names, dep)
berghofe@17144
   544
          (new_node (hd names, (NONE, "", "")) gr)) (hd names) names module intrs;
berghofe@22271
   545
      val (extra_modes, extra_arities) = lookup_modes gr' (hd names);
berghofe@22271
   546
      val (clauses, arities) = fold add_clause intrs ([], []);
berghofe@15031
   547
      val modes = constrain modecs
berghofe@22271
   548
        (infer_modes thy extra_modes arities arg_vs clauses);
berghofe@22271
   549
      val _ = print_arities arities;
berghofe@11537
   550
      val _ = print_modes modes;
berghofe@17144
   551
      val (gr'', s) = compile_preds thy defs gr' (hd names) module (terms_vs intrs)
berghofe@17144
   552
        arg_vs (modes @ extra_modes) clauses;
berghofe@11537
   553
    in
berghofe@17144
   554
      (map_node (hd names)
berghofe@22271
   555
        (K (SOME (Modes (modes, arities)), module, s)) gr'')
berghofe@16645
   556
    end;
berghofe@11537
   557
berghofe@22271
   558
fun find_mode gr dep s u modes is = (case find_first (fn Mode (_, js, _) => is=js)
wenzelm@15660
   559
  (modes_of modes u handle Option => []) of
berghofe@17144
   560
     NONE => codegen_error gr dep
berghofe@17144
   561
       ("No such mode for " ^ s ^ ": " ^ string_of_mode ([], is))
berghofe@15031
   562
   | mode => mode);
berghofe@15031
   563
berghofe@22271
   564
fun mk_ind_call thy defs gr dep module is_query s T ts names thyname k intrs =
berghofe@22271
   565
  let
berghofe@22271
   566
    val (ts1, ts2) = chop k ts;
berghofe@22271
   567
    val u = list_comb (Const (s, T), ts1);
berghofe@22271
   568
berghofe@22271
   569
    fun mk_mode (((ts, mode), i), Const ("dummy_pattern", _)) =
berghofe@22271
   570
          ((ts, mode), i+1)
berghofe@22271
   571
      | mk_mode (((ts, mode), i), t) = ((ts @ [t], mode @ [i]), i+1);
berghofe@11537
   572
berghofe@22271
   573
    val module' = if_library thyname module;
berghofe@22271
   574
    val gr1 = mk_extra_defs thy defs
berghofe@22271
   575
      (mk_ind_def thy defs gr dep names module'
berghofe@22271
   576
      [] (prep_intrs intrs) k) dep names module' [u];
berghofe@22271
   577
    val (modes, _) = lookup_modes gr1 dep;
berghofe@22271
   578
    val (ts', is) = if is_query then
berghofe@22271
   579
        fst (Library.foldl mk_mode ((([], []), 1), ts2))
berghofe@22271
   580
      else (ts2, 1 upto length (binder_types T) - k);
berghofe@22271
   581
    val mode = find_mode gr1 dep s u modes is;
berghofe@22271
   582
    val (gr2, in_ps) = foldl_map
berghofe@22271
   583
      (invoke_codegen thy defs dep module true) (gr1, ts');
berghofe@22271
   584
    val (gr3, ps) =
berghofe@22271
   585
      compile_expr thy defs dep module false modes (gr2, (mode, u))
berghofe@22271
   586
  in
berghofe@22271
   587
    (gr3, Pretty.block (ps @ (if null in_ps then [] else [Pretty.brk 1]) @
berghofe@22271
   588
       separate (Pretty.brk 1) in_ps))
berghofe@22271
   589
  end;
berghofe@15031
   590
berghofe@15031
   591
fun clause_of_eqn eqn =
berghofe@15031
   592
  let
berghofe@15031
   593
    val (t, u) = HOLogic.dest_eq (HOLogic.dest_Trueprop (concl_of eqn));
berghofe@15031
   594
    val (Const (s, T), ts) = strip_comb t;
berghofe@15031
   595
    val (Ts, U) = strip_type T
berghofe@15031
   596
  in
berghofe@22271
   597
    rename_term (Logic.list_implies (prems_of eqn, HOLogic.mk_Trueprop
berghofe@22271
   598
      (list_comb (Const (s ^ "_aux", Ts @ [U] ---> HOLogic.boolT), ts @ [u]))))
berghofe@15031
   599
  end;
berghofe@15031
   600
berghofe@17144
   601
fun mk_fun thy defs name eqns dep module module' gr =
berghofe@17144
   602
  case try (get_node gr) name of
berghofe@17144
   603
    NONE =>
berghofe@15031
   604
    let
berghofe@15031
   605
      val clauses = map clause_of_eqn eqns;
berghofe@17144
   606
      val pname = name ^ "_aux";
berghofe@15031
   607
      val arity = length (snd (strip_comb (fst (HOLogic.dest_eq
berghofe@15031
   608
        (HOLogic.dest_Trueprop (concl_of (hd eqns)))))));
berghofe@15031
   609
      val mode = 1 upto arity;
berghofe@17144
   610
      val (gr', (fun_id, mode_id)) = gr |>
berghofe@17144
   611
        mk_const_id module' name |>>>
berghofe@17144
   612
        modename module' pname ([], mode);
berghofe@15031
   613
      val vars = map (fn i => Pretty.str ("x" ^ string_of_int i)) mode;
berghofe@15031
   614
      val s = Pretty.string_of (Pretty.block
berghofe@17144
   615
        [mk_app false (Pretty.str ("fun " ^ snd fun_id)) vars, Pretty.str " =",
berghofe@15031
   616
         Pretty.brk 1, Pretty.str "Seq.hd", Pretty.brk 1,
berghofe@22271
   617
         parens (Pretty.block (separate (Pretty.brk 1) (Pretty.str mode_id ::
berghofe@22271
   618
           vars)))]) ^ ";\n\n";
berghofe@17144
   619
      val gr'' = mk_ind_def thy defs (add_edge (name, dep)
berghofe@17144
   620
        (new_node (name, (NONE, module', s)) gr')) name [pname] module'
berghofe@22271
   621
        [(pname, [([], mode)])] clauses 0;
berghofe@17144
   622
      val (modes, _) = lookup_modes gr'' dep;
berghofe@22271
   623
      val _ = find_mode gr'' dep pname (head_of (HOLogic.dest_Trueprop
berghofe@22271
   624
        (Logic.strip_imp_concl (hd clauses)))) modes mode
berghofe@17144
   625
    in (gr'', mk_qual_id module fun_id) end
berghofe@17144
   626
  | SOME _ =>
berghofe@17144
   627
      (add_edge (name, dep) gr, mk_qual_id module (get_const_id name gr));
berghofe@15031
   628
berghofe@22271
   629
fun inductive_codegen thy defs gr dep module brack t = (case strip_comb t of
berghofe@22271
   630
    (Const ("Collect", Type (_, [_, Type (_, [U])])), [u]) => (case strip_comb u of
berghofe@22271
   631
        (Const (s, T), ts) => (case (get_clauses thy s, get_assoc_code thy s T) of
berghofe@22271
   632
          (SOME (names, thyname, k, intrs), NONE) =>
berghofe@22271
   633
            let val (gr', call_p) = mk_ind_call thy defs gr dep module true
berghofe@22271
   634
              s T (ts @ [Term.dummy_pattern U]) names thyname k intrs
berghofe@22271
   635
            in SOME (gr', (if brack then parens else I) (Pretty.block
berghofe@22271
   636
              [Pretty.str "Seq.list_of", Pretty.brk 1, Pretty.str "(",
berghofe@22271
   637
               call_p, Pretty.str ")"]))
berghofe@22271
   638
            end
berghofe@22271
   639
        | _ => NONE)
berghofe@22271
   640
      | _ => NONE)
berghofe@22271
   641
  | (Const (s, T), ts) => (case Symtab.lookup (#eqns (CodegenData.get thy)) s of
berghofe@22271
   642
      NONE => (case (get_clauses thy s, get_assoc_code thy s T) of
berghofe@22271
   643
        (SOME (names, thyname, k, intrs), NONE) =>
berghofe@22271
   644
          if length ts < k then NONE else SOME
berghofe@22271
   645
            (let val (gr', call_p) = mk_ind_call thy defs gr dep module false
berghofe@22271
   646
               s T (map Term.no_dummy_patterns ts) names thyname k intrs
berghofe@22271
   647
             in (gr', mk_funcomp brack "?!"
berghofe@22271
   648
               (length (binder_types T) - length ts) (parens call_p))
berghofe@22271
   649
             end handle TERM _ => mk_ind_call thy defs gr dep module true
berghofe@22271
   650
               s T ts names thyname k intrs)
berghofe@22271
   651
      | _ => NONE)
berghofe@22271
   652
    | SOME eqns =>
berghofe@22271
   653
        let
haftmann@22642
   654
          val (_, thyname) :: _ = eqns;
haftmann@22642
   655
          val (gr', id) = mk_fun thy defs s (preprocess thy (map fst (rev eqns)))
berghofe@22271
   656
            dep module (if_library thyname module) gr;
berghofe@22271
   657
          val (gr'', ps) = foldl_map
berghofe@22271
   658
            (invoke_codegen thy defs dep module true) (gr', ts);
berghofe@22271
   659
        in SOME (gr'', mk_app brack (Pretty.str id) ps)
berghofe@22271
   660
        end)
berghofe@22271
   661
  | _ => NONE);
berghofe@11537
   662
berghofe@12557
   663
val setup =
wenzelm@18708
   664
  add_codegen "inductive" inductive_codegen #>
wenzelm@18708
   665
  CodegenData.init #>
berghofe@22271
   666
  add_attribute "ind" (Scan.option (Args.$$$ "target" |-- Args.colon |-- Args.name) --
berghofe@22271
   667
    Scan.option (Args.$$$ "params" |-- Args.colon |-- Args.nat) >> uncurry add);
berghofe@11537
   668
berghofe@11537
   669
end;
berghofe@12453
   670
berghofe@12453
   671
berghofe@12453
   672
(**** combinators for code generated from inductive predicates ****)
berghofe@12453
   673
berghofe@12453
   674
infix 5 :->;
berghofe@12453
   675
infix 3 ++;
berghofe@12453
   676
wenzelm@19861
   677
fun s :-> f = Seq.maps f s;
berghofe@12453
   678
wenzelm@19861
   679
fun s1 ++ s2 = Seq.append s1 s2;
berghofe@12453
   680
berghofe@12453
   681
fun ?? b = if b then Seq.single () else Seq.empty;
berghofe@12453
   682
berghofe@22271
   683
fun ??? f g = f o g;
berghofe@22271
   684
wenzelm@19861
   685
fun ?! s = is_some (Seq.pull s);
berghofe@12453
   686
berghofe@17144
   687
fun equal__1 x = Seq.single x;
berghofe@12453
   688
berghofe@17144
   689
val equal__2 = equal__1;
berghofe@12557
   690
berghofe@17144
   691
fun equal__1_2 (x, y) = ?? (x = y);