this Boolector program printing output in binary format. But I need in hexadecimal format.
so how to print hexdecimal format in boolector.
(set-logic QF_BV)
(set-info :smt-lib-version 2.0)
(declare-const val1 (_ BitVec 16))
(declare-const val2 (_ BitVec 16))
(declare-const gen_mul (_ BitVec 16))
(declare-const eval1 (_ BitVec 32))
(declare-const eval2 (_ BitVec 32))
(declare-const org_mul (_ BitVec 32))
(declare-const rem17 (_ BitVec 32))
(declare-const res (_ BitVec 16))
(assert (= gen_mul (bvmul val1 val2)))
(assert (= eval1 (concat #x0000 val1)))
(assert (= eval2 (concat #x0000 val2)))
(assert (= org_mul (bvmul eval1 eval2)))
(assert (= rem17 (bvurem org_mul #x00010001)))
(assert (= res ((_ extract 15 0) rem17)))
(assert (= val1 #xb621))
(assert (= val2 #xd620))
(check-sat)
(get-value (val1))
(get-value (val2))
(get-value (org_mul))
(get-value (gen_mul))
(get-value (eval1))
(get-value (eval2))
(get-value (org_mul))
(get-value (rem17))
(get-value (res))
(exit)
Run:
./boolector ex.smt2
Boolector (at least version 2.2.0) has the -x, --hex options to force hexadecimal output. It's possible that it may ignore those options if a bit-vector's size is not a multiple of 4.
Related
I'm working on an assignment from one of my CS courses and got kinda stuck.
The assignment is to create a message-oriented implementation of associative arrays. These arrays should understand (among others) the following messages:
cdr: returns an associative array that contains the current array without the first pair, or an empty array if the array is empty
cons: expects a pair (key value) as an argument and generates a new associative array by prepending the given key-value-pair to the current array
Basically, the associative array is a list of pairs like ((a 1) (b 7) (c 4) (a 6) (c 5) (d 4) (b 2)) where the characters are the keys and the integers are the values in this case.
To implement the message-oriented style, we should use a dispatch function that takes two arguments (where the second argument is only used by the messages that understand it).
What I have so far is this:
(define (make-empty-as)
(define (dispatch msg arg)
(let ((lst '()))
(cond
((eq? msg 'cdr)
(cond ((not (null? lst)) (set! lst (cdr lst))))
dispatch)
((eq? msg 'cons)
(cond ((= (length arg) 2) (set! lst (append lst (list arg)))))
dispatch)
;; other messages ...
(else (error "Don't understand " msg)))))
dispatch)
To construct the array above, for example, we could do this:
(define a1 (make-empty-as))
(define a2 (a1 'cons '(b 2)))
(define a3 (a2 'cons '(d 4)))
(define a4 (a3 'cons '(c 5)))
(define a5 (a4 'cons '(a 6)))
(define a6 (a5 'cons '(c 4)))
(define a7 (a6 'cons '(b 7)))
(define a8 (a7 'cons '(a 1)))
;; a8 should now have the list ((a 1) (b 7) (c 4) (a 6) (c 5) (d 4) (b 2))
(a8 'cdr 'dummy) ;; should be the list ((b 7) (c 4) (a 6) (c 5) (d 4) (b 2))
My problem is the two functions that should return an associative array again.
My approach to return dispatch again does not work because this does not keep the modifications I did to lst.
Coming from object-oriented languages I tried to find a way to modify the instance variable lst and return "myself" (i.e. the modified version of the "object") which would be the function that is currently evaluated(?). Googling around did not bring up anything helpful, unfortunately.
So, the question is if there actually is a way to achieve this or if not if there's another way that I'm missing.
Thanks.
Just for completeness: #ad absurdum's comment was actually the solution for my problem.
However, the approach of simply wrapping the let around the define resulted in only one "object" being modified by the cons messages. I.e. changing the code to this
(define (make-empty-as)
(let ((lst '()))
(define (dispatch msg arg)
(cond
((eq? msg 'cdr)
(cond ((not (null? lst)) (set! lst (cdr lst))))
dispatch)
((eq? msg 'cons)
(cond ((= (length arg) 2) (set! lst (append (list arg) lst))))
dispatch)
((eq? msg 'print) (display lst) (newline)) ;; added for debugging
(else (error "Don't understand " msg))))
dispatch))
and running
(define a1 (make-empty-as))
(a1 'print 'dummy)
(define a2 (a1 'cons '(b 2)))
(a2 'print 'dummy)
(define a3 (a2 'cons '(d 4)))
(a3 'print 'dummy)
(define a4 (a3 'cons '(c 5)))
(a4 'print 'dummy)
(define a5 (a4 'cons '(a 6)))
(a5 'print 'dummy)
(define a6 (a5 'cons '(c 4)))
(a6 'print 'dummy)
(define a7 (a6 'cons '(b 7)))
(a7 'print 'dummy)
(define a8 (a7 'cons '(a 1)))
(a8 'print 'dummy)
(a1 'print 'dummy)
resulted in the following output
() ;; a1
((b 2)) ;; a2
((d 4) (b 2)) ;; a3
((c 5) (d 4) (b 2)) ;; a4
((a 6) (c 5) (d 4) (b 2)) ;; a5
((c 4) (a 6) (c 5) (d 4) (b 2)) ;; a6
((b 7) (c 4) (a 6) (c 5) (d 4) (b 2)) ;; a7
((a 1) (b 7) (c 4) (a 6) (c 5) (d 4) (b 2)) ;; a8
((a 1) (b 7) (c 4) (a 6) (c 5) (d 4) (b 2)) ;; a1
meaning all of a1 to a8 actually represented the same "object".
But the assignment actually wanted cons and cdr to return copies of the associative arrays. Therefore, I simply added a copy message that copies the internal lst from another object's lst and changed cons and cdr to return new associative arrays:
(define (make-empty-as)
(let ((lst '()))
(define (dispatch msg arg)
(cond
((eq? msg 'cdr)
(cond
((not (null? lst)) ((make-empty-as) 'copy (cdr lst)))
(else (make-empty-as))))
((eq? msg 'cons)
(cond
((= (length arg) 2) ((make-empty-as) 'copy (append (list arg) lst)))
(else (make-empty-as))))
((eq? msg 'print) (display lst) (newline))
((eq? msg 'copy)
(cond
((list? arg) (set! lst arg))
(else (set! lst '())))
dispatch)
(else (error "Don't understand " msg))))
dispatch))
Running
(define a1 (make-empty-as))
(a1 'print 'dummy)
(define a2 (a1 'cons '(b 2)))
(a2 'print 'dummy)
(define a3 (a2 'cons '(d 4)))
(a3 'print 'dummy)
(define a4 (a3 'cons '(c 5)))
(a4 'print 'dummy)
(define a5 (a4 'cons '(a 6)))
(a5 'print 'dummy)
(define a6 (a5 'cons '(c 4)))
(a6 'print 'dummy)
(define a7 (a6 'cons '(b 7)))
(a7 'print 'dummy)
(define a8 (a7 'cons '(a 1)))
(a8 'print 'dummy)
(a1 'print 'dummy)
again now gives the desired result
() ;; a1
((b 2)) ;; a2
((d 4) (b 2)) ;; a3
((c 5) (d 4) (b 2)) ;; a4
((a 6) (c 5) (d 4) (b 2)) ;; a5
((c 4) (a 6) (c 5) (d 4) (b 2)) ;; a6
((b 7) (c 4) (a 6) (c 5) (d 4) (b 2)) ;; a7
((a 1) (b 7) (c 4) (a 6) (c 5) (d 4) (b 2)) ;; a8
() ;; a1
Thanks, again to #ad absurdum for nudging me in the right direction.
I want to solve this in the z3 solver with bit vector 48:
(declare-fun x () Int)
(declare-fun y () Int)
(assert (= *someNumber* (* x y)))
(assert (> x 1))
(assert (> y 1))
(check-sat)
(get-model)
(exit)
I'm trying to figure out how to use the arithmetic function however, it does not work out very well.
The problems with that (for me) are the correct Syntax of the functions && how to set the values in there.
(set-option :produce-models true)
(set-logic QF_BV)
;; Declaring all the variables
(declare-const a (_ BitVec 48))
(declare-const b (_ BitVec 48))
(declare-const c (_ BitVec 48))
;; Soft constraints to limit reuse
(assert (= c #xnumberInHex))
(assert-soft (not (= a b)))
(check-sat-using (then simplify solve-eqs bit-blast sat))
(simplify (= c (bvmul a b))
(simplify (bvugt a #b000000000001))
(simplify (bvugt b #b000000000001))
(check-sat)
(get-model)
Any help is much appreciated.
Syntax / how to write the correct bit vector there
Looks like you've got almost all the pieces in there, but perhaps not exactly getting the syntax right. Here's a complete encoding with c = 18:
(set-option :produce-models true)
(set-logic ALL)
;; Declaring all the variables
(declare-const a (_ BitVec 48))
(declare-const b (_ BitVec 48))
(declare-const c (_ BitVec 48))
(assert (= c #x000000000012)) ; 18 is 0x12 in hex
(assert (= c (bvmul a b)))
; don't allow overflow
(assert (bvumul_noovfl a b))
(assert (bvult #x000000000001 a))
(assert (bvult #x000000000001 b))
;; Soft constraints to limit reuse
(assert-soft (not (= a b)))
(check-sat)
(get-model)
Note the use of the ALL logic and the function bvumul_noovfl which detects unsigned bit-vector multiplication overflow. (This function is z3 specific, and is only available when you pick the logic to be ALL.) Since you're doing bit-vector arithmetic, it is subject to wrap-around, and I'm guessing this is something you'd like to avoid. By explicitly stating we do not want the multiplication of a and b to overflow, we're achieving that goal.
For this input, z3 says:
sat
(model
(define-fun b () (_ BitVec 48)
#x000000000009)
(define-fun a () (_ BitVec 48)
#x000000000002)
(define-fun c () (_ BitVec 48)
#x000000000012)
)
which correctly factors the number 18 (written here in hexadecimal as 12) into 2 and 9.
Note that multiplication is a hard problem. As you increase the bit-size (here you picked 48, but could be larger), or if the number c itself gets larger, the problem will become harder and harder for z3 to solve. This is, of course, not surprising: Factorization is a hard problem in general, and there's no magic here for z3 to correctly factor your input value without solving a huge set of internal equations, which increase exponentially in size as the bit-width increases.
But fear not: Bit-vector logic is complete: This means that z3 will always be able to factor, albeit slowly, assuming you do not run out of memory or patience first!
This is what I did now.
It might help others in the future:
(set-option :produce-models true)
(set-logic ALL)
;; Declaring all the variables
(declare-const a (_ BitVec 48))
(declare-const b (_ BitVec 48))
(declare-const c (_ BitVec 48))
(assert (= c #x00000000affe))
(assert (= c (bvmul a b)))
; don't allow overflow
(assert (= c (bvumul_noovfl a b)))
(assert (bvult #x000000000001 a))
(assert (bvult a c))
(assert (bvult #x000000000001 b))
(assert (bvult b c))
;; Soft constraints to limit reuse
(assert-soft (not (= a b)))
(check-sat)
(get-model)
I added two more asserts to make sure a or b does not exceed c (the input in hexadecimal)
in this example, I used "affe" that's 45054 in decimal.
it should work for bigger ones as well.
Output:
sat
(model
(define-fun b () (_ BitVec 48)
#x00000000138e)
(define-fun a () (_ BitVec 48)
#x000000000009)
(define-fun c () (_ BitVec 48)
#x00000000affe)
)
hex: 138e * 9 = affe
dec: 5006 * 9 = 45054
Hope this will help someone else in the future.
Can someone explain me how can I import the WEKA created rules in CLIPS and evaluate its efficience in TRS and TES data?
The data I use
I have written 7 rules out of 20 from WEKA tree. I include also 3 instances from the glass datasheet
small test code
(deftemplate glass
(slot n(type FLOAT))
(slot m(type FLOAT))
(slot a(type FLOAT))
(slot b(type FLOAT))
(slot r(type FLOAT))
(slot s(type FLOAT))
(slot k(type FLOAT))
(slot c(type FLOAT)))
(deftemplate Type
(slot type))
(deffacts instances1
(glass (n 13.00)
(m 2.28)
(a 1.00)
(b 0.00)))
(deffacts instances2
(glass (n 13.70)
(m 1.80)
(a 1.40)
(b 0.00)))
(deffacts instances3
(glass (n 13.70)
(m 1.90)
(a 1.40)
(b 0.00)))
(defrule R1
(glass (b ?b))
(test (<= ?b 0.27))
(glass (m ?m))
(test (<= ?m 2.41))
(glass (n ?n))
(test (<= ?n 13.78))
(glass (a ?a))
(test (<= ?a 1.38))
=>
(assert (Type (type buildwindnonfloat1)))
(printout t "buildwindnonfloat1 detected" crlf))
(defrule R2
(glass (b ?b))
(test (<= ?b 0.27))
(glass (m ?m))
(test (<= ?m 2.41))
(glass (n ?n))
(test (<= ?n 13.78))
(glass (a ?a))
(test (> ?a 1.38))
(glass (m ?m))
(test (<= ?m 1.88))
=>
(assert (Type (type containers2)))
(printout t "containers2 detected" crlf))
(defrule R3
(glass (b ?b))
(test (<= ?b 0.27))
(glass (m ?m))
(test (<= ?m 2.41))
(glass (n ?n))
(test (<= ?n 13.78))
(glass (a ?a))
(test (> ?a 1.38))
(glass (m ?m))
(test (> ?m 1.88))
=>
(assert (Type (type buildwindnonfloat3)))
(printout t "buildwindnonfloat3 detected" crlf))
(defrule R4
(glass (b ?b))
(test (<= ?b 0.27))
(glass (m ?m))
(test (<= ?m 2.41))
(glass (n ?n))
(test (> ?n 13.78))
=>
(assert (Type (type tableware4)))
(printout t "tableware detected" crlf))
(defrule R5
(glass (b ?b))
(test (<= ?b 0.27))
(glass (m ?m))
(test (> ?m 2.41))
(glass (a ?a))
(test (<= ?a 1.4))
(glass (m ?m))
(test (<= ?m 3.34))
(glass (a ?a))
(test (<= ?a 1.25))
=>
(assert (Type (type buildwindnonfloat5)))
(printout t "buildwindnonfloat5 detected" crlf))
(defrule R6
(glass (b ?b))
(test (<= ?b 0.27))
(glass (m ?m))
(test (> ?m 2.41))
(glass (a ?a))
(test (<= ?a 1.4))
(glass (m ?m))
(test (<= ?m 3.34))
(glass (a ?a))
(test (> ?a 1.25))
=>
(assert (Type (type buildwindfloat6)))
(printout t "buildwindfloat6 detected" crlf))
(defrule R7
(glass (b ?b))
(test (<= ?b 0.27))
(glass (m ?m))
(test (> ?m 2.41))
(glass (a ?a))
(test (<= ?a 1.4))
(glass (m ?m))
(test (> ?m 3.34))
(glass (m ?m))
(test (<= ?m 3.82))
(glass (r ?r))
(test (<= ?r 1.51707))
(glass (r ?r))
(test (<= ?r 51596))
=>
(assert (Type (type buildwindfloat7)))
(printout t "buildwindfloat7 detected" crlf))
To convert your data, it's easiest to read the data from the file when your program is running and directly assert the facts. So if your data looks like the following with each entry on its own line
1.5159,13.24,3.34,1.47,73.1,0.39,8.22,0,0,'build wind non-float'
1.5167,13.24,3.57,1.38,72.7,0.56,8.44,0,0.1,'vehic wind float'
then your can read your data by reading each line as a single string, replacing the commas with spaces, and then splitting the string into multiple values. You can then have a separate rule map the values from your file to the appropriate slots in your deftemplate facts.
Store the expected result with each glass fact and then you can compare that value to the value that your rule is proposing.
CLIPS (6.31 6/12/19)
CLIPS>
(deftemplate glass
(slot n (type FLOAT))
(slot m (type FLOAT))
(slot a (type FLOAT))
(slot b (type FLOAT))
(slot r (type FLOAT))
(slot s (type FLOAT))
(slot k (type FLOAT))
(slot c (type FLOAT))
(slot f (type FLOAT))
(slot type))
CLIPS>
(deftemplate input
(multislot data))
CLIPS>
(deffunction str-rpl (?str ?find ?replace)
(if (eq ?find "")
then
(return ?str))
(bind ?rs "")
(bind ?fl (str-length ?find))
(bind ?i (str-index ?find ?str))
(while (neq ?i FALSE)
(bind ?rs (str-cat ?rs (sub-string 1 (- ?i 1) ?str) ?replace))
(bind ?str (sub-string (+ ?i ?fl) (str-length ?str) ?str))
(bind ?i (str-index ?find ?str)))
(bind ?rs (str-cat ?rs ?str))
?rs)
CLIPS>
(defrule get-data
=>
(printout t "Input File? ")
(bind ?file (readline))
(if (not (open ?file data))
then
(printout t "Unable to open file" crlf)
(return))
(bind ?line (readline data))
(while (neq ?line EOF)
(bind ?line (str-rpl ?line "," " "))
(bind ?line (str-rpl ?line "'" "\""))
(assert (input (data (explode$ ?line))))
(bind ?line (readline data)))
(close data))
CLIPS>
(defrule convert-data
?i <- (input (data ?r ?n ?m ?a ?s ?k ?c ?b ?f ?type))
=>
(retract ?i)
(assert (glass (r ?r) (n ?n) (m ?m) (a ?a) (s ?s) (k ?k) (c ?c) (b ?b) (f ?f) (type ?type))))
CLIPS>
(defrule R1
(glass (b ?b)
(m ?m)
(n ?n)
(a ?a)
(type ?type))
(test (<= ?b 0.27))
(test (<= ?m 2.41))
(test (<= ?n 13.78))
(test (<= ?a 1.38))
=>
(printout t "buildwindnonfloat1 detected type = " ?type crlf))
CLIPS>
(defrule R2
(glass (b ?b)
(m ?m)
(n ?n)
(a ?a)
(type ?type))
(test (<= ?b 0.27))
(test (<= ?m 2.41))
(test (<= ?n 13.78))
(test (> ?a 1.38))
(test (<= ?m 1.88))
=>
(printout t "containers2 detected type = " ?type crlf))
CLIPS>
(defrule R3
(glass (b ?b)
(m ?m)
(n ?n)
(a ?a)
(type ?type))
(test (<= ?b 0.27))
(test (<= ?m 2.41))
(test (<= ?n 13.78))
(test (> ?a 1.38))
(test (> ?m 1.88))
=>
(printout t "buildwindnonfloat3 detected type = " ?type crlf))
CLIPS>
(defrule R4
(glass (b ?b)
(m ?m)
(n ?n)
(type ?type))
(test (<= ?b 0.27))
(test (<= ?m 2.41))
(test (> ?n 13.78))
=>
(printout t "tableware detected type = " ?type crlf))
CLIPS>
(defrule R5
(glass (b ?b)
(m ?m)
(a ?a)
(type ?type))
(test (<= ?b 0.27))
(test (> ?m 2.41))
(test (<= ?a 1.4))
(test (<= ?m 3.34))
(test (<= ?a 1.25))
=>
(printout t "buildwindnonfloat5 detected type = " ?type crlf))
CLIPS>
(defrule R6
(glass (b ?b)
(m ?m)
(a ?a)
(type ?type))
(test (<= ?b 0.27))
(test (> ?m 2.41))
(test (<= ?a 1.4))
(test (<= ?m 3.34))
(test (> ?a 1.25))
=>
(printout t "buildwindfloat6 detected type = " ?type crlf))
CLIPS>
(defrule R7
(glass (b ?b)
(m ?m)
(a ?a)
(r ?r)
(type ?type))
(test (<= ?b 0.27))
(test (> ?m 2.41))
(test (<= ?a 1.4))
(test (> ?m 3.34))
(test (<= ?m 3.82))
(test (<= ?r 1.51707))
(test (<= ?r 51596))
=>
(printout t "buildwindfloat7 detected type = " ?type crlf))
CLIPS> (reset)
CLIPS> (run)
Input File? weka.txt
buildwindfloat7 detected type = vehic wind float
CLIPS>
How to make fact in multiple type? like in this code get the same rank fact together.
(P X Y) means X is Y's elder member
i had tried this:
(deffacts people
(P a b)
(P b c)
(P a d)
(P d e)
(P d f)
)
(defrule ranking
(P ?x ?y)
(P ?y ?z)
=>
(assert (R ?x $?y $?z))
)
i want to make a complete seniority in the family,
and get (R a bd cef), but i just get (R a b c) (R a d e) (R a d f)
can u help me?
It's a bit more complicated than what you've attempted, particularly if you want it to work properly for more than 3 generations and/or multiple family groups.
CLIPS>
(defmethod concat$ ((?m1 MULTIFIELD) (?m2 MULTIFIELD (>= (length$ ?m1) (length$ ?m2))))
(bind ?rv (create$))
(loop-for-count (?i 1 (length$ ?m2))
(bind ?rv (create$ ?rv (sym-cat (nth$ ?i ?m1) (nth$ ?i ?m2)))))
(create$ ?rv (mv-subseq (+ 1 (length$ ?m2)) (length$ ?m1) ?m1)))
CLIPS>
(defmethod concat$ ((?m1 MULTIFIELD) (?m2 MULTIFIELD (< (length$ ?m1) (length$ ?m2))))
(bind ?rv (create$))
(loop-for-count (?i 1 (length$ ?m1))
(bind ?rv (create$ ?rv (sym-cat (nth$ ?i ?m1) (nth$ ?i ?m2)))))
(create$ ?rv (mv-subseq (+ 1 (length$ ?m1)) (length$ ?m2) ?m2)))
CLIPS>
(deffacts people
(P a b) ; Family 1
(P b c)
(P a d)
(P d e)
(P d f)
(P g h) ; Family 2
(P h j)
(P h k)
(P k l)
(P k m)
(P k n)
(P j o)
(P j p)
(P j q)
(P q r)
(P q s))
CLIPS>
(defrule copy
(P ?x ?y)
=>
(assert (R ?x ?y)))
CLIPS>
(defrule extend
?f1 <- (R $?b ?x ?ym $?e1)
?f2 <- (R ?y $?z)
(test (str-index ?y ?ym))
=>
(retract ?f1 ?f2)
(assert (R ?b ?x ?ym (concat$ ?e1 ?z))))
CLIPS>
(defrule combine
?f1 <- (R ?x $?b ?y1 $?e1)
?f2 <- (R ?x $?b ?y2&~?y1 $?e2)
=>
(retract ?f1 ?f2)
(assert (R ?x ?b (sym-cat ?y1 ?y2) (concat$ ?e1 ?e2))))
CLIPS> (reset)
CLIPS> (run)
CLIPS> (facts)
f-0 (initial-fact)
f-1 (P a b)
f-2 (P b c)
f-3 (P a d)
f-4 (P d e)
f-5 (P d f)
f-6 (P g h)
f-7 (P h j)
f-8 (P h k)
f-9 (P k l)
f-10 (P k m)
f-11 (P k n)
f-12 (P j o)
f-13 (P j p)
f-14 (P j q)
f-15 (P q r)
f-16 (P q s)
f-37 (R g h jk opqlmn rs)
f-46 (R a bd cef)
For a total of 19 facts.
CLIPS>
Let's say that I have a rule like this:
(defrule get_next_N_poz
?id <- (get_next_poz $?)
(world (limit $?) (ball ?b1 ?b2) (men $? ?x ?y - $?) (id ?))
(and
(test (= ?x ?b1))
(test (= ?y (- ?b2 1))))
=>
(printout t "north ready position:" ?x ?y)
(modify ?id (get_next_poz 1)))
How do I add a new "and"?
Thank you.
It depends on what logic you're trying to implement. The existing and you have is redundant anyway, but if you wanted a second one, you'd just add it after the end of the last:
(and
(test (= ?x ?b1))
(test (= ?y (- ?b2 1))))
(and
(test (= ?x ?b2))
(test (= ?y (+ ?b1 1))))
If you wanted one or the other of these conditions you'd do this:
(or (and
(test (= ?x ?b1))
(test (= ?y (- ?b2 1))))
(and
(test (= ?x ?b2))
(test (= ?y (+ ?b1 1)))))
Rather than using and/or conditional elements, you could use the and/or boolean functions within a single test conditional element:
(test (or (and (= ?x ?b1)
(= ?y (- ?b2 1)))
(and (= ?x ?b2)
(= ?y (+ ?b1 1)))))