src/Tools/isac/ProgLang/rewrite.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Sat, 24 Feb 2018 11:14:56 +0100
changeset 59381 cb7e75507c05
parent 59380 8b08d9296654
child 59382 364ce4699452
permissions -rw-r--r--
Rewrite: cleanup source file
neuper@37906
     1
(* isac's rewriter
neuper@37906
     2
   (c) Walther Neuper 2000
neuper@37906
     3
*)
neuper@37906
     4
wneuper@59380
     5
signature REWRITE =
wneuper@59380
     6
  sig
wneuper@59380
     7
    val assoc_thm': theory -> thm' -> thm
wneuper@59380
     8
    val assoc_thm'': theory -> thmID -> thm
wneuper@59380
     9
    val calculate_: theory -> string * eval_fn -> term -> (term * (string * thm)) option
wneuper@59380
    10
    val eval__true: theory -> int -> term list -> (term * term) list -> rls -> term list * bool
wneuper@59380
    11
    val eval_listexpr_: theory -> rls -> term -> term
wneuper@59380
    12
    val eval_true: theory -> term list -> rls -> bool
wneuper@59380
    13
    val eval_true_: theory' -> rls -> term -> bool
wneuper@59380
    14
    val rew_sub: theory -> int -> (term * term) list -> ((term * term) list -> term * term -> bool) -> rls -> bool -> lrd list -> term -> term -> term * term list * lrd list * bool
wneuper@59380
    15
    val rewrite_: theory -> ((term * term) list -> term * term -> bool) -> rls -> bool -> thm -> term -> (term * term list) option
wneuper@59380
    16
    val rewrite_inst_: theory -> ((term * term) list -> term * term -> bool) -> rls -> bool -> (term * term) list -> thm -> term -> (term * term list) option
wneuper@59380
    17
    val rewrite_set_: theory -> bool -> rls -> term -> (term * term list) option
wneuper@59380
    18
    val rewrite_set_inst_: theory -> bool -> (term * term) list -> rls -> term -> (term * term list) option
wneuper@59380
    19
    val rewrite_terms_: theory -> ((term * term) list -> term * term -> bool) -> rls -> term list -> term -> (term * term list) option
wneuper@59380
    20
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
wneuper@59380
    21
  (* NONE *)
wneuper@59380
    22
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
wneuper@59380
    23
  (* NONE *)
wneuper@59380
    24
(* probably shift up ---------------
wneuper@59380
    25
    exception NO_REWRITE
wneuper@59380
    26
    exception STOP_REW_SUB
wneuper@59380
    27
    val adhoc_thm': theory' -> string * eval_fn -> cterm' -> thm' option
wneuper@59380
    28
    val app_rev: theory -> int -> rls -> term -> term * term list * bool
wneuper@59380
    29
    val app_sub: theory -> int -> rls -> term -> term * term list * bool
wneuper@59380
    30
    val calculate: theory' -> string * eval_fn -> cterm' -> (string * thm') option
wneuper@59380
    31
    val convert: 'a -> string -> string
wneuper@59380
    32
    val convert_metaview_to_thmid: theory -> string -> thm
wneuper@59380
    33
    val eval_true': theory' -> rls' -> term -> bool
wneuper@59380
    34
    val get_rls_scr: rls' -> scr
wneuper@59380
    35
    val mk_thm: theory -> string -> thm
wneuper@59380
    36
    val mk_thm'': theory -> term -> thm
wneuper@59380
    37
    val parse': theory' -> cterm' -> cterm' option
wneuper@59380
    38
    val rewrite: theory' -> rew_ord' -> rls' -> bool -> thm' -> cterm' -> (string * string) option
wneuper@59380
    39
    val rewrite__: theory -> int -> (term * term) list -> ((term * term) list -> term * term -> bool) -> rls -> bool -> thm -> term -> (term * term list) option
wneuper@59380
    40
    val rewrite__set_: theory -> int -> bool -> (term * term) list -> rls -> term -> (term * term list) option
wneuper@59380
    41
    val rewrite_inst: theory' -> rew_ord' -> rls' -> bool -> 'a -> thm' -> cterm' -> (cterm' * cterm' list) option
wneuper@59380
    42
    val rewrite_set: theory' -> bool -> rls' -> cterm' -> (string * string) option
wneuper@59380
    43
    val rewrite_set_inst: theory' -> bool -> subs' -> rls' -> cterm' -> (string * string) option
wneuper@59380
    44
    val subs'2subst: theory -> subs' -> subst
wneuper@59380
    45
    val trace: int -> string -> unit
wneuper@59380
    46
    val trace1: int -> string -> unit
wneuper@59380
    47
--------------------------------------*)
wneuper@59380
    48
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59380
    49
  end
wneuper@59380
    50
wneuper@59380
    51
(**)
wneuper@59380
    52
structure Rewrite(**): REWRITE(**) =
wneuper@59380
    53
struct
wneuper@59380
    54
(**)
neuper@37906
    55
neuper@37906
    56
exception NO_REWRITE;
neuper@37906
    57
exception STOP_REW_SUB; (*WN050820 quick and dirty*)
neuper@37906
    58
neuper@52101
    59
fun trace i str = 
neuper@52101
    60
  if ! trace_rewrite andalso i < ! depth then tracing (idt "#" i ^ str) else ()
neuper@52101
    61
fun trace1 i str = 
neuper@52101
    62
  if ! trace_rewrite andalso i < ! depth then tracing (idt "#" (i + 1) ^ str) else ()
neuper@52101
    63
neuper@37906
    64
fun rewrite__ thy i bdv tless rls put_asm thm ct =
wneuper@59381
    65
  let
wneuper@59381
    66
    val(t', asms, _ (*lrd*), rew) = rew_sub thy i bdv tless rls put_asm [(*root of the term*)]
wneuper@59381
    67
		  (((inst_bdv bdv) o norm o #prop o Thm.rep_thm) thm) ct
wneuper@59381
    68
  in if rew then SOME (t', distinct asms) else NONE end
wneuper@59381
    69
and rew_sub thy i bdv tless rls put_asm lrd r t = 
neuper@38022
    70
  (let
wneuper@59381
    71
    val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r
wneuper@59381
    72
    (*?alternative Unify.matchers:
wneuper@59381
    73
      http://isabelle.in.tum.de/repos/isabelle/file/Isabelle2017/src/Pure/more_unify.ML*)
wneuper@59381
    74
    val r' = Envir.subst_term (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) r
wneuper@59381
    75
    val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'))
wneuper@59381
    76
    val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r'
wneuper@59381
    77
    val _ = if ! trace_rewrite andalso i < ! depth andalso p' <> []
wneuper@59381
    78
	    then tracing (idt "#" (i + 1) ^ " eval asms: " ^ term2str r') else ()
wneuper@59381
    79
    val (t'', p'') =                                                     (*conditional rewriting*)
wneuper@59381
    80
      let
wneuper@59381
    81
        val (simpl_p', nofalse) = eval__true thy (i + 1) p' bdv rls 	     
wneuper@59381
    82
	    in
wneuper@59381
    83
	      if nofalse
wneuper@59381
    84
        then
wneuper@59381
    85
          (if ! trace_rewrite andalso i < ! depth andalso p' <> []
wneuper@59381
    86
          then tracing (idt "#" (i + 1) ^ " asms accepted: " ^ terms2str p' ^
wneuper@59381
    87
  	        "   stored: " ^ terms2str simpl_p')
wneuper@59381
    88
  	      else();
wneuper@59381
    89
          (t',simpl_p'))                                               (* uncond.rew. from above*)
wneuper@59381
    90
        else 
wneuper@59381
    91
          (if ! trace_rewrite andalso i < ! depth 
wneuper@59381
    92
          then tracing (idt "#" (i + 1) ^ " asms false: " ^ terms2str p')
wneuper@59381
    93
          else();
wneuper@59381
    94
          raise STOP_REW_SUB (* don't go into subterms of cond *))
wneuper@59381
    95
	    end
wneuper@59381
    96
    in
wneuper@59381
    97
      if perm lhs rhs andalso not (tless bdv (t', t))                        (*ordered rewriting*)
wneuper@59381
    98
      then (if ! trace_rewrite andalso i < ! depth 
wneuper@59381
    99
  	    then tracing (idt"#"i ^ " not: \"" ^ term2str t ^ "\" > \"" ^ term2str t' ^ "\"")
wneuper@59381
   100
  	    else (); 
wneuper@59381
   101
  	    raise NO_REWRITE)
wneuper@59381
   102
  	  else (t'', p'', [], true)
wneuper@59381
   103
    end
wneuper@59381
   104
    ) handle Pattern.MATCH (*TODO Pattern.MATCH when tests are ready *) => 
wneuper@59381
   105
      (case t of
wneuper@59381
   106
        Const(s,T) => (Const(s,T),[],lrd,false)
neuper@37906
   107
      | Free(s,T) => (Free(s,T),[],lrd,false)
neuper@37906
   108
      | Var(n,T) => (Var(n,T),[],lrd,false)
neuper@37906
   109
      | Bound i => (Bound i,[],lrd,false)
neuper@37906
   110
      | Abs(s,T,body) => 
wneuper@59381
   111
        let val (t', asms, _ (*lrd*), rew) =  rew_sub thy i bdv tless rls put_asm (lrd @ [D]) r body
wneuper@59381
   112
    	   in (Abs(s, T, t'), asms, [], rew) end
neuper@37906
   113
      | t1 $ t2 => 
wneuper@59381
   114
    	   let val (t2', asm2, lrd, rew2) = rew_sub thy i bdv tless rls put_asm (lrd@[R]) r t2
wneuper@59381
   115
    	   in
wneuper@59381
   116
    	    if rew2 then (t1 $ t2', asm2, lrd, true)
wneuper@59381
   117
    	    else
wneuper@59381
   118
    	      let val (t1', asm1, lrd, rew1) = rew_sub thy i bdv tless rls put_asm (lrd@[L]) r t1
wneuper@59381
   119
    	      in if rew1 then (t1' $ t2, asm1, lrd, true) else (t1 $ t2,[], lrd, false) end
wneuper@59381
   120
  end)
wneuper@59381
   121
and eval__true thy i asms bdv rls =         (* simplify asumptions until one evaluates to false *)
neuper@48760
   122
  if asms = [@{term True}] orelse asms = [] then ([], true)
wneuper@59381
   123
  else (* this allows to check Rrls with prepat = ([@{term True}], pat) *)
wneuper@59381
   124
    if asms = [@{term False}] then ([], false)
wneuper@59381
   125
    else
wneuper@59381
   126
      let                            
wneuper@59381
   127
        fun chk indets [] = (indets, true) (*return asms<>True until false*)
wneuper@59381
   128
          | chk indets (a :: asms) =
wneuper@59381
   129
            (case rewrite__set_ thy (i + 1) false bdv rls a of
wneuper@59381
   130
              NONE => (chk (indets @ [a]) asms)
wneuper@59381
   131
            | SOME (t, a') =>
wneuper@59381
   132
              if t = @{term True} then (chk (indets @ a') asms) 
wneuper@59381
   133
              else if t = @{term False} then ([], false)
wneuper@59381
   134
            (*asm false .. thm not applied ^^^; continue until False vvv*)
wneuper@59381
   135
            else chk (indets @ [t] @ a') asms);
wneuper@59381
   136
      in chk [] asms end
wneuper@59381
   137
and rewrite__set_ _ _ __ Erls t =                                    (* rewrite with a rule set *)
neuper@52101
   138
    error ("rewrite__set_ called with 'Erls' for '" ^ term2str t ^ "'")
wneuper@59381
   139
  | rewrite__set_ thy i _ _ (rrls as Rrls _) t =           (* rewrite with a 'reverse rule set' *)
neuper@52101
   140
    let
neuper@52101
   141
      val _= trace i (" rls: " ^ id_rls rrls ^ " on: " ^ term2str t)
neuper@52101
   142
	    val (t', asm, rew) = app_rev thy (i + 1) rrls t
neuper@52101
   143
    in if rew then SOME (t', distinct asm) else NONE end
wneuper@59381
   144
  | rewrite__set_ thy i put_asm bdv rls ct =        (* rule set containing Thms or Calculations *)
neuper@52101
   145
    let
neuper@52101
   146
      datatype switch = Appl | Noap;
wneuper@59381
   147
      fun rew_once _ asm ct Noap [] = (ct, asm) (* ?TODO unify with rewtools.sml *)
neuper@52101
   148
        | rew_once ruls asm ct Appl [] = 
neuper@52101
   149
          (case rls of Rls _ => rew_once ruls asm ct Noap ruls
wneuper@59381
   150
          | Seq _ => (ct, asm)
wneuper@59381
   151
          | rls => raise ERROR ("rew_once not appl. to \"" ^ rls2str rls ^ "\""))
wneuper@59381
   152
        | rew_once ruls asm ct apno (rul :: thms) =
neuper@52101
   153
          case rul of
neuper@52101
   154
            Thm (thmid, thm) =>
neuper@52101
   155
              (trace1 i (" try thm: " ^ thmid);
neuper@52101
   156
              case rewrite__ thy (i + 1) bdv ((snd o #rew_ord o rep_rls) rls)
neuper@52101
   157
                  ((#erls o rep_rls) rls) put_asm thm ct of
neuper@52101
   158
                NONE => rew_once ruls asm ct apno thms
wneuper@59381
   159
              | SOME (ct', asm') => 
neuper@52101
   160
                (trace1 i (" rewrites to: " ^ term2str ct');
neuper@52101
   161
                rew_once ruls (union (op =) asm asm') ct' Appl (rul::thms)))
neuper@52101
   162
          | Calc (cc as (op_, _)) => 
neuper@52101
   163
            let val _= trace1 i (" try calc: " ^ op_ ^ "'")
neuper@52101
   164
              val ct = uminus_to_string ct
wneuper@59255
   165
            in case adhoc_thm thy cc ct of
neuper@52101
   166
                NONE => rew_once ruls asm ct apno thms
wneuper@59381
   167
              | SOME (_, thm') => 
neuper@52101
   168
                let 
neuper@52101
   169
                  val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o rep_rls) rls)
neuper@52101
   170
                    ((#erls o rep_rls) rls) put_asm thm' ct;
neuper@52101
   171
                  val _ = if pairopt <> NONE then () else error ("rewrite_set_, rewrite_ \"" ^ 
neuper@52101
   172
                    string_of_thmI thm' ^ "\" " ^ term2str ct ^ " = NONE")
neuper@52101
   173
                  val _ = trace1 i (" calc. to: " ^ term2str ((fst o the) pairopt))
neuper@52101
   174
                in rew_once ruls asm ((fst o the) pairopt) Appl (rul :: thms) end
neuper@52101
   175
            end
neuper@52101
   176
          | Cal1 (cc as (op_, _)) => 
neuper@52101
   177
            let val _= trace1 i (" try cal1: " ^ op_ ^ "'");
neuper@52101
   178
              val ct = uminus_to_string ct
wneuper@59255
   179
            in case adhoc_thm1_ thy cc ct of
neuper@52101
   180
                NONE => (ct, asm)
wneuper@59381
   181
              | SOME (_, thm') =>
neuper@52101
   182
                let 
neuper@52101
   183
                  val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o rep_rls) rls)
neuper@52101
   184
                    ((#erls o rep_rls) rls) put_asm thm' ct;
neuper@52101
   185
                  val _ = if pairopt <> NONE then () else error ("rewrite_set_, rewrite_ \"" ^
neuper@52101
   186
                     string_of_thmI thm' ^ "\" " ^ term2str ct ^ " = NONE")
neuper@52101
   187
                  val _ = trace1 i (" cal1. to: " ^ term2str ((fst o the) pairopt))
neuper@52101
   188
                in the pairopt end
neuper@52101
   189
            end
neuper@52101
   190
          | Rls_ rls' => 
neuper@52101
   191
            (case rewrite__set_ thy (i + 1) put_asm bdv rls' ct of
neuper@52101
   192
              SOME (t', asm') => rew_once ruls (union (op =) asm asm') t' Appl thms
wneuper@59381
   193
            | NONE => rew_once ruls asm ct apno thms)
wneuper@59381
   194
          | r => raise ERROR ("rew_once not appl. to \"" ^ rule2str r ^ "\"");
neuper@52101
   195
      val ruls = (#rules o rep_rls) rls;
wneuper@59381
   196
      val _ = trace i (" rls: " ^ id_rls rls ^ " on: " ^ term2str ct)
neuper@52101
   197
      val (ct', asm') = rew_once ruls [] ct Noap ruls;
neuper@52101
   198
	  in if ct = ct' then NONE else SOME (ct', distinct asm') end
wneuper@59381
   199
and app_rev thy i rrls t =            (* apply an Rrls; if not applicable proceed with subterms *)
neuper@52085
   200
  let (* check a (precond, pattern) of a rev-set; stops with 1st true *)
wneuper@59381
   201
    fun chk_prepat _ _ [] _ = true
neuper@52085
   202
      | chk_prepat thy erls prepat t =
neuper@52085
   203
        let
neuper@52085
   204
          fun chk (pres, pat) =
neuper@52085
   205
            (let 
neuper@52085
   206
              val subst: Type.tyenv * Envir.tenv =
neuper@52085
   207
                Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty)
neuper@52085
   208
             in
neuper@52085
   209
              snd (eval__true thy (i + 1) (map (Envir.subst_term subst) pres) [] erls)
wneuper@59381
   210
             end) handle _ (*TODO Pattern.MATCH*) => false
wneuper@59381
   211
           fun scan_ _ [] = false
neuper@52085
   212
             | scan_ f (pp::pps) =
neuper@52085
   213
               if f pp then true else scan_ f pps;
neuper@52085
   214
        in scan_ chk prepat end;
neuper@52085
   215
    (* apply the normal_form of a rev-set *)
neuper@52101
   216
    fun app_rev' thy (Rrls {erls, prepat, scr = Rfuns {normal_form, ...}, ...}) t =
wneuper@59381
   217
      if chk_prepat thy erls prepat t then normal_form t else NONE
wneuper@59381
   218
      | app_rev' _ r _ = raise ERROR ("app_rev' not appl. to \"" ^ rls2str r ^ "\"");
neuper@52085
   219
    val opt = app_rev' thy rrls t
neuper@52085
   220
  in
neuper@52085
   221
    case opt of
neuper@52085
   222
      SOME (t', asm) => (t', asm, true)
neuper@52085
   223
    | NONE => app_sub thy i rrls t
neuper@52085
   224
  end
wneuper@59381
   225
and app_sub thy i rrls t =                                         (* apply an Rrls to subterms *)
neuper@52085
   226
  case t of
neuper@52085
   227
    Const (s, T) => (Const(s, T), [], false)
neuper@52085
   228
  | Free (s, T) => (Free(s, T), [], false)
neuper@52085
   229
  | Var (n, T) => (Var(n, T), [], false)
neuper@52085
   230
  | Bound i => (Bound i, [], false)
neuper@52085
   231
  | Abs (s, T, body) => 
neuper@37906
   232
	  let val (t', asm, rew) = app_rev thy i rrls body
neuper@37906
   233
	  in (Abs(s, T, t'), asm, rew) end
neuper@52085
   234
  | t1 $ t2 => 
neuper@52085
   235
    let val (t2', asm2, rew2) = app_rev thy i rrls t2
neuper@52085
   236
    in
neuper@52085
   237
      if rew2 then (t1 $ t2', asm2, true)
neuper@52085
   238
      else
neuper@52085
   239
        let val (t1', asm1, rew1) = app_rev thy i rrls t1
neuper@52085
   240
        in if rew1 then (t1' $ t2, asm1, true)
neuper@52085
   241
           else (t1 $ t2, [], false)
neuper@52085
   242
        end
wneuper@59381
   243
    end;
neuper@37906
   244
wneuper@59381
   245
(* rewriting without argument [] for rew_ord;  WN110603: shouldnt asm<>[] lead to false? *)
neuper@37906
   246
fun eval_true thy terms rls = (snd o (eval__true thy 1 terms [])) rls;
neuper@37906
   247
neuper@52101
   248
(* rewriting without internal argument [] *)
neuper@52101
   249
fun rewrite_ thy rew_ord erls bool thm term = rewrite__ thy 1 [] rew_ord erls bool thm term;
neuper@52101
   250
fun rewrite_set_ thy bool rls term = rewrite__set_ thy 1 bool [] rls term;
neuper@37906
   251
wneuper@59381
   252
(* variants of rewrite; TODO del. put_asm *)
wneuper@59381
   253
fun rewrite_inst_  thy rew_ord rls put_asm subst thm ct =
wneuper@59381
   254
  rewrite__ thy 1 subst rew_ord rls put_asm thm ct;
wneuper@59381
   255
fun rewrite_set_inst_ thy put_asm subst rls ct = rewrite__set_ thy 1 put_asm subst rls ct;
neuper@37906
   256
neuper@38025
   257
(* given a list of equalities (lhs = rhs) and a term, 
neuper@38025
   258
   replace all occurrences of lhs in the term with rhs;
neuper@38025
   259
   thus the order or equalities matters: put variables in lhs first. *)
neuper@38025
   260
fun rewrite_terms_ thy ord erls equs t =
neuper@42359
   261
  let
neuper@42359
   262
	  fun rew_ (t', asm') [] _ = (t', asm')
neuper@42359
   263
	    | rew_ (t', asm') (rules as r::rs) t =
neuper@42359
   264
	        let
wneuper@59381
   265
	          val (t'', asm'', _(*lrd*), rew) = rew_sub thy 1 [] ord erls false [] (Trueprop $ r) t
neuper@42359
   266
	        in 
neuper@42359
   267
	          if rew 
neuper@42359
   268
	          then rew_ (t'', asm' @ asm'') rules t''
neuper@42359
   269
	          else rew_ (t', asm') rs t'
neuper@42359
   270
	        end
neuper@42359
   271
	  val (t'', asm'') = rew_ (e_term, []) equs t
neuper@42359
   272
    in if t'' = e_term then NONE else SOME (t'', asm'')
neuper@42359
   273
    end;
neuper@37906
   274
wneuper@59381
   275
(* search ct for adjacent numerals and calculate them by operator isa_fn *)
neuper@37906
   276
fun calculate_ thy isa_fn ct =
neuper@37906
   277
  let val ct = uminus_to_string ct
wneuper@59255
   278
    in case adhoc_thm thy isa_fn ct of
neuper@37906
   279
	   NONE => NONE
wneuper@59381
   280
	 | SOME (thmID, thm) =>
wneuper@59381
   281
	   (let val rew = case rewrite_ thy dummy_ord e_rls false thm ct of
wneuper@59381
   282
         SOME (rew, _) => rew
wneuper@59381
   283
       | NONE => raise ERROR ""
wneuper@59381
   284
     in SOME (rew, (thmID, thm)) end)
wneuper@59381
   285
	   handle _ (*TODO Pattern.MATCH ?del?*)=> error ("calculate_: " ^ thmID ^ " does not rewrite")
neuper@37906
   286
  end;
neuper@37906
   287
wneuper@59381
   288
(* Thm.make_thm added to Pure/thm.ML *)
neuper@37906
   289
fun mk_thm thy str = 
wneuper@59186
   290
    let val t = (Thm.term_of o the o (parse thy)) str
neuper@37906
   291
	val t' = case t of
neuper@37906
   292
		     Const ("==>",_) $ _ $ _ => t
neuper@37906
   293
		   | _ => Trueprop $ t
wneuper@59185
   294
    in Thm.make_thm (Thm.global_cterm_of thy t') end;
neuper@37906
   295
wneuper@59108
   296
(* "metaview" as seen from programs and from user input (both are parsed like terms presently) *)
wneuper@59108
   297
fun convert_metaview_to_thmid thy thmid =
wneuper@59108
   298
  let val thmid' = case thmid of
wneuper@59108
   299
      "add_commute" => "add.commute"
wneuper@59108
   300
    | "mult_commute" => "mult.commute"
wneuper@59108
   301
    | "add_left_commute" => "add.left_commute"
wneuper@59108
   302
    | "mult_left_commute" => "mult.left_commute"
wneuper@59108
   303
    | "add_assoc" => "add.assoc"
wneuper@59108
   304
    | "mult_assoc" => "mult.assoc"
wneuper@59108
   305
    | _ => thmid
wneuper@59108
   306
  in (Global_Theory.get_thm thy) thmid' end;
wneuper@59108
   307
wneuper@59381
   308
(* get the theorem associated with the xstring-identifier;
neuper@37906
   309
   if the identifier starts with "sym_" then swap lhs = rhs around =
neuper@37906
   310
   (ATTENTION: "RS sym" attaches a [.] -- remove it with string_of_thmI);
neuper@37906
   311
   identifiers starting with "#" come from Calc and
wneuper@59381
   312
   get a hand-made theorem (containing numerals only) *)
wneuper@59381
   313
fun assoc_thm'' thy thmid =
wneuper@59253
   314
  case Symbol.explode thmid of
wneuper@59253
   315
    "s"::"y"::"m"::"_"::"#"::_ => error ("assoc_thm'' not impl.for " ^ thmid)
wneuper@59253
   316
  | "s"::"y"::"m"::"_"::id => ((num_str o (Global_Theory.get_thm thy)) (implode id)) RS sym
wneuper@59253
   317
  | "#"::_ => error ("assoc_thm'' not impl.for " ^ thmid)
wneuper@59253
   318
  | _ => thmid |> convert_metaview_to_thmid thy |> num_str
wneuper@59381
   319
fun assoc_thm' thy (thmid, ct') =
neuper@55487
   320
  (case Symbol.explode thmid of
neuper@55487
   321
    "s"::"y"::"m"::"_"::id => 
neuper@55487
   322
      if hd id = "#" 
neuper@55487
   323
      then mk_thm thy ct'
neuper@55487
   324
      else ((num_str o (Global_Theory.get_thm thy)) (implode id)) RS sym
wneuper@59109
   325
  | id =>
neuper@55487
   326
    if hd id = "#" 
neuper@55487
   327
    then mk_thm thy ct'
wneuper@59109
   328
    else thmid |> convert_metaview_to_thmid thy |> num_str
wneuper@59381
   329
  ) handle _ (*TODO: find exn behind ERROR: Undefined fact: "add_commute"*) => 
neuper@55487
   330
    error ("assoc_thm': \"" ^ thmid ^ "\" not in \"" ^ theory2domID thy ^ "\" (and parents)")
neuper@37906
   331
neuper@37906
   332
fun eval_listexpr_ thy srls t =
neuper@52139
   333
  let val rew = rewrite_set_ thy false srls t;
neuper@52139
   334
  in case rew of SOME (res,_) => res | NONE => t end;
neuper@37906
   335
neuper@41928
   336
fun eval_true_ _ _ (Const ("HOL.True",_)) = true
neuper@37906
   337
  | eval_true_ (thy':theory') rls t =
neuper@37906
   338
    case rewrite_set_ (assoc_thy thy') false rls t of
neuper@41928
   339
	   SOME (Const ("HOL.True",_),_) => true
neuper@37906
   340
	 | _ => false;
neuper@37906
   341
wneuper@59380
   342
end