src/HOL/Matrix/eq_codegen.ML
author skalberg
Fri, 04 Mar 2005 15:07:34 +0100
changeset 15574 b1d1b5bfc464
parent 15570 8d8c70b41bab
permissions -rw-r--r--
Removed practically all references to Library.foldr.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     1
fun inst_cterm inst ct = fst (Drule.dest_equals
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     2
  (Thm.cprop_of (Thm.instantiate inst (reflexive ct))));
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     3
fun tyinst_cterm tyinst = inst_cterm (tyinst, []);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     4
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     5
val bla = ref ([] : term list);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     6
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     7
(******************************************************)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     8
(*        Code generator for equational proofs        *)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
     9
(******************************************************)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    10
fun my_mk_meta_eq thm =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    11
  let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    12
    val (_, eq) = Thm.dest_comb (cprop_of thm);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    13
    val (ct, rhs) = Thm.dest_comb eq;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    14
    val (_, lhs) = Thm.dest_comb ct
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
    15
  in Thm.implies_elim (Drule.instantiate' [SOME (ctyp_of_term lhs)]
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
    16
    [SOME lhs, SOME rhs] eq_reflection) thm
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    17
  end; 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    18
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    19
structure SimprocsCodegen =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    20
struct
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    21
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    22
val simp_thms = ref ([] : thm list);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    23
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    24
fun parens b = if b then Pretty.enclose "(" ")" else Pretty.block;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    25
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    26
fun gen_mk_val f xs ps = Pretty.block ([Pretty.str "val ",
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    27
  f (length xs > 1) (List.concat
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    28
    (separate [Pretty.str ",", Pretty.brk 1] (map (single o Pretty.str) xs))),
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    29
  Pretty.str " =", Pretty.brk 1] @ ps @ [Pretty.str ";"]);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    30
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    31
val mk_val = gen_mk_val parens;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    32
val mk_vall = gen_mk_val (K (Pretty.enclose "[" "]"));
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    33
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    34
fun rename s = if s mem ThmDatabase.ml_reserved then s ^ "'" else s;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    35
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    36
fun mk_decomp_name (Var ((s, i), _)) = rename (if i=0 then s else s ^ string_of_int i)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    37
  | mk_decomp_name (Const (s, _)) = rename (Codegen.mk_id (Sign.base_name s))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    38
  | mk_decomp_name _ = "ct";
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    39
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    40
fun decomp_term_code cn ((vs, bs, ps), (v, t)) =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    41
  if exists (equal t o fst) bs then (vs, bs, ps)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    42
  else (case t of
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    43
      Var _ => (vs, bs @ [(t, v)], ps)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    44
    | Const _ => (vs, if cn then bs @ [(t, v)] else bs, ps)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    45
    | Bound _ => (vs, bs, ps)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    46
    | Abs (s, T, t) =>
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    47
      let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    48
        val v1 = variant vs s;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    49
        val v2 = variant (v1 :: vs) (mk_decomp_name t)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    50
      in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    51
        decomp_term_code cn ((v1 :: v2 :: vs,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    52
          bs @ [(Free (s, T), v1)],
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    53
          ps @ [mk_val [v1, v2] [Pretty.str "Thm.dest_abs", Pretty.brk 1,
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
    54
            Pretty.str "NONE", Pretty.brk 1, Pretty.str v]]), (v2, t))
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    55
      end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    56
    | t $ u =>
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    57
      let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    58
        val v1 = variant vs (mk_decomp_name t);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    59
        val v2 = variant (v1 :: vs) (mk_decomp_name u);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    60
        val (vs', bs', ps') = decomp_term_code cn ((v1 :: v2 :: vs, bs,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    61
          ps @ [mk_val [v1, v2] [Pretty.str "Thm.dest_comb", Pretty.brk 1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    62
            Pretty.str v]]), (v1, t));
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    63
        val (vs'', bs'', ps'') = decomp_term_code cn ((vs', bs', ps'), (v2, u))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    64
      in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    65
        if bs'' = bs then (vs, bs, ps) else (vs'', bs'', ps'')
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    66
      end);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    67
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    68
val strip_tv = implode o tl o explode;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    69
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    70
fun mk_decomp_tname (TVar ((s, i), _)) =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    71
      strip_tv ((if i=0 then s else s ^ string_of_int i) ^ "T")
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    72
  | mk_decomp_tname (Type (s, _)) = Codegen.mk_id (Sign.base_name s) ^ "T"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    73
  | mk_decomp_tname _ = "cT";
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    74
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    75
fun decomp_type_code ((vs, bs, ps), (v, TVar (ixn, _))) =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    76
      if exists (equal ixn o fst) bs then (vs, bs, ps)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    77
      else (vs, bs @ [(ixn, v)], ps)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    78
  | decomp_type_code ((vs, bs, ps), (v, Type (_, Ts))) =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    79
      let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    80
        val vs' = variantlist (map mk_decomp_tname Ts, vs);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    81
        val (vs'', bs', ps') =
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    82
          Library.foldl decomp_type_code ((vs @ vs', bs, ps @
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    83
            [mk_vall vs' [Pretty.str "Thm.dest_ctyp", Pretty.brk 1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    84
              Pretty.str v]]), vs' ~~ Ts)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    85
      in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    86
        if bs' = bs then (vs, bs, ps) else (vs'', bs', ps')
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    87
      end;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    88
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    89
fun gen_mk_bindings s dest decomp ((vs, bs, ps), (v, x)) =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    90
  let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    91
    val s' = variant vs s;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    92
    val (vs', bs', ps') = decomp ((s' :: vs, bs, ps @
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    93
      [mk_val [s'] (dest v)]), (s', x))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    94
  in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    95
    if bs' = bs then (vs, bs, ps) else (vs', bs', ps')
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    96
  end;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    97
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    98
val mk_term_bindings = gen_mk_bindings "ct"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
    99
  (fn s => [Pretty.str "cprop_of", Pretty.brk 1, Pretty.str s])
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   100
  (decomp_term_code true);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   101
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   102
val mk_type_bindings = gen_mk_bindings "cT"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   103
  (fn s => [Pretty.str "Thm.ctyp_of_term", Pretty.brk 1, Pretty.str s])
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   104
  decomp_type_code;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   105
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   106
fun pretty_pattern b (Const (s, _)) = Pretty.block [Pretty.str "Const",
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   107
      Pretty.brk 1, Pretty.str ("(\"" ^ s ^ "\", _)")]
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   108
  | pretty_pattern b (t as _ $ _) = parens b
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   109
      (List.concat (separate [Pretty.str " $", Pretty.brk 1]
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   110
        (map (single o pretty_pattern true) (op :: (strip_comb t)))))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   111
  | pretty_pattern b _ = Pretty.str "_";
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   112
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   113
fun term_consts' t = foldl_aterms
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   114
  (fn (cs, c as Const _) => c ins cs | (cs, _) => cs) ([], t);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   115
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   116
fun mk_apps s b p [] = p
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   117
  | mk_apps s b p (q :: qs) = 
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   118
      mk_apps s b (parens (b orelse not (null qs))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   119
        [Pretty.str s, Pretty.brk 1, p, Pretty.brk 1, q]) qs;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   120
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   121
fun mk_refleq eq ct = mk_val [eq] [Pretty.str ("Thm.reflexive " ^ ct)];
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   122
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   123
fun mk_tyinst ((s, i), s') =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   124
  Pretty.block [Pretty.str ("((" ^ quote s ^ ","), Pretty.brk 1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   125
    Pretty.str (string_of_int i ^ "),"), Pretty.brk 1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   126
    Pretty.str (s' ^ ")")];
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   127
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   128
fun inst_ty b ty_bs t s = (case term_tvars t of
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   129
    [] => Pretty.str s
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   130
  | Ts => parens b [Pretty.str "tyinst_cterm", Pretty.brk 1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   131
      Pretty.list "[" "]" (map (fn (ixn, _) => mk_tyinst
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   132
        (ixn, valOf (assoc (ty_bs, ixn)))) Ts),
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   133
      Pretty.brk 1, Pretty.str s]);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   134
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   135
fun mk_cterm_code b ty_bs ts xs (vals, t $ u) =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   136
      let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   137
        val (vals', p1) = mk_cterm_code true ty_bs ts xs (vals, t);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   138
        val (vals'', p2) = mk_cterm_code true ty_bs ts xs (vals', u)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   139
      in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   140
        (vals'', parens b [Pretty.str "Thm.capply", Pretty.brk 1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   141
          p1, Pretty.brk 1, p2])
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   142
      end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   143
  | mk_cterm_code b ty_bs ts xs (vals, Abs (s, T, t)) =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   144
      let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   145
        val u = Free (s, T);
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   146
        val SOME s' = assoc (ts, u);
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   147
        val p = Pretty.str s';
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   148
        val (vals', p') = mk_cterm_code true ty_bs ts (p :: xs)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   149
          (if null (typ_tvars T) then vals
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   150
           else vals @ [(u, (("", s'), [mk_val [s'] [inst_ty true ty_bs u s']]))], t)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   151
      in (vals',
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   152
        parens b [Pretty.str "Thm.cabs", Pretty.brk 1, p, Pretty.brk 1, p'])
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   153
      end
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   154
  | mk_cterm_code b ty_bs ts xs (vals, Bound i) = (vals, List.nth (xs, i))
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   155
  | mk_cterm_code b ty_bs ts xs (vals, t) = (case assoc (vals, t) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   156
        NONE =>
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   157
          let val SOME s = assoc (ts, t)
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   158
          in (if is_Const t andalso not (null (term_tvars t)) then
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   159
              vals @ [(t, (("", s), [mk_val [s] [inst_ty true ty_bs t s]]))]
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   160
            else vals, Pretty.str s)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   161
          end
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   162
      | SOME ((_, s), _) => (vals, Pretty.str s));
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   163
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   164
fun get_cases sg =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   165
  Symtab.foldl (fn (tab, (k, {case_rewrites, ...})) => Symtab.update_new
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   166
    ((fst (dest_Const (head_of (fst (HOLogic.dest_eq (HOLogic.dest_Trueprop
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   167
      (prop_of (hd case_rewrites))))))), map my_mk_meta_eq case_rewrites), tab))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   168
        (Symtab.empty, DatatypePackage.get_datatypes_sg sg);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   169
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   170
fun decomp_case th =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   171
  let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   172
    val (lhs, _) = Logic.dest_equals (prop_of th);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   173
    val (f, ts) = strip_comb lhs;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   174
    val (us, u) = split_last ts;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   175
    val (Const (s, _), vs) = strip_comb u
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   176
  in (us, s, vs, u) end;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   177
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   178
fun rename vs t =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   179
  let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   180
    fun mk_subst ((vs, subs), Var ((s, i), T)) =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   181
      let val s' = variant vs s
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   182
      in if s = s' then (vs, subs)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   183
        else (s' :: vs, ((s, i), Var ((s', i), T)) :: subs)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   184
      end;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   185
    val (vs', subs) = Library.foldl mk_subst ((vs, []), term_vars t)
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   186
  in (vs', subst_Vars subs t) end;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   187
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   188
fun is_instance sg t u = t = subst_TVars_Vartab
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   189
  (Type.typ_match (Sign.tsig_of sg) (Vartab.empty,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   190
    (fastype_of u, fastype_of t))) u handle Type.TYPE_MATCH => false;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   191
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   192
(*
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   193
fun lookup sg fs t = Option.map snd (Library.find_first
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   194
  (is_instance sg t o fst) fs);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   195
*)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   196
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   197
fun lookup sg fs t = (case Library.find_first (is_instance sg t o fst) fs of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   198
    NONE => (bla := (t ins !bla); NONE)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   199
  | SOME (_, x) => SOME x);
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   200
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   201
fun unint sg fs t = forall (is_none o lookup sg fs) (term_consts' t);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   202
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   203
fun mk_let s i xs ys =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   204
  Pretty.blk (0, [Pretty.blk (i, separate Pretty.fbrk (Pretty.str s :: xs)),
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   205
    Pretty.fbrk,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   206
    Pretty.blk (i, ([Pretty.str "in", Pretty.fbrk] @ ys)),
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   207
    Pretty.fbrk, Pretty.str "end"]);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   208
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   209
(*****************************************************************************)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   210
(* Generate bindings for simplifying term t                                  *)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   211
(* mkeq: whether to generate reflexivity theorem for uninterpreted terms     *)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   212
(* fs:   interpreted functions                                               *)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   213
(* ts:   atomic terms                                                        *)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   214
(* vs:   used identifiers                                                    *)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   215
(* vals: list of bindings of the form ((eq, ct), ps) where                   *)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   216
(*       eq: name of equational theorem                                      *)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   217
(*       ct: name of simplified cterm                                        *)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   218
(*       ps: ML code for creating the above two items                        *)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   219
(*****************************************************************************)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   220
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   221
fun mk_simpl_code sg case_tab mkeq fs ts ty_bs thm_bs ((vs, vals), t) =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   222
  (case assoc (vals, t) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   223
    SOME ((eq, ct), ps) =>  (* binding already generated *) 
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   224
      if mkeq andalso eq="" then
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   225
        let val eq' = variant vs "eq"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   226
        in ((eq' :: vs, overwrite (vals,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   227
          (t, ((eq', ct), ps @ [mk_refleq eq' ct])))), (eq', ct))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   228
        end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   229
      else ((vs, vals), (eq, ct))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   230
  | NONE => (case assoc (ts, t) of
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   231
      SOME v =>  (* atomic term *)
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   232
        let val xs = if not (null (term_tvars t)) andalso is_Const t then
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   233
          [mk_val [v] [inst_ty false ty_bs t v]] else []
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   234
        in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   235
          if mkeq then
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   236
            let val eq = variant vs "eq"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   237
            in ((eq :: vs, vals @
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   238
              [(t, ((eq, v), xs @ [mk_refleq eq v]))]), (eq, v))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   239
            end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   240
          else ((vs, if null xs then vals else vals @
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   241
            [(t, (("", v), xs))]), ("", v))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   242
        end
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   243
    | NONE =>  (* complex term *)
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   244
        let val (f as Const (cname, _), us) = strip_comb t
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   245
        in case Symtab.lookup (case_tab, cname) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   246
            SOME cases =>  (* case expression *)
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   247
              let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   248
                val (us', u) = split_last us;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   249
                val b = unint sg fs u;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   250
                val ((vs1, vals1), (eq, ct)) =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   251
                  mk_simpl_code sg case_tab (not b) fs ts ty_bs thm_bs ((vs, vals), u);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   252
                val xs = variantlist (replicate (length us') "f", vs1);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   253
                val (vals2, ps) = foldl_map
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   254
                  (mk_cterm_code false ty_bs ts []) (vals1, us');
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   255
                val fvals = map (fn (x, p) => mk_val [x] [p]) (xs ~~ ps);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   256
                val uT = fastype_of u;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   257
                val (us'', _, _, u') = decomp_case (hd cases);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   258
                val (vs2, ty_bs', ty_vals) = mk_type_bindings
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   259
                  (mk_type_bindings ((vs1 @ xs, [], []),
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   260
                    (hd xs, fastype_of (hd us''))), (ct, fastype_of u'));
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   261
                val insts1 = map mk_tyinst ty_bs';
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   262
                val i = length vals2;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   263
   
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   264
                fun mk_case_code ((vs, vals), (f, (name, eqn))) =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   265
                  let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   266
                    val (fvs, cname, cvs, _) = decomp_case eqn;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   267
                    val Ts = binder_types (fastype_of f);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   268
                    val ys = variantlist (map (fst o fst o dest_Var) cvs, vs);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   269
                    val cvs' = map Var (map (rpair 0) ys ~~ Ts);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   270
                    val rs = cvs' ~~ cvs;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   271
                    val lhs = list_comb (Const (cname, Ts ---> uT), cvs');
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   272
                    val rhs = Library.foldl betapply (f, cvs');
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   273
                    val (vs', tm_bs, tm_vals) = decomp_term_code false
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   274
                      ((vs @ ys, [], []), (ct, lhs));
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   275
                    val ((vs'', all_vals), (eq', ct')) = mk_simpl_code sg case_tab
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   276
                      false fs (tm_bs @ ts) ty_bs thm_bs ((vs', vals), rhs);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   277
                    val (old_vals, eq_vals) = splitAt (i, all_vals);
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   278
                    val vs''' = vs @ List.filter (fn v => exists
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   279
                      (fn (_, ((v', _), _)) => v = v') old_vals) (vs'' \\ vs');
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   280
                    val insts2 = map (fn (t, s) => Pretty.block [Pretty.str "(",
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   281
                      inst_ty false ty_bs' t (valOf (assoc (thm_bs, t))), Pretty.str ",",
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   282
                      Pretty.brk 1, Pretty.str (s ^ ")")]) ((fvs ~~ xs) @
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   283
                        (map (fn (v, s) => (valOf (assoc (rs, v)), s)) tm_bs));
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   284
                    val eq'' = if null insts1 andalso null insts2 then Pretty.str name
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   285
                      else parens (eq' <> "") [Pretty.str
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   286
                          (if null cvs then "Thm.instantiate" else "Drule.instantiate"),
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   287
                        Pretty.brk 1, Pretty.str "(", Pretty.list "[" "]" insts1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   288
                        Pretty.str ",", Pretty.brk 1, Pretty.list "[" "]" insts2,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   289
                        Pretty.str ")", Pretty.brk 1, Pretty.str name];
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   290
                    val eq''' = if eq' = "" then eq'' else
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   291
                      Pretty.block [Pretty.str "Thm.transitive", Pretty.brk 1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   292
                        eq'', Pretty.brk 1, Pretty.str eq']
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   293
                  in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   294
                    ((vs''', old_vals), Pretty.block [pretty_pattern false lhs,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   295
                      Pretty.str " =>",
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   296
                      Pretty.brk 1, mk_let "let" 2 (tm_vals @ List.concat (map (snd o snd) eq_vals))
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   297
                        [Pretty.str ("(" ^ ct' ^ ","), Pretty.brk 1, eq''', Pretty.str ")"]])
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   298
                  end;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   299
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   300
                val case_names = map (fn i => Sign.base_name cname ^ "_" ^
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   301
                  string_of_int i) (1 upto length cases);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   302
                val ((vs3, vals3), case_ps) = foldl_map mk_case_code
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   303
                  ((vs2, vals2), us' ~~ (case_names ~~ cases));
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   304
                val eq' = variant vs3 "eq";
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   305
                val ct' = variant (eq' :: vs3) "ct";
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   306
                val eq'' = variant (eq' :: ct' :: vs3) "eq";
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   307
                val case_vals =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   308
                  fvals @ ty_vals @
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   309
                  [mk_val [ct', eq'] ([Pretty.str "(case", Pretty.brk 1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   310
                    Pretty.str ("term_of " ^ ct ^ " of"), Pretty.brk 1] @
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   311
                    List.concat (separate [Pretty.brk 1, Pretty.str "| "]
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   312
                      (map single case_ps)) @ [Pretty.str ")"])]
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   313
              in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   314
                if b then
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   315
                  ((eq' :: ct' :: vs3, vals3 @
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   316
                     [(t, ((eq', ct'), case_vals))]), (eq', ct'))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   317
                else
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   318
                  let val ((vs4, vals4), (_, ctcase)) = mk_simpl_code sg case_tab false
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   319
                    fs ts ty_bs thm_bs ((eq' :: eq'' :: ct' :: vs3, vals3), f)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   320
                  in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   321
                    ((vs4, vals4 @ [(t, ((eq'', ct'), case_vals @
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   322
                       [mk_val [eq''] [Pretty.str "Thm.transitive", Pretty.brk 1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   323
                          Pretty.str "(Thm.combination", Pretty.brk 1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   324
                          Pretty.str "(Thm.reflexive", Pretty.brk 1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   325
                          mk_apps "Thm.capply" true (Pretty.str ctcase)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   326
                            (map Pretty.str xs),
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   327
                          Pretty.str ")", Pretty.brk 1, Pretty.str (eq ^ ")"),
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   328
                          Pretty.brk 1, Pretty.str eq']]))]), (eq'', ct'))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   329
                  end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   330
              end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   331
          
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   332
          | NONE =>
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   333
            let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   334
              val b = forall (unint sg fs) us;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   335
              val (q, eqs) = foldl_map
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   336
                (mk_simpl_code sg case_tab (not b) fs ts ty_bs thm_bs) ((vs, vals), us);
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   337
              val ((vs', vals'), (eqf, ctf)) = if isSome (lookup sg fs f) andalso b
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   338
                then (q, ("", ""))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   339
                else mk_simpl_code sg case_tab (not b) fs ts ty_bs thm_bs (q, f);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   340
              val ct = variant vs' "ct";
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   341
              val eq = variant (ct :: vs') "eq";
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   342
              val ctv = mk_val [ct] [mk_apps "Thm.capply" false
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   343
                (Pretty.str ctf) (map (Pretty.str o snd) eqs)];
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   344
              fun combp b = mk_apps "Thm.combination" b
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   345
                (Pretty.str eqf) (map (Pretty.str o fst) eqs)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   346
            in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   347
              case (lookup sg fs f, b) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   348
                (NONE, true) =>  (* completely uninterpreted *)
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   349
                  if mkeq then ((ct :: eq :: vs', vals' @
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   350
                    [(t, ((eq, ct), [ctv, mk_refleq eq ct]))]), (eq, ct))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   351
                  else ((ct :: vs', vals' @ [(t, (("", ct), [ctv]))]), ("", ct))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   352
              | (NONE, false) =>  (* function uninterpreted *)
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   353
                  ((eq :: ct :: vs', vals' @
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   354
                     [(t, ((eq, ct), [ctv, mk_val [eq] [combp false]]))]), (eq, ct))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   355
              | (SOME (s, _, _), true) =>  (* arguments uninterpreted *)
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   356
                  ((eq :: ct :: vs', vals' @
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   357
                     [(t, ((eq, ct), [mk_val [ct, eq] (separate (Pretty.brk 1)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   358
                       (Pretty.str s :: map (Pretty.str o snd) eqs))]))]), (eq, ct))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   359
              | (SOME (s, _, _), false) =>  (* function and arguments interpreted *)
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   360
                  let val eq' = variant (eq :: ct :: vs') "eq"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   361
                  in ((eq' :: eq :: ct :: vs', vals' @ [(t, ((eq', ct),
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   362
                    [mk_val [ct, eq] (separate (Pretty.brk 1)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   363
                       (Pretty.str s :: map (Pretty.str o snd) eqs)),
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   364
                     mk_val [eq'] [Pretty.str "Thm.transitive", Pretty.brk 1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   365
                       combp true, Pretty.brk 1, Pretty.str eq]]))]), (eq', ct))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   366
                  end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   367
            end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   368
        end));
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   369
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   370
fun lhs_of thm = fst (Logic.dest_equals (prop_of thm));
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   371
fun rhs_of thm = snd (Logic.dest_equals (prop_of thm));
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   372
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   373
fun mk_funs_code sg case_tab fs fs' =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   374
  let
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   375
    val case_thms = List.mapPartial (fn s => (case Symtab.lookup (case_tab, s) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   376
        NONE => NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   377
      | SOME thms => SOME (unsuffix "_case" (Sign.base_name s) ^ ".cases",
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   378
          map (fn i => Sign.base_name s ^ "_" ^ string_of_int i)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   379
            (1 upto length thms) ~~ thms)))
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   380
      (foldr add_term_consts [] (map (prop_of o snd)
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   381
        (List.concat (map (#3 o snd) fs'))));
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   382
    val case_vals = map (fn (s, cs) => mk_vall (map fst cs)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   383
      [Pretty.str "map my_mk_meta_eq", Pretty.brk 1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   384
       Pretty.str ("(thms \"" ^ s ^ "\")")]) case_thms;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   385
    val (vs, thm_bs, thm_vals) = Library.foldl mk_term_bindings (([], [], []),
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   386
      List.concat (map (map (apsnd prop_of) o #3 o snd) fs') @
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   387
      map (apsnd prop_of) (List.concat (map snd case_thms)));
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   388
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   389
    fun mk_fun_code (prfx, (fname, d, eqns)) =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   390
      let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   391
        val (f, ts) = strip_comb (lhs_of (snd (hd eqns)));
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   392
        val args = variantlist (replicate (length ts) "ct", vs);
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   393
        val (vs', ty_bs, ty_vals) = Library.foldl mk_type_bindings
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   394
          ((vs @ args, [], []), args ~~ map fastype_of ts);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   395
        val insts1 = map mk_tyinst ty_bs;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   396
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   397
        fun mk_eqn_code (name, eqn) =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   398
          let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   399
            val (_, argts) = strip_comb (lhs_of eqn);
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   400
            val (vs'', tm_bs, tm_vals) = Library.foldl (decomp_term_code false)
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   401
              ((vs', [], []), args ~~ argts);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   402
            val ((vs''', eq_vals), (eq, ct)) = mk_simpl_code sg case_tab false fs
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   403
              (tm_bs @ filter_out (is_Var o fst) thm_bs) ty_bs thm_bs
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   404
              ((vs'', []), rhs_of eqn);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   405
            val insts2 = map (fn (t, s) => Pretty.block [Pretty.str "(",
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   406
              inst_ty false ty_bs t (valOf (assoc (thm_bs, t))), Pretty.str ",", Pretty.brk 1,
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   407
              Pretty.str (s ^ ")")]) tm_bs
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   408
            val eq' = if null insts1 andalso null insts2 then Pretty.str name
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   409
              else parens (eq <> "") [Pretty.str "Thm.instantiate",
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   410
                Pretty.brk 1, Pretty.str "(", Pretty.list "[" "]" insts1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   411
                Pretty.str ",", Pretty.brk 1, Pretty.list "[" "]" insts2,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   412
                Pretty.str ")", Pretty.brk 1, Pretty.str name];
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   413
            val eq'' = if eq = "" then eq' else
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   414
              Pretty.block [Pretty.str "Thm.transitive", Pretty.brk 1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   415
                eq', Pretty.brk 1, Pretty.str eq]
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   416
          in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   417
            Pretty.block [parens (length argts > 1)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   418
                (Pretty.commas (map (pretty_pattern false) argts)),
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   419
              Pretty.str " =>",
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   420
              Pretty.brk 1, mk_let "let" 2 (ty_vals @ tm_vals @ List.concat (map (snd o snd) eq_vals))
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   421
                [Pretty.str ("(" ^ ct ^ ","), Pretty.brk 1, eq'', Pretty.str ")"]]
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   422
          end;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   423
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   424
        val default = if d then
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   425
            let
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15178
diff changeset
   426
              val SOME s = assoc (thm_bs, f);
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   427
              val ct = variant vs' "ct"
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   428
            in [Pretty.brk 1, Pretty.str "handle", Pretty.brk 1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   429
              Pretty.str "Match =>", Pretty.brk 1, mk_let "let" 2
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   430
                (ty_vals @ (if null (term_tvars f) then [] else
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   431
                   [mk_val [s] [inst_ty false ty_bs f s]]) @
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   432
                 [mk_val [ct] [mk_apps "Thm.capply" false (Pretty.str s)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   433
                    (map Pretty.str args)]])
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   434
                [Pretty.str ("(" ^ ct ^ ","), Pretty.brk 1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   435
                 Pretty.str "Thm.reflexive", Pretty.brk 1, Pretty.str (ct ^ ")")]]
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   436
            end
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   437
          else []
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   438
      in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   439
        ("and ", Pretty.block (separate (Pretty.brk 1)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   440
            (Pretty.str (prfx ^ fname) :: map Pretty.str args) @
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   441
          [Pretty.str " =", Pretty.brk 1, Pretty.str "(case", Pretty.brk 1,
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   442
           Pretty.list "(" ")" (map (fn s => Pretty.str ("term_of " ^ s)) args),
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   443
           Pretty.str " of", Pretty.brk 1] @
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   444
          List.concat (separate [Pretty.brk 1, Pretty.str "| "]
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   445
            (map (single o mk_eqn_code) eqns)) @ [Pretty.str ")"] @ default))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   446
      end;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   447
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   448
    val (_, decls) = foldl_map mk_fun_code ("fun ", map snd fs')
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   449
  in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   450
    mk_let "local" 2 (case_vals @ thm_vals) (separate Pretty.fbrk decls)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   451
  end;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   452
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   453
fun mk_simprocs_code sg eqns =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   454
  let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   455
    val case_tab = get_cases sg;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   456
    fun get_head th = head_of (fst (Logic.dest_equals (prop_of th)));
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   457
    fun attach_term (x as (_, _, (_, th) :: _)) = (get_head th, x);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   458
    val eqns' = map attach_term eqns;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   459
    fun mk_node (s, _, (_, th) :: _) = (s, get_head th);
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   460
    fun mk_edges (s, _, ths) = map (pair s) (distinct
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   461
      (List.mapPartial (fn t => Option.map #1 (lookup sg eqns' t))
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   462
        (List.concat (map (term_consts' o prop_of o snd) ths))));
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   463
    val gr = foldr (uncurry Graph.add_edge)
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   464
      (Library.foldr (uncurry Graph.new_node)
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   465
         (("", Bound 0) :: map mk_node eqns, Graph.empty))
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   466
      (map (pair "" o #1) eqns @ List.concat (map mk_edges eqns));
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   467
    val keys = rev (Graph.all_succs gr [""] \ "");
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   468
    fun gr_ord (x :: _, y :: _) =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   469
      int_ord (find_index (equal x) keys, find_index (equal y) keys);
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   470
    val scc = map (fn xs => List.filter (fn (_, (s, _, _)) => s mem xs) eqns')
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   471
      (sort gr_ord (Graph.strong_conn gr \ [""]));
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   472
  in
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   473
    List.concat (separate [Pretty.str ";", Pretty.fbrk, Pretty.str " ", Pretty.fbrk]
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   474
      (map (fn eqns'' => [mk_funs_code sg case_tab eqns' eqns'']) scc)) @
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   475
    [Pretty.str ";", Pretty.fbrk]
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   476
  end;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   477
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   478
fun use_simprocs_code sg eqns =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   479
  let
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   480
    fun attach_name (i, x) = (i+1, ("simp_thm_" ^ string_of_int i, x));
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   481
    fun attach_names (i, (s, b, eqs)) =
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   482
      let val (i', eqs') = foldl_map attach_name (i, eqs)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   483
      in (i', (s, b, eqs')) end;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   484
    val (_, eqns') = foldl_map attach_names (1, eqns);
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   485
    val (names, thms) = split_list (List.concat (map #3 eqns'));
15178
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   486
    val s = setmp print_mode [] Pretty.string_of
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   487
      (mk_let "local" 2 [mk_vall names [Pretty.str "!SimprocsCodegen.simp_thms"]]
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   488
        (mk_simprocs_code sg eqns'))
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   489
  in
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   490
    (simp_thms := thms; use_text Context.ml_output false s)
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   491
  end;
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   492
5f621aa35c25 Matrix theory, linear programming
obua
parents:
diff changeset
   493
end;