src/Pure/isac/Scripts/Tools.thy
author Walther Neuper <neuper@ist.tugraz.at>
Mon, 26 Jul 2010 15:27:06 +0200
branchisac-update-Isa09-2
changeset 37883 44bd7bdfdb33
parent 37871 875b6efa7ced
permissions -rw-r--r--
intermed.update scrtools.sml + added smltest/Pure/General/Basics.thy
neuper@37871
     1
(* auxiliary functions used in scripts
neuper@37871
     2
   author: Walther Neuper 000301
neuper@37871
     3
   WN0509 shift into Atools ?!? (because used also in where of models !)
neuper@37871
     4
neuper@37871
     5
   (c) copyright due to lincense terms.
neuper@37871
     6
neuper@37871
     7
remove_thy"Tools";
neuper@37871
     8
use_thy"Scripts/Tools";
neuper@37871
     9
*)
neuper@37871
    10
neuper@37871
    11
theory Tools
neuper@37871
    12
imports ListG
neuper@37871
    13
begin
neuper@37871
    14
neuper@37871
    15
(*belongs to theory ListG*)
neuper@37871
    16
ML {*
neuper@37871
    17
val first_isac_thy = @{theory ListG}
neuper@37871
    18
*}
neuper@37871
    19
neuper@37871
    20
(*for Descript.thy*)
neuper@37871
    21
neuper@37871
    22
  (***********************************************************************)
neuper@37871
    23
  (* 'fun is_dsc' in Scripts/scrtools.smlMUST contain ALL these types !!!*)
neuper@37871
    24
  (***********************************************************************)
neuper@37871
    25
typedecl nam     (* named variables                                             *)
neuper@37871
    26
typedecl  una     (* unnamed variables                                           *)
neuper@37871
    27
typedecl  unl     (* unnamed variables of type list, elementwise input prohibited*)
neuper@37871
    28
typedecl  str     (* structured variables                                        *)
neuper@37871
    29
typedecl  toreal  (* var with undef real value: forces typing                    *)
neuper@37871
    30
typedecl  toreall (* var with undef real list value: forces typing               *)
neuper@37871
    31
typedecl  tobooll (* var with undef bool list value: forces typing               *)
neuper@37871
    32
typedecl  unknow  (* input without dsc in fmz=[]                                 *)
neuper@37871
    33
typedecl  cpy     (* UNUSED: copy-named variables
neuper@37871
    34
             identified by .._0, .._i .._' in pbt                        *)
neuper@37871
    35
  (***********************************************************************)
neuper@37871
    36
  (* 'fun is_dsc' in Scripts/scrtools.smlMUST contain ALL these types !!!*)
neuper@37871
    37
  (***********************************************************************)
neuper@37871
    38
  
neuper@37871
    39
consts
neuper@37871
    40
neuper@37871
    41
  UniversalList   :: "bool list"
neuper@37871
    42
neuper@37871
    43
  lhs             :: "bool => real"           (*of an equality*)
neuper@37871
    44
  rhs             :: "bool => real"           (*of an equality*)
neuper@37871
    45
  Vars            :: "'a => real list"        (*get the variables of a term *)
neuper@37871
    46
  matches         :: "['a, 'a] => bool"
neuper@37871
    47
  matchsub        :: "['a, 'a] => bool"
neuper@37871
    48
neuper@37871
    49
constdefs
neuper@37871
    50
  
neuper@37871
    51
  Testvar   :: "[real, 'a] => bool"  (*is a variable in a term: unused 6.5.03*)
neuper@37871
    52
 "Testvar v t == v mem (Vars t)"     (*by rewriting only,no Calcunused 6.5.03*)
neuper@37871
    53
neuper@37883
    54
ML {* (*the former Tools.ML*)
neuper@37883
    55
(* auxiliary functions for scripts  WN.9.00*)
neuper@37883
    56
(*11.02: for equation solving only*)
neuper@37883
    57
val UniversalList = (term_of o the o (parse @{theory})) "UniversalList";
neuper@37883
    58
val EmptyList = (term_of o the o (parse @{theory}))  "[]::bool list";     
neuper@37883
    59
neuper@37883
    60
(*+ for Or_to_List +*)
neuper@37883
    61
fun or2list (Const ("True",_)) = (writeln"### or2list True";UniversalList)
neuper@37883
    62
  | or2list (Const ("False",_)) = (writeln"### or2list False";EmptyList)
neuper@37883
    63
  | or2list (t as Const ("op =",_) $ _ $ _) = 
neuper@37883
    64
    (writeln"### or2list _ = _";list2isalist bool [t])
neuper@37883
    65
  | or2list ors =
neuper@37883
    66
    (writeln"### or2list _ | _";
neuper@37883
    67
    let fun get ls (Const ("op |",_) $ o1 $ o2) =
neuper@37883
    68
	    case o2 of
neuper@37883
    69
		Const ("op |",_) $ _ $ _ => get (ls @ [o1]) o2
neuper@37883
    70
	      | _ => ls @ [o1, o2] 
neuper@37883
    71
    in (((list2isalist bool) o (get [])) ors)
neuper@37883
    72
       handle _ => raise error ("or2list: no ORs= "^(term2str ors)) end
neuper@37883
    73
	);
neuper@37883
    74
(*>val t = HOLogic.true_const;
neuper@37883
    75
> val t' = or2list t;
neuper@37883
    76
> term2str t';
neuper@37883
    77
"Atools.UniversalList"
neuper@37883
    78
> val t = HOLogic.false_const;
neuper@37883
    79
> val t' = or2list t;
neuper@37883
    80
> term2str t';
neuper@37883
    81
"[]"
neuper@37883
    82
> val t=(term_of o the o (parse thy)) "x=3";
neuper@37883
    83
> val t' = or2list t;
neuper@37883
    84
> term2str t';
neuper@37883
    85
"[x = 3]"
neuper@37883
    86
> val t=(term_of o the o (parse thy))"(x=3) | (x=-3) | (x=0)";
neuper@37883
    87
> val t' = or2list t;
neuper@37883
    88
> term2str t';
neuper@37883
    89
"[x = #3, x = #-3, x = #0]" : string *)
neuper@37883
    90
neuper@37883
    91
neuper@37883
    92
(** evaluation on the meta-level **)
neuper@37883
    93
neuper@37883
    94
(*. evaluate the predicate matches (match on whole term only) .*)
neuper@37883
    95
(*("matches",("Tools.matches",eval_matches "#matches_")):calc*)
neuper@37883
    96
fun eval_matches (thmid:string) "Tools.matches"
neuper@37883
    97
		 (t as Const ("Tools.matches",_) $ pat $ tst) thy = 
neuper@37883
    98
    if matches thy tst pat
neuper@37883
    99
    then let val prop = Trueprop $ (mk_equality (t, true_as_term))
neuper@37883
   100
	 in SOME (Syntax.string_of_term @{context} prop, prop) end
neuper@37883
   101
    else let val prop = Trueprop $ (mk_equality (t, false_as_term))
neuper@37883
   102
	 in SOME (Syntax.string_of_term @{context} prop, prop) end
neuper@37883
   103
  | eval_matches _ _ _ _ = NONE; 
neuper@37883
   104
(*
neuper@37883
   105
> val t  = (term_of o the o (parse thy)) 
neuper@37883
   106
	      "matches (?x = 0) (1 * x ^^^ 2 = 0)";
neuper@37883
   107
> eval_matches "/thmid/" "/op_/" t thy;
neuper@37883
   108
val it =
neuper@37883
   109
  SOME
neuper@37883
   110
    ("matches (x = 0) (1 * x ^^^ 2 = 0) = False",
neuper@37883
   111
     Const (#,#) $ (# $ # $ Const #)) : (string * term) option
neuper@37883
   112
neuper@37883
   113
> val t  = (term_of o the o (parse thy)) 
neuper@37883
   114
	      "matches (?a = #0) (#1 * x ^^^ #2 = #0)";
neuper@37883
   115
> eval_matches "/thmid/" "/op_/" t thy;
neuper@37883
   116
val it =
neuper@37883
   117
  SOME
neuper@37883
   118
    ("matches (?a = #0) (#1 * x ^^^ #2 = #0) = True",
neuper@37883
   119
     Const (#,#) $ (# $ # $ Const #)) : (string * term) option
neuper@37883
   120
neuper@37883
   121
> val t  = (term_of o the o (parse thy)) 
neuper@37883
   122
	      "matches (?a * x = #0) (#1 * x ^^^ #2 = #0)";
neuper@37883
   123
> eval_matches "/thmid/" "/op_/" t thy;
neuper@37883
   124
val it =
neuper@37883
   125
  SOME
neuper@37883
   126
    ("matches (?a * x = #0) (#1 * x ^^^ #2 = #0) = False",
neuper@37883
   127
     Const (#,#) $ (# $ # $ Const #)) : (string * term) option
neuper@37883
   128
neuper@37883
   129
> val t  = (term_of o the o (parse thy)) 
neuper@37883
   130
	      "matches (?a * x ^^^ #2 = #0) (#1 * x ^^^ #2 = #0)";
neuper@37883
   131
> eval_matches "/thmid/" "/op_/" t thy;
neuper@37883
   132
val it =
neuper@37883
   133
  SOME
neuper@37883
   134
    ("matches (?a * x ^^^ #2 = #0) (#1 * x ^^^ #2 = #0) = True",
neuper@37883
   135
     Const (#,#) $ (# $ # $ Const #)) : (string * term) option                  
neuper@37883
   136
----- before ?patterns ---:
neuper@37883
   137
> val t  = (term_of o the o (parse thy)) 
neuper@37883
   138
	      "matches (a * b^^^#2 = c) (#3 * x^^^#2 = #1)";
neuper@37883
   139
> eval_matches "/thmid/" "/op_/" t thy;
neuper@37883
   140
SOME
neuper@37883
   141
    ("matches (a * b ^^^ #2 = c) (#3 * x ^^^ #2 = #1) = True",
neuper@37883
   142
     Const ("Trueprop","bool => prop") $ (Const # $ (# $ #) $ Const (#,#)))
neuper@37883
   143
  : (string * term) option 
neuper@37883
   144
neuper@37883
   145
> val t = (term_of o the o (parse thy)) 
neuper@37883
   146
	      "matches (a * b^^^#2 = c) (#3 * x^^^#2222 = #1)";
neuper@37883
   147
> eval_matches "/thmid/" "/op_/" t thy;
neuper@37883
   148
SOME ("matches (a * b ^^^ #2 = c) (#3 * x ^^^ #2222 = #1) = False",
neuper@37883
   149
     Const ("Trueprop","bool => prop") $ (Const # $ (# $ #) $ Const (#,#)))
neuper@37883
   150
neuper@37883
   151
> val t = (term_of o the o (parse thy)) 
neuper@37883
   152
               "matches (a = b) (x + #1 + #-1 * #2 = #0)";
neuper@37883
   153
> eval_matches "/thmid/" "/op_/" t thy;
neuper@37883
   154
SOME ("matches (a = b) (x + #1 + #-1 * #2 = #0) = True",Const # $ (# $ #))
neuper@37883
   155
*)
neuper@37883
   156
neuper@37883
   157
(*.does a pattern match some subterm ?.*)
neuper@37883
   158
fun matchsub thy t pat =  
neuper@37883
   159
    let fun matchs (t as Const _) = matches thy t pat
neuper@37883
   160
	  | matchs (t as Free _) = matches thy t pat
neuper@37883
   161
	  | matchs (t as Var _) = matches thy t pat
neuper@37883
   162
	  | matchs (Bound _) = false
neuper@37883
   163
	  | matchs (t as Abs (_, _, body)) = 
neuper@37883
   164
	    if matches thy t pat then true else matches thy body pat
neuper@37883
   165
	  | matchs (t as f1 $ f2) =
neuper@37883
   166
	     if matches thy t pat then true 
neuper@37883
   167
	     else if matchs f1 then true else matchs f2
neuper@37883
   168
    in matchs t end;
neuper@37883
   169
neuper@37883
   170
(*("matchsub",("Tools.matchsub",eval_matchsub "#matchsub_")):calc*)
neuper@37883
   171
fun eval_matchsub (thmid:string) "Tools.matchsub"
neuper@37883
   172
		 (t as Const ("Tools.matchsub",_) $ pat $ tst) thy = 
neuper@37883
   173
    if matchsub thy tst pat
neuper@37883
   174
    then let val prop = Trueprop $ (mk_equality (t, true_as_term))
neuper@37883
   175
	 in SOME (Syntax.string_of_term @{context} prop, prop) end
neuper@37883
   176
    else let val prop = Trueprop $ (mk_equality (t, false_as_term))
neuper@37883
   177
	 in SOME (Syntax.string_of_term @{context} prop, prop) end
neuper@37883
   178
  | eval_matchsub _ _ _ _ = NONE; 
neuper@37883
   179
neuper@37883
   180
(*get the variables in an isabelle-term*)
neuper@37883
   181
(*("Vars"    ,("Tools.Vars"    ,eval_var "#Vars_")):calc*)
neuper@37883
   182
fun eval_var (thmid:string) "Tools.Vars"
neuper@37883
   183
  (t as (Const(op0,t0) $ arg)) thy = 
neuper@37883
   184
  let 
neuper@37883
   185
    val t' = ((list2isalist HOLogic.realT) o vars) t;
neuper@37883
   186
    val thmId = thmid^(Syntax.string_of_term @{context} arg);
neuper@37883
   187
  in SOME (thmId, Trueprop $ (mk_equality (t,t'))) end
neuper@37883
   188
  | eval_var _ _ _ _ = NONE;
neuper@37883
   189
neuper@37883
   190
fun lhs (Const ("op =",_) $ l $ _) = l
neuper@37883
   191
  | lhs t = error("lhs called with (" ^ term2str t ^ ")");
neuper@37883
   192
(*("lhs"    ,("Tools.lhs"    ,eval_lhs "")):calc*)
neuper@37883
   193
fun eval_lhs _ "Tools.lhs"
neuper@37883
   194
	     (t as (Const ("Tools.lhs",_) $ (Const ("op =",_) $ l $ _))) _ = 
neuper@37883
   195
    SOME ((term2str t) ^ " = " ^ (term2str l),
neuper@37883
   196
	  Trueprop $ (mk_equality (t, l)))
neuper@37883
   197
  | eval_lhs _ _ _ _ = NONE;
neuper@37883
   198
(*
neuper@37883
   199
> val t = (term_of o the o (parse thy)) "lhs (1 * x ^^^ 2 = 0)";
neuper@37883
   200
> val SOME (id,t') = eval_lhs 0 0 t 0;
neuper@37883
   201
val id = "Tools.lhs (1 * x ^^^ 2 = 0) = 1 * x ^^^ 2" : string
neuper@37883
   202
> term2str t';
neuper@37883
   203
val it = "Tools.lhs (1 * x ^^^ 2 = 0) = 1 * x ^^^ 2" : string
neuper@37883
   204
*)
neuper@37883
   205
neuper@37883
   206
fun rhs (Const ("op =",_) $ _ $ r) = r
neuper@37883
   207
  | rhs t = error("rhs called with (" ^ term2str t ^ ")");
neuper@37883
   208
(*("rhs"    ,("Tools.rhs"    ,eval_rhs "")):calc*)
neuper@37883
   209
fun eval_rhs _ "Tools.rhs"
neuper@37883
   210
	     (t as (Const ("Tools.rhs",_) $ (Const ("op =",_) $ _ $ r))) _ = 
neuper@37883
   211
    SOME ((term2str t) ^ " = " ^ (term2str r),
neuper@37883
   212
	  Trueprop $ (mk_equality (t, r)))
neuper@37883
   213
  | eval_rhs _ _ _ _ = NONE;
neuper@37883
   214
neuper@37883
   215
neuper@37883
   216
(*for evaluating scripts*) 
neuper@37883
   217
neuper@37883
   218
val list_rls = append_rls "list_rls" list_rls
neuper@37883
   219
			  [Calc ("Tools.rhs",eval_rhs "")];
neuper@37883
   220
ruleset' := overwritelthy @{theory} (!ruleset',
neuper@37883
   221
  [("list_rls",list_rls)
neuper@37883
   222
   ]);
neuper@37883
   223
calclist':= overwritel (!calclist', 
neuper@37883
   224
   [("matches",("Tools.matches",eval_matches "#matches_")),
neuper@37883
   225
    ("matchsub",("Tools.matchsub",eval_matchsub "#matchsub_")),
neuper@37883
   226
    ("Vars"    ,("Tools.Vars"    ,eval_var "#Vars_")),
neuper@37883
   227
    ("lhs"    ,("Tools.lhs"    ,eval_lhs "")),
neuper@37883
   228
    ("rhs"    ,("Tools.rhs"    ,eval_rhs ""))
neuper@37883
   229
    ]);
neuper@37883
   230
neuper@37883
   231
*}
neuper@37871
   232
end