src/Tools/isac/CalcElements/rule-def.sml
author Walther Neuper <walther.neuper@jku.at>
Mon, 06 Apr 2020 11:44:36 +0200
changeset 59851 4dd533681fef
parent 59850 f3cac3053e7b
child 59857 cbb3fae0381d
permissions -rw-r--r--
use "Rule_Set" for shorter identifiers
walther@59849
     1
(* Title:  CalcElements/rule-def.sml
walther@59849
     2
   Author: Walther Neuper
walther@59849
     3
   (c) due to copyright terms
walther@59849
     4
walther@59850
     5
A minimum of code for a mutual recursive datatype definition
walther@59850
     6
awaiting individual specification later.
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@59850
    13
  type rew_ord
walther@59850
    14
  type rew_ord'
walther@59850
    15
  type rew_ord_
walther@59850
    16
  type eval_fn
walther@59849
    17
walther@59851
    18
  datatype rule_set =
walther@59851
    19
       Empty
walther@59851
    20
     | Repeat of {calc: calc list, erls: rule_set, errpatts: errpatID list, id: string,
walther@59851
    21
       preconds: term list, rew_ord: rew_ord, rules: rule list, scr: program, srls: rule_set}
walther@59851
    22
     | Seqence of {calc: calc list, erls: rule_set, errpatts: errpatID list, id: string,
walther@59851
    23
       preconds: term list, rew_ord: rew_ord, rules: rule list, scr: program, srls: rule_set}
walther@59851
    24
     | Rrls of {calc: calc list, erls: rule_set, errpatts: errpatID list, id: string,
walther@59850
    25
       prepat: (term list * term) list, rew_ord: rew_ord, scr: program}
walther@59850
    26
  and rule = Cal1 of string * eval_fn | Num_Calc of string * eval_fn | Erule
walther@59851
    27
     | Rls_ of rule_set | Thm of string * thm
walther@59850
    28
  and program =
walther@59850
    29
       EmptyScr
walther@59850
    30
     | Prog of term
walther@59850
    31
     | Rfuns of
walther@59850
    32
         {attach_form: rule list list -> term -> term -> (rule * (term * term list)) list,
walther@59850
    33
          init_state: term -> term * term * rule list list * (rule * (term * term list)) list,
walther@59850
    34
          locate_rule: rule list list -> term -> rule -> (rule * (term * term list)) list,
walther@59850
    35
          next_rule: rule list list -> term -> rule option, normal_form: term ->
walther@59850
    36
          (term * term list) option}
walther@59849
    37
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59849
    38
  (*NONE*)
walther@59849
    39
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59849
    40
  (*NONE*)
walther@59849
    41
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
walther@59850
    42
end
walther@59849
    43
walther@59849
    44
(**)
walther@59849
    45
structure Rule_Def(**): RULE_DEFINITION(**) =
walther@59849
    46
struct
walther@59849
    47
(**)
walther@59849
    48
walther@59850
    49
walther@59849
    50
(* eval function calling sml code during rewriting.
walther@59849
    51
Unifying "type cal" and "type calc" would make Lucas-Interpretation more efficient,
walther@59849
    52
  see "fun rule2stac": instead of 
walther@59849
    53
    Num_Calc: calID * eval_fn -> rule
walther@59849
    54
  would be better
walther@59849
    55
    Num_Calc: prog_calcID * (calID * eval_fn)) -> rule*)
walther@59849
    56
type eval_fn = (string -> term -> theory -> (string * term) option);
walther@59849
    57
walther@59849
    58
type rew_ord' = string;
walther@59850
    59
type rew_ord_ = (term * term) list -> term * term -> bool;
walther@59849
    60
type rew_ord = rew_ord' * rew_ord_;
walther@59849
    61
walther@59850
    62
type calID = string;
walther@59849
    63
type cal = calID * eval_fn;
walther@59849
    64
type prog_calcID = string;
walther@59849
    65
type calc = (prog_calcID * cal);
walther@59849
    66
walther@59849
    67
(* error patterns and fill patterns *)
walther@59849
    68
type errpatID = string
walther@59849
    69
walther@59849
    70
(* all these different data justify a step in a calculation *)
walther@59849
    71
datatype rule =
walther@59849
    72
    Erule                (*.the empty rule                     .*)
walther@59849
    73
  | Thm of (string * Basic_Thm.thm) (* see TODO CLEANUP Thm     *)
walther@59849
    74
  | Num_Calc of string *     (*.sml-code manipulating a (sub)term  .*)
walther@59849
    75
  	  eval_fn
walther@59849
    76
  | Cal1 of string *     (*.sml-code applied only to whole term
walther@59849
    77
                            or left/right-hand-side of eqality .*)
walther@59849
    78
  	  eval_fn
walther@59851
    79
  | Rls_ of rule_set          (*.ie. rule sets may be nested.*)
walther@59849
    80
and program =
walther@59849
    81
    EmptyScr
walther@59849
    82
  | Prog of term (* a leaf is either a tactic or an 'exp' in 'let v = expr' 
walther@59849
    83
                    where 'exp' does not contain a tactic. *)
walther@59849
    84
  | Rfuns of     (* for Rrls, usage see rational.sml ----- reverse rewrite -----      *)
walther@59849
    85
    {init_state : (* initialise for reverse rewriting by the Interpreter              *)
walther@59849
    86
      term ->         (* for this the rrlsstate is initialised:                       *)
walther@59849
    87
      term *          (* the current formula:                                         *)
walther@59849
    88
                      (* goes locate_input_tactic -> find_next_step via istate        *)
walther@59849
    89
      term *          (* the final formula                                            *)
walther@59849
    90
      rule list       (* of reverse rewrite set (#1#)                                 *)
walther@59849
    91
        list *        (*   may be serveral, eg. in norm_rational                      *)
walther@59849
    92
      ( rule *        (* Thm (+ Thm generated from Num_Calc) resulting in ...             *)
walther@59849
    93
        (term *       (*   ... rewrite with ...                                       *)
walther@59849
    94
        term list))   (*   ... assumptions                                            *)
walther@59849
    95
      list,           (* derivation from given term to normalform
walther@59849
    96
                         in reverse order with sym_thm;
walther@59849
    97
                                                (#1#) could be extracted from here #1 *)  
walther@59849
    98
	  normal_form:  (* the function which drives the Rrls ##############################*)
walther@59849
    99
	    term -> (term * term list) option,
walther@59849
   100
	  locate_rule:  (* checks a rule R for being a cancel-rule, and if it is,
walther@59849
   101
                     then return the list of rules (+ the terms they are rewriting to)
walther@59849
   102
                     which need to be applied before R should be applied.
walther@59849
   103
                     precondition: the rule is applicable to the argument-term.       *)
walther@59849
   104
	    rule list list -> (* the reverse rule list                                      *)
walther@59849
   105
	    term ->         (* ... to which the rule shall be applied                       *)
walther@59849
   106
	    rule ->         (* ... to be applied to term                                    *)
walther@59849
   107
	    ( rule *        (* value: a rule rewriting to ...                               *)
walther@59849
   108
	      (term *       (*   ... the resulting term ...                                 *)
walther@59849
   109
	      term list))   (*   ... with the assumptions ( //#0)                           *)
walther@59849
   110
	    list,           (*   there may be several such rules; the list is empty,          
walther@59849
   111
                           if the rule has nothing to do with e.g. cancelation        *)
walther@59849
   112
	  next_rule:    (* for a given term return the next rules to be done for cancelling *)
walther@59849
   113
	    rule list list->(* the reverse rule list                                        *)
walther@59849
   114
	    term ->         (* the term for which ...                                       *)
walther@59849
   115
	    rule option,    (* ... this rule is appropriate for cancellation;
walther@59849
   116
		                     there may be no such rule (if the term is eg.canceled already*)
walther@59849
   117
	  attach_form:  (* checks an input term TI, if it may belong to e.g. a current 
walther@59849
   118
                     cancellation, by trying to derive it from the given term TG.     
walther@59849
   119
                     NOT IMPLEMENTED                                                  *)
walther@59849
   120
	    rule list list->(**)
walther@59849
   121
	    term ->         (* TG, the last one agreed upon by user + math-eng              *)
walther@59849
   122
	    term ->         (* TI, the next one input by the user                           *)
walther@59849
   123
	    ( rule *        (* the rule to be applied in order to reach TI                  *) 
walther@59849
   124
	      (term *       (* ... obtained by applying the rule ...                        *)
walther@59849
   125
	      term list))   (* ... and the respective assumptions                           *) 
walther@59849
   126
	    list}           (* there may be several such rules; the list is empty, if the 
walther@59849
   127
                         users term does not belong to e.g. a cancellation of the term 
walther@59849
   128
                         last agreed upon.                                            *)
walther@59851
   129
and rule_set =
walther@59851
   130
    Empty 
walther@59851
   131
  | Repeat of (*a confluent and terminating ruleset, in general                         *)
walther@59849
   132
    {id : string,          (*for trace_rewrite:=true                                 *)
walther@59849
   133
     preconds : term list, (*unused WN020820                                         *)
walther@59849
   134
     (*WN060616 for efficiency...                                                    
walther@59849
   135
      bdvs    : false,       (*set in prep_rls' for get_bdvs *)*)                    
walther@59849
   136
     rew_ord  : rew_ord,   (*for rules*)                                             
walther@59851
   137
     erls     : rule_set,       (*for the conditions in rules                             *)
walther@59851
   138
     srls     : rule_set,       (*for evaluation of list_fns in script                    *)
walther@59849
   139
     calc     : calc list, (*for Calculate in scr, set by prep_rls'                  *)
walther@59849
   140
     rules    : rule list,                                                           
walther@59849
   141
     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy                *)
walther@59849
   142
     scr      : program}   (*Prog term: generating intermed.steps  *)                
walther@59851
   143
  | Seqence of (*a sequence of rules to be tried only once                               *)
walther@59849
   144
    {id : string,          (*for trace_rewrite:=true                                 *)
walther@59849
   145
     preconds : term list, (*unused 20.8.02                                          *)
walther@59849
   146
     (*WN060616 for efficiency...                                                    
walther@59849
   147
      bdvs    : false,       (*set in prep_rls' for get_bdvs *)*)                    
walther@59849
   148
     rew_ord  : rew_ord,   (*for rules                                               *)
walther@59851
   149
     erls     : rule_set,       (*for the conditions in rules                             *)
walther@59851
   150
     srls     : rule_set,       (*for evaluation of list_fns in script                    *)
walther@59849
   151
     calc     : calc list, (*for Calculate in scr, set by prep_rls'                  *)
walther@59849
   152
     rules    : rule list,
walther@59849
   153
     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)
walther@59849
   154
     scr      : program}   (*Prog term  (how to restrict type ???)*)
walther@59849
   155
walther@59851
   156
  (*Rrls call SML-code and simulate an rule_set
walther@59849
   157
    difference: there is always _ONE_ redex rewritten in 1 call,
walther@59849
   158
    thus wrap Rrls by: Rls (Rls_ ...)*)
walther@59849
   159
  | Rrls of (* SML-functions within rewriting; step-wise execution provided;   
walther@59851
   160
               Rrls simulate an rule_set
walther@59849
   161
               difference: there is always _ONE_ redex rewritten in 1 call,
walther@59849
   162
               thus wrap Rrls by: Rls (Rls_ ...)                                     *)
walther@59849
   163
    {id : string,          (* for trace_rewrite := true                              *)
walther@59849
   164
     prepat  : (term list *(* preconds, eval with subst from pattern;                
walther@59849
   165
                              if [@{term True}], match decides alone                 *)
walther@59849
   166
		            term )     (* pattern matched with current (sub)term                 *)
walther@59849
   167
		   list,               (* meta-conjunction is or                                 *)
walther@59849
   168
     rew_ord  : rew_ord,   (* for rules                                              *)
walther@59851
   169
     erls     : rule_set,       (* for the conditions in rules and preconds               *)
walther@59849
   170
     calc     : calc list, (* for Calculate in scr, set automatic.in prep_rls'       *)
walther@59849
   171
     errpatts : errpatID list,(*dialog-authoring in Build_Thydata.thy*)              
walther@59849
   172
     scr      : program};      (* Rfuns {...}  (how to restrict type ???)                *)
walther@59849
   173
walther@59850
   174
walther@59849
   175
(**)end(**)
walther@59849
   176