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