src/Tools/isac/BaseDefinitions/rule-def.sml
author wneuper <Walther.Neuper@jku.at>
Thu, 04 Aug 2022 12:48:37 +0200
changeset 60509 2e0b7ca391dc
parent 60504 8cc1415b3530
child 60516 795d1352493a
permissions -rw-r--r--
polish naming in Rewrite_Order
walther@59866
     1
(* Title:  BaseDefinitions/rule-def.sml
walther@59849
     2
   Author: Walther Neuper
walther@59849
     3
   (c) due to copyright terms
walther@59849
     4
walther@59919
     5
Here is a minimum of code required for Know_Store.thy.
walther@59919
     6
For main code see structure Rule.
walther@59849
     7
*)
walther@59849
     8
signature RULE_DEFINITION =
walther@59850
     9
sig
walther@59850
    10
  type calc
walther@59850
    11
  eqtype calID
walther@59850
    12
  eqtype errpatID
Walther@60509
    13
Walther@60509
    14
  type rew_ord_id = string
Walther@60509
    15
  type rew_ord_function = LibraryC.subst -> term * term -> bool
Walther@60509
    16
  type rew_ord_T = rew_ord_id * rew_ord_function
Walther@60509
    17
Walther@60504
    18
  type eval_fn = string -> term -> Proof.context -> (string * term) option
walther@59849
    19
walther@59851
    20
  datatype rule_set =
walther@59851
    21
       Empty
walther@59851
    22
     | Repeat of {calc: calc list, erls: rule_set, errpatts: errpatID list, id: string,
Walther@60509
    23
       preconds: term list, rew_ord: rew_ord_T, rules: rule list, scr: program, srls: rule_set}
walther@59878
    24
     | Sequence of {calc: calc list, erls: rule_set, errpatts: errpatID list, id: string,
Walther@60509
    25
       preconds: term list, rew_ord: rew_ord_T, rules: rule list, scr: program, srls: rule_set}
walther@59851
    26
     | Rrls of {calc: calc list, erls: rule_set, errpatts: errpatID list, id: string,
Walther@60509
    27
       prepat: (term list * term) list, rew_ord: rew_ord_T, scr: program}
walther@59878
    28
  and rule = Cal1 of string * eval_fn | Eval of string * eval_fn | Erule
walther@59851
    29
     | Rls_ of rule_set | Thm of string * thm
walther@59850
    30
  and program =
walther@59878
    31
       Empty_Prog
walther@59850
    32
     | Prog of term
walther@59850
    33
     | Rfuns of
walther@59850
    34
         {attach_form: rule list list -> term -> term -> (rule * (term * term list)) list,
walther@59850
    35
          init_state: term -> term * term * rule list list * (rule * (term * term list)) list,
walther@59850
    36
          locate_rule: rule list list -> term -> rule -> (rule * (term * term list)) list,
walther@59850
    37
          next_rule: rule list list -> term -> rule option, normal_form: term ->
walther@59850
    38
          (term * term list) option}
Walther@60440
    39
  val program_to_string: program -> string
walther@59850
    40
end
walther@59849
    41
walther@59849
    42
(**)
walther@59849
    43
structure Rule_Def(**): RULE_DEFINITION(**) =
walther@59849
    44
struct
walther@59849
    45
(**)
walther@59849
    46
Walther@60504
    47
type eval_fn = string -> term -> Proof.context -> (string * term) option;
walther@59849
    48
Walther@60509
    49
type rew_ord_id = string;
Walther@60509
    50
type rew_ord_function = LibraryC.subst -> term * term -> bool;
Walther@60509
    51
type rew_ord_T = rew_ord_id * rew_ord_function;
walther@59849
    52
walther@59850
    53
type calID = string;
walther@59849
    54
type cal = calID * eval_fn;
walther@59849
    55
type prog_calcID = string;
walther@59849
    56
type calc = (prog_calcID * cal);
walther@59849
    57
walther@59849
    58
(* error patterns and fill patterns *)
walther@59849
    59
type errpatID = string
walther@59849
    60
walther@59849
    61
(* all these different data justify a step in a calculation *)
walther@59849
    62
datatype rule =
walther@59869
    63
    Erule             (* the empty rule                                               *)
walther@59869
    64
  | Thm of            (* theorem associated with the identifier for reflection        *)
walther@60370
    65
      string *        (* required for ThmC.sym_thm                                    *)
walther@59869
    66
        Thm.thm       (* see TODO CLEANUP Thm                                         *)
wenzelm@60293
    67
  | Eval of           (* evaluation by rewriting                                      *)
walther@59869
    68
      string *        (* unique identifier for the evaluation function                *)
walther@59869
    69
  	    eval_fn       (* evaluation function in ML code                               *)
walther@59869
    70
  | Cal1 of string *  (* applied once to whole term or lhs/rhs of an eqality          *)
walther@59869
    71
  	  eval_fn         (* evaluation function in ML code                               *)
walther@59869
    72
  | Rls_ of rule_set  (* rule sets may be nested                                      *)
walther@59869
    73
and program =         (* program for Lucas-Interpretation                             *)
wenzelm@60293
    74
    Empty_Prog        (* DEPRECATED: needless after removal of Rfuns                  *)
walther@59869
    75
  | Prog of term      (* Lucas-Interpretation works on Isabelle's terms               *)
walther@59869
    76
  | Rfuns of          (* DEPRECATED, replaced by Auto_Prog                            *)
walther@59869
    77
    {init_state :     (* initialise for reverse rewriting by the Interpreter          *)
walther@59849
    78
      term ->         (* for this the rrlsstate is initialised:                       *)
walther@59869
    79
      term *          (* the current formula via istate                               *)
walther@59849
    80
      term *          (* the final formula                                            *)
walther@59849
    81
      rule list       (* of reverse rewrite set (#1#)                                 *)
walther@59849
    82
        list *        (*   may be serveral, eg. in norm_rational                      *)
walther@59878
    83
      ( rule *        (* Thm (+ Thm generated from Eval) resulting in ...         *)
walther@59849
    84
        (term *       (*   ... rewrite with ...                                       *)
walther@59849
    85
        term list))   (*   ... assumptions                                            *)
walther@59869
    86
      list,           (* derivation from given term to normalform in reverse order    *)  
walther@59869
    87
	  normal_form:      (* the function which drives the Rrls                           *)
walther@59849
    88
	    term -> (term * term list) option,
walther@59869
    89
	  locate_rule:      (* checks a rule R for being a cancel-rule                      *)
walther@59849
    90
	    rule list list -> (* the reverse rule list                                      *)
walther@59849
    91
	    term ->         (* ... to which the rule shall be applied                       *)
walther@59849
    92
	    rule ->         (* ... to be applied to term                                    *)
walther@59849
    93
	    ( rule *        (* value: a rule rewriting to ...                               *)
walther@59849
    94
	      (term *       (*   ... the resulting term ...                                 *)
walther@59849
    95
	      term list))   (*   ... with the assumptions ( //#0)                           *)
walther@59869
    96
	    list,           (*   there may be several such rules                            *)
walther@59869
    97
	  next_rule:        (* for a given term return the next rules to be done for cancel *)
walther@59849
    98
	    rule list list->(* the reverse rule list                                        *)
walther@59849
    99
	    term ->         (* the term for which ...                                       *)
walther@59869
   100
	    rule option,    (* ... this rule is appropriate for cancellation                *)
walther@59869
   101
	  attach_form:      (* checks an input term TI, if it may belong to a current cancel*)
walther@59869
   102
	    rule list list->
walther@59849
   103
	    term ->         (* TG, the last one agreed upon by user + math-eng              *)
walther@59849
   104
	    term ->         (* TI, the next one input by the user                           *)
walther@59849
   105
	    ( rule *        (* the rule to be applied in order to reach TI                  *) 
walther@59849
   106
	      (term *       (* ... obtained by applying the rule ...                        *)
walther@59849
   107
	      term list))   (* ... and the respective assumptions                           *) 
walther@59869
   108
	    list}           (* there may be several such rules; the list is empty, if ...   *)
walther@59869
   109
and rule_set =        (* a collection for ordered & conditional rewriting             *)
walther@59869
   110
    Empty             (* empty Rule_Set.T                                             *)
walther@59869
   111
  | Repeat of         (*a confluent and terminating ruleset, in principle             *)
walther@59869
   112
    {id: string,      (* for trace_rewrite                                            *)
walther@59869
   113
     preconds: term list, (* STILL UNUSED                                             *)
Walther@60509
   114
     rew_ord: rew_ord_T,(* for rules                                                  *)                                             
walther@59869
   115
     erls: rule_set,  (* for the conditions in rules                                  *)
walther@59869
   116
     srls: rule_set,  (* for evaluation of Prog_Expr                                  *)
walther@59869
   117
     calc: calc list, (* for Calculate in program, set by prep_rls'                   *)
walther@59869
   118
     rules: rule list,
walther@59869
   119
     errpatts: errpatID list,(* by dialog-authoring in Build_Thydata.thy              *)
walther@59869
   120
     scr: program}    (* DEPRECATED: intersteps are created on the fly now            *)
wenzelm@60293
   121
  | Sequence of       (* a sequence of rules to be tried only once                    *)
Walther@60509
   122
    {id: string, preconds: term list, rew_ord: rew_ord_T, erls: rule_set, srls: rule_set,
walther@59869
   123
     calc: calc list, rules: rule list, errpatts: errpatID list, scr: program}
walther@59869
   124
  | Rrls of           (* ML-functions within rewriting, _ONE_ redex rewritten per call*)
walther@59869
   125
    {id: string,      (* for trace_rewrite                                            *)
walther@59869
   126
     prepat:          (* a guard which guarantees a redex                             *)
walther@59869
   127
       (term list *   (* preconds, eval with subst from pattern below                 *)
walther@59869
   128
		     term)        (* pattern matched with current (sub)term                       *)
walther@59869
   129
		   list,          (* meta-conjunction in guarding is or                           *)
Walther@60509
   130
     rew_ord: rew_ord_T, erls: rule_set, calc: calc list, errpatts: errpatID list, scr: program}
walther@59850
   131
Walther@60440
   132
fun program_to_string Empty_Prog = "Empty_Prog"
Walther@60440
   133
  | program_to_string (Prog s) = "Prog " ^ UnparseC.term s
Walther@60440
   134
  | program_to_string (Rfuns _)  = "Rfuns";
Walther@60436
   135
walther@59849
   136
(**)end(**)
walther@59849
   137