src/Tools/isac/CalcElements/rule-set.sml
author Walther Neuper <walther.neuper@jku.at>
Wed, 08 Apr 2020 12:32:51 +0200
changeset 59852 ea7e6679080e
parent 59851 4dd533681fef
child 59853 e18f30c44998
permissions -rw-r--r--
use new struct "Rule_Set" for renaming identifiers
walther@59852
     1
(* Title:  CalcElements/rule-set.sml
walther@59850
     2
   Author: Walther Neuper
walther@59850
     3
   (c) due to copyright terms
walther@59850
     4
walther@59850
     5
In ISAC a Rule_Set serves rewriting. KEStore holds all rule-sets for ubiquitous access.
walther@59850
     6
*)
walther@59850
     7
signature RULE_SET =
walther@59850
     8
sig
walther@59851
     9
  datatype T = datatype Rule_Def.rule_set
walther@59852
    10
  eqtype identifier
walther@59850
    11
walther@59852
    12
  val rep: T -> {calc: Rule_Def.calc list, erls: T, errpats: Rule_Def.errpatID list, id: string,
walther@59852
    13
    preconds: term list, rew_ord: Rule_Def.rew_ord, rules: Rule_Def.rule list, scr: Rule_Def.program, srls: T}
walther@59852
    14
walther@59852
    15
  val append_rules: string -> T -> Rule_Def.rule list -> T
walther@59852
    16
  val keep_unique_rules: string -> T ->  Rule_Def.rule list -> T
walther@59852
    17
  val merge: string -> T -> T -> T
walther@59852
    18
  val get_rules: T -> Rule_Def.rule list
walther@59852
    19
walther@59852
    20
  type for_kestore
walther@59852
    21
  val equal: (''a * ('b * 'c)) * (''a * ('d * 'e)) -> bool
walther@59852
    22
  val to_kestore: for_kestore list * for_kestore list -> for_kestore list
walther@59852
    23
walther@59852
    24
(*/------- this will disappear eventually -----------\*)
walther@59852
    25
  val empty: T
walther@59850
    26
walther@59850
    27
  type rrlsstate = term * term * Rule_Def.rule list list * (Rule_Def.rule * (term * term list)) list
walther@59850
    28
  val e_rrlsstate: rrlsstate
walther@59851
    29
  val e_rrls: T
walther@59852
    30
(*\------- this will disappear eventually -----------/*)
walther@59850
    31
walther@59852
    32
(*val id_rls: T -> string*)
walther@59852
    33
(*/------- back to rule.sml -----------------------\*)
walther@59852
    34
  val rls2str: T(*Rule_Def.rule_set*) -> string
walther@59852
    35
  val id_rls: T(*Rule_Def.rule_set*) -> string
walther@59850
    36
  val id_rule: Rule_Def.rule -> string
walther@59850
    37
  val eq_rule: Rule_Def.rule * Rule_Def.rule -> bool
walther@59850
    38
  val rule2str: Rule_Def.rule -> string
walther@59850
    39
  val rule2str': Rule_Def.rule -> string
walther@59852
    40
(*\------- back to rule.sml -----------------------/*)
walther@59850
    41
walther@59852
    42
(*/------- to exec-def.sml ------------------------\*)
walther@59850
    43
  eqtype prog_calcID
walther@59850
    44
  type cal
walther@59850
    45
  type calc_elem
walther@59850
    46
  val calc_eq: calc_elem * calc_elem -> bool
walther@59850
    47
  val e_evalfn: Rule_Def.eval_fn
walther@59852
    48
(*\------- to exec-def.sml ------------------------/*)
walther@59850
    49
walther@59850
    50
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59850
    51
  (*NONE*)
walther@59850
    52
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59852
    53
  val insert_merge: for_kestore -> for_kestore list -> for_kestore list
walther@59850
    54
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
walther@59850
    55
end
walther@59852
    56
walther@59850
    57
(**)
walther@59850
    58
structure Rule_Set(**): RULE_SET(**) =
walther@59850
    59
struct
walther@59850
    60
(**)
walther@59851
    61
datatype T = datatype Rule_Def.rule_set
walther@59852
    62
type identifier = string
walther@59850
    63
walther@59852
    64
(*/------- this will disappear eventually ----------------------------------------------------\*)
walther@59852
    65
fun dummy_ord (_: (term * term) list) (_: term, _: term) = true;
walther@59852
    66
val empty =
walther@59852
    67
  Rule_Def.Repeat {id = "empty", preconds = [], rew_ord = ("dummy_ord", dummy_ord), erls = Rule_Def.Empty,
walther@59852
    68
    srls = Rule_Def.Empty, calc = [], rules = [], errpatts = [], scr = Rule_Def.EmptyScr}
walther@59852
    69
(*\------- this will disappear eventually ----------------------------------------------------/*)
walther@59850
    70
walther@59852
    71
fun rep Rule_Def.Empty = rep empty
walther@59852
    72
  | rep (Rule_Def.Repeat {id, preconds, rew_ord, erls, srls, calc, errpatts, rules, scr}) =
walther@59852
    73
    {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls, errpats = errpatts,
walther@59852
    74
      calc = calc, rules = rules, scr = scr}
walther@59852
    75
  | rep (Rule_Def.Seqence {id, preconds, rew_ord, erls, srls, calc, errpatts, rules, scr}) =
walther@59852
    76
    {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls, errpats = errpatts,
walther@59852
    77
      calc = calc, rules = rules, scr = scr}
walther@59852
    78
  | rep (Rule_Def.Rrls _)  = rep empty
walther@59850
    79
walther@59852
    80
fun append_rules id Rule_Def.Empty _ = raise ERROR ("append_rules: with \"" ^ id ^ "\" not for Rule_Def.Empty")
walther@59852
    81
  | append_rules id (Rule_Def.Repeat {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    82
			rules = rs, errpatts = errpatts, scr = sc}) r =
walther@59852
    83
    Rule_Def.Repeat {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    84
      rules = rs @ r, errpatts = errpatts, scr = sc}
walther@59852
    85
  | append_rules id (Rule_Def.Seqence {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    86
			rules = rs, errpatts = errpatts, scr = sc}) r =
walther@59852
    87
    Rule_Def.Seqence {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    88
      rules = rs @ r, errpatts = errpatts, scr = sc}
walther@59852
    89
  | append_rules id (Rule_Def.Rrls _) _ = raise ERROR ("append_rules: not for reverse-rewrite-rule-set " ^ id);
walther@59852
    90
walther@59852
    91
fun merge_ids rls1 rls2 =
walther@59852
    92
  let 
walther@59852
    93
    val id1 = (#id o rep) rls1
walther@59852
    94
    val id2 = (#id o rep) rls2
walther@59852
    95
  in
walther@59852
    96
    if id1 = id2 then id1 else "merged_" ^ id1 ^ "_" ^ id2
walther@59852
    97
  end
walther@59852
    98
walther@59852
    99
(*/------- to exec-def.sml ------------------------\*)
walther@59852
   100
type prog_calcID = string;
walther@59852
   101
(* op in isa-term "Const(op,_)" *)
walther@59852
   102
type cal = Rule_Def.calID * Rule_Def.eval_fn;
walther@59852
   103
type calc_elem =  (* fun calculate_ fetches the evaluation-function via this list *)
walther@59852
   104
  prog_calcID *   (* a simple identifier used in programs                    *)
walther@59852
   105
  (Rule_Def.calID *        (* a long identifier used in Const                         *)
walther@59852
   106
    Rule_Def.eval_fn)      (* an ML function                                          *)
walther@59852
   107
fun calc_eq ((pi1, (ci1, _)), (pi2, (ci2, _))) =
walther@59852
   108
  if pi1 = pi2
walther@59852
   109
  then if ci1 = ci2 then true else error ("calc_eq: " ^ ci1 ^ " <> " ^ ci2)
walther@59852
   110
  else false
walther@59852
   111
fun e_evalfn (_ : 'a) (_ : term) (_ : theory) = NONE : (string * term) option;
walther@59852
   112
(*\------- to exec-def.sml ------------------------/*)
walther@59852
   113
walther@59852
   114
(*val id_rls = Rule.identifier *)
walther@59852
   115
walther@59852
   116
(*/------- back to rule.sml -----------------------\*)
walther@59852
   117
fun id_rls Rule_Def.Empty = "empty" (*WN060714 quick and dirty: recursive defs! TODO "Rule_Def.Empty"*)
walther@59852
   118
  | id_rls (Rule_Def.Repeat {id, ...}) = id
walther@59852
   119
  | id_rls (Rule_Def.Seqence {id, ...}) = id
walther@59852
   120
  | id_rls (Rule_Def.Rrls {id, ...}) = id;
walther@59852
   121
val rls2str = id_rls; (*drop*)
walther@59852
   122
walther@59852
   123
fun id_rule (Rule_Def.Thm (id, _)) = id
walther@59852
   124
  | id_rule (Rule_Def.Num_Calc (id, _)) = id
walther@59852
   125
  | id_rule (Rule_Def.Cal1 (id, _)) = id
walther@59852
   126
  | id_rule (Rule_Def.Rls_ rls) = id_rls rls
walther@59852
   127
  | id_rule Rule_Def.Erule = "Erule";
walther@59852
   128
fun eq_rule (Rule_Def.Thm (thm1, _), Rule_Def.Thm (thm2, _)) = thm1 = thm2
walther@59852
   129
  | eq_rule (Rule_Def.Num_Calc (id1, _), Rule_Def.Num_Calc (id2, _)) = id1 = id2
walther@59852
   130
  | eq_rule (Rule_Def.Rls_ rls1, Rule_Def.Rls_ rls2) = id_rls rls1 = id_rls rls2
walther@59852
   131
  | eq_rule _ = false;
walther@59852
   132
(*\------- back to rule.sml -----------------------/*)
walther@59852
   133
walther@59852
   134
fun merge _ Rule_Def.Empty rls = rls
walther@59852
   135
  | merge _ rls Rule_Def.Empty = rls
walther@59852
   136
  | merge _ (Rrls x) _ = Rrls x (* required for merging Theory_Data *)
walther@59852
   137
  | merge _ _ (Rrls x) = Rrls x
walther@59852
   138
  | merge id
walther@59852
   139
	  (Rule_Def.Repeat {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
walther@59852
   140
	    rules = rs1, errpatts = eps1, scr = sc1, ...})
walther@59852
   141
	  (Rule_Def.Repeat {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
walther@59852
   142
	    rules = rs2, errpatts = eps2, ...})
walther@59852
   143
    =
walther@59852
   144
	  Rule_Def.Repeat {id = id, rew_ord = ro1, scr = sc1,
walther@59852
   145
	    preconds = union (op =) pc1 pc2,	    
walther@59852
   146
	    erls = merge (merge_ids er1 er2) er1 er2,
walther@59852
   147
	    srls = merge (merge_ids sr1 sr2) sr1 sr2,
walther@59852
   148
	    calc = union calc_eq ca1 ca2,
walther@59852
   149
		  rules = union eq_rule rs1 rs2,
walther@59852
   150
      errpatts = union (op =) eps1 eps2}
walther@59852
   151
  | merge id
walther@59852
   152
	  (Rule_Def.Seqence {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
walther@59852
   153
	    rules = rs1, errpatts = eps1, scr = sc1, ...})
walther@59852
   154
	  (Rule_Def.Seqence {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
walther@59852
   155
	    rules = rs2, errpatts = eps2, ...})
walther@59852
   156
    =
walther@59852
   157
	  Rule_Def.Seqence {id = id, rew_ord = ro1, scr = sc1,
walther@59852
   158
	    preconds = union (op =) pc1 pc2,	    
walther@59852
   159
	    erls = merge (merge_ids er1 er2) er1 er2,
walther@59852
   160
	    srls = merge (merge_ids sr1 sr2) sr1 sr2,
walther@59852
   161
	    calc = union calc_eq ca1 ca2,
walther@59852
   162
		  rules = union eq_rule rs1 rs2,
walther@59852
   163
      errpatts = union (op =) eps1 eps2}
walther@59852
   164
  | merge id _ _ = error ("merge: \"" ^ id ^ 
walther@59852
   165
    "\"; not for reverse-rewrite-rule-sets and not for mixed Rls -- Rule_Def.Seqence");
walther@59852
   166
walther@59852
   167
(*/------- this will disappear eventually -----------\*)
walther@59850
   168
type rrlsstate =  (* state for reverse rewriting, comments see type rule and scr | Rfuns *)
walther@59850
   169
  (term * term * Rule_Def.rule list list * (Rule_Def.rule * (term * term list)) list);
walther@59850
   170
val e_rrlsstate = (Rule.e_term, Rule.e_term, [[Rule_Def.Erule]], [(Rule_Def.Erule, (Rule.e_term, []))]) : rrlsstate;
walther@59850
   171
local
walther@59850
   172
    fun ii (_: term) = e_rrlsstate;
walther@59850
   173
    fun no (_: term) = SOME (Rule.e_term, [Rule.e_term]);
walther@59850
   174
    fun lo (_: Rule_Def.rule list list) (_: term) (_: Rule_Def.rule) = [(Rule_Def.Erule, (Rule.e_term, [Rule.e_term]))];
walther@59850
   175
    fun ne (_: Rule_Def.rule list list) (_: term) = SOME Rule_Def.Erule;
walther@59850
   176
    fun fo (_: Rule_Def.rule list list) (_: term) (_: term) = [(Rule_Def.Erule, (Rule.e_term, [Rule.e_term]))];
walther@59850
   177
in
walther@59850
   178
val e_rfuns = Rule_Def.Rfuns {init_state = ii, normal_form = no, locate_rule = lo,
walther@59850
   179
		  next_rule = ne, attach_form = fo};
walther@59850
   180
end;
walther@59850
   181
val e_rrls =
walther@59852
   182
  Rule_Def.Rrls {id = "e_rrls", prepat = [], rew_ord = ("dummy_ord", dummy_ord), erls = Rule_Def.Empty,
walther@59851
   183
    calc = [], errpatts = [], scr = e_rfuns}
walther@59852
   184
(*\------- this will disappear eventually -----------/*)
walther@59850
   185
walther@59852
   186
(*/------- back to rule.sml -----------------------\*)
walther@59850
   187
fun eq_rule (Rule_Def.Thm (thm1, _), Rule_Def.Thm (thm2, _)) = thm1 = thm2
walther@59850
   188
  | eq_rule (Rule_Def.Num_Calc (id1, _), Rule_Def.Num_Calc (id2, _)) = id1 = id2
walther@59850
   189
  | eq_rule (Rule_Def.Rls_ rls1, Rule_Def.Rls_ rls2) = id_rls rls1 = id_rls rls2
walther@59850
   190
  | eq_rule _ = false;
walther@59850
   191
walther@59852
   192
fun rule2str Rule_Def.Erule = "Erule" 
walther@59852
   193
  | rule2str (Rule_Def.Thm (str, thm)) = "Thm (\"" ^ str ^ "\"," ^ Rule.string_of_thmI thm ^ ")"
walther@59852
   194
  | rule2str (Rule_Def.Num_Calc (str, _)) = "Num_Calc (\"" ^ str ^ "\",fn)"
walther@59852
   195
  | rule2str (Rule_Def.Cal1 (str, _)) = "Cal1 (\"" ^ str ^ "\",fn)"
walther@59852
   196
  | rule2str (Rule_Def.Rls_ rls) = "Rls_ (\"" ^ id_rls rls ^ "\")";
walther@59850
   197
fun rule2str' Rule_Def.Erule = "Erule"
walther@59852
   198
  | rule2str' (Rule_Def.Thm (str, _)) = "Thm (\"" ^ str ^ "\",\"\")"
walther@59852
   199
  | rule2str' (Rule_Def.Num_Calc (str, _)) = "Num_Calc (\"" ^ str ^ "\",fn)"
walther@59852
   200
  | rule2str' (Rule_Def.Cal1 (str, _)) = "Cal1 (\"" ^ str ^ "\",fn)"
walther@59852
   201
  | rule2str' (Rule_Def.Rls_ rls) = "Rls_ (\"" ^ id_rls rls ^ "\")";
walther@59852
   202
(*\------- back to rule.sml -----------------------/*)
walther@59850
   203
walther@59850
   204
walther@59850
   205
(* datastructure for KEStore_Elems, intermediate for thehier *)
walther@59852
   206
type for_kestore = 
walther@59852
   207
  (identifier *     (* identifier unique within Isac *)
walther@59850
   208
  (Rule.theory' *  (* just for assignment in thehier, not appropriate for parsing etc *)
walther@59852
   209
    T))     (* ((#id o rep) T) = identifier   by coding discipline *)
walther@59852
   210
fun equal ((id1, (_, _)), (id2, (_, _))) = id1 = id2
walther@59850
   211
walther@59852
   212
fun insert_merge (re as (id, (thyID, r1))) ys = 
walther@59852
   213
    case get_index (fn y => if curry equal re y then SOME y else NONE) ys of
walther@59850
   214
      NONE => re :: ys
walther@59850
   215
    | SOME (i, (_, (_, r2))) => 
walther@59850
   216
      let
walther@59852
   217
        val r12 = merge id r1 r2
walther@59850
   218
      in list_update ys i (id, (thyID, r12)) end
walther@59852
   219
fun to_kestore (s1, s2) = fold insert_merge s1 s2;
walther@59850
   220
walther@59852
   221
(* used only for one hack *)
walther@59852
   222
fun keep_unique_rules id 
walther@59852
   223
     (Rule_Def.Repeat {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
   224
  		  rules = rs, errpatts = eps, scr = sc}) r =
walther@59852
   225
      Rule_Def.Repeat {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
   226
  	    rules = gen_rems eq_rule (rs, r),
walther@59852
   227
  	    errpatts = eps,
walther@59852
   228
  	    scr = sc}
walther@59852
   229
  | keep_unique_rules id
walther@59852
   230
     (Rule_Def.Seqence {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
   231
  		  rules = rs, errpatts = eps, scr = sc}) r =
walther@59852
   232
      Rule_Def.Seqence {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
   233
  	    rules = gen_rems eq_rule (rs, r),
walther@59852
   234
  	    errpatts = eps,
walther@59852
   235
  	    scr = sc}
walther@59852
   236
  | keep_unique_rules id (Rule_Def.Rrls _) _ = raise ERROR ("keep_unique_rules: not for reverse-rewrite-rule-set "^id)
walther@59852
   237
  | keep_unique_rules _ rls _ = raise ERROR ("remove_rls called with " ^ rls2str rls);
walther@59850
   238
walther@59851
   239
fun get_rules Rule_Def.Empty = []
walther@59851
   240
  | get_rules (Rule_Def.Repeat {rules, ...}) = rules
walther@59851
   241
  | get_rules (Rule_Def.Seqence {rules, ...}) = rules
walther@59851
   242
  | get_rules (Rule_Def.Rrls _) = [];
walther@59850
   243
walther@59850
   244
walther@59850
   245
(**)end(**)