src/Tools/isac/CalcElements/rule-set.sml
author Walther Neuper <walther.neuper@jku.at>
Thu, 09 Apr 2020 18:21:09 +0200
changeset 59863 0dcc8f801578
parent 59861 65ec9f679c3f
child 59864 167472fbce77
permissions -rw-r--r--
rearrange code in Rule_Set and Rule

note: mutual recursion inhibits nice separation. ThyC and ThmC are involved, too.
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@59863
    12
  val rls2str: T -> string
walther@59852
    13
  val rep: T -> {calc: Rule_Def.calc list, erls: T, errpats: Rule_Def.errpatID list, id: string,
walther@59857
    14
    preconds: term list, rew_ord: Rewrite_Ord.rew_ord, rules: Rule_Def.rule list, scr: Rule_Def.program, srls: T}
walther@59852
    15
walther@59852
    16
  val append_rules: string -> T -> Rule_Def.rule list -> T
walther@59852
    17
  val keep_unique_rules: string -> T ->  Rule_Def.rule list -> T
walther@59852
    18
  val merge: string -> T -> T -> T
walther@59852
    19
  val get_rules: T -> Rule_Def.rule list
walther@59852
    20
walther@59852
    21
  type for_kestore
walther@59852
    22
  val equal: (''a * ('b * 'c)) * (''a * ('d * 'e)) -> bool
walther@59852
    23
  val to_kestore: for_kestore list * for_kestore list -> for_kestore list
walther@59852
    24
walther@59852
    25
(*/------- this will disappear eventually -----------\*)
walther@59852
    26
  val empty: T
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@59850
    32
  val rule2str: Rule_Def.rule -> string
walther@59850
    33
  val rule2str': Rule_Def.rule -> string
walther@59850
    34
walther@59850
    35
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59850
    36
  (*NONE*)
walther@59850
    37
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59852
    38
  val insert_merge: for_kestore -> for_kestore list -> for_kestore list
walther@59850
    39
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
walther@59850
    40
end
walther@59852
    41
walther@59850
    42
(**)
walther@59850
    43
structure Rule_Set(**): RULE_SET(**) =
walther@59850
    44
struct
walther@59850
    45
(**)
walther@59851
    46
datatype T = datatype Rule_Def.rule_set
walther@59852
    47
type identifier = string
walther@59850
    48
walther@59852
    49
(*/------- this will disappear eventually ----------------------------------------------------\*)
walther@59852
    50
fun dummy_ord (_: (term * term) list) (_: term, _: term) = true;
walther@59852
    51
val empty =
walther@59852
    52
  Rule_Def.Repeat {id = "empty", preconds = [], rew_ord = ("dummy_ord", dummy_ord), erls = Rule_Def.Empty,
walther@59852
    53
    srls = Rule_Def.Empty, calc = [], rules = [], errpatts = [], scr = Rule_Def.EmptyScr}
walther@59852
    54
(*\------- this will disappear eventually ----------------------------------------------------/*)
walther@59850
    55
walther@59863
    56
fun rls2str xxx = Rule.id_rls xxx;
walther@59852
    57
fun rep Rule_Def.Empty = rep empty
walther@59852
    58
  | rep (Rule_Def.Repeat {id, preconds, rew_ord, erls, srls, calc, errpatts, rules, scr}) =
walther@59852
    59
    {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls, errpats = errpatts,
walther@59852
    60
      calc = calc, rules = rules, scr = scr}
walther@59852
    61
  | rep (Rule_Def.Seqence {id, preconds, rew_ord, erls, srls, calc, errpatts, rules, scr}) =
walther@59852
    62
    {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls, errpats = errpatts,
walther@59852
    63
      calc = calc, rules = rules, scr = scr}
walther@59852
    64
  | rep (Rule_Def.Rrls _)  = rep empty
walther@59850
    65
walther@59852
    66
fun append_rules id Rule_Def.Empty _ = raise ERROR ("append_rules: with \"" ^ id ^ "\" not for Rule_Def.Empty")
walther@59852
    67
  | append_rules id (Rule_Def.Repeat {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    68
			rules = rs, errpatts = errpatts, scr = sc}) r =
walther@59852
    69
    Rule_Def.Repeat {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    70
      rules = rs @ r, errpatts = errpatts, scr = sc}
walther@59852
    71
  | append_rules id (Rule_Def.Seqence {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    72
			rules = rs, errpatts = errpatts, scr = sc}) r =
walther@59852
    73
    Rule_Def.Seqence {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    74
      rules = rs @ r, errpatts = errpatts, scr = sc}
walther@59852
    75
  | append_rules id (Rule_Def.Rrls _) _ = raise ERROR ("append_rules: not for reverse-rewrite-rule-set " ^ id);
walther@59852
    76
walther@59852
    77
fun merge_ids rls1 rls2 =
walther@59852
    78
  let 
walther@59852
    79
    val id1 = (#id o rep) rls1
walther@59852
    80
    val id2 = (#id o rep) rls2
walther@59852
    81
  in
walther@59852
    82
    if id1 = id2 then id1 else "merged_" ^ id1 ^ "_" ^ id2
walther@59852
    83
  end
walther@59852
    84
walther@59852
    85
fun merge _ Rule_Def.Empty rls = rls
walther@59852
    86
  | merge _ rls Rule_Def.Empty = rls
walther@59852
    87
  | merge _ (Rrls x) _ = Rrls x (* required for merging Theory_Data *)
walther@59852
    88
  | merge _ _ (Rrls x) = Rrls x
walther@59852
    89
  | merge id
walther@59852
    90
	  (Rule_Def.Repeat {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
walther@59852
    91
	    rules = rs1, errpatts = eps1, scr = sc1, ...})
walther@59852
    92
	  (Rule_Def.Repeat {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
walther@59852
    93
	    rules = rs2, errpatts = eps2, ...})
walther@59852
    94
    =
walther@59852
    95
	  Rule_Def.Repeat {id = id, rew_ord = ro1, scr = sc1,
walther@59852
    96
	    preconds = union (op =) pc1 pc2,	    
walther@59852
    97
	    erls = merge (merge_ids er1 er2) er1 er2,
walther@59852
    98
	    srls = merge (merge_ids sr1 sr2) sr1 sr2,
walther@59853
    99
	    calc = union Exec_Def.calc_eq ca1 ca2,
walther@59863
   100
		  rules = union Rule.eq_rule rs1 rs2,
walther@59852
   101
      errpatts = union (op =) eps1 eps2}
walther@59852
   102
  | merge id
walther@59852
   103
	  (Rule_Def.Seqence {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
walther@59852
   104
	    rules = rs1, errpatts = eps1, scr = sc1, ...})
walther@59852
   105
	  (Rule_Def.Seqence {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
walther@59852
   106
	    rules = rs2, errpatts = eps2, ...})
walther@59852
   107
    =
walther@59852
   108
	  Rule_Def.Seqence {id = id, rew_ord = ro1, scr = sc1,
walther@59852
   109
	    preconds = union (op =) pc1 pc2,	    
walther@59852
   110
	    erls = merge (merge_ids er1 er2) er1 er2,
walther@59852
   111
	    srls = merge (merge_ids sr1 sr2) sr1 sr2,
walther@59853
   112
	    calc = union Exec_Def.calc_eq ca1 ca2,
walther@59863
   113
		  rules = union Rule.eq_rule rs1 rs2,
walther@59852
   114
      errpatts = union (op =) eps1 eps2}
walther@59852
   115
  | merge id _ _ = error ("merge: \"" ^ id ^ 
walther@59852
   116
    "\"; not for reverse-rewrite-rule-sets and not for mixed Rls -- Rule_Def.Seqence");
walther@59852
   117
walther@59852
   118
fun rule2str Rule_Def.Erule = "Erule" 
walther@59858
   119
  | rule2str (Rule_Def.Thm (str, thm)) = "Thm (\"" ^ str ^ "\"," ^ ThmC.string_of_thmI thm ^ ")"
walther@59852
   120
  | rule2str (Rule_Def.Num_Calc (str, _)) = "Num_Calc (\"" ^ str ^ "\",fn)"
walther@59852
   121
  | rule2str (Rule_Def.Cal1 (str, _)) = "Cal1 (\"" ^ str ^ "\",fn)"
walther@59863
   122
  | rule2str (Rule_Def.Rls_ rls) = "Rls_ (\"" ^ rls2str rls ^ "\")";
walther@59850
   123
fun rule2str' Rule_Def.Erule = "Erule"
walther@59852
   124
  | rule2str' (Rule_Def.Thm (str, _)) = "Thm (\"" ^ str ^ "\",\"\")"
walther@59852
   125
  | rule2str' (Rule_Def.Num_Calc (str, _)) = "Num_Calc (\"" ^ str ^ "\",fn)"
walther@59852
   126
  | rule2str' (Rule_Def.Cal1 (str, _)) = "Cal1 (\"" ^ str ^ "\",fn)"
walther@59863
   127
  | rule2str' (Rule_Def.Rls_ rls) = "Rls_ (\"" ^ rls2str rls ^ "\")";
walther@59850
   128
walther@59850
   129
(* datastructure for KEStore_Elems, intermediate for thehier *)
walther@59852
   130
type for_kestore = 
walther@59852
   131
  (identifier *     (* identifier unique within Isac *)
walther@59854
   132
  (ThyC.theory' *  (* just for assignment in thehier, not appropriate for parsing etc *)
walther@59852
   133
    T))     (* ((#id o rep) T) = identifier   by coding discipline *)
walther@59852
   134
fun equal ((id1, (_, _)), (id2, (_, _))) = id1 = id2
walther@59850
   135
walther@59852
   136
fun insert_merge (re as (id, (thyID, r1))) ys = 
walther@59852
   137
    case get_index (fn y => if curry equal re y then SOME y else NONE) ys of
walther@59850
   138
      NONE => re :: ys
walther@59850
   139
    | SOME (i, (_, (_, r2))) => 
walther@59850
   140
      let
walther@59852
   141
        val r12 = merge id r1 r2
walther@59850
   142
      in list_update ys i (id, (thyID, r12)) end
walther@59852
   143
fun to_kestore (s1, s2) = fold insert_merge s1 s2;
walther@59850
   144
walther@59852
   145
(* used only for one hack *)
walther@59852
   146
fun keep_unique_rules id 
walther@59852
   147
     (Rule_Def.Repeat {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
   148
  		  rules = rs, errpatts = eps, scr = sc}) r =
walther@59852
   149
      Rule_Def.Repeat {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59863
   150
  	    rules = gen_rems Rule.eq_rule (rs, r),
walther@59852
   151
  	    errpatts = eps,
walther@59852
   152
  	    scr = sc}
walther@59852
   153
  | keep_unique_rules id
walther@59852
   154
     (Rule_Def.Seqence {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
   155
  		  rules = rs, errpatts = eps, scr = sc}) r =
walther@59852
   156
      Rule_Def.Seqence {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59863
   157
  	    rules = gen_rems Rule.eq_rule (rs, r),
walther@59852
   158
  	    errpatts = eps,
walther@59852
   159
  	    scr = sc}
walther@59852
   160
  | keep_unique_rules id (Rule_Def.Rrls _) _ = raise ERROR ("keep_unique_rules: not for reverse-rewrite-rule-set "^id)
walther@59852
   161
  | keep_unique_rules _ rls _ = raise ERROR ("remove_rls called with " ^ rls2str rls);
walther@59850
   162
walther@59851
   163
fun get_rules Rule_Def.Empty = []
walther@59851
   164
  | get_rules (Rule_Def.Repeat {rules, ...}) = rules
walther@59851
   165
  | get_rules (Rule_Def.Seqence {rules, ...}) = rules
walther@59851
   166
  | get_rules (Rule_Def.Rrls _) = [];
walther@59850
   167
walther@59863
   168
(*/------- this will disappear eventually -----------\*)
walther@59863
   169
type rrlsstate =  (* state for reverse rewriting, comments see type rule and scr | Rfuns *)
walther@59863
   170
  (term * term * Rule_Def.rule list list * (Rule_Def.rule * (term * term list)) list);
walther@59863
   171
val e_rrlsstate = (UnparseC.e_term, UnparseC.e_term, [[Rule_Def.Erule]], [(Rule_Def.Erule, (UnparseC.e_term, []))]) : rrlsstate;
walther@59863
   172
local
walther@59863
   173
    fun ii (_: term) = e_rrlsstate;
walther@59863
   174
    fun no (_: term) = SOME (UnparseC.e_term, [UnparseC.e_term]);
walther@59863
   175
    fun lo (_: Rule_Def.rule list list) (_: term) (_: Rule_Def.rule) = [(Rule_Def.Erule, (UnparseC.e_term, [UnparseC.e_term]))];
walther@59863
   176
    fun ne (_: Rule_Def.rule list list) (_: term) = SOME Rule_Def.Erule;
walther@59863
   177
    fun fo (_: Rule_Def.rule list list) (_: term) (_: term) = [(Rule_Def.Erule, (UnparseC.e_term, [UnparseC.e_term]))];
walther@59863
   178
in
walther@59863
   179
val e_rfuns = Rule_Def.Rfuns {init_state = ii, normal_form = no, locate_rule = lo,
walther@59863
   180
		  next_rule = ne, attach_form = fo};
walther@59863
   181
end;
walther@59863
   182
val e_rrls =
walther@59863
   183
  Rule_Def.Rrls {id = "e_rrls", prepat = [], rew_ord = ("dummy_ord", dummy_ord), erls = Rule_Def.Empty,
walther@59863
   184
    calc = [], errpatts = [], scr = e_rfuns}
walther@59863
   185
(*\------- this will disappear eventually -----------/*)
walther@59863
   186
walther@59850
   187
walther@59850
   188
(**)end(**)