src/Tools/isac/ProgLang/scrtools.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Thu, 15 Mar 2018 10:17:44 +0100
changeset 59405 49d7d410b83c
parent 59393 4274a44ec183
child 59409 b832f1f20bce
permissions -rw-r--r--
separate structure Celem: CALC_ELEMENT, all but Knowledge/
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
wneuper@59374
     5
signature LANGUAGE_TOOLS =
wneuper@59374
     6
  sig
wneuper@59376
     7
    datatype stacexpr = Expr of term | STac of term
wneuper@59376
     8
    val rep_stacexpr: stacexpr -> term
wneuper@59376
     9
    val subst_stacexpr: (term * term) list -> term option -> term -> term -> term option * stacexpr
wneuper@59405
    10
    val contain_bdv: Celem.rule list -> bool
wneuper@59376
    11
    val contains_bdv: thm -> bool
wneuper@59376
    12
    type env = (term * term) list
wneuper@59376
    13
    val upd_env: env -> term * term -> env
wneuper@59376
    14
    val is_booll_dsc: term -> bool
wneuper@59376
    15
    val is_calc: term -> bool
wneuper@59376
    16
    val is_dsc: term -> bool
wneuper@59376
    17
    val is_list_dsc: term -> bool
wneuper@59376
    18
    val is_reall_dsc: term -> bool
wneuper@59376
    19
    val is_unl: term -> bool
wneuper@59405
    20
    val pblterm: Celem.domID -> Celem.pblID -> term
wneuper@59376
    21
    val subpbl: string -> string list -> term
wneuper@59376
    22
    val stacpbls: term -> term list
wneuper@59376
    23
    val one_scr_arg: term -> term
wneuper@59376
    24
    val two_scr_arg: term -> term * term
wneuper@59376
    25
    val op_of_calc: term -> string
wneuper@59405
    26
    val get_calcs: theory -> term -> (Celem.prog_calcID * (Celem.calID * Celem.eval_fn)) list
wneuper@59405
    27
    val prep_rls: theory -> Celem.rls -> Celem.rls
wneuper@59374
    28
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
wneuper@59376
    29
  (* NONE *)
wneuper@59374
    30
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
wneuper@59377
    31
    val rule2stac: theory -> rule -> term
wneuper@59377
    32
    val rule2stac_inst: theory -> rule -> term
wneuper@59377
    33
    val @@ : term list -> term
wneuper@59374
    34
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59374
    35
  end
wneuper@59374
    36
wneuper@59374
    37
(**)
wneuper@59374
    38
structure LTool(**): LANGUAGE_TOOLS(**) =
wneuper@59374
    39
struct
wneuper@59374
    40
(**)
neuper@37906
    41
wneuper@59376
    42
(* distinguish input to Model (deep embedding of terms as Isac's object language) *)
wneuper@59377
    43
fun is_reall_dsc (Const (_, Type("fun", [Type ("List.list", [Type ("Real.real", [])]), _]))) = true
wneuper@59377
    44
  | is_reall_dsc (Const (_, Type("fun", [Type ("List.list", [Type ("Real.real", [])]),_])) $ _) = true
neuper@37906
    45
  | is_reall_dsc _ = false;
wneuper@59377
    46
fun is_booll_dsc (Const (_, Type("fun", [Type ("List.list", [Type ("HOL.bool", [])]), _]))) = true
wneuper@59377
    47
  | is_booll_dsc (Const (_, Type("fun", [Type ("List.list", [Type ("HOL.bool", [])]),_])) $ _) = true
neuper@37906
    48
  | is_booll_dsc _ = false;
wneuper@59376
    49
fun is_list_dsc (Const (_, Type("fun", [Type("List.list", _), _]))) = true
wneuper@59376
    50
  | is_list_dsc (Const (_, Type("fun", [Type("List.list", _), _])) $ _) = true
wneuper@59376
    51
  (*WN:8.5.03: ???   TODO test/../scrtools.sml                ~~~~ ???*)
wneuper@59376
    52
  | is_list_dsc _ = false;
wneuper@59376
    53
fun is_unl (Const (_, Type("fun", [_, Type("Tools.unl", _)]))) = true
wneuper@59376
    54
  | is_unl _ = false;
wneuper@59376
    55
fun is_dsc (Const(_, Type("fun", [_, Type ("Tools.nam",_)]))) = true
wneuper@59376
    56
  | is_dsc (Const(_, Type("fun", [_, Type ("Tools.una",_)]))) = true
wneuper@59376
    57
  | is_dsc (Const(_, Type("fun", [_, Type ("Tools.unl",_)]))) = true
wneuper@59376
    58
  | is_dsc (Const(_, Type("fun", [_, Type ("Tools.str",_)]))) = true
wneuper@59376
    59
  | is_dsc (Const(_, Type("fun", [_, Type ("Tools.toreal",_)]))) = true
wneuper@59376
    60
  | is_dsc (Const(_, Type("fun", [_, Type ("Tools.toreall",_)])))= true
wneuper@59376
    61
  | is_dsc (Const(_, Type("fun", [_, Type ("Tools.tobooll",_)])))= true
wneuper@59376
    62
  | is_dsc (Const(_, Type("fun", [_, Type ("Tools.unknow",_)])))= true
wneuper@59376
    63
  | is_dsc (Const(_, Type("fun", [_, Type ("Tools.cpy",_)])))= true
wneuper@59376
    64
  | is_dsc _ = false;
neuper@37906
    65
neuper@37906
    66
wneuper@59376
    67
(* make the term 'Subproblem (domID, pblID)' to a formula for frontend;
wneuper@59376
    68
  needs to be here after def. Subproblem in Script.thy *)
wneuper@59376
    69
val subpbl_t $ (pair_t $ Free (_, _) $ _) = 
wneuper@59389
    70
  (Thm.term_of o the o (TermC.parse @{theory Script})) "Subproblem (Isac,[equation,univar])"
wneuper@59376
    71
val pbl_t $ _ = 
wneuper@59389
    72
  (Thm.term_of o the o (TermC.parse @{theory Script})) "Problem (Isac,[equation,univar])"
wneuper@59389
    73
val Free (_, ID_type) = (Thm.term_of o the o (TermC.parse @{theory Script})) "xxx::ID"
neuper@37906
    74
neuper@37906
    75
fun subpbl domID pblID =
wneuper@59376
    76
  subpbl_t $ (pair_t $ Free (domID, ID_type) $ 
wneuper@59389
    77
    (((TermC.list2isalist ID_type) o (map (TermC.mk_free ID_type))) pblID));
wneuper@59405
    78
fun pblterm domID pblID =
wneuper@59376
    79
  pbl_t $ (pair_t $ Free (domID,ID_type) $ 
wneuper@59389
    80
	  (((TermC.list2isalist ID_type) o (map (TermC.mk_free ID_type))) pblID));
neuper@37906
    81
wneuper@59376
    82
(* construct scr-env from scr(created automatically) and Rewrite_Set *)
wneuper@59376
    83
fun one_scr_arg (Const _ $ arg $ _) = arg
wneuper@59405
    84
  | one_scr_arg t = error ("one_scr_arg: called by " ^ Celem.term2str t);
wneuper@59376
    85
fun two_scr_arg (Const _ $ a1 $ a2 $ _) = (a1, a2)
wneuper@59405
    86
  | two_scr_arg t = error ("two_scr_arg: called by " ^ Celem.term2str t);
neuper@37906
    87
neuper@37906
    88
wneuper@59376
    89
(** generate a "type calc" from a script **)
neuper@37906
    90
wneuper@59376
    91
(* instantiate a stactic or scriptexpr, and ev. attach (curried) argument
neuper@37906
    92
args:
neuper@37906
    93
   E       environment
neuper@37906
    94
   v       current value, is attached to curried stactics
neuper@37906
    95
   stac     stactic to be instantiated
neuper@37906
    96
precond:
neuper@37906
    97
   not (a = NONE) /\ (v = e_term) /\ (stac curried, i.e. without last arg.)
neuper@37906
    98
   this ........................ is the initialization for assy with l=[],
neuper@37906
    99
   but the 1st stac is
neuper@37906
   100
   (a) curried:     then (a = SOME _), or 
neuper@37906
   101
   (b) not curried: then the values of the initialization are not used
wneuper@59376
   102
*)
neuper@37906
   103
datatype stacexpr = STac of term | Expr of term
neuper@37906
   104
fun rep_stacexpr (STac t ) = t
neuper@37906
   105
  | rep_stacexpr (Expr t) = 
wneuper@59405
   106
    error ("rep_stacexpr called with t= " ^ Celem.term2str t);
neuper@37906
   107
neuper@37906
   108
type env = (term * term) list;
neuper@37906
   109
wneuper@59376
   110
(* update environment; t <> empty if coming from listexpr *)
neuper@37906
   111
fun upd_env (env:env) (v,t) =
wneuper@59405
   112
  let val env' = if t = Celem.e_term then env else overwrite (env,(v,t));
neuper@37906
   113
  in env' end;
neuper@37906
   114
wneuper@59376
   115
(* substitute the script's environment in a leaf of the script's parse-tree
neuper@37906
   116
   and attach the curried argument of a tactic, if any.
neuper@37906
   117
   a leaf is either a tactic or an 'exp' in 'let v = expr'
neuper@37906
   118
   where 'exp' does not contain a tactic.
neuper@37906
   119
CAUTION: (1) currying with @@ requires 2 patterns for each tactic
neuper@37906
   120
         (2) the non-curried version must return NONE for a 
wneuper@59376
   121
	       (3) non-matching patterns become an Expr by fall-through.
wneuper@59376
   122
WN060906 quick and dirty fix: due to (2) a is returned, too *)
wneuper@59377
   123
fun subst_stacexpr E _ _ (t as (Const ("Script.Rewrite",_) $ _ $ _ $ _)) =
neuper@37906
   124
    (NONE, STac (subst_atomic E t))
wneuper@59377
   125
  | subst_stacexpr E a v (t as (Const ("Script.Rewrite",_) $ _ $ _)) =
neuper@37906
   126
    (a, (*in these cases we hope, that a = SOME _*)
neuper@37906
   127
     STac (case a of SOME a' => (subst_atomic E (t $ a'))
neuper@37906
   128
		   | NONE => ((subst_atomic E t) $ v)))
wneuper@59376
   129
  | subst_stacexpr E _ _ 
wneuper@59377
   130
	      (t as (Const ("Script.Rewrite'_Inst", _) $ _ $ _ $ _ $ _)) =
neuper@37906
   131
    (NONE, STac (subst_atomic E t))
wneuper@59377
   132
  | subst_stacexpr E a v (t as (Const ("Script.Rewrite'_Inst", _) $ _ $ _ $ _)) =
neuper@37906
   133
    (a, STac (case a of SOME a' => subst_atomic E (t $ a')
neuper@37906
   134
	     | NONE => ((subst_atomic E t) $ v)))
wneuper@59377
   135
  | subst_stacexpr E _ _ (t as (Const ("Script.Rewrite'_Set", _) $ _ $ _ $ _)) =
neuper@37906
   136
    (NONE, STac (subst_atomic E t))
wneuper@59377
   137
  | subst_stacexpr E a v (t as (Const ("Script.Rewrite'_Set", _) $ _ $ _)) =
neuper@37906
   138
    (a, STac (case a of SOME a' => subst_atomic E (t $ a')
neuper@37906
   139
	     | NONE => ((subst_atomic E t) $ v)))
wneuper@59376
   140
  | subst_stacexpr E _ _ 
wneuper@59377
   141
	      (t as (Const ("Script.Rewrite'_Set'_Inst", _) $ _ $ _ $ _ $ _)) =
wneuper@59376
   142
    (NONE, STac (subst_atomic E t))
neuper@37906
   143
  | subst_stacexpr E a v 
wneuper@59377
   144
	      (t as (Const ("Script.Rewrite'_Set'_Inst", _) $ _ $ _ $ _)) =
neuper@37906
   145
    (a, STac (case a of SOME a' => subst_atomic E (t $ a')
neuper@37906
   146
	     | NONE => ((subst_atomic E t) $ v)))
wneuper@59377
   147
  | subst_stacexpr E _ _ (t as (Const ("Script.Calculate", _) $ _ $ _)) =
neuper@37906
   148
    (NONE, STac (subst_atomic E t))
wneuper@59377
   149
  | subst_stacexpr E a v (t as (Const ("Script.Calculate", _) $ _)) =
neuper@37906
   150
    (a, STac (case a of SOME a' => subst_atomic E (t $ a')
neuper@37906
   151
	     | NONE => ((subst_atomic E t) $ v)))
wneuper@59376
   152
  | subst_stacexpr E _ _ 
wneuper@59377
   153
	      (t as (Const ("Script.Check'_elementwise",_) $ _ $ _)) = 
neuper@37906
   154
    (NONE, STac (subst_atomic E t))
wneuper@59377
   155
  | subst_stacexpr E a v (t as (Const ("Script.Check'_elementwise", _) $ _)) = 
neuper@37906
   156
    (a, STac (case a of SOME a' => subst_atomic E (t $ a')
neuper@37906
   157
		 | NONE => ((subst_atomic E t) $ v)))
wneuper@59377
   158
  | subst_stacexpr E _ _ (t as (Const ("Script.Or'_to'_List", _) $ _)) = 
neuper@37906
   159
    (NONE, STac (subst_atomic E t))
wneuper@59376
   160
  | subst_stacexpr E a v (t as (Const ("Script.Or'_to'_List", _))) = (*t $ v*)
neuper@37906
   161
    (a, STac (case a of SOME a' => subst_atomic E (t $ a')
neuper@37906
   162
		 | NONE => ((subst_atomic E t) $ v)))
wneuper@59377
   163
  | subst_stacexpr E _ _ (t as (Const ("Script.SubProblem", _) $ _ $ _)) =
neuper@37906
   164
    (NONE, STac (subst_atomic E t))
wneuper@59377
   165
  | subst_stacexpr E _ _ (t as (Const ("Script.Take",_) $ _)) =
neuper@37906
   166
    (NONE, STac (subst_atomic E t))
wneuper@59377
   167
  | subst_stacexpr E _ _ (t as (Const ("Script.Substitute", _) $ _ $ _)) =
neuper@37906
   168
    (NONE, STac (subst_atomic E t))
wneuper@59377
   169
  | subst_stacexpr E a v (t as (Const ("Script.Substitute", _) $ _)) =
neuper@37906
   170
    (a, STac (case a of SOME a' => subst_atomic E (t $ a')
neuper@37906
   171
		 | NONE => ((subst_atomic E t) $ v)))
neuper@37906
   172
  (*now all tactics are matched out and this leaf must be without a tactic*)
neuper@37906
   173
  | subst_stacexpr E a v t = 
neuper@37906
   174
    (a, Expr (subst_atomic (case a of SOME a => upd_env E (a,v) 
neuper@37906
   175
				| NONE => E) t));
neuper@37906
   176
wneuper@59376
   177
(* fetch tactics from a program omitting tacticals like While, Repeat,.. *)
wneuper@59376
   178
fun stacpbls (_ $ body) =
wneuper@59376
   179
    let
wneuper@59376
   180
      fun scan (Const ("HOL.Let", _) $ e $ (Abs (_, _, b))) = (scan e) @ (scan b)
wneuper@59376
   181
        | scan (Const ("If", _) $ _ $ e1 $ e2) = (scan e1) @ (scan e2)
wneuper@59376
   182
        | scan (Const ("Script.While", _) $ _ $ e $ _) = scan e
wneuper@59376
   183
        | scan (Const ("Script.While", _) $ _ $ e) = scan e
wneuper@59376
   184
        | scan (Const ("Script.Repeat", _) $ e $ _) = scan e
wneuper@59376
   185
        | scan (Const ("Script.Repeat", _) $ e) = scan e
wneuper@59376
   186
        | scan (Const ("Script.Try", _) $ e $ _) = scan e
wneuper@59376
   187
        | scan (Const ("Script.Try", _) $ e) = scan e
wneuper@59376
   188
        | scan (Const ("Script.Or", _) $ e1 $ e2 $ _) = (scan e1) @ (scan e2)
wneuper@59376
   189
        | scan (Const ("Script.Or", _) $ e1 $ e2) = (scan e1) @ (scan e2)
wneuper@59376
   190
        | scan (Const ("Script.Seq", _) $ e1 $ e2 $ _) = (scan e1) @ (scan e2)
wneuper@59376
   191
        | scan (Const ("Script.Seq", _) $ e1 $ e2) = (scan e1) @ (scan e2)
wneuper@59405
   192
        | scan t = case subst_stacexpr [] NONE Celem.e_term t of
wneuper@59376
   193
  			  (_, STac _) => [t] | (_, Expr _) => []
wneuper@59376
   194
    in (distinct o scan) body end
wneuper@59405
   195
  | stacpbls t = raise ERROR ("fun stacpbls not applicable to '" ^ Celem.term2str t ^ "'")
neuper@37906
   196
wneuper@59376
   197
(* get operators out of a program *)
neuper@37906
   198
fun is_calc (Const ("Script.Calculate",_) $ _) = true
neuper@37906
   199
  | is_calc (Const ("Script.Calculate",_) $ _ $ _) = true
neuper@37906
   200
  | is_calc _ = false;
neuper@37906
   201
fun op_of_calc (Const ("Script.Calculate",_) $ Free (op_,_)) = op_
neuper@37906
   202
  | op_of_calc (Const ("Script.Calculate",_) $ Free (op_,_) $ _) = op_
wneuper@59405
   203
  | op_of_calc t = error ("op_of_calc called with" ^ Celem.term2str t);
wneuper@59377
   204
fun get_calcs thy sc =
wneuper@59377
   205
    sc
wneuper@59377
   206
    |> stacpbls
wneuper@59377
   207
    |> filter is_calc
wneuper@59377
   208
    |> map op_of_calc
wneuper@59378
   209
    |> (assoc_calc' thy |> map);
neuper@37906
   210
wneuper@59376
   211
(** build up a program from rules **)
neuper@37906
   212
wneuper@59376
   213
(* transform type rule to a term *)
wneuper@59393
   214
val ScrStep $ _ $ _ = (TermC.inst_abs o Thm.term_of o the o (TermC.parse @{theory}))
wneuper@59376
   215
  (*'z not affected by parse: 'a --> real*)
neuper@38057
   216
	"Script Stepwise (t_t::'z) =\
neuper@37906
   217
        \(Repeat\
neuper@37906
   218
	\  ((Try (Repeat (Rewrite real_diff_minus False))) @@  \
neuper@37971
   219
	\   (Try (Repeat (Rewrite add_commute False))) @@ \
neuper@48763
   220
	\   (Try (Repeat (Rewrite mult_commute False))))  \
neuper@37923
   221
	\  t_t)";
wneuper@59376
   222
(*WN060605 script-arg (t_::'z) and "Free (t_, 'a)" at end of body are inconsistent !!!*)
wneuper@59393
   223
val ScrStep_inst $ Term $ Bdv $ _= (TermC.inst_abs o Thm.term_of o the o (TermC.parse @{theory}))
wneuper@59376
   224
  (*'z not affected by parse: 'a --> real*)
neuper@38057
   225
	"Script Stepwise_inst (t_t::'z) (v::real) =\
neuper@37906
   226
        \(Repeat\
neuper@37906
   227
	\  ((Try (Repeat (Rewrite_Inst [(bdv,v)] real_diff_minus False))) @@ \
neuper@37971
   228
	\   (Try (Repeat (Rewrite_Inst [(bdv,v)] add_commute False))) @@\
neuper@48763
   229
	\   (Try (Repeat (Rewrite_Inst [(bdv,v)] mult_commute False)))) \
neuper@38057
   230
	\  t_t)"; 
wneuper@59393
   231
val Repeat $ _ = (TermC.inst_abs o Thm.term_of o the o (TermC.parseN @{theory})) 
neuper@37906
   232
	"Repeat (Rewrite real_diff_minus False t)";
wneuper@59393
   233
val Try $ _ = (TermC.inst_abs o Thm.term_of o the o (TermC.parseN @{theory})) 
neuper@37906
   234
	"Try (Rewrite real_diff_minus False t)";
wneuper@59393
   235
val Cal $ _ = (TermC.inst_abs o Thm.term_of o the o (TermC.parseN @{theory})) 
neuper@37921
   236
	"Calculate PLUS";
wneuper@59393
   237
val Ca1 $ _ = (TermC.inst_abs o Thm.term_of o the o (TermC.parseN @{theory})) 
neuper@37921
   238
	"Calculate1 PLUS";
wneuper@59393
   239
val Rew $ (Free (_, IDtype)) $ _ $ _ = (TermC.inst_abs o Thm.term_of o the o (TermC.parseN @{theory})) 
neuper@37906
   240
	"Rewrite real_diff_minus False t";
wneuper@59393
   241
val Rew_Inst $ Subs $ _ $ _ = (TermC.inst_abs o Thm.term_of o the o (TermC.parseN @{theory})) 
neuper@37906
   242
	"Rewrite_Inst [(bdv,v)] real_diff_minus False";
wneuper@59393
   243
val Rew_Set $ _ $ _ = (TermC.inst_abs o Thm.term_of o the o (TermC.parseN @{theory})) 
neuper@37906
   244
	"Rewrite_Set real_diff_minus False";
wneuper@59393
   245
val Rew_Set_Inst $ _ $ _ $ _ = (TermC.inst_abs o Thm.term_of o the o (TermC.parseN @{theory})) 
neuper@37906
   246
	"Rewrite_Set_Inst [(bdv,v)] real_diff_minus False";
wneuper@59393
   247
val SEq $ _ $ _ $ _ = (TermC.inst_abs o Thm.term_of o the o (TermC.parseN @{theory})) 
neuper@37906
   248
	"  ((Try (Repeat (Rewrite real_diff_minus False))) @@  \
neuper@37971
   249
        \   (Try (Repeat (Rewrite add_commute False))) @@ \
neuper@48763
   250
        \   (Try (Repeat (Rewrite mult_commute False)))) t";
neuper@37906
   251
wneuper@59405
   252
fun rule2stac _ (Celem.Thm (thmID, _)) = Try $ (Repeat $ (Rew $ Free (thmID, IDtype) $ @{term False}))
wneuper@59405
   253
  | rule2stac thy (Celem.Calc (c, _)) = Try $ (Repeat $ (Cal $ Free (assoc_calc thy c, IDtype)))
wneuper@59405
   254
  | rule2stac thy (Celem.Cal1 (c, _)) = Try $ (Repeat $ (Ca1 $ Free (assoc_calc thy c, IDtype)))
wneuper@59405
   255
  | rule2stac _ (Celem.Rls_ rls) = Try $ (Rew_Set $ Free (Celem.id_rls rls, IDtype) $ @{term False})
wneuper@59405
   256
  | rule2stac _ r = raise ERROR ("rule2stac: not applicable to \"" ^ Celem.rule2str r ^ "\"");
wneuper@59405
   257
fun rule2stac_inst _ (Celem.Thm (thmID, _)) = 
neuper@37906
   258
    Try $ (Repeat $ (Rew_Inst $ Subs $ Free (thmID, IDtype) $ 
neuper@48760
   259
			      @{term False}))
wneuper@59405
   260
  | rule2stac_inst thy (Celem.Calc (c, _)) = 
s1210629013@52153
   261
    Try $ (Repeat $ (Cal $ Free (assoc_calc thy c, IDtype)))
wneuper@59405
   262
  | rule2stac_inst thy (Celem.Cal1 (c, _)) = 
s1210629013@52153
   263
    Try $ (Repeat $ (Cal $ Free (assoc_calc thy c, IDtype)))
wneuper@59405
   264
  | rule2stac_inst _ (Celem.Rls_ rls) = 
wneuper@59405
   265
    Try $ (Rew_Set_Inst $ Subs $ Free (Celem.id_rls rls, IDtype) $ 
wneuper@59376
   266
			@{term False})
wneuper@59405
   267
  | rule2stac_inst _ r = raise ERROR ("rule2stac_inst: not applicable to \"" ^ Celem.rule2str r ^ "\"");
neuper@37906
   268
neuper@37906
   269
(*for appropriate nesting take stacs in _reverse_ order*)
wneuper@59376
   270
fun op @@@ sts [s] = SEq $ s $ sts
wneuper@59376
   271
  | op @@@ sts (s::ss) = op @@@ (SEq $ s $ sts) ss
wneuper@59376
   272
  | op @@@ t ts =
wneuper@59405
   273
    raise ERROR ("fun @@@ not applicable to \"" ^ Celem.term2str t ^ "\" \"" ^ Celem.terms2str ts  ^ "\"");
neuper@37906
   274
fun @@ [stac] = stac
wneuper@59376
   275
  | @@ [s1, s2] = SEq $ s1 $ s2
wneuper@59376
   276
  | @@ stacs = case rev stacs of
wneuper@59376
   277
      s3 :: s2 :: ss => op @@@ (SEq $ s2 $ s3) ss
wneuper@59405
   278
    | ts => raise ERROR ("fun @@ not applicable to \"" ^ Celem.terms2str ts ^ "\"")
neuper@37906
   279
wneuper@59389
   280
val contains_bdv = (not o null o (filter TermC.is_bdv) o TermC.ids2str o #prop o Thm.rep_thm);
neuper@37906
   281
wneuper@59376
   282
(* does a rule contain a 'bdv'; descend recursively into Rls_ *)
neuper@37906
   283
fun contain_bdv [] = false
wneuper@59405
   284
  | contain_bdv (Celem.Thm (_, thm) :: rs) = 
neuper@37906
   285
    if (not o contains_bdv) thm
neuper@37906
   286
    then contain_bdv rs
neuper@37906
   287
    else true
wneuper@59405
   288
  | contain_bdv (Celem.Calc _ :: rs) = contain_bdv rs
wneuper@59405
   289
  | contain_bdv (Celem.Cal1 _ :: rs) = contain_bdv rs
wneuper@59405
   290
  | contain_bdv (Celem.Rls_ rls :: rs) = 
wneuper@59405
   291
    contain_bdv (Celem.get_rules rls) orelse contain_bdv rs
wneuper@59376
   292
  | contain_bdv (r :: _) = 
wneuper@59405
   293
    error ("contain_bdv called with [" ^ Celem.id_rule r ^ ",...]");
neuper@37906
   294
s1210629013@52153
   295
fun rules2scr_Rls thy rules = (*WN100816 t_ -> t_t like "Script Stepwise..*)
neuper@37906
   296
    if contain_bdv rules
wneuper@59405
   297
    then ScrStep_inst $ Term $ Bdv $ (Repeat $ (((@@ o (map (rule2stac_inst thy))) rules) $ Celem.e_term))
wneuper@59405
   298
    else ScrStep $ Term $ (Repeat $ (((@@ o (map (rule2stac thy))) rules) $ Celem.e_term));
s1210629013@52153
   299
fun rules2scr_Seq thy rules = (*WN100816 t_ -> t_t like "Script Stepwise..*)
neuper@37906
   300
    if contain_bdv rules
neuper@37906
   301
    then ScrStep_inst $ Term $ Bdv $ 
wneuper@59405
   302
	 (((@@ o (map (rule2stac_inst thy))) rules) $ Celem.e_term)
neuper@37906
   303
    else ScrStep $ Term $
wneuper@59405
   304
	 (((@@ o (map (rule2stac thy))) rules) $ Celem.e_term);
neuper@37906
   305
wneuper@59376
   306
(* prepare the input for an rls for use:
neuper@37906
   307
   # generate a script for stepwise execution of the rls
neuper@42318
   308
   # filter the operators for Calc out of the script ?WN111014?
neuper@52155
   309
   !!!use this function while storing by or integrate into KEStore_Elems.add_rlss.*)
wneuper@59405
   310
fun prep_rls _ Celem.Erls = error "prep_rls' not impl. for Erls"
wneuper@59405
   311
  | prep_rls thy (Celem.Rls {id, preconds, rew_ord, erls, srls, rules, errpatts, ...}) = 
s1210629013@52153
   312
      let val sc = (rules2scr_Rls thy rules)
wneuper@59405
   313
      in Celem.Rls {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls,
neuper@42318
   314
	      srls = srls,
wneuper@59377
   315
	      calc = get_calcs thy sc,
neuper@42318
   316
	      rules = rules,
neuper@42451
   317
	      errpatts=errpatts,
wneuper@59405
   318
	      scr = Celem.Prog sc} end
wneuper@59405
   319
  | prep_rls thy (Celem.Seq {id, preconds, rew_ord, erls, srls, rules, errpatts, ...}) = 
s1210629013@52153
   320
      let val sc = (rules2scr_Seq thy rules)
wneuper@59405
   321
      in Celem.Seq {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls,
wneuper@59376
   322
	      srls = srls,
wneuper@59377
   323
	      calc = get_calcs thy sc,
wneuper@59376
   324
	      rules = rules,
wneuper@59376
   325
	      errpatts = errpatts,
wneuper@59405
   326
	      scr = Celem.Prog sc} end
wneuper@59405
   327
  | prep_rls _ (Celem.Rrls {id, ...}) = 
s1210629013@55444
   328
      error ("prep_rls' not required for Rrls \"" ^ id ^ "\"");
wneuper@59374
   329
wneuper@59374
   330
end