src/Tools/isac/CalcElements/rule-set.sml
author Walther Neuper <walther.neuper@jku.at>
Thu, 09 Apr 2020 17:13:17 +0200
changeset 59861 65ec9f679c3f
parent 59858 a2c32a38327a
child 59863 0dcc8f801578
permissions -rw-r--r--
separate struct. UnparseC, shift code to ThmC
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@59857
    13
    preconds: term list, rew_ord: Rewrite_Ord.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@59850
    42
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59850
    43
  (*NONE*)
walther@59850
    44
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59852
    45
  val insert_merge: for_kestore -> for_kestore list -> for_kestore list
walther@59850
    46
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
walther@59850
    47
end
walther@59852
    48
walther@59850
    49
(**)
walther@59850
    50
structure Rule_Set(**): RULE_SET(**) =
walther@59850
    51
struct
walther@59850
    52
(**)
walther@59851
    53
datatype T = datatype Rule_Def.rule_set
walther@59852
    54
type identifier = string
walther@59850
    55
walther@59852
    56
(*/------- this will disappear eventually ----------------------------------------------------\*)
walther@59852
    57
fun dummy_ord (_: (term * term) list) (_: term, _: term) = true;
walther@59852
    58
val empty =
walther@59852
    59
  Rule_Def.Repeat {id = "empty", preconds = [], rew_ord = ("dummy_ord", dummy_ord), erls = Rule_Def.Empty,
walther@59852
    60
    srls = Rule_Def.Empty, calc = [], rules = [], errpatts = [], scr = Rule_Def.EmptyScr}
walther@59852
    61
(*\------- this will disappear eventually ----------------------------------------------------/*)
walther@59850
    62
walther@59852
    63
fun rep Rule_Def.Empty = rep empty
walther@59852
    64
  | rep (Rule_Def.Repeat {id, preconds, rew_ord, erls, srls, calc, errpatts, rules, scr}) =
walther@59852
    65
    {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls, errpats = errpatts,
walther@59852
    66
      calc = calc, rules = rules, scr = scr}
walther@59852
    67
  | rep (Rule_Def.Seqence {id, preconds, rew_ord, erls, srls, calc, errpatts, rules, scr}) =
walther@59852
    68
    {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls, errpats = errpatts,
walther@59852
    69
      calc = calc, rules = rules, scr = scr}
walther@59852
    70
  | rep (Rule_Def.Rrls _)  = rep empty
walther@59850
    71
walther@59852
    72
fun append_rules id Rule_Def.Empty _ = raise ERROR ("append_rules: with \"" ^ id ^ "\" not for Rule_Def.Empty")
walther@59852
    73
  | append_rules id (Rule_Def.Repeat {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    74
			rules = rs, errpatts = errpatts, scr = sc}) r =
walther@59852
    75
    Rule_Def.Repeat {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    76
      rules = rs @ r, errpatts = errpatts, scr = sc}
walther@59852
    77
  | append_rules id (Rule_Def.Seqence {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    78
			rules = rs, errpatts = errpatts, scr = sc}) r =
walther@59852
    79
    Rule_Def.Seqence {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    80
      rules = rs @ r, errpatts = errpatts, scr = sc}
walther@59852
    81
  | append_rules id (Rule_Def.Rrls _) _ = raise ERROR ("append_rules: not for reverse-rewrite-rule-set " ^ id);
walther@59852
    82
walther@59852
    83
fun merge_ids rls1 rls2 =
walther@59852
    84
  let 
walther@59852
    85
    val id1 = (#id o rep) rls1
walther@59852
    86
    val id2 = (#id o rep) rls2
walther@59852
    87
  in
walther@59852
    88
    if id1 = id2 then id1 else "merged_" ^ id1 ^ "_" ^ id2
walther@59852
    89
  end
walther@59852
    90
walther@59852
    91
(*/------- back to rule.sml -----------------------\*)
walther@59852
    92
fun id_rls Rule_Def.Empty = "empty" (*WN060714 quick and dirty: recursive defs! TODO "Rule_Def.Empty"*)
walther@59852
    93
  | id_rls (Rule_Def.Repeat {id, ...}) = id
walther@59852
    94
  | id_rls (Rule_Def.Seqence {id, ...}) = id
walther@59852
    95
  | id_rls (Rule_Def.Rrls {id, ...}) = id;
walther@59852
    96
val rls2str = id_rls; (*drop*)
walther@59852
    97
walther@59852
    98
fun id_rule (Rule_Def.Thm (id, _)) = id
walther@59852
    99
  | id_rule (Rule_Def.Num_Calc (id, _)) = id
walther@59852
   100
  | id_rule (Rule_Def.Cal1 (id, _)) = id
walther@59852
   101
  | id_rule (Rule_Def.Rls_ rls) = id_rls rls
walther@59852
   102
  | id_rule Rule_Def.Erule = "Erule";
walther@59852
   103
fun eq_rule (Rule_Def.Thm (thm1, _), Rule_Def.Thm (thm2, _)) = thm1 = thm2
walther@59852
   104
  | eq_rule (Rule_Def.Num_Calc (id1, _), Rule_Def.Num_Calc (id2, _)) = id1 = id2
walther@59852
   105
  | eq_rule (Rule_Def.Rls_ rls1, Rule_Def.Rls_ rls2) = id_rls rls1 = id_rls rls2
walther@59852
   106
  | eq_rule _ = false;
walther@59852
   107
(*\------- back to rule.sml -----------------------/*)
walther@59852
   108
walther@59852
   109
fun merge _ Rule_Def.Empty rls = rls
walther@59852
   110
  | merge _ rls Rule_Def.Empty = rls
walther@59852
   111
  | merge _ (Rrls x) _ = Rrls x (* required for merging Theory_Data *)
walther@59852
   112
  | merge _ _ (Rrls x) = Rrls x
walther@59852
   113
  | merge id
walther@59852
   114
	  (Rule_Def.Repeat {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
walther@59852
   115
	    rules = rs1, errpatts = eps1, scr = sc1, ...})
walther@59852
   116
	  (Rule_Def.Repeat {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
walther@59852
   117
	    rules = rs2, errpatts = eps2, ...})
walther@59852
   118
    =
walther@59852
   119
	  Rule_Def.Repeat {id = id, rew_ord = ro1, scr = sc1,
walther@59852
   120
	    preconds = union (op =) pc1 pc2,	    
walther@59852
   121
	    erls = merge (merge_ids er1 er2) er1 er2,
walther@59852
   122
	    srls = merge (merge_ids sr1 sr2) sr1 sr2,
walther@59853
   123
	    calc = union Exec_Def.calc_eq ca1 ca2,
walther@59852
   124
		  rules = union eq_rule rs1 rs2,
walther@59852
   125
      errpatts = union (op =) eps1 eps2}
walther@59852
   126
  | merge id
walther@59852
   127
	  (Rule_Def.Seqence {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
walther@59852
   128
	    rules = rs1, errpatts = eps1, scr = sc1, ...})
walther@59852
   129
	  (Rule_Def.Seqence {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
walther@59852
   130
	    rules = rs2, errpatts = eps2, ...})
walther@59852
   131
    =
walther@59852
   132
	  Rule_Def.Seqence {id = id, rew_ord = ro1, scr = sc1,
walther@59852
   133
	    preconds = union (op =) pc1 pc2,	    
walther@59852
   134
	    erls = merge (merge_ids er1 er2) er1 er2,
walther@59852
   135
	    srls = merge (merge_ids sr1 sr2) sr1 sr2,
walther@59853
   136
	    calc = union Exec_Def.calc_eq ca1 ca2,
walther@59852
   137
		  rules = union eq_rule rs1 rs2,
walther@59852
   138
      errpatts = union (op =) eps1 eps2}
walther@59852
   139
  | merge id _ _ = error ("merge: \"" ^ id ^ 
walther@59852
   140
    "\"; not for reverse-rewrite-rule-sets and not for mixed Rls -- Rule_Def.Seqence");
walther@59852
   141
walther@59852
   142
(*/------- this will disappear eventually -----------\*)
walther@59850
   143
type rrlsstate =  (* state for reverse rewriting, comments see type rule and scr | Rfuns *)
walther@59850
   144
  (term * term * Rule_Def.rule list list * (Rule_Def.rule * (term * term list)) list);
walther@59861
   145
val e_rrlsstate = (UnparseC.e_term, UnparseC.e_term, [[Rule_Def.Erule]], [(Rule_Def.Erule, (UnparseC.e_term, []))]) : rrlsstate;
walther@59850
   146
local
walther@59850
   147
    fun ii (_: term) = e_rrlsstate;
walther@59861
   148
    fun no (_: term) = SOME (UnparseC.e_term, [UnparseC.e_term]);
walther@59861
   149
    fun lo (_: Rule_Def.rule list list) (_: term) (_: Rule_Def.rule) = [(Rule_Def.Erule, (UnparseC.e_term, [UnparseC.e_term]))];
walther@59850
   150
    fun ne (_: Rule_Def.rule list list) (_: term) = SOME Rule_Def.Erule;
walther@59861
   151
    fun fo (_: Rule_Def.rule list list) (_: term) (_: term) = [(Rule_Def.Erule, (UnparseC.e_term, [UnparseC.e_term]))];
walther@59850
   152
in
walther@59850
   153
val e_rfuns = Rule_Def.Rfuns {init_state = ii, normal_form = no, locate_rule = lo,
walther@59850
   154
		  next_rule = ne, attach_form = fo};
walther@59850
   155
end;
walther@59850
   156
val e_rrls =
walther@59852
   157
  Rule_Def.Rrls {id = "e_rrls", prepat = [], rew_ord = ("dummy_ord", dummy_ord), erls = Rule_Def.Empty,
walther@59851
   158
    calc = [], errpatts = [], scr = e_rfuns}
walther@59852
   159
(*\------- this will disappear eventually -----------/*)
walther@59850
   160
walther@59852
   161
(*/------- back to rule.sml -----------------------\*)
walther@59850
   162
fun eq_rule (Rule_Def.Thm (thm1, _), Rule_Def.Thm (thm2, _)) = thm1 = thm2
walther@59850
   163
  | eq_rule (Rule_Def.Num_Calc (id1, _), Rule_Def.Num_Calc (id2, _)) = id1 = id2
walther@59850
   164
  | eq_rule (Rule_Def.Rls_ rls1, Rule_Def.Rls_ rls2) = id_rls rls1 = id_rls rls2
walther@59850
   165
  | eq_rule _ = false;
walther@59850
   166
walther@59852
   167
fun rule2str Rule_Def.Erule = "Erule" 
walther@59858
   168
  | rule2str (Rule_Def.Thm (str, thm)) = "Thm (\"" ^ str ^ "\"," ^ ThmC.string_of_thmI thm ^ ")"
walther@59852
   169
  | rule2str (Rule_Def.Num_Calc (str, _)) = "Num_Calc (\"" ^ str ^ "\",fn)"
walther@59852
   170
  | rule2str (Rule_Def.Cal1 (str, _)) = "Cal1 (\"" ^ str ^ "\",fn)"
walther@59852
   171
  | rule2str (Rule_Def.Rls_ rls) = "Rls_ (\"" ^ id_rls rls ^ "\")";
walther@59850
   172
fun rule2str' Rule_Def.Erule = "Erule"
walther@59852
   173
  | rule2str' (Rule_Def.Thm (str, _)) = "Thm (\"" ^ str ^ "\",\"\")"
walther@59852
   174
  | rule2str' (Rule_Def.Num_Calc (str, _)) = "Num_Calc (\"" ^ str ^ "\",fn)"
walther@59852
   175
  | rule2str' (Rule_Def.Cal1 (str, _)) = "Cal1 (\"" ^ str ^ "\",fn)"
walther@59852
   176
  | rule2str' (Rule_Def.Rls_ rls) = "Rls_ (\"" ^ id_rls rls ^ "\")";
walther@59852
   177
(*\------- back to rule.sml -----------------------/*)
walther@59850
   178
walther@59850
   179
walther@59850
   180
(* datastructure for KEStore_Elems, intermediate for thehier *)
walther@59852
   181
type for_kestore = 
walther@59852
   182
  (identifier *     (* identifier unique within Isac *)
walther@59854
   183
  (ThyC.theory' *  (* just for assignment in thehier, not appropriate for parsing etc *)
walther@59852
   184
    T))     (* ((#id o rep) T) = identifier   by coding discipline *)
walther@59852
   185
fun equal ((id1, (_, _)), (id2, (_, _))) = id1 = id2
walther@59850
   186
walther@59852
   187
fun insert_merge (re as (id, (thyID, r1))) ys = 
walther@59852
   188
    case get_index (fn y => if curry equal re y then SOME y else NONE) ys of
walther@59850
   189
      NONE => re :: ys
walther@59850
   190
    | SOME (i, (_, (_, r2))) => 
walther@59850
   191
      let
walther@59852
   192
        val r12 = merge id r1 r2
walther@59850
   193
      in list_update ys i (id, (thyID, r12)) end
walther@59852
   194
fun to_kestore (s1, s2) = fold insert_merge s1 s2;
walther@59850
   195
walther@59852
   196
(* used only for one hack *)
walther@59852
   197
fun keep_unique_rules id 
walther@59852
   198
     (Rule_Def.Repeat {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
   199
  		  rules = rs, errpatts = eps, scr = sc}) r =
walther@59852
   200
      Rule_Def.Repeat {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
   201
  	    rules = gen_rems eq_rule (rs, r),
walther@59852
   202
  	    errpatts = eps,
walther@59852
   203
  	    scr = sc}
walther@59852
   204
  | keep_unique_rules id
walther@59852
   205
     (Rule_Def.Seqence {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
   206
  		  rules = rs, errpatts = eps, scr = sc}) r =
walther@59852
   207
      Rule_Def.Seqence {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
   208
  	    rules = gen_rems eq_rule (rs, r),
walther@59852
   209
  	    errpatts = eps,
walther@59852
   210
  	    scr = sc}
walther@59852
   211
  | keep_unique_rules id (Rule_Def.Rrls _) _ = raise ERROR ("keep_unique_rules: not for reverse-rewrite-rule-set "^id)
walther@59852
   212
  | keep_unique_rules _ rls _ = raise ERROR ("remove_rls called with " ^ rls2str rls);
walther@59850
   213
walther@59851
   214
fun get_rules Rule_Def.Empty = []
walther@59851
   215
  | get_rules (Rule_Def.Repeat {rules, ...}) = rules
walther@59851
   216
  | get_rules (Rule_Def.Seqence {rules, ...}) = rules
walther@59851
   217
  | get_rules (Rule_Def.Rrls _) = [];
walther@59850
   218
walther@59850
   219
walther@59850
   220
(**)end(**)