neuper@38025
|
1 |
(* Title: extends Isabelle/src/Pure/term.ML
|
e0726734@41962
|
2 |
Author: Walther Neuper 1999, Mathias Lehnfeld
|
neuper@38025
|
3 |
(c) due to copyright terms
|
neuper@38025
|
4 |
*)
|
wneuper@59507
|
5 |
infix contains_one_of
|
wneuper@59507
|
6 |
|
wneuper@59577
|
7 |
(* TERM_C extends Isabelle's naming conventions: "C" indicates Isac add-ons to an Isabelle module *)
|
wneuper@59577
|
8 |
signature TERM_C =
|
wneuper@59389
|
9 |
sig
|
wneuper@59389
|
10 |
val contains_Var: term -> bool
|
wneuper@59389
|
11 |
val dest_binop_typ: typ -> typ * typ * typ
|
wneuper@59395
|
12 |
val dest_equals: term -> term * term
|
wneuper@59389
|
13 |
val free2str: term -> string
|
wneuper@59389
|
14 |
val ids2str: term -> string list
|
wneuper@59389
|
15 |
val ins_concl: term -> term -> term
|
wneuper@59393
|
16 |
val inst_abs: term -> term
|
wneuper@59389
|
17 |
val inst_bdv: (term * term) list -> term -> term
|
wneuper@59390
|
18 |
|
wneuper@59392
|
19 |
val term_of_num: typ -> int -> term
|
wneuper@59395
|
20 |
val num_of_term: term -> int
|
wneuper@59395
|
21 |
val int_of_str_opt: string -> int option
|
wneuper@59395
|
22 |
val int_of_str: string -> int
|
wneuper@59392
|
23 |
val isastr_of_int: int -> string
|
wneuper@59390
|
24 |
|
wneuper@59389
|
25 |
val isalist2list: term -> term list
|
wneuper@59390
|
26 |
val list2isalist: typ -> term list -> term
|
wneuper@59492
|
27 |
val isapair2pair: term -> term * term (* rename to dest_pair, compare HOLogic.dest_string *)
|
wneuper@59390
|
28 |
|
wneuper@59389
|
29 |
val is_atom: term -> bool
|
wneuper@59389
|
30 |
val is_bdv: string -> bool
|
wneuper@59389
|
31 |
val is_bdv_subst: term -> bool
|
wneuper@59389
|
32 |
val is_equality: term -> bool
|
wneuper@59389
|
33 |
val is_expliceq: term -> bool
|
wneuper@59389
|
34 |
val is_f_x: term -> bool
|
wneuper@59389
|
35 |
val is_list: term -> bool
|
wneuper@59389
|
36 |
val is_num: term -> bool
|
wneuper@59395
|
37 |
val is_num': string -> bool
|
wneuper@59390
|
38 |
|
wneuper@59389
|
39 |
val mk_add: term -> term -> term
|
wneuper@59389
|
40 |
val mk_free: typ -> string -> term
|
wneuper@59389
|
41 |
val mk_equality: term * term -> term
|
wneuper@59389
|
42 |
val mk_factroot: string -> typ -> int -> int -> term
|
wneuper@59389
|
43 |
val mk_Free: string * typ -> term
|
wneuper@59392
|
44 |
val mk_thmid: string -> string -> string -> string
|
wneuper@59395
|
45 |
val mk_num_op_num: typ -> typ -> string * typ -> int -> int -> term
|
wneuper@59395
|
46 |
val mk_num_op_var: term -> string -> typ -> typ -> int -> term
|
wneuper@59395
|
47 |
val mk_var_op_num: term -> string -> typ -> typ -> int -> term
|
wneuper@59390
|
48 |
|
wneuper@59390
|
49 |
(*val list_implies: term list * term -> term --> Logic.list_implies*)
|
wneuper@59390
|
50 |
val matches: theory -> term -> term -> bool
|
wneuper@59389
|
51 |
val parse: theory -> string -> cterm option
|
wneuper@59389
|
52 |
val parseN: theory -> string -> cterm option
|
wneuper@59389
|
53 |
val parseNEW: Proof.context -> string -> term option
|
wneuper@59389
|
54 |
val parseold: theory -> string -> cterm option
|
wneuper@59389
|
55 |
val parse_patt: theory -> string -> term
|
wneuper@59389
|
56 |
val perm: term -> term -> bool
|
wneuper@59390
|
57 |
|
wneuper@59389
|
58 |
val str_of_free_opt: term -> string option
|
wneuper@59389
|
59 |
val str_of_int: int -> string
|
wneuper@59389
|
60 |
val str2term: string -> term
|
wneuper@59389
|
61 |
val strip_imp_prems': term -> term option
|
wneuper@59389
|
62 |
val subst_atomic_all: (term * term) list -> term -> bool * term
|
wneuper@59389
|
63 |
val term_detail2str: term -> string
|
wneuper@59390
|
64 |
|
wneuper@59390
|
65 |
val pairt: term -> term -> term
|
wneuper@59390
|
66 |
val pairT: typ -> typ -> typ
|
wneuper@59390
|
67 |
val strip_trueprop: term -> term
|
wneuper@59390
|
68 |
|
wneuper@59402
|
69 |
val num_str: thm -> thm
|
wneuper@59550
|
70 |
val numbers_to_string: term -> term
|
wneuper@59389
|
71 |
val uminus_to_string: term -> term
|
wneuper@59389
|
72 |
val var2free: term -> term
|
wneuper@59389
|
73 |
val vars: term -> term list
|
wneuper@59532
|
74 |
val vars_of : term -> term list (* probably should replace "fun vars" *)
|
wneuper@59577
|
75 |
val dest_list' : term -> term list
|
wneuper@59532
|
76 |
|
wneuper@59389
|
77 |
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
|
wneuper@59399
|
78 |
val scala_of_term: term -> string
|
wneuper@59392
|
79 |
val atomtyp(*<-- atom_typ TODO*): typ -> unit
|
wneuper@59392
|
80 |
val atomty: term -> unit
|
wneuper@59392
|
81 |
val atomw: term -> unit
|
wneuper@59392
|
82 |
val atomwy: term -> unit
|
wneuper@59416
|
83 |
val atomty_thy: Rule.thyID -> term -> unit
|
wneuper@59392
|
84 |
val free2var: term -> term
|
wneuper@59507
|
85 |
val contains_one_of: thm * (string * typ) list -> bool
|
wneuper@59389
|
86 |
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
|
wneuper@59389
|
87 |
val atomt: term -> unit
|
wneuper@59389
|
88 |
val typ_a2real: term -> term
|
wneuper@59389
|
89 |
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
|
wneuper@59389
|
90 |
end
|
wneuper@59389
|
91 |
|
wneuper@59389
|
92 |
(**)
|
wneuper@59577
|
93 |
structure TermC(**): TERM_C(**) =
|
wneuper@59389
|
94 |
struct
|
wneuper@59389
|
95 |
(**)
|
neuper@38025
|
96 |
|
neuper@52087
|
97 |
fun isastr_of_int i = if i >= 0 then string_of_int i else "-" ^ string_of_int (abs i)
|
neuper@52087
|
98 |
|
neuper@38025
|
99 |
fun matches thy tm pa =
|
neuper@38025
|
100 |
(Pattern.match thy (pa, tm) (Vartab.empty, Vartab.empty); true)
|
wneuper@59392
|
101 |
handle Pattern.MATCH => false
|
neuper@38025
|
102 |
|
wneuper@59392
|
103 |
(** transform typ / term to a String to be parsed by Scala after transport via libisabelle **)
|
wneuper@59392
|
104 |
|
wneuper@59318
|
105 |
fun scala_of_typ (Type (s, typs)) =
|
wneuper@59318
|
106 |
enclose "Type(" ")" (quote s ^ ", " ^
|
wneuper@59318
|
107 |
(typs |> map scala_of_typ |> commas |> enclose "List(" ")"))
|
wneuper@59318
|
108 |
| scala_of_typ (TFree (s, sort)) =
|
wneuper@59318
|
109 |
enclose "TFree(" ")" (quote s ^ ", " ^ (sort |> map quote |> commas |> enclose "List(" ")"))
|
wneuper@59318
|
110 |
| scala_of_typ (TVar ((s, i), sort)) =
|
wneuper@59318
|
111 |
enclose "TVar(" ")" (
|
wneuper@59318
|
112 |
enclose "(" ")," (quote s ^ "," ^ quote (string_of_int i)) ^
|
wneuper@59318
|
113 |
(sort |> map quote |> commas |> enclose "List(" ")"))
|
wneuper@59318
|
114 |
fun scala_of_term (Const (s, T)) =
|
wneuper@59318
|
115 |
enclose "Const(" ")" (quote s ^ ", " ^ scala_of_typ T)
|
wneuper@59318
|
116 |
| scala_of_term (Free (s, T)) =
|
wneuper@59318
|
117 |
enclose "Free(" ")" (quote s ^ ", " ^ scala_of_typ T)
|
wneuper@59318
|
118 |
| scala_of_term (Var ((s, i), T)) =
|
wneuper@59318
|
119 |
enclose "TVar(" ")" (
|
wneuper@59318
|
120 |
enclose "(" ")," (quote s ^ "," ^ quote (string_of_int i)) ^
|
wneuper@59318
|
121 |
scala_of_typ T)
|
wneuper@59318
|
122 |
| scala_of_term (Bound i) = enclose "Bound(" ")" (string_of_int i)
|
wneuper@59318
|
123 |
| scala_of_term (Abs (s, T, t)) =
|
wneuper@59318
|
124 |
enclose "Abs(" ")" (
|
wneuper@59318
|
125 |
quote s ^ ", " ^
|
wneuper@59318
|
126 |
scala_of_typ T ^ ", " ^
|
wneuper@59318
|
127 |
scala_of_term t)
|
wneuper@59318
|
128 |
| scala_of_term (t1 $ t2) =
|
wneuper@59318
|
129 |
enclose "App(" ")" (scala_of_term t1 ^ ", " ^ scala_of_term t2)
|
wneuper@59318
|
130 |
|
wneuper@59392
|
131 |
(* see structure's bare bones.
|
wneuper@59392
|
132 |
for Isabelle standard output compare 2017 "structure ML_PP" *)
|
wneuper@59392
|
133 |
fun atomtyp t =
|
neuper@38025
|
134 |
let
|
wneuper@59392
|
135 |
fun ato n (Type (s, [])) = "\n*** " ^ indent n ^ "Type (" ^ s ^",[])"
|
wneuper@59392
|
136 |
| ato n (Type (s, Ts)) = "\n*** " ^ indent n ^ "Type (" ^ s ^ ",[" ^ atol (n + 1) Ts
|
wneuper@59392
|
137 |
| ato n (TFree (s, sort)) = "\n*** " ^ indent n ^ "TFree (" ^ s ^ "," ^ strs2str' sort
|
wneuper@59392
|
138 |
| ato n (TVar ((s, i), sort)) =
|
wneuper@59392
|
139 |
"\n*** " ^ indent n ^ "TVar ((" ^ s ^ "," ^ string_of_int i ^ strs2str' sort
|
wneuper@59392
|
140 |
and atol n [] = "\n*** " ^ indent n ^ "]"
|
wneuper@59392
|
141 |
| atol n (T :: Ts) = (ato n T ^ atol n Ts)
|
neuper@38048
|
142 |
in tracing (ato 0 t ^ "\n") end;
|
neuper@38025
|
143 |
|
neuper@38067
|
144 |
local
|
wneuper@59392
|
145 |
fun ato (Const (a, _)) n = "\n*** " ^ indent n ^ "Const (" ^ a ^ ", _)"
|
wneuper@59392
|
146 |
| ato (Free (a, _)) n = "\n*** " ^ indent n ^ "Free (" ^ a ^ ", _)"
|
neuper@38025
|
147 |
| ato (Var ((a, i), _)) n =
|
wneuper@59392
|
148 |
"\n*** " ^ indent n ^ "Var (" ^ a ^ ", " ^ string_of_int i ^ "), _)"
|
wneuper@59392
|
149 |
| ato (Bound i) n = "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
|
wneuper@59392
|
150 |
| ato (Abs (a, _, body)) n = "\n*** " ^ indent n ^ "Abs(" ^ a ^ ", _" ^ ato body (n+1)
|
neuper@38025
|
151 |
| ato (f $ t) n = (ato f n ^ ato t (n + 1))
|
neuper@38067
|
152 |
in
|
wneuper@59392
|
153 |
fun atomw t = writeln ("\n*** -------------" ^ ato t 0 ^ "\n***");
|
wneuper@59392
|
154 |
fun atomt t = tracing ("\n*** -------------" ^ ato t 0 ^ "\n***");
|
neuper@38067
|
155 |
end;
|
neuper@38025
|
156 |
|
neuper@38025
|
157 |
fun term_detail2str t =
|
wneuper@59392
|
158 |
let
|
wneuper@59416
|
159 |
fun ato (Const (a, T)) n = "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ Rule.string_of_typ T ^ ")"
|
wneuper@59416
|
160 |
| ato (Free (a, T)) n = "\n*** " ^ indent n ^ "Free (" ^ a ^ ", " ^ Rule.string_of_typ T ^ ")"
|
wneuper@59392
|
161 |
| ato (Var ((a, i), T)) n =
|
wneuper@59416
|
162 |
"\n*** " ^ indent n ^ "Var ((" ^ a ^ ", " ^ string_of_int i ^ "), " ^ Rule.string_of_typ T ^ ")"
|
wneuper@59392
|
163 |
| ato (Bound i) n = "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
|
wneuper@59392
|
164 |
| ato (Abs(a, T, body)) n =
|
wneuper@59416
|
165 |
"\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ Rule.string_of_typ T ^ ",.." ^ ato body (n + 1)
|
wneuper@59392
|
166 |
| ato (f $ t) n = ato f n ^ ato t (n + 1)
|
wneuper@59392
|
167 |
in "\n*** " ^ ato t 0 ^ "\n***" end;
|
neuper@42376
|
168 |
fun term_detail2str_thy thy t =
|
wneuper@59392
|
169 |
let
|
wneuper@59392
|
170 |
fun ato (Const (a, T)) n =
|
wneuper@59416
|
171 |
"\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ Rule.string_of_typ_thy thy T ^ ")"
|
wneuper@59392
|
172 |
| ato (Free (a, T)) n =
|
wneuper@59416
|
173 |
"\n*** " ^ indent n ^ "Free (" ^ a ^ ", " ^ Rule.string_of_typ_thy thy T ^ ")"
|
wneuper@59392
|
174 |
| ato (Var ((a, i), T)) n =
|
wneuper@59392
|
175 |
"\n*** " ^ indent n ^ "Var ((" ^ a ^ ", " ^ string_of_int i ^ "), " ^
|
wneuper@59416
|
176 |
Rule.string_of_typ_thy thy T ^ ")"
|
wneuper@59392
|
177 |
| ato (Bound i) n =
|
wneuper@59392
|
178 |
"\n*** " ^ indent n ^ "Bound " ^ string_of_int i
|
wneuper@59392
|
179 |
| ato (Abs(a, T, body)) n =
|
wneuper@59416
|
180 |
"\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ Rule.string_of_typ_thy thy T ^ ",.." ^
|
wneuper@59392
|
181 |
ato body (n + 1)
|
wneuper@59392
|
182 |
| ato (f $ t) n = ato f n ^ ato t (n + 1)
|
wneuper@59392
|
183 |
in "\n*** " ^ ato t 0 ^ "\n***" end;
|
neuper@38067
|
184 |
fun atomwy t = (writeln o term_detail2str) t;
|
neuper@38067
|
185 |
fun atomty t = (tracing o term_detail2str) t;
|
neuper@42376
|
186 |
fun atomty_thy thy t = (tracing o (term_detail2str_thy thy)) t;
|
neuper@38025
|
187 |
|
wneuper@59392
|
188 |
(* contains the term a VAR(("*",_),_) ? *)
|
neuper@38025
|
189 |
fun contains_Var (Abs(_,_,body)) = contains_Var body
|
neuper@38025
|
190 |
| contains_Var (f $ f') = contains_Var f orelse contains_Var f'
|
neuper@38025
|
191 |
| contains_Var (Var _) = true
|
neuper@38025
|
192 |
| contains_Var _ = false;
|
neuper@38025
|
193 |
|
wneuper@59392
|
194 |
fun str_of_int n =
|
wneuper@59392
|
195 |
if n < 0 then "-" ^ ((string_of_int o abs) n)
|
wneuper@59392
|
196 |
else string_of_int n;
|
wneuper@59457
|
197 |
val int_of_str = Value.parse_int;
|
wneuper@59392
|
198 |
fun int_of_str_opt str =
|
wneuper@59390
|
199 |
let
|
wneuper@59390
|
200 |
val ss = Symbol.explode str
|
wneuper@59392
|
201 |
val ss' = case ss of "(" :: s => drop_last s | _ => ss
|
wneuper@59392
|
202 |
val (sign, istr) = case ss' of "-" :: istr => (~1, istr) | _ => (1, ss')
|
wneuper@59392
|
203 |
in
|
wneuper@59392
|
204 |
case Library.read_int istr of (i, []) => SOME (sign * i) | _ => NONE
|
wneuper@59392
|
205 |
end;
|
wneuper@59392
|
206 |
fun is_num' str = case int_of_str_opt str of SOME _ => true | NONE => false;
|
wneuper@59392
|
207 |
fun is_num (Free (s, _)) = if is_num' s then true else false | is_num _ = false;
|
wneuper@59392
|
208 |
fun term_of_num ntyp n = Free (str_of_int n, ntyp);
|
wneuper@59392
|
209 |
fun num_of_term (t as (Free (istr, _))) =
|
wneuper@59392
|
210 |
(case int_of_str_opt istr of SOME i => i | NONE => raise TERM ("num_of_term: NOT int ", [t]))
|
wneuper@59392
|
211 |
| num_of_term t = raise TERM ("num_of_term: NOT Free ", [t])
|
neuper@52064
|
212 |
|
wneuper@59392
|
213 |
fun is_Free (Free _) = true | is_Free _ = false;
|
neuper@38025
|
214 |
fun is_fun_id (Const _) = true
|
neuper@38025
|
215 |
| is_fun_id (Free _) = true
|
neuper@38025
|
216 |
| is_fun_id _ = false;
|
neuper@38025
|
217 |
fun is_f_x (f $ x) = is_fun_id f andalso is_Free x
|
neuper@38025
|
218 |
| is_f_x _ = false;
|
wneuper@59392
|
219 |
|
wneuper@59392
|
220 |
fun vars t =
|
neuper@38025
|
221 |
let
|
wneuper@59392
|
222 |
fun scan vs (Const _) = vs
|
wneuper@59392
|
223 |
| scan vs (t as Free (s, _)) = if is_num' s then vs else t :: vs
|
wneuper@59392
|
224 |
| scan vs (t as Var _) = t :: vs
|
wneuper@59392
|
225 |
| scan vs (Bound _) = vs
|
wneuper@59392
|
226 |
| scan vs (Abs (_, _, t)) = scan vs t
|
neuper@38025
|
227 |
| scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
|
neuper@38025
|
228 |
in (distinct o (scan [])) t end;
|
wneuper@59392
|
229 |
(* bypass Isabelle's Pretty, which requires ctxt *)
|
neuper@38025
|
230 |
fun ids2str t =
|
neuper@38025
|
231 |
let
|
wneuper@59392
|
232 |
fun scan vs (Const (s, _)) = if is_num' s then vs else s :: vs
|
wneuper@59392
|
233 |
| scan vs (Free (s, _)) = if is_num' s then vs else s :: vs
|
wneuper@59392
|
234 |
| scan vs (Var ((s, i), _)) = (s ^ "_" ^ string_of_int i) :: vs
|
wneuper@59392
|
235 |
| scan vs (Bound _) = vs
|
wneuper@59392
|
236 |
| scan vs (Abs (s, _, t)) = scan (s :: vs) t
|
neuper@38025
|
237 |
| scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2)
|
neuper@38025
|
238 |
in (distinct o (scan [])) t end;
|
wneuper@59392
|
239 |
fun is_bdv str = case Symbol.explode str of "b"::"d"::"v"::_ => true | _ => false;
|
wneuper@59394
|
240 |
(* instantiate #prop thm with bound variables (as Free) *)
|
wneuper@59394
|
241 |
fun inst_bdv [] t = t
|
wneuper@59394
|
242 |
| inst_bdv (instl: (term*term) list) t =
|
wneuper@59394
|
243 |
let
|
wneuper@59394
|
244 |
fun subst (v as Var((s, _), T)) =
|
wneuper@59394
|
245 |
(case Symbol.explode s of
|
wneuper@59394
|
246 |
"b"::"d"::"v"::_ => if_none (assoc(instl,Free(s,T))) (Free(s,T))
|
wneuper@59394
|
247 |
| _ => v)
|
wneuper@59394
|
248 |
| subst (Abs(a, T, body)) = Abs(a, T, subst body)
|
wneuper@59394
|
249 |
| subst (f $ t') = subst f $ subst t'
|
wneuper@59394
|
250 |
| subst t = if_none (assoc (instl, t)) t
|
wneuper@59394
|
251 |
in subst t end;
|
neuper@38025
|
252 |
|
wneuper@59494
|
253 |
(* is a term a substitution for a bdv as found in programs and tactics *)
|
neuper@42426
|
254 |
fun is_bdv_subst (Const ("List.list.Cons", _) $
|
wneuper@59494
|
255 |
(Const ("Product_Type.Pair", _) $ str $ _) $ _) = is_bdv (HOLogic.dest_string str)
|
neuper@42426
|
256 |
| is_bdv_subst _ = false;
|
neuper@42426
|
257 |
|
wneuper@59392
|
258 |
fun free2str (Free (s, _)) = s
|
wneuper@59416
|
259 |
| free2str t = error ("free2str not for " ^ Rule.term2str t);
|
wneuper@59392
|
260 |
fun str_of_free_opt (Free (s, _)) = SOME s
|
neuper@52103
|
261 |
| str_of_free_opt _ = NONE
|
neuper@38025
|
262 |
|
wneuper@59392
|
263 |
(* compare Logic.unvarify_global, which rejects Free *)
|
wneuper@59392
|
264 |
fun var2free (t as Const _) = t
|
wneuper@59392
|
265 |
| var2free (t as Free _) = t
|
wneuper@59392
|
266 |
| var2free (Var((s, _), T)) = Free (s,T)
|
wneuper@59392
|
267 |
| var2free (t as Bound _) = t
|
wneuper@59392
|
268 |
| var2free (Abs(s, T, t)) = Abs(s, T, var2free t)
|
neuper@38025
|
269 |
| var2free (t1 $ t2) = (var2free t1) $ (var2free t2);
|
neuper@38025
|
270 |
|
wneuper@59392
|
271 |
(* Logic.varify does NOT take care of 'Free ("1", _)'*)
|
wneuper@59392
|
272 |
fun free2var (t as Const _) = t
|
wneuper@59390
|
273 |
| free2var (t as Free (s, T)) = if is_num' s then t else Var ((s, 0), T)
|
wneuper@59392
|
274 |
| free2var (t as Var _) = t
|
wneuper@59392
|
275 |
| free2var (t as Bound _) = t
|
neuper@38025
|
276 |
| free2var (Abs (s, T, t)) = Abs (s, T, free2var t)
|
neuper@38025
|
277 |
| free2var (t1 $ t2) = (free2var t1) $ (free2var t2);
|
neuper@38025
|
278 |
|
neuper@38025
|
279 |
fun mk_listT T = Type ("List.list", [T]);
|
wneuper@59392
|
280 |
fun list_const T = Const ("List.list.Cons", [T, mk_listT T] ---> mk_listT T);
|
wneuper@59392
|
281 |
fun list2isalist T [] = Const ("List.list.Nil", mk_listT T)
|
wneuper@59392
|
282 |
| list2isalist T (t :: ts) = (list_const T) $ t $ (list2isalist T ts);
|
neuper@38025
|
283 |
|
wneuper@59392
|
284 |
fun isapair2pair (Const ("Product_Type.Pair",_) $ a $ b) = (a, b)
|
neuper@38025
|
285 |
| isapair2pair t =
|
wneuper@59416
|
286 |
error ("isapair2pair called with " ^ Rule.term2str t);
|
neuper@38025
|
287 |
fun isalist2list ls =
|
neuper@38025
|
288 |
let
|
wneuper@59392
|
289 |
fun get es (Const("List.list.Cons", _) $ t $ ls) = get (t :: es) ls
|
wneuper@59392
|
290 |
| get es (Const("List.list.Nil", _)) = es
|
wneuper@59403
|
291 |
| get _ t = raise TERM ("isalist2list applied to NON-list: ", [t])
|
neuper@38025
|
292 |
in (rev o (get [])) ls end;
|
neuper@38025
|
293 |
|
wneuper@59392
|
294 |
fun is_list ((Const ("List.list.Cons", _)) $ _ $ _) = true
|
wneuper@59392
|
295 |
| is_list _ = false;
|
wneuper@59392
|
296 |
fun dest_binop_typ (Type ("fun", [range, Type ("fun", [arg2, arg1])])) = (arg1, arg2, range)
|
wneuper@59392
|
297 |
| dest_binop_typ _ = raise ERROR "dest_binop_typ: not binary";
|
wneuper@59392
|
298 |
fun dest_equals (Const("HOL.eq", _) $ t $ u) = (t, u) (* Pure/logic.ML: Const ("==", ..*)
|
wneuper@59390
|
299 |
| dest_equals t = raise TERM ("dest_equals'", [t]);
|
wneuper@59390
|
300 |
fun is_equality (Const("HOL.eq",_) $ _ $ _) = true (* logic.ML: Const("=="*)
|
neuper@38025
|
301 |
| is_equality _ = false;
|
wneuper@59392
|
302 |
fun mk_equality (t, u) = (Const("HOL.eq", [type_of t, type_of u] ---> HOLogic.boolT) $ t $ u);
|
wneuper@59390
|
303 |
fun is_expliceq (Const("HOL.eq",_) $ (Free _) $ _) = true
|
neuper@38025
|
304 |
| is_expliceq _ = false;
|
wneuper@59390
|
305 |
fun strip_trueprop (Const ("HOL.Trueprop", _) $ t) = t
|
neuper@38025
|
306 |
| strip_trueprop t = t;
|
neuper@38025
|
307 |
|
wneuper@59392
|
308 |
(* (A1==>...An==>B) goes to (A1==>...An==>) Pure/logic.ML: term -> term list*)
|
wneuper@59403
|
309 |
fun strip_imp_prems' (Const ("Pure.imp", _) $ A $ t) =
|
wneuper@59392
|
310 |
let
|
wneuper@59403
|
311 |
fun coll_prems As (Const("Pure.imp", _) $ A $ t) =
|
wneuper@59392
|
312 |
coll_prems (As $ (Logic.implies $ A)) t
|
wneuper@59392
|
313 |
| coll_prems As _ = SOME As
|
neuper@38025
|
314 |
in coll_prems (Logic.implies $ A) t end
|
wneuper@59392
|
315 |
| strip_imp_prems' _ = NONE; (* *)
|
neuper@38025
|
316 |
|
wneuper@59392
|
317 |
(* (A1==>...An==>) (B) goes to (A1==>...An==>B), where B is lowest branch, 2002 Pure/thm.ML *)
|
wneuper@59403
|
318 |
fun ins_concl (Const ("Pure.imp", _) $ A $ t) B = Logic.implies $ A $ (ins_concl t B)
|
wneuper@59403
|
319 |
| ins_concl (Const ("Pure.imp", _) $ A ) B = Logic.implies $ A $ B
|
wneuper@59390
|
320 |
| ins_concl t B = raise TERM ("ins_concl", [t, B]);
|
neuper@38025
|
321 |
|
wneuper@59392
|
322 |
fun vperm (Var _, Var _) = true (* 2002 Pure/thm.ML *)
|
neuper@38025
|
323 |
| vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t)
|
neuper@38025
|
324 |
| vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2)
|
neuper@38025
|
325 |
| vperm (t, u) = (t = u);
|
neuper@38025
|
326 |
|
neuper@38025
|
327 |
(*2002 cp from Pure/term.ML --- since 2009 in Pure/old_term.ML*)
|
neuper@38025
|
328 |
fun mem_term (_, []) = false
|
wneuper@59392
|
329 |
| mem_term (t, t' :: ts) = t aconv t' orelse mem_term (t, ts);
|
wneuper@59390
|
330 |
fun subset_term ([], _) = true
|
wneuper@59390
|
331 |
| subset_term (x :: xs, ys) = mem_term (x, ys) andalso subset_term (xs, ys);
|
neuper@38025
|
332 |
fun eq_set_term (xs, ys) =
|
neuper@38025
|
333 |
xs = ys orelse (subset_term (xs, ys) andalso subset_term (ys, xs));
|
neuper@38025
|
334 |
(*a total, irreflexive ordering on index names*)
|
wneuper@59392
|
335 |
fun xless ((a, i), (b, j): indexname) = i<j orelse (i = j andalso a < b);
|
neuper@38025
|
336 |
(*a partial ordering (not reflexive) for atomic terms*)
|
wneuper@59392
|
337 |
fun atless (Const (a, _), Const (b, _)) = a < b
|
wneuper@59392
|
338 |
| atless (Free (a, _), Free (b, _)) = a < b
|
wneuper@59392
|
339 |
| atless (Var (v, _), Var (w, _)) = xless (v, w)
|
wneuper@59392
|
340 |
| atless (Bound i, Bound j) = i < j
|
wneuper@59392
|
341 |
| atless _ = false;
|
neuper@38025
|
342 |
(*insert atomic term into partially sorted list, suppressing duplicates (?)*)
|
neuper@38025
|
343 |
fun insert_aterm (t,us) =
|
neuper@38025
|
344 |
let fun inserta [] = [t]
|
neuper@38025
|
345 |
| inserta (us as u::us') =
|
neuper@38025
|
346 |
if atless(t,u) then t::us
|
neuper@38025
|
347 |
else if t=u then us (*duplicate*)
|
wneuper@59392
|
348 |
else u :: inserta us'
|
wneuper@59392
|
349 |
in inserta us end;
|
neuper@38025
|
350 |
|
wneuper@59390
|
351 |
(* Accumulates the Vars in the term, suppressing duplicates *)
|
neuper@38025
|
352 |
fun add_term_vars (t, vars: term list) = case t of
|
wneuper@59392
|
353 |
Var _ => insert_aterm (t, vars)
|
wneuper@59392
|
354 |
| Abs (_, _, body) => add_term_vars (body, vars)
|
wneuper@59392
|
355 |
| f$t => add_term_vars (f, add_term_vars (t, vars))
|
neuper@38025
|
356 |
| _ => vars;
|
wneuper@59390
|
357 |
fun term_vars t = add_term_vars (t, []);
|
neuper@38025
|
358 |
|
wneuper@59392
|
359 |
(*2002 Pure/thm.ML *)
|
wneuper@59392
|
360 |
fun var_perm (t, u) = vperm (t, u) andalso eq_set_term (term_vars t, term_vars u);
|
neuper@38025
|
361 |
(*2002 fun decomp_simp, Pure/thm.ML *)
|
wneuper@59390
|
362 |
fun perm lhs rhs = var_perm (lhs, rhs) andalso not (lhs aconv rhs) andalso not (is_Var lhs);
|
neuper@38025
|
363 |
|
neuper@38025
|
364 |
|
neuper@38025
|
365 |
fun pairT T1 T2 = Type ("*", [T1, T2]);
|
neuper@38025
|
366 |
fun PairT T1 T2 = ([T1, T2] ---> Type ("*", [T1, T2]));
|
wneuper@59392
|
367 |
fun pairt t1 t2 = Const ("Product_Type.Pair", PairT (type_of t1) (type_of t2)) $ t1 $ t2;
|
neuper@38025
|
368 |
|
neuper@38025
|
369 |
fun mk_factroot op_(*=thy.sqrt*) T fact root =
|
neuper@38034
|
370 |
Const ("Groups.times_class.times", [T, T] ---> T) $ (term_of_num T fact) $
|
wneuper@59392
|
371 |
(Const (op_, T --> T) $ term_of_num T root);
|
wneuper@59390
|
372 |
fun mk_var_op_num v op_ optype ntyp n = Const (op_, optype) $ v $ Free (str_of_int n, ntyp);
|
wneuper@59390
|
373 |
fun mk_num_op_var v op_ optype ntyp n = Const (op_, optype) $ Free (str_of_int n, ntyp) $ v;
|
wneuper@59390
|
374 |
fun mk_num_op_num T1 T2 (op_, Top) n1 n2 =
|
wneuper@59390
|
375 |
Const (op_, Top) $ Free (str_of_int n1, T1) $ Free (str_of_int n2, T2);
|
wneuper@59392
|
376 |
fun mk_thmid thmid n1 n2 =
|
wneuper@59392
|
377 |
thmid ^ (strip_thy n1) ^ "_" ^ (strip_thy n2);
|
wneuper@59392
|
378 |
fun mk_add t1 t2 =
|
wneuper@59392
|
379 |
let
|
wneuper@59392
|
380 |
val (T1, T2) = (type_of t1, type_of t2)
|
wneuper@59392
|
381 |
in
|
wneuper@59392
|
382 |
if T1 <> T2 then raise TYPE ("mk_add gets ", [T1, T2], [t1,t2])
|
wneuper@59392
|
383 |
else (Const ("Groups.plus_class.plus", [T1, T2] ---> T1) $ t1 $ t2)
|
wneuper@59392
|
384 |
end;
|
neuper@38025
|
385 |
|
neuper@38025
|
386 |
(** transform binary numeralsstrings **)
|
neuper@38025
|
387 |
(*Makarius 100308, hacked by WN*)
|
neuper@38025
|
388 |
val numbers_to_string =
|
neuper@38025
|
389 |
let
|
neuper@38025
|
390 |
fun dest_num t =
|
neuper@38025
|
391 |
(case try HOLogic.dest_number t of
|
neuper@38025
|
392 |
SOME (T, i) =>
|
neuper@38025
|
393 |
(*if T = @{typ int} orelse T = @{typ real} then WN*)
|
neuper@38025
|
394 |
SOME (Free (signed_string_of_int i, T))
|
neuper@38025
|
395 |
(*else NONE WN*)
|
neuper@38025
|
396 |
| NONE => NONE);
|
neuper@38025
|
397 |
fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b)
|
neuper@38025
|
398 |
| to_str (t as (u1 $ u2)) =
|
neuper@38025
|
399 |
(case dest_num t of
|
neuper@38025
|
400 |
SOME t' => t'
|
neuper@38025
|
401 |
| NONE => to_str u1 $ to_str u2)
|
neuper@38025
|
402 |
| to_str t = perhaps dest_num t;
|
neuper@38025
|
403 |
in to_str end
|
neuper@38025
|
404 |
val uminus_to_string =
|
wneuper@59392
|
405 |
let
|
wneuper@59392
|
406 |
fun dest_num t =
|
wneuper@59392
|
407 |
case t of
|
wneuper@59392
|
408 |
(Const ("Groups.uminus_class.uminus", _) $ Free (s, T)) =>
|
wneuper@59392
|
409 |
(case int_of_str_opt s of
|
wneuper@59392
|
410 |
SOME i => SOME (Free (signed_string_of_int (~1 * i), T))
|
wneuper@59392
|
411 |
| NONE => NONE)
|
wneuper@59392
|
412 |
| _ => NONE;
|
wneuper@59392
|
413 |
fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b)
|
wneuper@59392
|
414 |
| to_str (t as (u1 $ u2)) =
|
wneuper@59392
|
415 |
(case dest_num t of SOME t' => t' | NONE => to_str u1 $ to_str u2)
|
wneuper@59392
|
416 |
| to_str t = perhaps dest_num t;
|
wneuper@59392
|
417 |
in to_str end;
|
neuper@38025
|
418 |
fun num_str thm =
|
wneuper@59392
|
419 |
let
|
wneuper@59392
|
420 |
val (deriv,
|
wneuper@59333
|
421 |
{cert = cert, tags = tags, maxidx = maxidx, shyps = shyps,
|
wneuper@59185
|
422 |
hyps = hyps, tpairs = tpairs, prop = prop}) = Thm.rep_thm_G thm
|
neuper@38025
|
423 |
val prop' = numbers_to_string prop;
|
wneuper@59333
|
424 |
in Thm.assbl_thm deriv cert tags maxidx shyps hyps tpairs prop' end;
|
neuper@38025
|
425 |
|
wneuper@59392
|
426 |
fun mk_Free (s,T) = Free (s, T);
|
wneuper@59392
|
427 |
fun mk_free T s = Free (s, T);
|
neuper@38025
|
428 |
|
neuper@38025
|
429 |
(*Special case: one argument cp from Isabelle2002/src/Pure/term.ML*)
|
wneuper@59392
|
430 |
fun subst_bound (arg, t) =
|
wneuper@59392
|
431 |
let
|
wneuper@59392
|
432 |
fun subst (t as Bound i, lev) =
|
wneuper@59392
|
433 |
if i < lev then t (*var is locally bound*)
|
wneuper@59392
|
434 |
else if i = lev then incr_boundvars lev arg
|
wneuper@59392
|
435 |
else Bound (i - 1) (*loose: change it*)
|
wneuper@59392
|
436 |
| subst (Abs(a, T, body), lev) = Abs (a, T, subst (body, lev + 1))
|
wneuper@59392
|
437 |
| subst (f$t, lev) = subst(f, lev) $ subst(t, lev)
|
wneuper@59392
|
438 |
| subst (t, _) = t
|
wneuper@59392
|
439 |
in subst (t, 0) end;
|
neuper@38025
|
440 |
|
wneuper@59392
|
441 |
(* instantiate let; necessary for ass_up *)
|
wneuper@59394
|
442 |
fun inst_abs (Const sT) = Const sT
|
wneuper@59393
|
443 |
| inst_abs (Free sT) = Free sT
|
wneuper@59393
|
444 |
| inst_abs (Bound n) = Bound n
|
wneuper@59393
|
445 |
| inst_abs (Var iT) = Var iT
|
wneuper@59393
|
446 |
| inst_abs (Const ("HOL.Let",T1) $ e $ (Abs (v, T2, b))) =
|
wneuper@59394
|
447 |
let val b' = subst_bound (Free (v, T2), b); (*fun variant_abs: term.ML*)
|
wneuper@59393
|
448 |
in Const ("HOL.Let", T1) $ inst_abs e $ (Abs (v, T2, inst_abs b')) end
|
wneuper@59393
|
449 |
| inst_abs (t1 $ t2) = inst_abs t1 $ inst_abs t2
|
wneuper@59393
|
450 |
| inst_abs t = t;
|
neuper@38025
|
451 |
|
neuper@38037
|
452 |
(* for parse and parse_patt: fix all types to real *)
|
neuper@38025
|
453 |
fun T_a2real (Type (s, [])) =
|
wneuper@59394
|
454 |
if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else Type (s, [])
|
neuper@38025
|
455 |
| T_a2real (Type (s, Ts)) = Type (s, map T_a2real Ts)
|
neuper@38025
|
456 |
| T_a2real (TFree (s, srt)) =
|
wneuper@59394
|
457 |
if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TFree (s, srt)
|
wneuper@59394
|
458 |
| T_a2real (TVar (("DUMMY", _), _)) = HOLogic.realT
|
neuper@38037
|
459 |
| T_a2real (TVar ((s, i), srt)) =
|
wneuper@59394
|
460 |
if s = "'a" orelse s = "'b" orelse s = "'c" then HOLogic.realT else TVar ((s, i), srt)
|
neuper@38025
|
461 |
fun typ_a2real (Const( s, T)) = (Const( s, T_a2real T))
|
neuper@38025
|
462 |
| typ_a2real (Free( s, T)) = (Free( s, T_a2real T))
|
neuper@38025
|
463 |
| typ_a2real (Var( n, T)) = (Var( n, T_a2real T))
|
neuper@38025
|
464 |
| typ_a2real (Bound i) = (Bound i)
|
neuper@38025
|
465 |
| typ_a2real (Abs(s,T,t)) = Abs(s, T, typ_a2real t)
|
neuper@38025
|
466 |
| typ_a2real (t1 $ t2) = (typ_a2real t1) $ (typ_a2real t2);
|
neuper@38025
|
467 |
|
wneuper@59394
|
468 |
(* TODO clarify parse with Test_Isac *)
|
wneuper@59394
|
469 |
fun parseold thy str = (* before 2002 *)
|
wneuper@59394
|
470 |
(let val t = ((*typ_a2real o*) numbers_to_string) (Syntax.read_term_global thy str)
|
wneuper@59184
|
471 |
in SOME (Thm.global_cterm_of thy t) end)
|
wneuper@59394
|
472 |
handle _(*EXN? ..Inner syntax error Failed to parse term*) => NONE;
|
wneuper@59394
|
473 |
fun parseN thy str = (* introduced 2002 *)
|
wneuper@59394
|
474 |
(let val t = (*(typ_a2real o numbers_to_string)*) (Syntax.read_term_global thy str)
|
wneuper@59184
|
475 |
in SOME (Thm.global_cterm_of thy t) end)
|
wneuper@59394
|
476 |
handle _(*EXN? ..Inner syntax error Failed to parse term*) => NONE;
|
wneuper@59394
|
477 |
fun parse thy str = (* introduced 2010 *)
|
wneuper@59394
|
478 |
(let val t = (typ_a2real o numbers_to_string) (Syntax.read_term_global thy str)
|
wneuper@59394
|
479 |
in SOME (Thm.global_cterm_of thy t) end)
|
wneuper@59394
|
480 |
handle _(*EXN? ..Inner syntax error Failed to parse term*) => NONE;
|
neuper@38025
|
481 |
|
neuper@41931
|
482 |
(*WN110317 parseNEW will replace parse after introduction of ctxt completed*)
|
bonzai@41949
|
483 |
fun parseNEW ctxt str = SOME (Syntax.read_term ctxt str |> numbers_to_string)
|
wneuper@59394
|
484 |
handle _ => NONE;
|
neuper@48879
|
485 |
(* parse term patterns; Var ("v",_), i.e. "?v", are required for instantiation
|
neuper@48879
|
486 |
WN130613 probably compare to
|
neuper@48879
|
487 |
http://www.mail-archive.com/isabelle-dev@mailbroy.informatik.tu-muenchen.de/msg04249.html*)
|
wneuper@59394
|
488 |
fun parse_patt thy str =
|
wneuper@59416
|
489 |
(thy, str) |>> Rule.thy2ctxt
|
wneuper@59394
|
490 |
|-> Proof_Context.read_term_pattern
|
wneuper@59394
|
491 |
|> numbers_to_string (*TODO drop*)
|
wneuper@59394
|
492 |
|> typ_a2real; (*TODO drop*)
|
wneuper@59416
|
493 |
fun str2term str = parse_patt (Rule.Thy_Info_get_theory "Isac") str
|
neuper@38025
|
494 |
|
wneuper@59394
|
495 |
(* TODO decide with Test_Isac *)
|
wneuper@59394
|
496 |
fun is_atom t = length (vars t) = 1
|
neuper@38025
|
497 |
fun is_atom (Const ("Float.Float",_) $ _) = true
|
neuper@38025
|
498 |
| is_atom (Const ("ComplexI.I'_'_",_)) = true
|
neuper@38034
|
499 |
| is_atom (Const ("Groups.times_class.times",_) $ t $ Const ("ComplexI.I'_'_",_)) = is_atom t
|
neuper@38025
|
500 |
| is_atom (Const ("Groups.plus_class.plus",_) $ t1 $ Const ("ComplexI.I'_'_",_)) = is_atom t1
|
neuper@38025
|
501 |
| is_atom (Const ("Groups.plus_class.plus",_) $ t1 $
|
neuper@38034
|
502 |
(Const ("Groups.times_class.times",_) $ t2 $ Const ("ComplexI.I'_'_",_))) =
|
neuper@38025
|
503 |
is_atom t1 andalso is_atom t2
|
neuper@38025
|
504 |
| is_atom (Const _) = true
|
neuper@38025
|
505 |
| is_atom (Free _) = true
|
neuper@38025
|
506 |
| is_atom (Var _) = true
|
neuper@38025
|
507 |
| is_atom _ = false;
|
neuper@38025
|
508 |
|
wneuper@59394
|
509 |
(* from Pure/term.ML; reports if ALL Free's have found a substitution
|
wneuper@59394
|
510 |
(required for evaluating the preconditions of _incomplete_ models) *)
|
wneuper@59394
|
511 |
fun subst_atomic_all [] t = (false (*TODO may be 'true' for some terms ?*), t)
|
wneuper@59394
|
512 |
| subst_atomic_all instl t =
|
wneuper@59394
|
513 |
let
|
wneuper@59394
|
514 |
fun subst (Abs (a, T, body)) =
|
wneuper@59394
|
515 |
let
|
wneuper@59394
|
516 |
val (all, body') = subst body
|
wneuper@59394
|
517 |
in (all, Abs(a, T, body')) end
|
wneuper@59394
|
518 |
| subst (f$tt) =
|
wneuper@59394
|
519 |
let
|
wneuper@59394
|
520 |
val (all1, f') = subst f
|
wneuper@59394
|
521 |
val (all2, tt') = subst tt
|
wneuper@59394
|
522 |
in (all1 andalso all2, f' $ tt') end
|
wneuper@59394
|
523 |
| subst (t as Free _) =
|
wneuper@59394
|
524 |
if is_num t then (true, t) (*numerals cannot be subst*)
|
wneuper@59394
|
525 |
else (case assoc (instl, t) of
|
wneuper@59394
|
526 |
SOME t' => (true, t')
|
wneuper@59394
|
527 |
| NONE => (false, t))
|
wneuper@59394
|
528 |
| subst t = (true, if_none (assoc(instl,t)) t)
|
wneuper@59394
|
529 |
in subst t end;
|
wneuper@59389
|
530 |
|
wneuper@59507
|
531 |
fun op contains_one_of (thm, ids) =
|
wneuper@59507
|
532 |
Term.exists_Const (fn id => member op= ids id) (Thm.prop_of thm)
|
wneuper@59507
|
533 |
|
wneuper@59532
|
534 |
fun var_for vs (t as Const (str, _)) id = if id = strip_thy str then t :: vs else vs
|
wneuper@59532
|
535 |
| var_for vs (t as Free (str, _)) id = if id = str then t :: vs else vs
|
wneuper@59532
|
536 |
| var_for vs (t as Var (idn, _)) id = if id = Term.string_of_vname idn then t :: vs else vs
|
wneuper@59532
|
537 |
| var_for vs (Bound _) _ = vs
|
wneuper@59532
|
538 |
| var_for vs (Abs (_, _, t)) id = var_for vs t id
|
wneuper@59532
|
539 |
| var_for vs (t1 $ t2) id = (var_for vs t1 id) @ (var_for vs t2 id)
|
wneuper@59532
|
540 |
|
wneuper@59532
|
541 |
val poly_consts = (* TODO: adopt syntax-const from Isabelle*)
|
wneuper@59532
|
542 |
["Groups.plus_class.plus", "Groups.minus_class.minus",
|
wneuper@59532
|
543 |
"Rings.divide_class.divide", "Groups.times_class.times",
|
wneuper@59532
|
544 |
"Atools.pow"];
|
wneuper@59532
|
545 |
(* treat Free, Const, Var as variables in polynomials *)
|
wneuper@59532
|
546 |
fun vars_of t =
|
wneuper@59532
|
547 |
let
|
wneuper@59532
|
548 |
val var_ids = t |> ids2str |> subtract op = poly_consts |> map strip_thy |> sort string_ord
|
wneuper@59532
|
549 |
in (map (var_for [] t) var_ids) |> flat |> distinct end
|
wneuper@59532
|
550 |
|
wneuper@59577
|
551 |
(* this may decompose an object-language isa-list;
|
wneuper@59577
|
552 |
use only, if description is not available, eg. not input ?WN:14.5.03 ??!?*)
|
wneuper@59577
|
553 |
fun dest_list' t = if is_list t then isalist2list t else [t];
|
wneuper@59577
|
554 |
|
wneuper@59389
|
555 |
end |