src/Tools/isac/BaseDefinitions/rule-set.sml
author Walther Neuper <walther.neuper@jku.at>
Thu, 23 Apr 2020 12:34:54 +0200
changeset 59907 4c62e16e842e
parent 59887 4616b145b1cd
child 59914 ab5bd5c37e13
permissions -rw-r--r--
use "Derive" for renaming identifiers
walther@59866
     1
(* Title:  BaseDefinitions/rule-set.sml
walther@59850
     2
   Author: Walther Neuper
walther@59850
     3
   (c) due to copyright terms
walther@59850
     4
walther@59887
     5
In ISAC a Rule_Set serves rewriting. Know_Store 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@59867
    10
  eqtype id
walther@59850
    11
walther@59867
    12
  val id: 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@59907
    20
(*val rule2rls' : Rule.rule -> string*)
walther@59907
    21
  val id_from_rule : Rule.rule -> string
walther@59852
    22
walther@59852
    23
  type for_kestore
walther@59852
    24
  val equal: (''a * ('b * 'c)) * (''a * ('d * 'e)) -> bool
walther@59852
    25
  val to_kestore: for_kestore list * for_kestore list -> for_kestore list
walther@59852
    26
walther@59869
    27
(*/------- this will disappear eventually ----------------------------------------------------\*)
walther@59852
    28
  val empty: T
walther@59850
    29
  type rrlsstate = term * term * Rule_Def.rule list list * (Rule_Def.rule * (term * term list)) list
walther@59850
    30
  val e_rrlsstate: rrlsstate
walther@59851
    31
  val e_rrls: T
walther@59869
    32
(*\------- this will disappear eventually ----------------------------------------------------/*)
walther@59850
    33
walther@59850
    34
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59850
    35
  (*NONE*)
walther@59886
    36
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59852
    37
  val insert_merge: for_kestore -> for_kestore list -> for_kestore list
walther@59886
    38
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
walther@59850
    39
end
walther@59852
    40
walther@59850
    41
(**)
walther@59850
    42
structure Rule_Set(**): RULE_SET(**) =
walther@59850
    43
struct
walther@59850
    44
(**)
walther@59867
    45
datatype T = datatype Rule_Def.rule_set;
walther@59867
    46
type id = string;
walther@59850
    47
walther@59852
    48
(*/------- this will disappear eventually ----------------------------------------------------\*)
walther@59852
    49
fun dummy_ord (_: (term * term) list) (_: term, _: term) = true;
walther@59852
    50
val empty =
walther@59852
    51
  Rule_Def.Repeat {id = "empty", preconds = [], rew_ord = ("dummy_ord", dummy_ord), erls = Rule_Def.Empty,
walther@59878
    52
    srls = Rule_Def.Empty, calc = [], rules = [], errpatts = [], scr = Rule_Def.Empty_Prog}
walther@59852
    53
(*\------- this will disappear eventually ----------------------------------------------------/*)
walther@59850
    54
walther@59867
    55
val id = Rule.set_id;
walther@59852
    56
fun rep Rule_Def.Empty = rep empty
walther@59852
    57
  | rep (Rule_Def.Repeat {id, preconds, rew_ord, erls, srls, calc, errpatts, rules, scr}) =
walther@59852
    58
    {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls, errpats = errpatts,
walther@59852
    59
      calc = calc, rules = rules, scr = scr}
walther@59878
    60
  | rep (Rule_Def.Sequence {id, preconds, rew_ord, erls, srls, calc, errpatts, rules, scr}) =
walther@59852
    61
    {id = id, preconds = preconds, rew_ord = rew_ord, erls = erls, srls = srls, errpats = errpatts,
walther@59852
    62
      calc = calc, rules = rules, scr = scr}
walther@59852
    63
  | rep (Rule_Def.Rrls _)  = rep empty
walther@59850
    64
walther@59852
    65
fun append_rules id Rule_Def.Empty _ = raise ERROR ("append_rules: with \"" ^ id ^ "\" not for Rule_Def.Empty")
walther@59852
    66
  | append_rules id (Rule_Def.Repeat {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    67
			rules = rs, errpatts = errpatts, scr = sc}) r =
walther@59852
    68
    Rule_Def.Repeat {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    69
      rules = rs @ r, errpatts = errpatts, scr = sc}
walther@59878
    70
  | append_rules id (Rule_Def.Sequence {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    71
			rules = rs, errpatts = errpatts, scr = sc}) r =
walther@59878
    72
    Rule_Def.Sequence {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
    73
      rules = rs @ r, errpatts = errpatts, scr = sc}
walther@59852
    74
  | append_rules id (Rule_Def.Rrls _) _ = raise ERROR ("append_rules: not for reverse-rewrite-rule-set " ^ id);
walther@59852
    75
walther@59852
    76
fun merge_ids rls1 rls2 =
walther@59852
    77
  let 
walther@59852
    78
    val id1 = (#id o rep) rls1
walther@59852
    79
    val id2 = (#id o rep) rls2
walther@59852
    80
  in
walther@59852
    81
    if id1 = id2 then id1 else "merged_" ^ id1 ^ "_" ^ id2
walther@59852
    82
  end
walther@59852
    83
walther@59852
    84
fun merge _ Rule_Def.Empty rls = rls
walther@59852
    85
  | merge _ rls Rule_Def.Empty = rls
walther@59852
    86
  | merge _ (Rrls x) _ = Rrls x (* required for merging Theory_Data *)
walther@59852
    87
  | merge _ _ (Rrls x) = Rrls x
walther@59852
    88
  | merge id
walther@59852
    89
	  (Rule_Def.Repeat {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
walther@59852
    90
	    rules = rs1, errpatts = eps1, scr = sc1, ...})
walther@59852
    91
	  (Rule_Def.Repeat {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
walther@59852
    92
	    rules = rs2, errpatts = eps2, ...})
walther@59852
    93
    =
walther@59852
    94
	  Rule_Def.Repeat {id = id, rew_ord = ro1, scr = sc1,
walther@59852
    95
	    preconds = union (op =) pc1 pc2,	    
walther@59852
    96
	    erls = merge (merge_ids er1 er2) er1 er2,
walther@59852
    97
	    srls = merge (merge_ids sr1 sr2) sr1 sr2,
walther@59853
    98
	    calc = union Exec_Def.calc_eq ca1 ca2,
walther@59867
    99
		  rules = union Rule.equal rs1 rs2,
walther@59852
   100
      errpatts = union (op =) eps1 eps2}
walther@59852
   101
  | merge id
walther@59878
   102
	  (Rule_Def.Sequence {preconds = pc1, rew_ord = ro1, erls = er1, srls = sr1, calc = ca1,
walther@59852
   103
	    rules = rs1, errpatts = eps1, scr = sc1, ...})
walther@59878
   104
	  (Rule_Def.Sequence {preconds = pc2, erls = er2, srls = sr2, calc = ca2,
walther@59852
   105
	    rules = rs2, errpatts = eps2, ...})
walther@59852
   106
    =
walther@59878
   107
	  Rule_Def.Sequence {id = id, rew_ord = ro1, scr = sc1,
walther@59852
   108
	    preconds = union (op =) pc1 pc2,	    
walther@59852
   109
	    erls = merge (merge_ids er1 er2) er1 er2,
walther@59852
   110
	    srls = merge (merge_ids sr1 sr2) sr1 sr2,
walther@59853
   111
	    calc = union Exec_Def.calc_eq ca1 ca2,
walther@59867
   112
		  rules = union Rule.equal rs1 rs2,
walther@59852
   113
      errpatts = union (op =) eps1 eps2}
walther@59852
   114
  | merge id _ _ = error ("merge: \"" ^ id ^ 
walther@59878
   115
    "\"; not for reverse-rewrite-rule-sets and not for mixed Rls -- Rule_Def.Sequence");
walther@59852
   116
walther@59850
   117
(* datastructure for KEStore_Elems, intermediate for thehier *)
walther@59852
   118
type for_kestore = 
walther@59867
   119
  (id *     (* id unique within Isac *)
walther@59879
   120
  (ThyC.id *  (* just for assignment in thehier, not appropriate for parsing etc *)
walther@59867
   121
    T))     (* ((#id o rep) T) = id   by coding discipline *)
walther@59852
   122
fun equal ((id1, (_, _)), (id2, (_, _))) = id1 = id2
walther@59850
   123
walther@59852
   124
fun insert_merge (re as (id, (thyID, r1))) ys = 
walther@59852
   125
    case get_index (fn y => if curry equal re y then SOME y else NONE) ys of
walther@59850
   126
      NONE => re :: ys
walther@59850
   127
    | SOME (i, (_, (_, r2))) => 
walther@59850
   128
      let
walther@59852
   129
        val r12 = merge id r1 r2
walther@59850
   130
      in list_update ys i (id, (thyID, r12)) end
walther@59852
   131
fun to_kestore (s1, s2) = fold insert_merge s1 s2;
walther@59850
   132
walther@59852
   133
(* used only for one hack *)
walther@59852
   134
fun keep_unique_rules id 
walther@59852
   135
     (Rule_Def.Repeat {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
   136
  		  rules = rs, errpatts = eps, scr = sc}) r =
walther@59852
   137
      Rule_Def.Repeat {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59867
   138
  	    rules = gen_rems Rule.equal (rs, r),
walther@59852
   139
  	    errpatts = eps,
walther@59852
   140
  	    scr = sc}
walther@59852
   141
  | keep_unique_rules id
walther@59878
   142
     (Rule_Def.Sequence {id = _, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59852
   143
  		  rules = rs, errpatts = eps, scr = sc}) r =
walther@59878
   144
      Rule_Def.Sequence {id = id, preconds = pc, rew_ord = ro, erls = er, srls = sr, calc = ca,
walther@59867
   145
  	    rules = gen_rems Rule.equal (rs, r),
walther@59852
   146
  	    errpatts = eps,
walther@59852
   147
  	    scr = sc}
walther@59852
   148
  | keep_unique_rules id (Rule_Def.Rrls _) _ = raise ERROR ("keep_unique_rules: not for reverse-rewrite-rule-set "^id)
walther@59867
   149
  | keep_unique_rules _ rls _ = raise ERROR ("remove_rls called with " ^ id rls);
walther@59850
   150
walther@59851
   151
fun get_rules Rule_Def.Empty = []
walther@59851
   152
  | get_rules (Rule_Def.Repeat {rules, ...}) = rules
walther@59878
   153
  | get_rules (Rule_Def.Sequence {rules, ...}) = rules
walther@59851
   154
  | get_rules (Rule_Def.Rrls _) = [];
walther@59850
   155
walther@59907
   156
fun id_from_rule (Rule.Rls_ rls) = id rls
walther@59907
   157
  | id_from_rule r = raise ERROR ("id_from_rule: not defined for " ^ Rule.to_string r);
walther@59907
   158
walther@59863
   159
(*/------- this will disappear eventually -----------\*)
walther@59863
   160
type rrlsstate =  (* state for reverse rewriting, comments see type rule and scr | Rfuns *)
walther@59863
   161
  (term * term * Rule_Def.rule list list * (Rule_Def.rule * (term * term list)) list);
walther@59868
   162
val e_rrlsstate = (UnparseC.term_empty, UnparseC.term_empty, [[Rule_Def.Erule]], [(Rule_Def.Erule, (UnparseC.term_empty, []))]) : rrlsstate;
walther@59907
   163
local           
walther@59863
   164
    fun ii (_: term) = e_rrlsstate;
walther@59868
   165
    fun no (_: term) = SOME (UnparseC.term_empty, [UnparseC.term_empty]);
walther@59868
   166
    fun lo (_: Rule_Def.rule list list) (_: term) (_: Rule_Def.rule) = [(Rule_Def.Erule, (UnparseC.term_empty, [UnparseC.term_empty]))];
walther@59863
   167
    fun ne (_: Rule_Def.rule list list) (_: term) = SOME Rule_Def.Erule;
walther@59868
   168
    fun fo (_: Rule_Def.rule list list) (_: term) (_: term) = [(Rule_Def.Erule, (UnparseC.term_empty, [UnparseC.term_empty]))];
walther@59863
   169
in
walther@59863
   170
val e_rfuns = Rule_Def.Rfuns {init_state = ii, normal_form = no, locate_rule = lo,
walther@59863
   171
		  next_rule = ne, attach_form = fo};
walther@59863
   172
end;
walther@59863
   173
val e_rrls =
walther@59863
   174
  Rule_Def.Rrls {id = "e_rrls", prepat = [], rew_ord = ("dummy_ord", dummy_ord), erls = Rule_Def.Empty,
walther@59863
   175
    calc = [], errpatts = [], scr = e_rfuns}
walther@59863
   176
(*\------- this will disappear eventually -----------/*)
walther@59863
   177
walther@59850
   178
walther@59850
   179
(**)end(**)