1 (* ID: $Id$ |
|
2 Author: Amine Chaieb, TU Muenchen |
|
3 |
|
4 The oracle for Mixed Real-Integer auantifier elimination |
|
5 based on the verified Code in ~/work/MIR/MIR.thy. |
|
6 *) |
|
7 |
|
8 structure ReflectedFerrack = |
|
9 struct |
|
10 |
|
11 open Ferrack; |
|
12 |
|
13 exception LINR; |
|
14 |
|
15 (* pseudo reification : term -> intterm *) |
|
16 val rT = Type ("RealDef.real",[]); |
|
17 val bT = HOLogic.boolT; |
|
18 val realC = @{term "RealDef.real :: int => real"}; |
|
19 val rzero = @{term "0 :: real"}; |
|
20 |
|
21 fun num_of_term vs t = case t |
|
22 of Free(xn,xT) => (case AList.lookup (op =) vs t of |
|
23 NONE => error "Variable not found in the list!" |
|
24 | SOME n => Bound n) |
|
25 | Const("RealDef.real",_)$ @{term "0::int"} => C 0 |
|
26 | Const("RealDef.real",_)$ @{term "1::int"} => C 1 |
|
27 | @{term "0::real"} => C 0 |
|
28 | @{term "0::real"} => C 1 |
|
29 | Term.Bound i => Bound i |
|
30 | Const (@{const_name "HOL.uminus"},_)$t' => Neg (num_of_term vs t') |
|
31 | Const (@{const_name "HOL.plus"},_)$t1$t2 => Add (num_of_term vs t1,num_of_term vs t2) |
|
32 | Const (@{const_name "HOL.minus"},_)$t1$t2 => Sub (num_of_term vs t1,num_of_term vs t2) |
|
33 | Const (@{const_name "HOL.times"},_)$t1$t2 => (case (num_of_term vs t1) of C i => |
|
34 Mul (i,num_of_term vs t2) |
|
35 | _ => error "num_of_term: unsupported Multiplication") |
|
36 | Const("RealDef.real",_) $ Const (@{const_name "Int.number_of"},_)$t' => C (HOLogic.dest_numeral t') |
|
37 | Const (@{const_name "Int.number_of"},_)$t' => C (HOLogic.dest_numeral t') |
|
38 | _ => error ("num_of_term: unknown term " ^ Syntax.string_of_term_global Pure.thy t); |
|
39 |
|
40 (* pseudo reification : term -> fm *) |
|
41 fun fm_of_term vs t = |
|
42 case t of |
|
43 Const("True",_) => T |
|
44 | Const("False",_) => F |
|
45 | Const(@{const_name HOL.less},_)$t1$t2 => Lt (Sub (num_of_term vs t1,num_of_term vs t2)) |
|
46 | Const(@{const_name HOL.less_eq},_)$t1$t2 => Le (Sub (num_of_term vs t1,num_of_term vs t2)) |
|
47 | Const("op =",eqT)$t1$t2 => |
|
48 if (domain_type eqT = rT) |
|
49 then Eq (Sub (num_of_term vs t1,num_of_term vs t2)) |
|
50 else Iff(fm_of_term vs t1,fm_of_term vs t2) |
|
51 | Const("op &",_)$t1$t2 => And(fm_of_term vs t1,fm_of_term vs t2) |
|
52 | Const("op |",_)$t1$t2 => Or(fm_of_term vs t1,fm_of_term vs t2) |
|
53 | Const("op -->",_)$t1$t2 => Imp(fm_of_term vs t1,fm_of_term vs t2) |
|
54 | Const("Not",_)$t' => Not(fm_of_term vs t') |
|
55 | Const("Ex",_)$Term.Abs(xn,xT,p) => |
|
56 E(fm_of_term (map (fn(v,n) => (v,1+ n)) vs) p) |
|
57 | Const("All",_)$Term.Abs(xn,xT,p) => |
|
58 A(fm_of_term (map (fn(v,n) => (v,1+ n)) vs) p) |
|
59 | _ => error ("fm_of_term : unknown term!" ^ Syntax.string_of_term_global Pure.thy t); |
|
60 |
|
61 |
|
62 fun start_vs t = |
|
63 let |
|
64 val fs = term_frees t |
|
65 in fs ~~ (0 upto (length fs - 1)) end; |
|
66 |
|
67 (* transform num and fm back to terms *) |
|
68 |
|
69 fun myassoc2 l v = |
|
70 case l of |
|
71 [] => NONE |
|
72 | (x,v')::xs => if v = v' then SOME x |
|
73 else myassoc2 xs v; |
|
74 |
|
75 fun term_of_num vs t = |
|
76 case t of |
|
77 C i => realC $ (HOLogic.mk_number HOLogic.intT i) |
|
78 | Bound n => the (myassoc2 vs n) |
|
79 | Neg t' => Const(@{const_name HOL.uminus},rT --> rT)$(term_of_num vs t') |
|
80 | Add(t1,t2) => Const(@{const_name HOL.plus},[rT,rT] ---> rT)$ |
|
81 (term_of_num vs t1)$(term_of_num vs t2) |
|
82 | Sub(t1,t2) => Const(@{const_name HOL.minus},[rT,rT] ---> rT)$ |
|
83 (term_of_num vs t1)$(term_of_num vs t2) |
|
84 | Mul(i,t2) => Const(@{const_name HOL.times},[rT,rT] ---> rT)$ |
|
85 (term_of_num vs (C i))$(term_of_num vs t2) |
|
86 | Cn(n,i,t) => term_of_num vs (Add (Mul(i,Bound n),t)); |
|
87 |
|
88 fun term_of_fm vs t = |
|
89 case t of |
|
90 T => HOLogic.true_const |
|
91 | F => HOLogic.false_const |
|
92 | Lt t => Const(@{const_name HOL.less},[rT,rT] ---> bT)$ |
|
93 (term_of_num vs t)$ rzero |
|
94 | Le t => Const(@{const_name HOL.less_eq},[rT,rT] ---> bT)$ |
|
95 (term_of_num vs t)$ rzero |
|
96 | Gt t => Const(@{const_name HOL.less},[rT,rT] ---> bT)$ |
|
97 rzero $(term_of_num vs t) |
|
98 | Ge t => Const(@{const_name HOL.less_eq},[rT,rT] ---> bT)$ |
|
99 rzero $(term_of_num vs t) |
|
100 | Eq t => Const("op =",[rT,rT] ---> bT)$ |
|
101 (term_of_num vs t)$ rzero |
|
102 | NEq t => term_of_fm vs (Not (Eq t)) |
|
103 | Not t' => HOLogic.Not$(term_of_fm vs t') |
|
104 | And(t1,t2) => HOLogic.conj$(term_of_fm vs t1)$(term_of_fm vs t2) |
|
105 | Or(t1,t2) => HOLogic.disj$(term_of_fm vs t1)$(term_of_fm vs t2) |
|
106 | Imp(t1,t2) => HOLogic.imp$(term_of_fm vs t1)$(term_of_fm vs t2) |
|
107 | Iff(t1,t2) => (HOLogic.eq_const bT)$(term_of_fm vs t1)$ |
|
108 (term_of_fm vs t2) |
|
109 | _ => error "If this is raised, Isabelle/HOL or generate_code is inconsistent!"; |
|
110 |
|
111 (* The oracle *) |
|
112 |
|
113 fun linrqe_oracle thy t = |
|
114 let |
|
115 val vs = start_vs t |
|
116 in HOLogic.mk_Trueprop (HOLogic.mk_eq (t, term_of_fm vs (linrqe (fm_of_term vs t)))) end; |
|
117 |
|
118 end; |
|