src/Tools/isac/ProgLang/scrtools.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Mon, 07 Dec 2015 11:25:02 +0100
changeset 59186 d9c3e373f8f5
parent 55444 ede4248a827b
child 59187 2b26acbd130c
permissions -rw-r--r--
Isabelle2014-->15: term_of-->Thm.term_of
neuper@38025
     1
(* tools which depend on Script.thy and thus are not in termC.sml
neuper@37906
     2
   (c) Walther Neuper 2000
neuper@37906
     3
*)
neuper@37906
     4
neuper@37906
     5
neuper@37906
     6
fun is_reall_dsc 
neuper@37906
     7
  (Const(_,Type("fun",[Type("List.list",
neuper@37906
     8
			    [Type ("real",[])]),_]))) = true
neuper@37906
     9
  | is_reall_dsc 
neuper@37906
    10
  (Const(_,Type("fun",[Type("List.list",
neuper@37906
    11
			    [Type ("real",[])]),_])) $ t) = true
neuper@37906
    12
  | is_reall_dsc _ = false;
neuper@37906
    13
fun is_booll_dsc 
neuper@37906
    14
  (Const(_,Type("fun",[Type("List.list",
neuper@37906
    15
			    [Type ("bool",[])]),_]))) = true
neuper@37906
    16
  | is_booll_dsc 
neuper@37906
    17
  (Const(_,Type("fun",[Type("List.list",
neuper@37906
    18
			    [Type ("bool",[])]),_])) $ t) = true
neuper@37906
    19
  | is_booll_dsc _ = false;
neuper@37906
    20
(*
wneuper@59186
    21
> val t = (Thm.term_of o the o (parse thy)) "relations";
neuper@37906
    22
> atomtyp (type_of t);
neuper@37906
    23
*** Type (fun,[
neuper@37906
    24
***   Type (List.list,[
neuper@37906
    25
***     Type (bool,[])
neuper@37906
    26
***     ]
neuper@37906
    27
***   Type (Tools.una,[])
neuper@37906
    28
***   ]
neuper@37906
    29
> is_booll_dsc t;
neuper@37906
    30
val it = true : bool
neuper@37906
    31
> is_reall_dsc t;
neuper@37906
    32
val it = false : bool
neuper@37906
    33
*)
neuper@37906
    34
neuper@37906
    35
fun is_list_dsc (Const(_,Type("fun",[Type("List.list",_),_]))) = true
neuper@37906
    36
  | is_list_dsc (Const(_,Type("fun",[Type("List.list",_),_])) $ t) = true
neuper@37906
    37
  (*WN:8.5.03: ???                                           ~~~~ ???*)
neuper@37906
    38
  | is_list_dsc _ = false;
neuper@37906
    39
(*
neuper@37906
    40
> val t = str2term "someList";
neuper@37906
    41
> is_list_dsc t; 
neuper@37906
    42
val it = true : bool
neuper@37906
    43
wneuper@59186
    44
> val t = (Thm.term_of o the o (parse thy))
neuper@37906
    45
          "additional_relations [a=b,c=(d::real)]";
neuper@37906
    46
> is_list_dsc t;
neuper@37906
    47
val it = true : bool
neuper@37906
    48
> is_list_dsc (head_of t);
neuper@37906
    49
val it = true : bool
neuper@37906
    50
wneuper@59186
    51
> val t = (Thm.term_of o the o (parse thy))"max_relation (A=#2*a*b-a^^^#2)";
neuper@37906
    52
> is_list_dsc t;
neuper@37906
    53
val it = false : bool
neuper@37906
    54
> is_list_dsc (head_of t);
neuper@37906
    55
val it = false : bool     
wneuper@59186
    56
> val t = (Thm.term_of o the o (parse thy)) "testdscforlist";
neuper@37906
    57
> is_list_dsc (head_of t);
neuper@37906
    58
val it = true : bool
neuper@37906
    59
*)
neuper@37906
    60
neuper@37906
    61
neuper@37906
    62
fun is_unl (Const(_,Type("fun",[_,Type("Tools.unl",_)]))) = true
neuper@37906
    63
  | is_unl _ = false;
neuper@37906
    64
(*
neuper@37906
    65
> val t = str2term "someList"; is_unl t;
neuper@37906
    66
val it = true : bool
wneuper@59186
    67
> val t = (Thm.term_of o the o (parse thy)) "maximum";
neuper@37906
    68
> is_unl t;
neuper@37906
    69
val it = false : bool
neuper@37906
    70
*)
neuper@37906
    71
neuper@41990
    72
(*WN110511  Const would be clearer !!! TODO combine both fun is_dsc ...
neuper@37906
    73
fun is_dsc (Const(_,Type("fun",[_,Type("Tools.nam",_)]))) = true
neuper@37906
    74
  | is_dsc (Const(_,Type("fun",[_,Type("Tools.una",_)]))) = true
neuper@37906
    75
  | is_dsc (Const(_,Type("fun",[_,Type("Tools.unl",_)]))) = true
neuper@37906
    76
  | is_dsc (Const(_,Type("fun",[_,Type("Tools.str",_)]))) = true
neuper@37906
    77
  | is_dsc (Const(_,Type("fun",[_,Type("Tools.toreal",_)]))) = true
neuper@37906
    78
  | is_dsc (Const(_,Type("fun",[_,Type("Tools.toreall",_)])))= true
neuper@37906
    79
  | is_dsc (Const(_,Type("fun",[_,Type("Tools.tobooll",_)])))= true
neuper@37906
    80
  | is_dsc (Const(_,Type("fun",[_,Type("Tools.unknow",_)])))= true
neuper@37906
    81
  | is_dsc (Const(_,Type("fun",[_,Type("Tools.cpy",_)])))= true
neuper@37906
    82
  | is_dsc _ = false;
neuper@41982
    83
*)
neuper@37906
    84
fun is_dsc term = 
neuper@37906
    85
    (case (range_type o type_of) term of
neuper@37906
    86
	Type("Tools.nam",_) => true
neuper@37906
    87
      | Type("Tools.una",_) => true
neuper@37906
    88
      | Type("Tools.unl",_) => true
neuper@37906
    89
      | Type("Tools.str",_) => true
neuper@37906
    90
      | Type("Tools.toreal",_) => true
neuper@37906
    91
      | Type("Tools.toreall",_) => true
neuper@37906
    92
      | Type("Tools.tobooll",_) => true
neuper@37906
    93
      | Type("Tools.unknow",_) => true
neuper@37906
    94
      | Type("Tools.cpy",_) => true
neuper@37906
    95
      | _ => false)
neuper@37906
    96
    handle Match => false;
neuper@37906
    97
neuper@37906
    98
neuper@37906
    99
(*
neuper@37906
   100
val t as t1 $ t2 = str2term "antiDerivativeName M_b";
neuper@37906
   101
val Const (_, Type ("fun", [Type ("fun", _), Type ("Tools.una",[])])) $ _ = t;
neuper@37906
   102
is_dsc t1;
neuper@37906
   103
wneuper@59186
   104
> val t = (Thm.term_of o the o (parse thy)) "maximum";
neuper@37906
   105
> is_dsc t;
neuper@37906
   106
val it = true : bool
wneuper@59186
   107
> val t = (Thm.term_of o the o (parse thy)) "testdscforlist";
neuper@37906
   108
> is_dsc t;
neuper@37906
   109
val it = true : bool
neuper@37906
   110
wneuper@59186
   111
> val t = (head_of o Thm.term_of o the o (parse thy)) "maximum A";
neuper@37906
   112
> is_dsc t;
neuper@37906
   113
val it = true : bool
wneuper@59186
   114
> val t = (head_of o Thm.term_of o the o (parse thy)) 
neuper@37906
   115
  "fixedValues [R=(R::real)]";
neuper@37906
   116
> is_dsc t;
neuper@37906
   117
val it = true : bool
neuper@37906
   118
*)
neuper@37906
   119
neuper@37906
   120
neuper@37906
   121
(*make the term 'Subproblem (domID, pblID)' to a formula for frontend;
neuper@37906
   122
  needs to be here after def. Subproblem in Script.thy*)
neuper@37906
   123
val t as (subpbl_t $ (pair_t $ Free (domID,_) $ pblID)) = 
wneuper@59186
   124
    (Thm.term_of o the o (parse @{theory Script})) 
neuper@37906
   125
	"Subproblem (Isac,[equation,univar])";
neuper@37906
   126
val t as (pbl_t $ _) = 
wneuper@59186
   127
    (Thm.term_of o the o (parse @{theory Script})) 
neuper@37906
   128
	"Problem (Isac,[equation,univar])";
wneuper@59186
   129
val Free (_, ID_type) = (Thm.term_of o the o (parse @{theory Script})) "x::ID";
neuper@37906
   130
neuper@37906
   131
neuper@37906
   132
fun subpbl domID pblID =
neuper@37906
   133
    subpbl_t $ (pair_t $ Free (domID,ID_type) $ 
neuper@37906
   134
	(((list2isalist ID_type) o (map (mk_free ID_type))) pblID));
neuper@37906
   135
(*> subpbl "Isac" ["equation","univar"] = t;
neuper@37906
   136
val it = true : bool *)
neuper@37906
   137
neuper@37906
   138
neuper@37906
   139
fun pblterm (domID:domID) (pblID:pblID) =
neuper@37906
   140
    pbl_t $ (pair_t $ Free (domID,ID_type) $ 
neuper@37906
   141
	(((list2isalist ID_type) o (map (mk_free ID_type))) pblID));
neuper@37906
   142
neuper@37906
   143
neuper@37906
   144
(**.construct scr-env from scr(created automatically) and Rewrite_Set.**)
neuper@37906
   145
neuper@37906
   146
fun one_scr_arg (Const _ $ arg $ _) = arg
neuper@38031
   147
  | one_scr_arg t = error ("one_scr_arg: called by "^(term2str t));
neuper@37906
   148
fun two_scr_arg (Const _ $ a1 $ a2 $ _) = (a1, a2)
neuper@38031
   149
  | two_scr_arg t = error ("two_scr_arg: called by "^(term2str t));
neuper@37906
   150
neuper@37906
   151
neuper@37906
   152
(**.generate calc from a script.**)
neuper@37906
   153
neuper@37906
   154
(*.instantiate a stactic or scriptexpr, and ev. attach (curried) argument
neuper@37906
   155
args:
neuper@37906
   156
   E       environment
neuper@37906
   157
   v       current value, is attached to curried stactics
neuper@37906
   158
   stac     stactic to be instantiated
neuper@37906
   159
precond:
neuper@37906
   160
   not (a = NONE) /\ (v = e_term) /\ (stac curried, i.e. without last arg.)
neuper@37906
   161
   this ........................ is the initialization for assy with l=[],
neuper@37906
   162
   but the 1st stac is
neuper@37906
   163
   (a) curried:     then (a = SOME _), or 
neuper@37906
   164
   (b) not curried: then the values of the initialization are not used
neuper@37906
   165
.*)
neuper@37906
   166
datatype stacexpr = STac of term | Expr of term
neuper@37906
   167
fun rep_stacexpr (STac t ) = t
neuper@37906
   168
  | rep_stacexpr (Expr t) = 
neuper@38031
   169
    error ("rep_stacexpr called with t= "^(term2str t));
neuper@37906
   170
neuper@37906
   171
type env = (term * term) list;
neuper@37906
   172
neuper@37906
   173
(*update environment; t <> empty if coming from listexpr*)
neuper@37906
   174
fun upd_env (env:env) (v,t) =
neuper@37906
   175
  let val env' = if t = e_term then env else overwrite (env,(v,t));
neuper@38015
   176
    (*val _= tracing("### upd_env: = "^(subst2str env'));*)
neuper@37906
   177
  in env' end;
neuper@37906
   178
neuper@42360
   179
(*.substitute the script's environment in a leaf of the script's parse-tree
neuper@37906
   180
   and attach the curried argument of a tactic, if any.
neuper@37906
   181
   a leaf is either a tactic or an 'exp' in 'let v = expr'
neuper@37906
   182
   where 'exp' does not contain a tactic.
neuper@37906
   183
CAUTION: (1) currying with @@ requires 2 patterns for each tactic
neuper@37906
   184
         (2) the non-curried version must return NONE for a 
neuper@37906
   185
	 (3) non-matching patterns become an Expr by fall-through.
neuper@37906
   186
WN060906 quick and dirty fix: due to (2) a is returned, too.*)
neuper@37906
   187
fun subst_stacexpr E a v (t as (Const ("Script.Rewrite",_) $ _ $ _ $ _ ))=
neuper@37906
   188
    (NONE, STac (subst_atomic E t))
neuper@37906
   189
neuper@37906
   190
  | subst_stacexpr E a v (t as (Const ("Script.Rewrite",_) $ _ $ _ ))=
neuper@37906
   191
    (a, (*in these cases we hope, that a = SOME _*)
neuper@37906
   192
     STac (case a of SOME a' => (subst_atomic E (t $ a'))
neuper@37906
   193
		   | NONE => ((subst_atomic E t) $ v)))
neuper@37906
   194
neuper@37906
   195
  | subst_stacexpr E a v 
neuper@37906
   196
	      (t as (Const ("Script.Rewrite'_Inst",_) $ _ $ _ $ _ $ _ )) =
neuper@37906
   197
    (NONE, STac (subst_atomic E t))
neuper@37906
   198
neuper@37906
   199
  | subst_stacexpr E a v (t as (Const ("Script.Rewrite'_Inst",_) $ _ $ _ $ _))=
neuper@37906
   200
    (a, STac (case a of SOME a' => subst_atomic E (t $ a')
neuper@37906
   201
	     | NONE => ((subst_atomic E t) $ v)))
neuper@37906
   202
neuper@37906
   203
  | subst_stacexpr E a v (t as (Const ("Script.Rewrite'_Set",_) $ _ $ _ $ _ ))=
neuper@37906
   204
    (NONE, STac (subst_atomic E t))
neuper@37906
   205
neuper@37906
   206
  | subst_stacexpr E a v (t as (Const ("Script.Rewrite'_Set",_) $ _ $ _ )) =
neuper@37906
   207
    (a, STac (case a of SOME a' => subst_atomic E (t $ a')
neuper@37906
   208
	     | NONE => ((subst_atomic E t) $ v)))
neuper@37906
   209
neuper@37906
   210
  | subst_stacexpr E a v 
neuper@37906
   211
	      (t as (Const ("Script.Rewrite'_Set'_Inst",_) $ _ $ _ $ _ $ _ )) =
neuper@37906
   212
    (NONE, STac (subst_atomic E t))
neuper@37906
   213
neuper@37906
   214
  | subst_stacexpr E a v 
neuper@37906
   215
	      (t as (Const ("Script.Rewrite'_Set'_Inst",_) $ _ $ _ $ _ )) =
neuper@37906
   216
    (a, STac (case a of SOME a' => subst_atomic E (t $ a')
neuper@37906
   217
	     | NONE => ((subst_atomic E t) $ v)))
neuper@37906
   218
neuper@37906
   219
  | subst_stacexpr E a v (t as (Const ("Script.Calculate",_) $ _ $ _ )) =
neuper@37906
   220
    (NONE, STac (subst_atomic E t))
neuper@37906
   221
neuper@37906
   222
  | subst_stacexpr E a v (t as (Const ("Script.Calculate",_) $ _ )) =
neuper@37906
   223
    (a, STac (case a of SOME a' => subst_atomic E (t $ a')
neuper@37906
   224
	     | NONE => ((subst_atomic E t) $ v)))
neuper@37906
   225
neuper@37906
   226
  | subst_stacexpr E a v 
neuper@37906
   227
	      (t as (Const("Script.Check'_elementwise",_) $ _ $ _ )) = 
neuper@37906
   228
    (NONE, STac (subst_atomic E t))
neuper@37906
   229
neuper@37906
   230
  | subst_stacexpr E a v (t as (Const("Script.Check'_elementwise",_) $ _ )) = 
neuper@37906
   231
    (a, STac (case a of SOME a' => subst_atomic E (t $ a')
neuper@37906
   232
		 | NONE => ((subst_atomic E t) $ v)))
neuper@37906
   233
neuper@37906
   234
  | subst_stacexpr E a v (t as (Const("Script.Or'_to'_List",_) $ _ )) = 
neuper@37906
   235
    (NONE, STac (subst_atomic E t))
neuper@37906
   236
neuper@37906
   237
  | subst_stacexpr E a v (t as (Const("Script.Or'_to'_List",_))) = (*t $ v*)
neuper@37906
   238
    (a, STac (case a of SOME a' => subst_atomic E (t $ a')
neuper@37906
   239
		 | NONE => ((subst_atomic E t) $ v)))
neuper@37906
   240
neuper@37906
   241
  | subst_stacexpr E a v (t as (Const ("Script.SubProblem",_) $ _ $ _ )) =
neuper@37906
   242
    (NONE, STac (subst_atomic E t))
neuper@37906
   243
neuper@37906
   244
  | subst_stacexpr E a v (t as (Const ("Script.Take",_) $ _ )) =
neuper@37906
   245
    (NONE, STac (subst_atomic E t))
neuper@37906
   246
neuper@37906
   247
  | subst_stacexpr E a v (t as (Const ("Script.Substitute",_) $ _ $ _ )) =
neuper@37906
   248
    (NONE, STac (subst_atomic E t))
neuper@37906
   249
neuper@37906
   250
  | subst_stacexpr E a v (t as (Const ("Script.Substitute",_) $ _ )) =
neuper@37906
   251
    (a, STac (case a of SOME a' => subst_atomic E (t $ a')
neuper@37906
   252
		 | NONE => ((subst_atomic E t) $ v)))
neuper@37906
   253
neuper@37906
   254
  (*now all tactics are matched out and this leaf must be without a tactic*)
neuper@37906
   255
  | subst_stacexpr E a v t = 
neuper@37906
   256
    (a, Expr (subst_atomic (case a of SOME a => upd_env E (a,v) 
neuper@37906
   257
				| NONE => E) t));
neuper@37984
   258
(*> val t = str2term "SubProblem(Test_, [linear, univariate, equation, test], [Test, solve_linear]) [BOOL e_, REAL v_]";
neuper@37906
   259
> subst_stacexpr [] NONE e_term t;*)
neuper@37906
   260
neuper@37906
   261
neuper@37906
   262
fun stacpbls (h $ body) =
neuper@37906
   263
  let
neuper@41968
   264
    fun scan ts (Const ("HOL.Let",_) $ e $ (Abs (v,T,b))) =
neuper@37906
   265
      (scan ts e) @ (scan ts b)
neuper@37906
   266
      | scan ts (Const ("If",_) $ c $ e1 $ e2) = (scan ts e1) @ (scan ts e2)
neuper@37906
   267
      | scan ts (Const ("Script.While",_) $ c $ e $ _) = scan ts e
neuper@37906
   268
      | scan ts (Const ("Script.While",_) $ c $ e) = scan ts e
neuper@37906
   269
      | scan ts (Const ("Script.Repeat",_) $ e $ _) = scan ts e
neuper@37906
   270
      | scan ts (Const ("Script.Repeat",_) $ e) = scan ts e
neuper@37906
   271
      | scan ts (Const ("Script.Try",_) $ e $ _) = scan ts e
neuper@37906
   272
      | scan ts (Const ("Script.Try",_) $ e) = scan ts e
neuper@37906
   273
      | scan ts (Const ("Script.Or",_) $e1 $ e2 $ _) = 
neuper@37906
   274
	(scan ts e1) @ (scan ts e2)
neuper@37906
   275
      | scan ts (Const ("Script.Or",_) $e1 $ e2) = 
neuper@37906
   276
	(scan ts e1) @ (scan ts e2)
neuper@37906
   277
      | scan ts (Const ("Script.Seq",_) $e1 $ e2 $ _) = 
neuper@37906
   278
	(scan ts e1) @ (scan ts e2)
neuper@37906
   279
      | scan ts (Const ("Script.Seq",_) $e1 $ e2) = 
neuper@37906
   280
	(scan ts e1) @ (scan ts e2)
neuper@37906
   281
      | scan ts t = case subst_stacexpr [] NONE e_term t of
neuper@37906
   282
			(_, STac _) => [t] | (_, Expr _) => []
neuper@37906
   283
  in (distinct o (scan [])) body end;
neuper@37906
   284
    (*sc = Solve_root_equation ...
neuper@37906
   285
> val ts = stacpbls sc;
neuper@38015
   286
> tracing (terms2str thy ts);
neuper@37906
   287
["Rewrite square_equation_left True e_",
neuper@37906
   288
 "Rewrite_Set SqRoot_simplify False e_",
neuper@37906
   289
 "Rewrite_Set rearrange_assoc False e_",
neuper@37906
   290
 "Rewrite_Set isolate_root False e_",
neuper@37906
   291
 "Rewrite_Set norm_equation False e_",
neuper@37906
   292
 "Rewrite_Set_Inst [(bdv, v_)] isolate_bdv False e_"]
neuper@37906
   293
*)
neuper@37906
   294
neuper@37906
   295
neuper@37906
   296
neuper@37906
   297
fun is_calc (Const ("Script.Calculate",_) $ _) = true
neuper@37906
   298
  | is_calc (Const ("Script.Calculate",_) $ _ $ _) = true
neuper@37906
   299
  | is_calc _ = false;
neuper@37906
   300
fun op_of_calc (Const ("Script.Calculate",_) $ Free (op_,_)) = op_
neuper@37906
   301
  | op_of_calc (Const ("Script.Calculate",_) $ Free (op_,_) $ _) = op_
neuper@38057
   302
  | op_of_calc t = error ("op_of_calc called with" ^ term2str t);
neuper@37906
   303
(*
neuper@48763
   304
 val Prog sc = (#scr o rep_rls) Test_simplify;
neuper@37906
   305
 val stacs = stacpbls sc;
neuper@37906
   306
neuper@37906
   307
 val calcs = filter is_calc stacs;
neuper@37906
   308
 val ids = map op_of_calc calcs;
neuper@37906
   309
 map (curry assoc1 (!calclist')) ids;
neuper@37906
   310
neuper@37906
   311
 (((map (curry assoc1 (!calclist'))) o (map op_of_calc) o 
neuper@37906
   312
  (filter is_calc) o stacpbls) sc):calc list;
neuper@37906
   313
*)
neuper@37906
   314
neuper@38057
   315
(*-.-.-.-.-.-isolate response.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
neuper@38057
   316
-.-.-.-.-.-.-isolate response.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.*)
neuper@38057
   317
neuper@38057
   318
(** for automatic creation of scripts from rls **)
neuper@37906
   319
(* naming of identifiers in scripts ???...
neuper@38057
   320
   not accepted !!!...
wneuper@59186
   321
((inst_abs @{theory}) o Thm.term_of o the o (parse @{theory})) "(t_::'z) = t_";
wneuper@59186
   322
((inst_abs @{theory}) o Thm.term_of o (the:cterm option -> cterm) o 
neuper@38057
   323
     (parse @{theory})) "(_t::'z) = _t";
neuper@38057
   324
*)
wneuper@59186
   325
((inst_abs @{theory}) o Thm.term_of o the o (parse @{theory})) "(t::'z) = t";
wneuper@59186
   326
((inst_abs @{theory}) o Thm.term_of o (the:cterm option -> cterm) o 
neuper@37906
   327
     (parse @{theory})) "(t't::'z) = t't";
wneuper@59186
   328
((inst_abs @{theory}) o Thm.term_of o the o (parse @{theory})) "(t_t::'z) = t_t";
neuper@38057
   329
wneuper@59186
   330
((inst_abs @{theory}) o Thm.term_of o the o (parse @{theory}))
neuper@38057
   331
"Script Stepwise (t_t::'z) =\
neuper@37906
   332
        \(Repeat\
neuper@37906
   333
	\  ((Try (Repeat (Rewrite real_diff_minus False))) @@  \
neuper@37971
   334
	\   (Try (Repeat (Rewrite add_commute False))) @@ \
neuper@48763
   335
	\   (Try (Repeat (Rewrite mult_commute False))))  \
neuper@37923
   336
	\  t_t)";
neuper@37906
   337
val ScrStep $ _ $ _ =     (*'z not affected by parse: 'a --> real*)
wneuper@59186
   338
    ((inst_abs @{theory}) o Thm.term_of o the o (parse @{theory}))  
neuper@38057
   339
	"Script Stepwise (t_t::'z) =\
neuper@37906
   340
        \(Repeat\
neuper@37906
   341
	\  ((Try (Repeat (Rewrite real_diff_minus False))) @@  \
neuper@37971
   342
	\   (Try (Repeat (Rewrite add_commute False))) @@ \
neuper@48763
   343
	\   (Try (Repeat (Rewrite mult_commute False))))  \
neuper@37923
   344
	\  t_t)";
neuper@37906
   345
(*WN060605 script-arg (t_::'z) and "Free (t_, 'a)" at end of body 
neuper@37906
   346
are inconsistent !!!*)
neuper@37906
   347
val ScrStep_inst $ Term $ Bdv $ _=(*'z not affected by parse: 'a --> real*)
wneuper@59186
   348
    ((inst_abs @{theory}) o Thm.term_of o the o (parse @{theory})) 
neuper@38057
   349
	"Script Stepwise_inst (t_t::'z) (v::real) =\
neuper@37906
   350
        \(Repeat\
neuper@37906
   351
	\  ((Try (Repeat (Rewrite_Inst [(bdv,v)] real_diff_minus False))) @@ \
neuper@37971
   352
	\   (Try (Repeat (Rewrite_Inst [(bdv,v)] add_commute False))) @@\
neuper@48763
   353
	\   (Try (Repeat (Rewrite_Inst [(bdv,v)] mult_commute False)))) \
neuper@38057
   354
	\  t_t)"; 
neuper@37921
   355
val Repeat $ _ =
wneuper@59186
   356
    ((inst_abs @{theory}) o Thm.term_of o the o (parseN @{theory})) 
neuper@37906
   357
	"Repeat (Rewrite real_diff_minus False t)";
neuper@37906
   358
val Try $ _ = 
wneuper@59186
   359
    ((inst_abs @{theory}) o Thm.term_of o the o (parseN @{theory})) 
neuper@37906
   360
	"Try (Rewrite real_diff_minus False t)";
neuper@37906
   361
val Cal $ _ =
wneuper@59186
   362
    ((inst_abs @{theory}) o Thm.term_of o the o (parseN @{theory})) 
neuper@37921
   363
	"Calculate PLUS";
neuper@37906
   364
val Ca1 $ _ =
wneuper@59186
   365
    ((inst_abs @{theory}) o Thm.term_of o the o (parseN @{theory})) 
neuper@37921
   366
	"Calculate1 PLUS";
neuper@37906
   367
val Rew $ (Free (_,IDtype)) $ _ $ t =
wneuper@59186
   368
    ((inst_abs @{theory}) o Thm.term_of o the o (parseN @{theory})) 
neuper@37906
   369
	"Rewrite real_diff_minus False t";
neuper@37906
   370
val Rew_Inst $ Subs $ _ $ _ =
wneuper@59186
   371
    ((inst_abs @{theory}) o Thm.term_of o the o (parseN @{theory})) 
neuper@37906
   372
	"Rewrite_Inst [(bdv,v)] real_diff_minus False";
neuper@37906
   373
val Rew_Set $ _ $ _ =
wneuper@59186
   374
    ((inst_abs @{theory}) o Thm.term_of o the o (parseN @{theory})) 
neuper@37906
   375
	"Rewrite_Set real_diff_minus False";
neuper@37906
   376
val Rew_Set_Inst $ _ $ _ $ _ =
wneuper@59186
   377
    ((inst_abs @{theory}) o Thm.term_of o the o (parseN @{theory})) 
neuper@37906
   378
	"Rewrite_Set_Inst [(bdv,v)] real_diff_minus False";
neuper@37906
   379
val SEq $ _ $ _ $ _ =
wneuper@59186
   380
    ((inst_abs @{theory}) o Thm.term_of o the o (parseN @{theory})) 
neuper@37906
   381
	"  ((Try (Repeat (Rewrite real_diff_minus False))) @@  \
neuper@37971
   382
        \   (Try (Repeat (Rewrite add_commute False))) @@ \
neuper@48763
   383
        \   (Try (Repeat (Rewrite mult_commute False)))) t";
neuper@37906
   384
s1210629013@52153
   385
fun rule2stac _ (Thm (thmID, _)) = 
neuper@48760
   386
    Try $ (Repeat $ (Rew $ Free (thmID, IDtype) $ @{term False}))
s1210629013@52153
   387
  | rule2stac thy (Calc (c, _)) = 
s1210629013@52153
   388
    Try $ (Repeat $ (Cal $ Free (assoc_calc thy c, IDtype)))
s1210629013@52153
   389
  | rule2stac thy (Cal1 (c, _)) = 
s1210629013@52153
   390
    Try $ (Repeat $ (Ca1 $ Free (assoc_calc thy c, IDtype)))
s1210629013@52153
   391
  | rule2stac _ (Rls_ rls) = 
neuper@48760
   392
    Try $ (Rew_Set $ Free (id_rls rls, IDtype) $ @{term False});
neuper@37969
   393
(*val t = rule2stac [] (Thm ("real_diff_minus", num_str @{thm real_diff_minus));
neuper@37906
   394
atomt t; term2str t;
neuper@38014
   395
val t = rule2stac calclist (Calc ("Groups.plus_class.plus", eval_binop "#add_"));
neuper@37906
   396
atomt t; term2str t;
neuper@37906
   397
val t = rule2stac [] (Rls_ rearrange_assoc);
neuper@37906
   398
atomt t; term2str t;
neuper@37906
   399
*)
s1210629013@52153
   400
fun rule2stac_inst _ (Thm (thmID, _)) = 
neuper@37906
   401
    Try $ (Repeat $ (Rew_Inst $ Subs $ Free (thmID, IDtype) $ 
neuper@48760
   402
			      @{term False}))
s1210629013@52153
   403
  | rule2stac_inst thy (Calc (c, _)) = 
s1210629013@52153
   404
    Try $ (Repeat $ (Cal $ Free (assoc_calc thy c, IDtype)))
s1210629013@52153
   405
  | rule2stac_inst thy (Cal1 (c, _)) = 
s1210629013@52153
   406
    Try $ (Repeat $ (Cal $ Free (assoc_calc thy c, IDtype)))
s1210629013@52153
   407
  | rule2stac_inst _ (Rls_ rls) = 
neuper@37906
   408
    Try $ (Rew_Set_Inst $ Subs $ Free (id_rls rls, IDtype) $ 
neuper@48760
   409
			@{term False});
neuper@37969
   410
(*val t = rule2stac_inst [] (Thm ("real_diff_minus", num_str @{thm real_diff_minus));
neuper@37906
   411
atomt t; term2str t;
neuper@38014
   412
val t = rule2stac_inst calclist (Calc ("Groups.plus_class.plus", eval_binop "#add_"));
neuper@37906
   413
atomt t; term2str t;
neuper@37906
   414
val t = rule2stac_inst [] (Rls_ rearrange_assoc);
neuper@37906
   415
atomt t; term2str t;
neuper@37906
   416
*)
neuper@37906
   417
neuper@37906
   418
(*for appropriate nesting take stacs in _reverse_ order*)
neuper@37906
   419
fun @@@ sts [s] = SEq $ s $ sts
neuper@37906
   420
  | @@@ sts (s::ss) = @@@ (SEq $ s $ sts) ss;
neuper@37906
   421
fun @@ [stac] = stac
neuper@37906
   422
  | @@ [s1, s2] = SEq $ s1 $ s2 (*---------vvv--*)
neuper@37906
   423
  | @@ stacs = 
neuper@37906
   424
    let val s3::s2::ss = rev stacs
neuper@37906
   425
    in @@@ (SEq $ s2 $ s3) ss end;
neuper@37906
   426
(*
neuper@37906
   427
 val rules = (#rules o rep_rls) isolate_root;
neuper@37906
   428
 val rs = map (rule2stac calclist) rules;
neuper@37906
   429
 val tt = @@ rs;
neuper@38015
   430
 atomt tt; tracing (term2str tt);
neuper@37906
   431
 *)
neuper@37906
   432
neuper@37906
   433
val contains_bdv = (not o null o (filter is_bdv) o ids2str o #prop o rep_thm);
neuper@37906
   434
neuper@37906
   435
(*.does a rule contain a 'bdv'; descend recursively into Rls_.*)
neuper@37906
   436
fun contain_bdv [] = false
neuper@37906
   437
  | contain_bdv (Thm (_, thm)::rs) = 
neuper@37906
   438
    if (not o contains_bdv) thm
neuper@37906
   439
    then contain_bdv rs
neuper@37906
   440
    else true
neuper@37906
   441
  | contain_bdv (Calc _ ::rs) = contain_bdv rs
neuper@37906
   442
  | contain_bdv (Cal1 _ ::rs) = contain_bdv rs
neuper@37906
   443
  | contain_bdv (Rls_ rls ::rs) = 
neuper@37906
   444
    contain_bdv (get_rules rls) orelse contain_bdv rs
neuper@37906
   445
  | contain_bdv (r::_) = 
neuper@38031
   446
    error ("contain_bdv called with ["^(id_rule r)^",...]");
neuper@37906
   447
s1210629013@52153
   448
fun rules2scr_Rls thy rules = (*WN100816 t_ -> t_t like "Script Stepwise..*)
neuper@37906
   449
    if contain_bdv rules
neuper@37906
   450
    then ScrStep_inst $ Term $ Bdv $ 
s1210629013@52153
   451
	 (Repeat $ (((@@ o (map (rule2stac_inst thy))) rules) $ e_term))
neuper@37906
   452
    else ScrStep $ Term $
s1210629013@52153
   453
	 (Repeat $ (((@@ o (map (rule2stac thy))) rules) $ e_term));
neuper@37906
   454
(* val (calc, rules) = (!calclist', rules);
neuper@37906
   455
   *)
s1210629013@52153
   456
fun rules2scr_Seq thy rules = (*WN100816 t_ -> t_t like "Script Stepwise..*)
neuper@37906
   457
    if contain_bdv rules
neuper@37906
   458
    then ScrStep_inst $ Term $ Bdv $ 
s1210629013@52153
   459
	 (((@@ o (map (rule2stac_inst thy))) rules) $ e_term)
neuper@37906
   460
    else ScrStep $ Term $
s1210629013@52153
   461
	 (((@@ o (map (rule2stac thy))) rules) $ e_term);
neuper@37906
   462
neuper@37906
   463
(*.prepare the input for an rls for use:
neuper@37906
   464
   # generate a script for stepwise execution of the rls
neuper@42318
   465
   # filter the operators for Calc out of the script ?WN111014?
neuper@52155
   466
   !!!use this function while storing by or integrate into KEStore_Elems.add_rlss.*)
s1210629013@55444
   467
fun prep_rls _ Erls = error "prep_rls' not impl. for Erls"
s1210629013@55444
   468
  | prep_rls thy (Rls {id,preconds,rew_ord,erls,srls,calc,rules,errpatts,...}) = 
s1210629013@52153
   469
      let val sc = (rules2scr_Rls thy rules)
neuper@42318
   470
      in Rls {id=id,preconds=preconds,rew_ord=rew_ord,erls=erls,
neuper@42318
   471
	      srls = srls,
neuper@42318
   472
	      calc = (*FIXXXME.040207 use also for met*)
s1210629013@52153
   473
	        ((assoc_calc' thy |> map) o (map op_of_calc) o 
neuper@42318
   474
	          (filter is_calc) o stacpbls) sc,
neuper@42318
   475
	      rules = rules,
neuper@42451
   476
	      errpatts=errpatts,
neuper@48763
   477
	      scr = Prog sc} end
s1210629013@55444
   478
  | prep_rls thy (Seq {id,preconds,rew_ord,erls,srls,calc,rules,errpatts,...}) = 
s1210629013@52153
   479
      let val sc = (rules2scr_Seq thy rules)
neuper@42318
   480
      in Seq {id=id,preconds=preconds,rew_ord=rew_ord,erls=erls,
neuper@42318
   481
	      srls=srls,
s1210629013@52153
   482
	      calc = ((assoc_calc' thy |> map) o (map op_of_calc) o 
neuper@42318
   483
		      (filter is_calc) o stacpbls) sc,
neuper@42318
   484
	      rules=rules,
neuper@42451
   485
	      errpatts=errpatts,
neuper@48763
   486
	      scr = Prog sc} end
s1210629013@55444
   487
  | prep_rls _ (Rrls {id,...}) = 
s1210629013@55444
   488
      error ("prep_rls' not required for Rrls \"" ^ id ^ "\"");