src/HOL/Modelcheck/mucke_oracle.ML
author wenzelm
Thu Aug 19 19:55:13 1999 +0200 (1999-08-19)
changeset 7295 fe09a0c5cebe
parent 6491 7954ffeb93f3
child 7305 dad3b686941c
permissions -rw-r--r--
quite a lot of tuning an cleanup;
wenzelm@7295
     1
exception MuckeOracleExn of term;
mueller@6473
     2
mueller@6473
     3
val trace_mc = ref false; 
mueller@6473
     4
mueller@6473
     5
mueller@6473
     6
(* transform_case post-processes output strings of the syntax "Mucke" *)
mueller@6473
     7
(* with respect to the syntax of the case construct                   *)
mueller@6473
     8
local
mueller@6473
     9
  fun extract_argument [] = []
mueller@6473
    10
  | extract_argument ("*"::_) = []
mueller@6473
    11
  | extract_argument (x::xs) = x::(extract_argument xs);
mueller@6473
    12
mueller@6473
    13
  fun cut_argument [] = []
mueller@6473
    14
  | cut_argument ("*"::r) = r
mueller@6473
    15
  | cut_argument (_::xs) = cut_argument xs;
mueller@6473
    16
mueller@6473
    17
  fun insert_case_argument [] s = []
mueller@6473
    18
  | insert_case_argument ("*"::"="::xl) (x::xs) =
mueller@6473
    19
         (explode(x)@(" "::"="::(insert_case_argument xl (x::xs))))
mueller@6473
    20
  | insert_case_argument ("c"::"a"::"s"::"e"::"*"::xl) s =
mueller@6473
    21
        (let
mueller@6473
    22
        val arg=implode(extract_argument xl);
mueller@6473
    23
        val xr=cut_argument xl
mueller@6473
    24
        in
mueller@6473
    25
         "c"::"a"::"s"::"e"::" "::(insert_case_argument xr (arg::s))
mueller@6473
    26
        end)
mueller@6473
    27
  | insert_case_argument ("e"::"s"::"a"::"c"::"*"::xl) (x::xs) =
mueller@6473
    28
        "e"::"s"::"a"::"c"::(insert_case_argument xl xs)
mueller@6473
    29
  | insert_case_argument (x::xl) s = x::(insert_case_argument xl s);
mueller@6473
    30
in
mueller@6473
    31
mueller@6473
    32
fun transform_case s = implode(insert_case_argument (explode s) []);
mueller@6473
    33
mueller@6473
    34
end;
mueller@6473
    35
mueller@6473
    36
mueller@6473
    37
(* if_full_simp_tac is a tactic for rewriting non-boolean ifs *)
mueller@6473
    38
local
mueller@6473
    39
  (* searching an if-term as below as possible *)
mueller@6473
    40
  fun contains_if (Abs(a,T,t)) = [] |
mueller@6473
    41
  contains_if (Const("If",T) $ b $ a1 $ a2) =
mueller@6473
    42
  let
mueller@6473
    43
  fun tn (Type(s,_)) = s |
mueller@6473
    44
  tn _ = error "cannot master type variables in if term";
mueller@6473
    45
  val s = tn(body_type T);
mueller@6473
    46
  in
mueller@6473
    47
  if (s="bool") then [] else [b,a1,a2]
mueller@6473
    48
  end |
mueller@6473
    49
  contains_if (a $ b) = if ((contains_if b)=[]) then (contains_if a)
mueller@6473
    50
		        else (contains_if b) |
mueller@6473
    51
  contains_if _ = [];
mueller@6473
    52
mueller@6473
    53
  fun find_replace_term (Abs(a,T,t)) = find_replace_term (snd(variant_abs(a,T,t))) |
mueller@6473
    54
  find_replace_term (a $ b) = if ((contains_if (a $ b))=[]) then
mueller@6473
    55
  (if (snd(find_replace_term b)=[]) then (find_replace_term a) else (find_replace_term b))
mueller@6473
    56
  else (a $ b,contains_if(a $ b))|
mueller@6473
    57
  find_replace_term t = (t,[]);
mueller@6473
    58
mueller@6473
    59
  fun if_substi (Abs(a,T,t)) trm = Abs(a,T,t) |
mueller@6473
    60
  if_substi (Const("If",T) $ b $ a1 $ a2) t = t |
mueller@6473
    61
  if_substi (a $ b) t = if ((contains_if b)=[]) then ((if_substi a t)$b)
mueller@6473
    62
		        else (a$(if_substi b t)) |
mueller@6473
    63
  if_substi t v = t;
mueller@6473
    64
mueller@6473
    65
  fun deliver_term (t,[]) = [] |
mueller@6473
    66
  deliver_term (t,[b,a1,a2]) =
mueller@6473
    67
  [
mueller@6473
    68
  Const("Trueprop",Type("fun",[Type("bool",[]),Type("prop",[])])) $
mueller@6473
    69
  (
mueller@6473
    70
Const("op =",Type("fun",[Type("bool",[]),Type("fun",[Type("bool",[]),Type("bool",[])])]))
mueller@6473
    71
  $ t $
mueller@6473
    72
  (
mueller@6473
    73
Const("op &",Type("fun",[Type("bool",[]),Type("fun",[Type("bool",[]),Type("bool",[])])]))
mueller@6473
    74
  $
mueller@6473
    75
  (
mueller@6473
    76
Const("op -->",Type("fun",[Type("bool",[]),Type("fun",[Type("bool",[]),Type("bool",[])])]))
mueller@6473
    77
  $ b $ (if_substi t a1))
mueller@6473
    78
  $
mueller@6473
    79
  (
mueller@6473
    80
Const("op -->",Type("fun",[Type("bool",[]),Type("fun",[Type("bool",[]),Type("bool",[])])]))
mueller@6473
    81
  $ (Const("Not",Type("fun",[Type("bool",[]),Type("bool",[])])) $ b) $ (if_substi t a2))
mueller@6473
    82
  ))] |
mueller@6473
    83
  deliver_term _ =
mueller@6473
    84
  error "tactic failed due to occurence of malformed if-term" (* shouldnt occur *);
mueller@6473
    85
mueller@6473
    86
  fun search_if (*((Const("==",_)) $ _ $*) (a) = deliver_term(find_replace_term a);
mueller@6473
    87
mueller@6473
    88
  fun search_ifs [] = [] |
mueller@6473
    89
  search_ifs (a::r) =
mueller@6473
    90
  let
mueller@6473
    91
  val i = search_if a
mueller@6473
    92
  in
mueller@6473
    93
  if (i=[]) then (search_ifs r) else i
mueller@6473
    94
  end;
mueller@6473
    95
in
mueller@6473
    96
mueller@6473
    97
fun if_full_simp_tac sset i state =
mueller@6473
    98
let val sign = #sign (rep_thm state);
mueller@6473
    99
        val (subgoal::_) = drop(i-1,prems_of state);
mueller@6473
   100
        val prems = Logic.strip_imp_prems subgoal;
mueller@6473
   101
	val concl = Logic.strip_imp_concl subgoal;
mueller@6473
   102
	val prems = prems @ [concl];
mueller@6473
   103
        val itrm = search_ifs prems;
mueller@6473
   104
in
mueller@6473
   105
if (itrm = []) then (PureGeneral.Seq.empty) else
mueller@6473
   106
(
mueller@6473
   107
let
mueller@6473
   108
val trm = hd(itrm)
mueller@6473
   109
in
mueller@6473
   110
(
mueller@6473
   111
push_proof();
mueller@6473
   112
goalw_cterm [] (cterm_of sign trm);
mueller@6473
   113
by (simp_tac (simpset()) 1);
mueller@6473
   114
        let
mueller@6473
   115
        val if_tmp_result = result()
mueller@6473
   116
        in
mueller@6473
   117
        (
mueller@6473
   118
        pop_proof();
mueller@6473
   119
        CHANGED(full_simp_tac (sset addsimps [if_tmp_result]) i) state)
mueller@6473
   120
        end
mueller@6473
   121
)
mueller@6473
   122
end)
mueller@6473
   123
end;
mueller@6473
   124
mueller@6473
   125
end;
mueller@6473
   126
mueller@6473
   127
(********************************************************)
mueller@6473
   128
(* All following stuff serves for defining mk_mc_oracle *)
mueller@6473
   129
(********************************************************)
mueller@6473
   130
mueller@6473
   131
(***************************************)
mueller@6473
   132
(* SECTION 0: some auxiliary functions *)
mueller@6473
   133
mueller@6473
   134
fun list_contains_key [] _ = false |
mueller@6473
   135
list_contains_key ((a,l)::r) t = if (a=t) then true else (list_contains_key r t);
mueller@6473
   136
mueller@6473
   137
fun search_in_keylist [] _ = [] |
mueller@6473
   138
search_in_keylist ((a,l)::r) t = if (a=t) then l else (search_in_keylist r t);
mueller@6473
   139
mueller@6473
   140
(* delivers the part of a qualified type/const-name after the last dot *)
mueller@6473
   141
fun post_last_dot str =
mueller@6473
   142
let
mueller@6473
   143
fun fl [] = [] |
mueller@6473
   144
fl (a::r) = if (a=".") then [] else (a::(fl r));
mueller@6473
   145
in
mueller@6473
   146
implode(rev(fl(rev(explode str))))
mueller@6473
   147
end;
mueller@6473
   148
mueller@6473
   149
(* OUTPUT - relevant *)
mueller@6473
   150
(* converts type to string by a mucke-suitable convention *)
mueller@6473
   151
fun type_to_string_OUTPUT (Type(a,[])) = post_last_dot a |
mueller@6473
   152
type_to_string_OUTPUT (Type("*",[a,b])) =
mueller@6473
   153
         "P_" ^ (type_to_string_OUTPUT a) ^ "_AI_" ^ (type_to_string_OUTPUT b) ^ "_R" |
mueller@6473
   154
type_to_string_OUTPUT (Type(a,l)) =
mueller@6473
   155
let
mueller@6473
   156
fun ts_to_string [] = "" |
mueller@6473
   157
ts_to_string (a::[]) = type_to_string_OUTPUT a |
mueller@6473
   158
ts_to_string (a::l) = (type_to_string_OUTPUT a) ^ "_I_" ^ (ts_to_string l);
mueller@6473
   159
in
mueller@6473
   160
(post_last_dot a) ^ "_A_" ^ (ts_to_string l) ^ "_C"
mueller@6473
   161
end |
mueller@6473
   162
type_to_string_OUTPUT _ =
mueller@6473
   163
error "unexpected type variable in type_to_string";
mueller@6473
   164
mueller@6473
   165
(* delivers type of a term *)
mueller@6473
   166
fun type_of_term (Const(_,t)) = t |
mueller@6473
   167
type_of_term (Free(_,t)) = t |
mueller@6473
   168
type_of_term (Var(_,t)) = t |
mueller@6473
   169
type_of_term (Abs(x,t,trm)) = Type("fun",[t,type_of_term(snd(variant_abs(x,t,trm)))]) |
mueller@6473
   170
type_of_term (a $ b) =
mueller@6473
   171
let
mueller@6473
   172
 fun accept_fun_type (Type("fun",[x,y])) = (x,y) |
mueller@6473
   173
 accept_fun_type _ =
mueller@6473
   174
 error "no function type returned, where it was expected (in type_of_term)";
mueller@6473
   175
 val (x,y) = accept_fun_type(type_of_term a)
mueller@6473
   176
in
mueller@6473
   177
y
mueller@6473
   178
end |
mueller@6473
   179
type_of_term _ = 
mueller@6473
   180
error "unexpected bound variable when calculating type of a term (in type_of_term)";
mueller@6473
   181
mueller@6473
   182
(* makes list [a1..an] and ty to type an -> .. -> a1 -> ty *)
mueller@6473
   183
fun fun_type_of [] ty = ty |
mueller@6473
   184
fun_type_of (a::r) ty = fun_type_of r (Type("fun",[a,ty]));
mueller@6473
   185
mueller@6473
   186
(* creates a constructor term from constructor and its argument terms *)
mueller@6473
   187
fun con_term_of t [] = t |
mueller@6473
   188
con_term_of t (a::r) = con_term_of (t $ a) r;
mueller@6473
   189
mueller@6473
   190
(* creates list of constructor terms *)
mueller@6473
   191
fun con_term_list_of trm [] = [] |
mueller@6473
   192
con_term_list_of trm (a::r) = (con_term_of trm a)::(con_term_list_of trm r);
mueller@6473
   193
mueller@6473
   194
(* list multiplication *)
mueller@6473
   195
fun multiply_element a [] = [] |
mueller@6473
   196
multiply_element a (l::r) = (a::l)::(multiply_element a r);
mueller@6473
   197
fun multiply_list [] l = [] |
mueller@6473
   198
multiply_list (a::r) l = (multiply_element a l)@(multiply_list r l);
mueller@6473
   199
mueller@6473
   200
(* To a list of types, delivers all lists of proper argument terms; tl has to *)
mueller@6473
   201
(* be a preprocessed type list with element type: (type,[(string,[type])])    *)
mueller@6473
   202
fun arglist_of sg tl [] = [[]] |
mueller@6473
   203
arglist_of sg tl (a::r) =
mueller@6473
   204
let
mueller@6473
   205
fun ispair (Type("*",x::y::[])) = (true,(x,y)) |
mueller@6473
   206
ispair x = (false,(x,x));
mueller@6473
   207
val erg =
mueller@6473
   208
(if (fst(ispair a))
mueller@6473
   209
 then (let
mueller@6473
   210
        val (x,y) = snd(ispair a)
mueller@6473
   211
       in
mueller@6473
   212
        con_term_list_of (Const("pair",Type("fun",[x,Type("fun",[y,a])])))
mueller@6473
   213
			 (arglist_of sg tl [x,y])
mueller@6473
   214
       end)
mueller@6473
   215
 else
mueller@6473
   216
 (let
mueller@6473
   217
  fun deliver_erg sg tl _ [] = [] |
mueller@6473
   218
  deliver_erg sg tl typ ((c,tyl)::r) = let
mueller@6473
   219
                        val ft = fun_type_of (rev tyl) typ;
mueller@6473
   220
                        val trm = #t(rep_cterm(read_cterm sg (c,ft)));
mueller@6473
   221
                        in
mueller@6473
   222
                        (con_term_list_of trm (arglist_of sg tl tyl))
mueller@6473
   223
			@(deliver_erg sg tl typ r)
mueller@6473
   224
                        end;
mueller@6473
   225
  val cl = search_in_keylist tl a;
mueller@6473
   226
  in
mueller@6473
   227
  deliver_erg sg tl a cl
mueller@6473
   228
  end))
mueller@6473
   229
in
mueller@6473
   230
multiply_list erg (arglist_of sg tl r)
mueller@6473
   231
end;
mueller@6473
   232
mueller@6473
   233
(*******************************************************************)
mueller@6473
   234
(* SECTION 1: Robert Sandner's source was improved and extended by *)
mueller@6473
   235
(* generation of function declarations                             *)
mueller@6473
   236
mueller@6473
   237
fun dest_Abs (Abs s_T_t) = s_T_t
mueller@6473
   238
  | dest_Abs t = raise TERM("dest_Abs", [t]);
mueller@6473
   239
mueller@6473
   240
(*
mueller@6473
   241
fun force_Abs (Abs s_T_t) = Abs s_T_t
mueller@6473
   242
  | force_Abs t = Abs("x", hd(fst(strip_type (type_of t))),
mueller@6473
   243
		      (incr_boundvars 1 t) $ (Bound 0));
mueller@6473
   244
mueller@6473
   245
fun etaexp_dest_Abs t = dest_Abs (force_Abs t);
mueller@6473
   246
*)
mueller@6473
   247
mueller@6473
   248
(* replace Vars bei Frees, freeze_thaw shares code of tactic/freeze_thaw
mueller@6473
   249
   and thm.instantiate *)
mueller@6473
   250
fun freeze_thaw t =
mueller@6473
   251
  let val used = add_term_names (t, [])
mueller@6473
   252
          and vars = term_vars t;
mueller@6473
   253
      fun newName (Var(ix,_), (pairs,used)) = 
mueller@6473
   254
          let val v = variant used (string_of_indexname ix)
mueller@6473
   255
          in  ((ix,v)::pairs, v::used)  end;
mueller@6473
   256
      val (alist, _) = foldr newName (vars, ([], used));
mueller@6473
   257
      fun mk_inst (Var(v,T)) = 
mueller@6473
   258
          (Var(v,T),
mueller@6473
   259
           Free(the (assoc(alist,v)), T));
mueller@6473
   260
      val insts = map mk_inst vars;
mueller@6473
   261
  in subst_atomic insts t end;
mueller@6473
   262
mueller@6473
   263
fun make_fun_type (a::b::l) = Type("fun",a::(make_fun_type (b::l))::[]) 
mueller@6473
   264
  | make_fun_type (a::l) = a;
mueller@6473
   265
mueller@6473
   266
fun make_decl muckeType id isaType =
mueller@6473
   267
  let val constMuckeType = Const(muckeType,isaType);
mueller@6473
   268
      val constId = Const(id,isaType);
mueller@6473
   269
      val constDecl = Const("_decl", make_fun_type [isaType,isaType,isaType]); 
mueller@6473
   270
  in (constDecl $ constMuckeType) $ constId end;
mueller@6473
   271
mueller@6473
   272
fun make_MuTerm muDeclTerm ParamDeclTerm muTerm isaType =
mueller@6473
   273
  let val constMu = Const("_mu",
mueller@6473
   274
			  make_fun_type [isaType,isaType,isaType,isaType]);
mueller@6473
   275
      val t1 = constMu $ muDeclTerm;
mueller@6473
   276
      val t2 = t1 $ ParamDeclTerm;
mueller@6473
   277
      val t3 = t2 $  muTerm
mueller@6473
   278
  in t3 end;
mueller@6473
   279
mueller@6473
   280
fun make_MuDecl muDeclTerm ParamDeclTerm isaType =
mueller@6473
   281
  let val constMu = Const("_mudec",
mueller@6473
   282
                          make_fun_type [isaType,isaType,isaType]);
mueller@6473
   283
      val t1 = constMu $ muDeclTerm;
mueller@6473
   284
      val t2 = t1 $ ParamDeclTerm
mueller@6473
   285
  in t2 end;
mueller@6473
   286
mueller@6473
   287
fun make_NuTerm muDeclTerm ParamDeclTerm muTerm isaType =
mueller@6473
   288
  let val constMu = Const("_nu",
mueller@6473
   289
                          make_fun_type [isaType,isaType,isaType,isaType]);
mueller@6473
   290
      val t1 = constMu $ muDeclTerm;
mueller@6473
   291
      val t2 = t1 $ ParamDeclTerm;
mueller@6473
   292
      val t3 = t2 $  muTerm
mueller@6473
   293
  in t3 end;
mueller@6473
   294
mueller@6473
   295
fun make_NuDecl muDeclTerm ParamDeclTerm isaType =
mueller@6473
   296
  let val constMu = Const("_nudec",
mueller@6473
   297
                          make_fun_type [isaType,isaType,isaType]);
mueller@6473
   298
      val t1 = constMu $ muDeclTerm;
mueller@6473
   299
      val t2 = t1 $ ParamDeclTerm
mueller@6473
   300
  in t2 end;
mueller@6473
   301
mueller@6473
   302
fun is_mudef (( Const("==",_) $ t1) $ ((Const("MuCalculus.mu",_)) $ t2)) = true
mueller@6473
   303
  | is_mudef _ = false;
mueller@6473
   304
mueller@6473
   305
fun is_nudef (( Const("==",_) $ t1) $ ((Const("MuCalculus.nu",_)) $ t2)) = true
mueller@6473
   306
  | is_nudef _ = false;
mueller@6473
   307
mueller@6473
   308
fun make_decls sign Dtype (Const(str,tp)::n::Clist) = 
mueller@6473
   309
    let val const_decls = Const("_decls",make_fun_type [Dtype,Dtype,Dtype]);
mueller@6473
   310
        val decl = make_decl (type_to_string_OUTPUT tp) str Dtype;
mueller@6473
   311
    in
mueller@6473
   312
    ((const_decls $ decl) $ (make_decls sign Dtype (n::Clist)))
mueller@6473
   313
    end
mueller@6473
   314
  | make_decls sign Dtype [Const(str,tp)] = 
mueller@6473
   315
      make_decl (type_to_string_OUTPUT tp) str Dtype;
mueller@6473
   316
mueller@6473
   317
mueller@6473
   318
(* make_mu_def transforms an Isabelle Mu-Definition into Mucke format
mueller@6473
   319
   Takes equation of the form f = Mu Q. % x. t *)
mueller@6473
   320
mueller@6473
   321
fun dest_atom (Const t) = dest_Const (Const t)
mueller@6473
   322
  | dest_atom (Free t)  = dest_Free (Free t);
mueller@6473
   323
mueller@6473
   324
fun get_decls sign Clist (Abs(s,tp,trm)) = 
mueller@6473
   325
    let val VarAbs = variant_abs(s,tp,trm);
mueller@6473
   326
    in get_decls sign (Const(fst VarAbs,tp)::Clist) (snd VarAbs)
mueller@6473
   327
    end
mueller@6473
   328
  | get_decls sign Clist ((Const("split",_)) $ trm) = get_decls sign Clist trm
mueller@6473
   329
  | get_decls sign Clist trm = (Clist,trm);
mueller@6473
   330
mueller@6473
   331
fun make_mu_def sign ((tt $ LHS) $ (ttt $ RHS)) =
mueller@6473
   332
  let 	val LHSStr = fst (dest_atom LHS);
mueller@6473
   333
	val MuType = Type("bool",[]); (* always ResType of mu, also serves
mueller@6473
   334
					 as dummy type *)
mueller@6473
   335
	val (_,_,PAbs) = dest_Abs (RHS); (* RHS is %Q. ... *)
mueller@6473
   336
  	val (PCon_LHS,MMuTerm) = get_decls sign [] (subst_bound (LHS,PAbs));
mueller@6473
   337
	val PConsts = rev PCon_LHS;
mueller@6473
   338
	val muDeclTerm = make_decl "bool" LHSStr MuType;
mueller@6473
   339
	val PDeclsTerm = make_decls sign MuType PConsts;
mueller@6473
   340
	val MuDefTerm = make_MuTerm muDeclTerm PDeclsTerm MMuTerm MuType;		
mueller@6473
   341
  in MuDefTerm end;
mueller@6473
   342
mueller@6473
   343
fun make_mu_decl sign ((tt $ LHS) $ (ttt $ RHS)) =
mueller@6473
   344
  let   val LHSStr = fst (dest_atom LHS);
mueller@6473
   345
        val MuType = Type("bool",[]); (* always ResType of mu, also serves
mueller@6473
   346
                                         as dummy type *)
mueller@6473
   347
        val (_,_,PAbs) = dest_Abs (RHS); (* RHS is %Q. ... *)
mueller@6473
   348
        val (PCon_LHS,MMuTerm) = get_decls sign [] (subst_bound (LHS,PAbs));
mueller@6473
   349
        val PConsts = rev PCon_LHS;
mueller@6473
   350
        val muDeclTerm = make_decl "bool" LHSStr MuType;
mueller@6473
   351
        val PDeclsTerm = make_decls sign MuType PConsts;
mueller@6473
   352
        val MuDeclTerm = make_MuDecl muDeclTerm PDeclsTerm MuType;
mueller@6473
   353
  in MuDeclTerm end;
mueller@6473
   354
mueller@6473
   355
fun make_nu_def sign ((tt $ LHS) $ (ttt $ RHS)) =
mueller@6473
   356
  let   val LHSStr = fst (dest_atom LHS);
mueller@6473
   357
        val MuType = Type("bool",[]); (* always ResType of mu, also serves
mueller@6473
   358
                                         as dummy type *)
mueller@6473
   359
        val (_,_,PAbs) = dest_Abs (RHS); (* RHS is %Q. ... *)
mueller@6473
   360
        val (PCon_LHS,MMuTerm) = get_decls sign [] (subst_bound (LHS,PAbs));
mueller@6473
   361
        val PConsts = rev PCon_LHS;
mueller@6473
   362
        val muDeclTerm = make_decl "bool" LHSStr MuType;
mueller@6473
   363
        val PDeclsTerm = make_decls sign MuType PConsts;
mueller@6473
   364
        val NuDefTerm = make_NuTerm muDeclTerm PDeclsTerm MMuTerm MuType;
mueller@6473
   365
  in NuDefTerm end;
mueller@6473
   366
mueller@6473
   367
fun make_nu_decl sign ((tt $ LHS) $ (ttt $ RHS)) =
mueller@6473
   368
  let   val LHSStr = fst (dest_atom LHS);
mueller@6473
   369
        val MuType = Type("bool",[]); (* always ResType of mu, also serves
mueller@6473
   370
                                         as dummy type *)
mueller@6473
   371
        val (_,_,PAbs) = dest_Abs (RHS); (* RHS is %Q. ... *)
mueller@6473
   372
        val (PCon_LHS,MMuTerm) = get_decls sign [] (subst_bound (LHS,PAbs));
mueller@6473
   373
        val PConsts = rev PCon_LHS; 
mueller@6473
   374
        val muDeclTerm = make_decl "bool" LHSStr MuType;
mueller@6473
   375
        val PDeclsTerm = make_decls sign MuType PConsts; 
mueller@6473
   376
        val NuDeclTerm = make_NuDecl muDeclTerm PDeclsTerm MuType;
mueller@6473
   377
  in NuDeclTerm end;
mueller@6473
   378
mueller@6473
   379
fun make_FunMuckeTerm FunDeclTerm ParamDeclTerm Term isaType =
mueller@6473
   380
  let 	val constFun = Const("_fun",
mueller@6473
   381
			    make_fun_type [isaType,isaType,isaType,isaType]);
mueller@6473
   382
      	val t1 = constFun $ FunDeclTerm;
mueller@6473
   383
      	val t2 = t1 $ ParamDeclTerm;
mueller@6473
   384
      	val t3 = t2 $  Term
mueller@6473
   385
  in t3 end;
mueller@6473
   386
mueller@6473
   387
fun make_FunMuckeDecl FunDeclTerm ParamDeclTerm isaType =
mueller@6473
   388
  let   val constFun = Const("_dec",
mueller@6473
   389
                            make_fun_type [isaType,isaType,isaType]);
mueller@6473
   390
      val t1 = constFun $ FunDeclTerm;
mueller@6473
   391
      val t2 = t1 $ ParamDeclTerm
mueller@6473
   392
  in t2 end;
mueller@6473
   393
mueller@6473
   394
fun is_fundef (( Const("==",_) $ _) $ ((Const("split",_)) $ _)) = true |
mueller@6473
   395
is_fundef (( Const("==",_) $ _) $ Abs(x_T_t)) = true 
mueller@6473
   396
| is_fundef _ = false; 
mueller@6473
   397
mueller@6473
   398
fun make_mucke_fun_def sign ((_ $ LHS) $ RHS) =
mueller@6473
   399
  let 	(* fun dest_atom (Const t) = dest_Const (Const t)
mueller@6473
   400
          | dest_atom (Free t)  = dest_Free (Free t); *)
mueller@6473
   401
	val LHSStr = fst (dest_atom LHS);
mueller@6473
   402
	val LHSResType = body_type(snd(dest_atom LHS));
mueller@6473
   403
	val LHSResTypeStr = type_to_string_OUTPUT LHSResType;
mueller@6473
   404
(*	val (_,AbsType,RawTerm) = dest_Abs(RHS);
mueller@6473
   405
*)	val (Consts_LHS_rev,Free_RHS) = get_decls sign [] RHS;
mueller@6473
   406
	val Consts_LHS = rev Consts_LHS_rev;
mueller@6473
   407
	val PDeclsTerm = make_decls sign LHSResType Consts_LHS; 
mueller@6473
   408
		(* Boolean functions only, list necessary in general *)
mueller@6473
   409
	val DeclTerm = make_decl LHSResTypeStr LHSStr LHSResType;
mueller@6473
   410
	val MuckeDefTerm = make_FunMuckeTerm DeclTerm PDeclsTerm Free_RHS
mueller@6473
   411
					 LHSResType;	
mueller@6473
   412
  in MuckeDefTerm end;
mueller@6473
   413
mueller@6473
   414
fun make_mucke_fun_decl sign ((_ $ LHS) $ RHS) =
mueller@6473
   415
  let   (* fun dest_atom (Const t) = dest_Const (Const t)
mueller@6473
   416
          | dest_atom (Free t)  = dest_Free (Free t); *)
mueller@6473
   417
        val LHSStr = fst (dest_atom LHS);
mueller@6473
   418
	val LHSResType = body_type(snd(dest_atom LHS));
mueller@6473
   419
        val LHSResTypeStr = type_to_string_OUTPUT LHSResType;
mueller@6473
   420
(*      val (_,AbsType,RawTerm) = dest_Abs(RHS);
mueller@6473
   421
*)      val (Consts_LHS_rev,Free_RHS) = get_decls sign [] RHS;
mueller@6473
   422
        val Consts_LHS = rev Consts_LHS_rev;
mueller@6473
   423
        val PDeclsTerm = make_decls sign LHSResType Consts_LHS;
mueller@6473
   424
                (* Boolean functions only, list necessary in general *)
mueller@6473
   425
        val DeclTerm = make_decl LHSResTypeStr LHSStr LHSResType;
mueller@6473
   426
	val MuckeDeclTerm = make_FunMuckeDecl DeclTerm PDeclsTerm LHSResType;
mueller@6473
   427
in MuckeDeclTerm end;
mueller@6473
   428
mueller@6473
   429
fun elim_quantifications sign ((Const("Ex",_)) $ Abs (str,tp,t)) =
mueller@6473
   430
    (let val ExConst = Const("_Ex",make_fun_type [tp,tp,tp,tp]);
mueller@6473
   431
     	 val TypeConst = Const (type_to_string_OUTPUT tp,tp);
mueller@6473
   432
	 val VarAbs = variant_abs(str,tp,t);
mueller@6473
   433
	 val BoundConst = Const(fst VarAbs,tp);
mueller@6473
   434
	 val t1 = ExConst $ TypeConst;
mueller@6473
   435
	 val t2 = t1 $ BoundConst;
mueller@6473
   436
 	 val t3 = elim_quantifications sign (snd VarAbs)
mueller@6473
   437
     in t2 $ t3 end)
mueller@6473
   438
  |  elim_quantifications sign ((Const("All",_)) $ Abs (str,tp,t)) =
mueller@6473
   439
    (let val AllConst = Const("_All",make_fun_type [tp,tp,tp,tp]);
mueller@6473
   440
    	 val TypeConst = Const (type_to_string_OUTPUT tp,tp);
mueller@6473
   441
	 val VarAbs = variant_abs(str,tp,t);
mueller@6473
   442
	 val BoundConst = Const(fst VarAbs,tp);
mueller@6473
   443
	 val t1 = AllConst $ TypeConst;
mueller@6473
   444
	 val t2 = t1 $ BoundConst;
mueller@6473
   445
	 val t3 = elim_quantifications sign (snd VarAbs)
mueller@6473
   446
     in t2 $ t3 end)
mueller@6473
   447
  | elim_quantifications sign (t1 $ t2) = 
mueller@6473
   448
   	(elim_quantifications sign t1) $ (elim_quantifications sign t2)
mueller@6473
   449
  | elim_quantifications sign (Abs(_,_,t)) = elim_quantifications sign t
mueller@6473
   450
  | elim_quantifications sign t = t;
mueller@6473
   451
fun elim_quant_in_list sign [] = []
mueller@6473
   452
  | elim_quant_in_list sign (trm::list) = 
mueller@6473
   453
			(elim_quantifications sign trm)::(elim_quant_in_list sign list);
mueller@6473
   454
mueller@6473
   455
fun dummy true = writeln "True\n" |
mueller@6473
   456
    dummy false = writeln "Fals\n";
mueller@6473
   457
mueller@6473
   458
fun transform_definitions sign [] = []
mueller@6473
   459
  | transform_definitions sign (trm::list) = 
mueller@6473
   460
      if is_mudef trm 
mueller@6473
   461
      then (make_mu_def sign trm)::(transform_definitions sign list)
mueller@6473
   462
      else 
mueller@6473
   463
	if is_nudef trm
mueller@6473
   464
	 then (make_nu_def sign trm)::(transform_definitions sign list)
mueller@6473
   465
     	 else 
mueller@6473
   466
	   if is_fundef trm
mueller@6473
   467
 	   then (make_mucke_fun_def sign trm)::(transform_definitions sign list)
mueller@6473
   468
		     else trm::(transform_definitions sign list);
mueller@6473
   469
mueller@6473
   470
fun terms_to_decls sign [] = []
mueller@6473
   471
 | terms_to_decls sign (trm::list) =
mueller@6473
   472
      if is_mudef trm
mueller@6473
   473
      then (make_mu_decl sign trm)::(terms_to_decls sign list)
mueller@6473
   474
      else
mueller@6473
   475
        if is_nudef trm
mueller@6473
   476
         then (make_nu_decl sign trm)::(terms_to_decls sign list)
mueller@6473
   477
         else
mueller@6473
   478
           if is_fundef trm
mueller@6473
   479
           then (make_mucke_fun_decl sign trm)::(terms_to_decls sign list)
mueller@6473
   480
                     else (transform_definitions sign list);
mueller@6473
   481
mueller@6473
   482
fun transform_terms sign list = 
mueller@6473
   483
let val DefsOk = transform_definitions sign list;
mueller@6473
   484
in elim_quant_in_list sign DefsOk
mueller@6473
   485
end;
mueller@6473
   486
mueller@6473
   487
fun string_of_terms sign terms =
mueller@6473
   488
let fun make_string sign [] = "" |
mueller@6473
   489
 	make_string sign (trm::list) = 
mueller@6473
   490
           ((Sign.string_of_term sign trm) ^ "\n" ^(make_string sign list))
mueller@6473
   491
in
mueller@6473
   492
(setmp print_mode ["Mucke"] (make_string sign) terms)
mueller@6473
   493
end;
mueller@6473
   494
wenzelm@6491
   495
fun callmc s =
wenzelm@6491
   496
  let
wenzelm@7295
   497
    val mucke_home = getenv "MUCKE_HOME";
wenzelm@7295
   498
    val mucke =
wenzelm@7295
   499
      if mucke_home = "" then error "Environment variable MUCKE_HOME not set"
wenzelm@7295
   500
      else mucke_home ^ "/mucke";
wenzelm@7295
   501
    val mucke_input_file = File.tmp_path (Path.basic "tmp.mu");
wenzelm@7295
   502
    val _ = File.write mucke_input_file s;
wenzelm@7295
   503
    val result = execute (mucke ^ " -nb " ^ (File.sysify_path mucke_input_file));
wenzelm@7295
   504
  in
wenzelm@7295
   505
    if not (!trace_mc) then (File.rm mucke_input_file) else (); 
wenzelm@7295
   506
    result
wenzelm@7295
   507
  end;
mueller@6473
   508
mueller@6473
   509
(* extract_result looks for true value before *) 
mueller@6473
   510
(* finishing line "===..." of mucke output    *)
wenzelm@7295
   511
(* ------------------------------------------ *)
wenzelm@7295
   512
(* Be Careful:                                *)
wenzelm@7295
   513
(* If the mucke version changes, some changes *)
wenzelm@7295
   514
(* have also to be made here:                 *)
wenzelm@7295
   515
(* In extract_result, the value               *)
wenzelm@7295
   516
(* answer_with_info_lines checks the output   *)
wenzelm@7295
   517
(* of the muche version, where the output     *)
wenzelm@7295
   518
(* finishes with information about memory and *)
wenzelm@7295
   519
(* time (segregated from the "true" value by  *)
wenzelm@7295
   520
(* a line of equality signs).                 *)
wenzelm@7295
   521
(* For older versions, where this line does   *)
wenzelm@7295
   522
(* exist, value general_answer checks whether *)
wenzelm@7295
   523
(* "true" stand at the end of the output.     *)
mueller@6473
   524
local
mueller@6473
   525
wenzelm@7295
   526
infix is_prefix_of contains at_post string_contains string_at_post;
mueller@6473
   527
mueller@6473
   528
  val is_blank : string -> bool =
mueller@6473
   529
      fn " " => true | "\t" => true | "\n" => true | "\^L" => true 
mueller@6473
   530
       | "\160" => true | _ => false;
mueller@6473
   531
mueller@6473
   532
  fun delete_blanks [] = []
mueller@6473
   533
    | delete_blanks (":"::xs) = delete_blanks xs
mueller@6473
   534
    | delete_blanks (x::xs) = 
mueller@6473
   535
        if (is_blank x) then (delete_blanks xs)
mueller@6473
   536
	       	        else x::(delete_blanks xs);
mueller@6473
   537
  
mueller@6473
   538
  fun delete_blanks_string s = implode(delete_blanks (explode s));
mueller@6473
   539
mueller@6473
   540
  fun [] is_prefix_of s = true
mueller@6473
   541
    | (p::ps) is_prefix_of  [] = false
mueller@6473
   542
    | (p::ps) is_prefix_of (x::xs) = (p = x) andalso (ps is_prefix_of xs);
mueller@6473
   543
mueller@6473
   544
  fun [] contains [] = true
mueller@6473
   545
    | [] contains s = false
mueller@6473
   546
    | (x::xs) contains s = (s is_prefix_of (x::xs)) orelse (xs contains s);
mueller@6473
   547
wenzelm@7295
   548
  fun [] at_post [] = true
wenzelm@7295
   549
    | [] at_post s = false
wenzelm@7295
   550
    | (x::xs) at_post s = (s = (x::xs)) orelse (xs at_post s);
wenzelm@7295
   551
 
mueller@6473
   552
  fun s string_contains s1 = 
mueller@6473
   553
      (explode s) contains (explode s1);
wenzelm@7295
   554
  fun s string_at_post s1 =
wenzelm@7295
   555
      (explode s) at_post (explode s1);
mueller@6473
   556
mueller@6473
   557
in 
mueller@6473
   558
mueller@6473
   559
fun extract_result goal answer =
mueller@6473
   560
  let 
mueller@6473
   561
    val search_text_true = "istrue===";
mueller@6473
   562
    val short_answer = delete_blanks_string answer;
wenzelm@7295
   563
    val answer_with_info_lines = short_answer string_contains search_text_true;
wenzelm@7295
   564
    val general_answer = short_answer string_at_post "true" 
mueller@6473
   565
  in
wenzelm@7295
   566
    (answer_with_info_lines) orelse (general_answer)
mueller@6473
   567
  end;
mueller@6473
   568
mueller@6473
   569
end; 
mueller@6473
   570
mueller@6473
   571
(**************************************************************)
mueller@6473
   572
(* SECTION 2: rewrites case-constructs over complex datatypes *)
mueller@6473
   573
local
mueller@6473
   574
mueller@6473
   575
(* check_case checks, whether a term is of the form a $ "(case x of ...)", *)
mueller@6473
   576
(* where x is of complex datatype; the second argument of the result is    *)
mueller@6473
   577
(* the number of constructors of the type of x                             *) 
mueller@6473
   578
fun check_case sg tl (a $ b) =
mueller@6473
   579
let
mueller@6473
   580
 (* tl_contains_complex returns true in the 1st argument when argument type is *)
mueller@6473
   581
 (* complex; the 2nd argument is the number of constructors                    *)
mueller@6473
   582
 fun tl_contains_complex [] _ = (false,0) |
mueller@6473
   583
 tl_contains_complex ((a,l)::r) t =
mueller@6473
   584
 let
mueller@6473
   585
  fun check_complex [] p = p |
mueller@6473
   586
  check_complex ((a,[])::r) (t,i) = check_complex r (t,i+1) |
mueller@6473
   587
  check_complex ((a,_)::r) (t,i) = check_complex r (true,i+1);
mueller@6473
   588
 in
mueller@6473
   589
	if (a=t) then (check_complex l (false,0)) else (tl_contains_complex r t)
mueller@6473
   590
 end;
mueller@6473
   591
 fun check_head_for_case sg (Const(s,_)) st 0 = 
mueller@6473
   592
	if (post_last_dot(s) = (st ^ "_case")) then true else false |
mueller@6473
   593
 check_head_for_case sg (a $ (Const(s,_))) st 0 = 
mueller@6473
   594
	if (post_last_dot(s) = (st ^ "_case")) then true else false |
mueller@6473
   595
 check_head_for_case _ _ _ 0 = false |
mueller@6473
   596
 check_head_for_case sg (a $ b) st n = check_head_for_case sg a st (n-1) |
mueller@6473
   597
 check_head_for_case _ _ _ _ = false;
mueller@6473
   598
 fun qtn (Type(a,_)) = a | 
mueller@6473
   599
 qtn _ = error "unexpected type variable in check_case";
mueller@6473
   600
 val t = type_of_term b;
mueller@6473
   601
 val (bv,n) = tl_contains_complex tl t
mueller@6473
   602
 val st = post_last_dot(qtn t); 
mueller@6473
   603
in
mueller@6473
   604
 if (bv) then ((check_head_for_case sg a st n),n) else (false,n) 
mueller@6473
   605
end |
mueller@6473
   606
check_case sg tl trm = (false,0);
mueller@6473
   607
mueller@6473
   608
(* enrich_case_with_terms enriches a case term with additional *)
mueller@6473
   609
(* conditions according to the context of the case replacement *)
mueller@6473
   610
fun enrich_case_with_terms sg [] t = t |
mueller@6473
   611
enrich_case_with_terms sg [trm] (Abs(x,T,t)) =
mueller@6473
   612
let
mueller@6473
   613
 val v = variant_abs(x,T,t);
mueller@6473
   614
 val f = fst v;
mueller@6473
   615
 val s = snd v
mueller@6473
   616
in
mueller@6473
   617
(Const("Ex",Type("fun",[Type("fun",[T,Type("bool",[])]),Type("bool",[])])) $
mueller@6473
   618
(Abs(x,T,
mueller@6473
   619
abstract_over(Free(f,T),
mueller@6473
   620
Const("op &",Type("fun",[Type("bool",[]),Type("fun",[Type("bool",[]),Type("bool",[])])]))
mueller@6473
   621
$
mueller@6473
   622
(Const("op =",Type("fun",[T,Type("fun",[T,Type("bool",[])])])) $ (Free(f,T)) $ trm)
mueller@6473
   623
$ s))))
mueller@6473
   624
end |
mueller@6473
   625
enrich_case_with_terms sg (a::r) (Abs(x,T,t)) =
mueller@6473
   626
        enrich_case_with_terms sg [a] (Abs(x,T,(enrich_case_with_terms sg r t))) |
mueller@6473
   627
enrich_case_with_terms sg (t::r) trm =
mueller@6473
   628
let val ty = type_of_term t
mueller@6473
   629
in
mueller@6473
   630
(Const("Ex",Type("fun",[Type("fun",[ ty ,Type("bool",[])]),Type("bool",[])])) $
mueller@6473
   631
Abs("a", ty,
mueller@6473
   632
Const("op &",Type("fun",[Type("bool",[]),Type("fun",[Type("bool",[]),Type("bool",[])])]))
mueller@6473
   633
$
mueller@6473
   634
(Const("op =",Type("fun",[ ty ,Type("fun",[ ty ,Type("bool",[])])])) $ Bound(0) $ t)
mueller@6473
   635
$
mueller@6473
   636
enrich_case_with_terms sg r (trm $ (Bound(length(r))))))
mueller@6473
   637
end;
mueller@6473
   638
mueller@6473
   639
fun replace_termlist_with_constrs sg tl (a::l1) (c::l2) t = 
mueller@6473
   640
let
mueller@6473
   641
 fun heart_of_trm (Abs(x,T,t)) = heart_of_trm(snd(variant_abs(x,T,t))) |
mueller@6473
   642
 heart_of_trm t = t;
mueller@6473
   643
 fun replace_termlist_with_args _ _ trm _ [] _ ([],[]) = trm (* should never occur *) |
mueller@6473
   644
 replace_termlist_with_args sg _ trm _ [a] _ ([],[]) =
mueller@6473
   645
	if ((heart_of_trm trm)= Const("False",Type("bool",[]))) then trm else 
mueller@6473
   646
	(enrich_case_with_terms sg a trm) |
mueller@6473
   647
 replace_termlist_with_args sg tl trm con [] t (l1,l2) =
mueller@6473
   648
	(replace_termlist_with_constrs sg tl l1 l2 t) | 
mueller@6473
   649
 replace_termlist_with_args sg tl trm con (a::r) t (l1,l2) =
mueller@6473
   650
 let
mueller@6473
   651
  val tty = type_of_term t;
mueller@6473
   652
  val con_term = con_term_of con a;
mueller@6473
   653
 in
mueller@6473
   654
	(Const("If",Type("fun",[Type("bool",[]),
mueller@6473
   655
        Type("fun",[Type("bool",[]),Type("fun",[Type("bool",[]),Type("bool",[])])])])) $
mueller@6473
   656
	(Const("op =",Type("fun",[tty,Type("fun",[tty,Type("bool",[])])])) $
mueller@6473
   657
        t $ con_term) $
mueller@6473
   658
	(if ((heart_of_trm trm)= Const("False",Type("bool",[]))) then trm else 
mueller@6473
   659
	(enrich_case_with_terms sg a trm)) $
mueller@6473
   660
	(replace_termlist_with_args sg tl trm con r t (l1,l2)))
mueller@6473
   661
 end;
mueller@6473
   662
 val arglist = arglist_of sg tl (snd c);
mueller@6473
   663
 val tty = type_of_term t;
mueller@6473
   664
 val con_typ = fun_type_of (rev (snd c)) tty;
mueller@6473
   665
 val con = #t(rep_cterm(read_cterm sg (fst c,con_typ)));
mueller@6473
   666
in
mueller@6473
   667
 replace_termlist_with_args sg tl a con arglist t (l1,l2)
mueller@6473
   668
end |
mueller@6473
   669
replace_termlist_with_constrs _ _ _ _ _ = 
mueller@6473
   670
error "malformed arguments in replace_termlist_with_constrs" (* shouldnt occur *);
mueller@6473
   671
mueller@6473
   672
(* rc_in_termlist constructs a cascading if with the case terms in trm_list, *)
mueller@6473
   673
(* which were found in rc_in_term (see replace_case)                         *)
mueller@6473
   674
fun rc_in_termlist sg tl trm_list trm =  
mueller@6473
   675
let
mueller@6473
   676
 val ty = type_of_term trm;
mueller@6473
   677
 val constr_list = search_in_keylist tl ty;
mueller@6473
   678
in
mueller@6473
   679
	replace_termlist_with_constrs sg tl trm_list constr_list trm
mueller@6473
   680
end;  
mueller@6473
   681
mueller@6473
   682
in
mueller@6473
   683
mueller@6473
   684
(* replace_case replaces each case statement over a complex datatype by a cascading if; *)
mueller@6473
   685
(* it is normally called with a 0 in the 4th argument, it is positive, if in the course *)
mueller@6473
   686
(* of calculation, a "case" is discovered and then indicates the distance to that case  *)
mueller@6473
   687
fun replace_case sg tl (a $ b) 0 =
mueller@6473
   688
let
mueller@6473
   689
 (* rc_in_term changes the case statement over term x to a cascading if; the last *)
mueller@6473
   690
 (* indicates the distance to the "case"-constant                                 *)
mueller@6473
   691
 fun rc_in_term sg tl (a $ b) l x 0 =
mueller@6473
   692
	 (replace_case sg tl a 0) $ (rc_in_termlist sg tl l x) |  
mueller@6473
   693
 rc_in_term sg tl  _ l x 0 = rc_in_termlist sg tl l x |
mueller@6473
   694
 rc_in_term sg tl (a $ b) l x n = rc_in_term sg tl a (b::l) x (n-1) |
mueller@6473
   695
 rc_in_term sg _ trm _ _ n =
mueller@6473
   696
 error("malformed term for case-replacement: " ^ (Sign.string_of_term sg trm));
mueller@6473
   697
 val (bv,n) = check_case sg tl (a $ b);
mueller@6473
   698
in
mueller@6473
   699
 if (bv) then 
mueller@6473
   700
	(let
mueller@6473
   701
	val t = (replace_case sg tl a n) 
mueller@6473
   702
	in 
mueller@6473
   703
	rc_in_term sg tl t [] b n	
mueller@6473
   704
	end)
mueller@6473
   705
 else ((replace_case sg tl a 0) $ (replace_case sg tl b 0))
mueller@6473
   706
end |
mueller@6473
   707
replace_case sg tl (a $ b) 1 = a $ (replace_case sg tl b 0) |
mueller@6473
   708
replace_case sg tl (a $ b) n = (replace_case sg tl a (n-1)) $ (replace_case sg tl b 0) |
mueller@6473
   709
replace_case sg tl (Abs(x,T,t)) _ = 
mueller@6473
   710
let 
mueller@6473
   711
 val v = variant_abs(x,T,t);
mueller@6473
   712
 val f = fst v;
mueller@6473
   713
 val s = snd v
mueller@6473
   714
in
mueller@6473
   715
 Abs(x,T,abstract_over(Free(f,T),replace_case sg tl s 0))
mueller@6473
   716
end |
mueller@6473
   717
replace_case _ _ t _ = t;
mueller@6473
   718
mueller@6473
   719
end;
mueller@6473
   720
mueller@6473
   721
(*******************************************************************)
mueller@6473
   722
(* SECTION 3: replacing variables being part of a constructor term *)
mueller@6473
   723
mueller@6473
   724
(* transforming terms so that nowhere a variable is subterm of *)
mueller@6473
   725
(* a constructor term; the transformation uses cascading ifs   *)
mueller@6473
   726
fun remove_vars sg tl (Abs(x,ty,trm)) =
mueller@6473
   727
let
mueller@6473
   728
(* checks freeness of variable x in term *)
mueller@6473
   729
fun xFree x (a $ b) = if (xFree x a) then true else (xFree x b) |
mueller@6473
   730
xFree x (Abs(a,T,trm)) = xFree x trm |
mueller@6473
   731
xFree x (Free(y,_)) = if (x=y) then true else false |
mueller@6473
   732
xFree _ _ = false;
mueller@6473
   733
(* really substitues variable x by term c *)
mueller@6473
   734
fun real_subst sg tl x c (a$b) = (real_subst sg tl x c a) $ (real_subst sg tl x c b) |
mueller@6473
   735
real_subst sg tl x c (Abs(y,T,trm)) = Abs(y,T,real_subst sg tl x c trm) |
mueller@6473
   736
real_subst sg tl x c (Free(y,t)) = if (x=y) then c else Free(y,t) |
mueller@6473
   737
real_subst sg tl x c t = t;
mueller@6473
   738
(* substituting variable x by term c *)
mueller@6473
   739
fun x_subst sg tl x c (a $ b) =
mueller@6473
   740
let
mueller@6473
   741
 val t = (type_of_term (a $ b))
mueller@6473
   742
in
mueller@6473
   743
 if ((list_contains_key tl t) andalso (xFree x (a$b)))
mueller@6473
   744
 then (real_subst sg tl x c (a$b)) else ((x_subst sg tl x c a) $ (x_subst sg tl x c b))
mueller@6473
   745
end |
mueller@6473
   746
x_subst sg tl x c (Abs(y,T,trm)) = Abs(y,T,x_subst sg tl x c trm) |
mueller@6473
   747
x_subst sg tl x c t = t;
mueller@6473
   748
(* genearting a cascading if *)
mueller@6473
   749
fun casc_if sg tl x ty (c::l) trm =
mueller@6473
   750
let
mueller@6473
   751
 val arglist = arglist_of sg tl (snd c);
mueller@6473
   752
 val con_typ = fun_type_of (rev (snd c)) ty;
mueller@6473
   753
 val con = #t(rep_cterm(read_cterm sg (fst c,con_typ)));
mueller@6473
   754
 fun casc_if2 sg tl x con [] ty trm [] = trm | (* should never occur *)
mueller@6473
   755
 casc_if2 sg tl x con [arg] ty trm [] = x_subst sg tl x (con_term_of con arg) trm |
mueller@6473
   756
 casc_if2 sg tl x con (a::r) ty trm cl =
mueller@6473
   757
        Const("If",Type("fun",[Type("bool",[]),
mueller@6473
   758
        Type("fun",[Type("bool",[]),Type("fun",[Type("bool",[]),Type("bool",[])])])
mueller@6473
   759
        ])) $
mueller@6473
   760
     (Const("op =",Type("fun",[ty,Type("fun",[ty,Type("bool",[])])])) $
mueller@6473
   761
        Free(x,ty) $ (real_subst sg tl x (con_term_of con a) (Free(x,ty)))) $
mueller@6473
   762
   (x_subst sg tl x (con_term_of con a) trm) $
mueller@6473
   763
   (casc_if2 sg tl x con r ty trm cl) |
mueller@6473
   764
 casc_if2 sg tl x con [] ty trm cl = casc_if sg tl x ty cl trm;
mueller@6473
   765
in
mueller@6473
   766
 casc_if2 sg tl x con arglist ty trm l
mueller@6473
   767
end |
mueller@6473
   768
casc_if _ _ _ _ [] trm = trm (* should never occur *); 
mueller@6473
   769
fun if_term sg tl x ty trm =
mueller@6473
   770
let
mueller@6473
   771
 val tyC_list = search_in_keylist tl ty;
mueller@6473
   772
in
mueller@6473
   773
 casc_if sg tl x ty tyC_list trm
mueller@6473
   774
end;
mueller@6473
   775
(* checking whether a variable occurs in a constructor term *)
mueller@6473
   776
fun constr_term_contains_var sg tl (a $ b) x =
mueller@6473
   777
let
mueller@6473
   778
 val t = type_of_term (a $ b)
mueller@6473
   779
in
mueller@6473
   780
 if ((list_contains_key tl t) andalso (xFree x (a$b))) then true
mueller@6473
   781
 else
mueller@6473
   782
 (if (constr_term_contains_var sg tl a x) then true 
mueller@6473
   783
  else (constr_term_contains_var sg tl b x))
mueller@6473
   784
end |
mueller@6473
   785
constr_term_contains_var sg tl (Abs(y,ty,trm)) x =
mueller@6473
   786
         constr_term_contains_var sg tl (snd(variant_abs(y,ty,trm))) x |
mueller@6473
   787
constr_term_contains_var _ _ _ _ = false;
mueller@6473
   788
val vt = variant_abs(x,ty,trm);
mueller@6473
   789
val tt = remove_vars sg tl (snd(vt))
mueller@6473
   790
in
mueller@6473
   791
if (constr_term_contains_var sg tl tt (fst vt))
mueller@6473
   792
(* fst vt muss frei vorkommen, als ECHTER TeilKonstruktorterm *)
mueller@6473
   793
then (Abs(x,ty,
mueller@6473
   794
        abstract_over(Free(fst vt,ty),
mueller@6473
   795
	if_term sg ((Type("bool",[]),[("True",[]),("False",[])])::tl) (fst vt) ty tt)))
mueller@6473
   796
else Abs(x,ty,abstract_over(Free(fst vt,ty),tt))
mueller@6473
   797
end |
mueller@6473
   798
remove_vars sg tl (a $ b) = (remove_vars sg tl a) $ (remove_vars sg tl b) |
mueller@6473
   799
remove_vars sg tl t = t;
mueller@6473
   800
mueller@6473
   801
(* dissolves equalities "=" of boolean terms, where one of them is a complex term *)
mueller@6473
   802
fun remove_equal sg tl (Abs(x,ty,trm)) = Abs(x,ty,remove_equal sg tl trm) |
mueller@6473
   803
remove_equal sg tl (Const("op =",Type("fun",[Type("bool",[]),
mueller@6473
   804
	Type("fun",[Type("bool",[]),Type("bool",[])])])) $ lhs $ rhs) =
mueller@6473
   805
let
mueller@6473
   806
fun complex_trm (Abs(_,_,_)) = true |
mueller@6473
   807
complex_trm (_ $ _) = true |
mueller@6473
   808
complex_trm _ = false;
mueller@6473
   809
in
mueller@6473
   810
(if ((complex_trm lhs) orelse (complex_trm rhs)) then
mueller@6473
   811
(let
mueller@6473
   812
val lhs = remove_equal sg tl lhs;
mueller@6473
   813
val rhs = remove_equal sg tl rhs
mueller@6473
   814
in
mueller@6473
   815
(
mueller@6473
   816
Const("op &",
mueller@6473
   817
Type("fun",[Type("bool",[]),Type("fun",[Type("bool",[]),Type("bool",[])])])) $
mueller@6473
   818
(Const("op -->",
mueller@6473
   819
 Type("fun",[Type("bool",[]),Type("fun",[Type("bool",[]),Type("bool",[])])])) $
mueller@6473
   820
	lhs $ rhs) $
mueller@6473
   821
(Const("op -->",
mueller@6473
   822
 Type("fun",[Type("bool",[]),Type("fun",[Type("bool",[]),Type("bool",[])])])) $
mueller@6473
   823
	rhs $ lhs)
mueller@6473
   824
)
mueller@6473
   825
end)
mueller@6473
   826
else
mueller@6473
   827
(Const("op =",
mueller@6473
   828
 Type("fun",[Type("bool",[]),Type("fun",[Type("bool",[]),Type("bool",[])])])) $
mueller@6473
   829
	lhs $ rhs))
mueller@6473
   830
end |
mueller@6473
   831
remove_equal sg tl (a $ b) = (remove_equal sg tl a) $ (remove_equal sg tl b) |
mueller@6473
   832
remove_equal sg tl trm = trm;
mueller@6473
   833
mueller@6473
   834
(* rewrites constructor terms (without vars) for mucke *)
mueller@6473
   835
fun rewrite_dt_term sg tl (Abs(x,ty,t)) = Abs(x,ty,(rewrite_dt_term sg tl t)) |
mueller@6473
   836
rewrite_dt_term sg tl (a $ b) =
mueller@6473
   837
let
mueller@6473
   838
mueller@6473
   839
(* OUTPUT - relevant *)
mueller@6473
   840
(* transforms constructor term to a mucke-suitable output *)
mueller@6473
   841
fun term_OUTPUT sg (Const("Pair",_) $ a $ b) =
mueller@6473
   842
		(term_OUTPUT sg a) ^ "_I_" ^ (term_OUTPUT sg b) |
mueller@6473
   843
term_OUTPUT sg (a $ b) = (term_OUTPUT sg a) ^ "_I_" ^ (term_OUTPUT sg b) |
mueller@6473
   844
term_OUTPUT sg (Const(s,t)) = post_last_dot s |
mueller@6473
   845
term_OUTPUT _ _ = 
mueller@6473
   846
error "term contains an unprintable constructor term (in term_OUTPUT)";
mueller@6473
   847
mueller@6473
   848
fun contains_bound i (Bound(j)) = if (j>=i) then true else false |
mueller@6473
   849
contains_bound i (a $ b) = if (contains_bound i a) then true else (contains_bound i b) |
mueller@6473
   850
contains_bound i (Abs(_,_,t)) = contains_bound (i+1) t |
mueller@6473
   851
contains_bound _ _ = false;
mueller@6473
   852
mueller@6473
   853
in
mueller@6473
   854
        if (contains_bound 0 (a $ b)) 
mueller@6473
   855
	then ((rewrite_dt_term sg tl a) $ (rewrite_dt_term sg tl b))
mueller@6473
   856
        else
mueller@6473
   857
        (
mueller@6473
   858
        let
mueller@6473
   859
        val t = type_of_term (a $ b);
mueller@6473
   860
        in
mueller@6473
   861
        if (list_contains_key tl t) then (Const((term_OUTPUT sg (a $ b)),t)) else
mueller@6473
   862
        ((rewrite_dt_term sg tl a) $ (rewrite_dt_term sg tl b))
mueller@6473
   863
        end)
mueller@6473
   864
end |
mueller@6473
   865
rewrite_dt_term sg tl t = t;
mueller@6473
   866
mueller@6473
   867
fun rewrite_dt_terms sg tl [] = [] |
mueller@6473
   868
rewrite_dt_terms sg tl (a::r) =
mueller@6473
   869
let
mueller@6473
   870
 val c = (replace_case sg ((Type("bool",[]),[("True",[]),("False",[])])::tl) a 0);
mueller@6473
   871
 val rv = (remove_vars sg tl c);  
mueller@6473
   872
 val rv = (remove_equal sg tl rv);
mueller@6473
   873
in
mueller@6473
   874
 ((rewrite_dt_term sg tl rv) 
mueller@6473
   875
 :: (rewrite_dt_terms sg tl r))
mueller@6473
   876
end;
mueller@6473
   877
mueller@6473
   878
(**********************************************************************)
mueller@6473
   879
(* SECTION 4: generating type declaration and preprocessing type list *)
mueller@6473
   880
mueller@6473
   881
fun make_type_decls [] tl = "" |
mueller@6473
   882
make_type_decls ((a,l)::r) tl = 
mueller@6473
   883
let
mueller@6473
   884
fun comma_list_of [] = "" |
mueller@6473
   885
comma_list_of (a::[]) = a |
mueller@6473
   886
comma_list_of (a::r) = a ^ "," ^ (comma_list_of r);
mueller@6473
   887
mueller@6473
   888
(* OUTPUT - relevant *)
mueller@6473
   889
(* produces for each type of the 2nd argument its constant names (see *)
mueller@6473
   890
(* concat_constr) and appends them to prestring (see concat_types)    *)
mueller@6473
   891
fun generate_constnames_OUTPUT prestring [] _ = [prestring] |
mueller@6473
   892
generate_constnames_OUTPUT prestring ((Type("*",[a,b]))::r) tl =
mueller@6473
   893
 generate_constnames_OUTPUT prestring (a::b::r) tl |
mueller@6473
   894
generate_constnames_OUTPUT prestring (a::r) tl =
mueller@6473
   895
let
mueller@6473
   896
 fun concat_constrs [] _ = [] |
mueller@6473
   897
 concat_constrs ((c,[])::r) tl = c::(concat_constrs r tl)  |
mueller@6473
   898
 concat_constrs ((c,l)::r) tl =
mueller@6473
   899
         (generate_constnames_OUTPUT (c ^ "_I_") l tl) @ (concat_constrs r tl);
mueller@6473
   900
 fun concat_types _ [] _ _ = [] |
mueller@6473
   901
 concat_types prestring (a::q) [] tl = [prestring ^ a] 
mueller@6473
   902
				       @ (concat_types prestring q [] tl) |
mueller@6473
   903
 concat_types prestring (a::q) r tl = 
mueller@6473
   904
		(generate_constnames_OUTPUT (prestring ^ a ^ "_I_") r tl) 
mueller@6473
   905
		@ (concat_types prestring q r tl);
mueller@6473
   906
 val g = concat_constrs (search_in_keylist tl a) tl;
mueller@6473
   907
in
mueller@6473
   908
 concat_types prestring g r tl
mueller@6473
   909
end;
mueller@6473
   910
mueller@6473
   911
fun make_type_decl a tl =
mueller@6473
   912
let
mueller@6473
   913
        val astr = type_to_string_OUTPUT a;
mueller@6473
   914
        val dl = generate_constnames_OUTPUT "" [a] tl;
mueller@6473
   915
        val cl = comma_list_of dl;
mueller@6473
   916
in
mueller@6473
   917
        ("enum " ^ astr ^ " {" ^ cl ^ "};\n")
mueller@6473
   918
end;
mueller@6473
   919
in
mueller@6473
   920
 (make_type_decl a tl) ^ (make_type_decls r tl)
mueller@6473
   921
end;
mueller@6473
   922
mueller@6473
   923
fun check_finity gl [] [] true = true |
mueller@6473
   924
check_finity gl bl [] true = (check_finity gl [] bl false) |
mueller@6473
   925
check_finity _ _ [] false =
mueller@6473
   926
error "used datatypes are not finite" |
mueller@6473
   927
check_finity gl bl ((t,cl)::r) b =
mueller@6473
   928
let
mueller@6473
   929
fun listmem [] _ = true |
mueller@6473
   930
listmem (a::r) l = if (a mem l) then (listmem r l) else false;
mueller@6473
   931
fun snd_listmem [] _ = true |
mueller@6473
   932
snd_listmem ((a,b)::r) l = if (listmem b l) then (snd_listmem r l) else false;
mueller@6473
   933
in
mueller@6473
   934
(if (snd_listmem cl gl) then (check_finity (t::gl) bl r true)
mueller@6473
   935
else (check_finity gl ((t,cl)::bl) r b))
mueller@6473
   936
end;
mueller@6473
   937
wenzelm@7295
   938
fun preprocess_td sg [] done = [] |
wenzelm@7295
   939
preprocess_td sg (a::b) done =
mueller@6473
   940
let
mueller@6473
   941
fun extract_hd sg (_ $ Abs(_,_,r)) = extract_hd sg r |
mueller@6473
   942
extract_hd sg (Const("Trueprop",_) $ r) = extract_hd sg r |
mueller@6473
   943
extract_hd sg (Var(_,_) $ r) = extract_hd sg r |
mueller@6473
   944
extract_hd sg (a $ b) = extract_hd sg a |
mueller@6473
   945
extract_hd sg (Const(s,t)) = post_last_dot s |
mueller@6473
   946
extract_hd _ _ =
mueller@6473
   947
error "malformed constructor term in a induct-theorem";
mueller@6473
   948
fun list_of_param_types sg tl pl (_ $ Abs(_,t,r)) =
mueller@6473
   949
let
mueller@6473
   950
 fun select_type [] [] t = t |
mueller@6473
   951
 select_type (a::r) (b::s) t =
mueller@6473
   952
 if (t=b) then a else (select_type r s t) |
mueller@6473
   953
 select_type _ _ _ =
mueller@6473
   954
 error "wrong number of argument of a constructor in a induct-theorem";
mueller@6473
   955
in
mueller@6473
   956
 (select_type tl pl t) :: (list_of_param_types sg tl pl r)
mueller@6473
   957
end |
mueller@6473
   958
list_of_param_types sg tl pl (Const("Trueprop",_) $ r) = list_of_param_types sg tl pl r |
mueller@6473
   959
list_of_param_types _ _ _ _ = [];
mueller@6473
   960
fun split_constr sg tl pl a = (extract_hd sg a,list_of_param_types sg tl pl a);
mueller@6473
   961
fun split_constrs _ _ _ [] = [] |
mueller@6473
   962
split_constrs sg tl pl (a::r) = (split_constr sg tl pl a) :: (split_constrs sg tl pl r);
mueller@6473
   963
fun new_types [] = [] |
mueller@6473
   964
new_types ((t,l)::r) =
mueller@6473
   965
let
mueller@6473
   966
 fun ex_bool [] = [] |
mueller@6473
   967
 ex_bool ((Type("bool",[]))::r) = ex_bool r |
mueller@6473
   968
 ex_bool ((Type("*",[a,b]))::r) = ex_bool (a::b::r) |
mueller@6473
   969
 ex_bool (s::r) = s:: (ex_bool r);
mueller@6473
   970
 val ll = ex_bool l
mueller@6473
   971
in
mueller@6473
   972
 (ll @ (new_types r))
mueller@6473
   973
end;
mueller@6473
   974
in
mueller@6473
   975
        if (a mem done)
wenzelm@7295
   976
        then (preprocess_td sg b done)
mueller@6473
   977
        else
mueller@6473
   978
        (let
mueller@6473
   979
	 fun qtn (Type(a,tl)) = (a,tl) |
mueller@6473
   980
	 qtn _ = error "unexpected type variable in preprocess_td";
mueller@6473
   981
	 val s =  post_last_dot(fst(qtn a));
mueller@6473
   982
	 fun param_types ((Const("Trueprop",_)) $ (_ $ (Var(_,Type(_,t)))))  = t |
mueller@6473
   983
	 param_types _ = error "malformed induct-theorem in preprocess_td";	
wenzelm@7295
   984
	 val pl = param_types (concl_of (get_thm (theory_of_sign sg) (s ^ ".induct")));		
wenzelm@7295
   985
         val l = split_constrs sg (snd(qtn a)) pl (prems_of (get_thm (theory_of_sign sg) (s ^ ".induct")));
mueller@6473
   986
	 val ntl = new_types l;
mueller@6473
   987
        in 
wenzelm@7295
   988
        ((a,l) :: (preprocess_td sg (ntl @ b) (a :: done)))
mueller@6473
   989
        end)
mueller@6473
   990
end;
mueller@6473
   991
mueller@6473
   992
fun extract_type_names_prems sg [] = [] |
mueller@6473
   993
extract_type_names_prems sg (a::b) =
mueller@6473
   994
let
mueller@6473
   995
fun analyze_types sg [] = [] |
mueller@6473
   996
analyze_types sg [Type(a,[])] =
mueller@6473
   997
(let
mueller@6473
   998
 val s = (Sign.string_of_typ sg (Type(a,[])))
mueller@6473
   999
in
mueller@6473
  1000
 (if (s="bool") then ([]) else ([Type(a,[])]))
mueller@6473
  1001
end) |
mueller@6473
  1002
analyze_types sg [Type("*",l)] = analyze_types sg l |
mueller@6473
  1003
analyze_types sg [Type("fun",l)] = analyze_types sg l |
mueller@6473
  1004
analyze_types sg [Type(t,l)] = ((Type(t,l))::(analyze_types sg l)) |
mueller@6473
  1005
analyze_types sg (a::l) = (analyze_types sg [a]) @ (analyze_types sg l);
mueller@6473
  1006
fun extract_type_names sg (Const("==",_)) = [] |
mueller@6473
  1007
extract_type_names sg (Const("Trueprop",_)) = [] |
mueller@6473
  1008
extract_type_names sg (Const(_,typ)) = analyze_types sg [typ] |
mueller@6473
  1009
extract_type_names sg (a $ b) = (extract_type_names sg a) @ (extract_type_names sg b) |
mueller@6473
  1010
extract_type_names sg (Abs(x,T,t)) = (analyze_types sg [T]) @ (extract_type_names sg t) |
mueller@6473
  1011
extract_type_names _ _ = [];
mueller@6473
  1012
in
mueller@6473
  1013
 (extract_type_names sg a) @ extract_type_names_prems sg b
mueller@6473
  1014
end;
mueller@6473
  1015
mueller@6473
  1016
(**********************************************************)
mueller@6473
  1017
(* mk_mc_oracle:  new argument of MuckeOracleExn: source_thy *)
mueller@6473
  1018
wenzelm@7295
  1019
fun mk_mucke_mc_oracle (sign, MuckeOracleExn (subgoal)) = 
mueller@6473
  1020
  let 	val Freesubgoal = freeze_thaw subgoal;
mueller@6473
  1021
mueller@6473
  1022
	val prems = Logic.strip_imp_prems Freesubgoal; 
mueller@6473
  1023
	val concl = Logic.strip_imp_concl Freesubgoal; 
mueller@6473
  1024
	
mueller@6473
  1025
	val Muckedecls = terms_to_decls sign prems;
mueller@6473
  1026
	val decls_str = string_of_terms sign Muckedecls;
mueller@6473
  1027
	
mueller@6473
  1028
	val type_list = (extract_type_names_prems sign (prems@[concl]));
wenzelm@7295
  1029
	val ctl =  preprocess_td sign type_list [];
mueller@6473
  1030
	val b = if (ctl=[]) then true else (check_finity [Type("bool",[])] [] ctl false);
mueller@6473
  1031
	val type_str = make_type_decls ctl 
mueller@6473
  1032
				((Type("bool",[]),("True",[])::("False",[])::[])::ctl);
mueller@6473
  1033
	
mueller@6473
  1034
	val mprems = rewrite_dt_terms sign ctl prems;
mueller@6473
  1035
	val mconcl = rewrite_dt_terms sign ctl [concl];
mueller@6473
  1036
mueller@6473
  1037
	val Muckeprems = transform_terms sign mprems;
mueller@6473
  1038
        val prems_str = transform_case(string_of_terms sign Muckeprems);
mueller@6473
  1039
mueller@6473
  1040
        val Muckeconcl = elim_quant_in_list sign mconcl;
mueller@6473
  1041
	val concl_str = transform_case(string_of_terms sign Muckeconcl);
mueller@6473
  1042
mueller@6473
  1043
	val MCstr = (
mueller@6473
  1044
		"/**** type declarations: ****/\n" ^ type_str ^
mueller@6473
  1045
		"/**** declarations: ****/\n" ^ decls_str ^
mueller@6473
  1046
		"/**** definitions: ****/\n" ^ prems_str ^ 
mueller@6473
  1047
		"/**** formula: ****/\n" ^ concl_str ^";");
mueller@6473
  1048
	val result = callmc MCstr;
mueller@6473
  1049
  in
mueller@6473
  1050
(if !trace_mc then 
mueller@6473
  1051
	(writeln ("\nmodelchecker input:\n" ^ MCstr ^ "\n/**** end ****/"))
mueller@6473
  1052
          else ();
mueller@6473
  1053
(case (extract_result concl_str result) of 
mueller@6473
  1054
	true  =>  (Logic.strip_imp_concl subgoal) | 
mueller@6473
  1055
	false => (error ("Mucke couldn't solve subgoal: \n" ^result)))) 
mueller@6473
  1056
  end;