Accessing members of composite sorts (data types) in SMT-LIBv2 - smt

According to sec. 3.9.3 of The SMT-LIBv2 Language and Tools: A Tutorial it is possible to declare a composite sort like this in SMT-LIBv2:
(set-logic QF_UF)
(declare-sort Triple 3)
(declare-fun state () (Triple Bool Bool Bool))
I am using CVC4 and it seems to accept this syntax. But how do I access the elements? I tried the following (and various variations of that and other things I found online):
(assert (_ state 1))
(assert (select 1 state))
But it looks like those operators only work on vectors and arrays. I can't find any example that uses such composite sorts and can't find anything about accessing those elements in the tutorial or the language standard. How is it done? Or did I completely misunderstand what this feature is for?
My application: I want to encode a temporal problem and want to do it in form of a state transition function that converts the old state into a new state, so I can write something like the following when experimenting with the system:
....
(declare-fun initial_state () MyStateSort)
(declare-fun state_after_step_1 () MyStateSort)
(declare-fun state_after_step_2 () MyStateSort)
(assert (= (MyTransitionFunc initial_state) state_after_step_1)
(assert (= (MyTransitionFunc state_after_step_1) state_after_step_2)

This is an answer to my own question. If anyone can post an example for a use-case of a user-defined sort with arity > 0, I'll happily accept that as answer.
After reading the SMT-LIBv2 standard more carefully I am now thinking that sort declarations with arity > 0 only have an application in theory definitions (for declaring sorts like Array), not in user-supplied code. The example in David Cok's Tutorial seems to be misleading, as it suggests that this can be used to declare composite sorts. I have not found any indication of that anywhere else. This includes the complete SMT-LIB benchmark that contains not a single sort declaration with arity > 0.
Instead of "composite sorts" one should use uninterpreted functions to create the equivalent of complex data structures. Example:
(set-logic QF_UF)
(declare-sort CONTAINER_SORT 0)
(declare-fun CONTAINER_MEMBER_1 (CONTAINER_SORT) Bool)
(declare-fun CONTAINER_MEMBER_2 (CONTAINER_SORT) Bool)
(declare-fun INSTANCE_1 () CONTAINER_SORT)
(declare-fun INSTANCE_2 () CONTAINER_SORT)
This will effectively declare the following 4 independent Bool expressions.
(CONTAINER_MEMBER_1 INSTANCE_1)
(CONTAINER_MEMBER_2 INSTANCE_1)
(CONTAINER_MEMBER_1 INSTANCE_2)
(CONTAINER_MEMBER_2 INSTANCE_2)

Related

Produce multiple models for CVC4 SMT queries

Can I get multiple models for a query like the following?
(set-logic LIA)
(set-option :produce-models true)
(declare-const x Int)
(assert (< x 20))
(check-sat)
(get-model)
Instead of just
sat
(
(define-fun x () Int 0)
)
I'd like to get 0, 1, -1, 2, ...
SMTLib language does not have a mechanism to retrieve "all-models." So, if you're bound to be using SMTLib only, you cannot do this; at least not easily.
However, most solvers (definitely including cvc4 and z3) can be scripted from higher-level languages. The idea is to make a check-sat call, and if you get a solution, you add an additional assertion that disallows that model, and query for a new one. See this answer for how to do this in z3, as scripted from Python: Trying to find all solutions to a boolean formula using Z3 in python. You can do the same from C/Java etc.; or use a higher-level binding that provides such a command out-of-the box.

How to print the entire model in cvc4 using smtlib

so I have just started to learn cvc4 after I have spent some time learning boolector. With it, it is possible to print the model just using boolector_print_model. Unfortunately the doc page for cvc4 is down at the moment and I cannot understand how to do the same with cvc4 in Java.
Can anyone help to do it please?
For instance, you could help me to see the model for this example.
EDIT: Just to be clear, with the entire model I mean a valid value for each BV or in general variable present within my model.
An example model could be:
(model
...
(define-fun number_6_0_7 () (_ BitVec 8) #x00)
(define-fun number_6_1_7 () (_ BitVec 8) #x00)
(define-fun number_6_2_7 () (_ BitVec 8) #x00)
(define-fun number_6_3_7 () (_ BitVec 8) #x78)
...
)
Thanks a lot
Unlike boolector, CVC4 does not have a mechanism of accessing the entire model in once call via the API. This is because CVC4 allows for much richer types, including data-types, uninterpreted-functions, etc.; which makes model construction more complicated.
Instead, you call the getValue method for each of the input variables you have, and print them yourself. Here's an example:
https://github.com/CVC4/CVC4/blob/e3cd4670a080554e4ae1f2f26ee4353d11f02f6b/examples/api/java/FloatingPointArith.java#L66-L68

How to represent Function names in Docco generated files?

The objective is to make the docco generated documentation for fluentnode (written in Coffee-Script) as easy to read and understand as possible. At the moment I'm struggling with the best way to represent the function names on the left hand side of the help pages you can access at http://o2platform.com/fluentnode/index.html
At the moment I'm exploring three syntax options:
A) #.{function-name} ({params})
B) {ClassName}::{function-name} ({params})
C) {ClassName}#{function-name} ({params})
As an example, the Array's .empty() method, would be represented as:
A) #.empty ()
B) Array::empty ()
C) Array#empty ()
Note that this would be seen inside the file for a particular class (so it would still be obvious on A that this is related to an array)
To see this in action I used these methodologies on three different help files:
A) #.{function-name} ({params}) on Array.html
B) {ClassName}::{function-name} ({params}) on Function.html
C) {ClassName}#{function-name} ({params}) on C) http://o2platform.com/fluentnode/Number.html
Btw: if there are other ways to represent this, please point me to existing docco generated sites which represent those techniques.
(Question also asked here https://github.com/o2platform/fluentnode/issues/31)
I'm going with a variation of option A) which is
#.empty {parmas}
Where I'm not using () in the params which makes it easy to see them

Racket FFI: initialize pointer to NULL

This is the first time I try to use Racket's FFI. I would like to create an application that binds to libgit2 in order to manipulate GIT repositories.
The first thing I would need to do is to initialize a repository, as shown in the libgit2 documentation:
git_repository *repo = NULL;
int error = git_repository_init(&repo, "/tmp/…", false);
Getting the function call in Racket is simple enough:
(require ffi/unsafe
ffi/unsafe/define)
(define-ffi-definer define-libgit (ffi-lib "/opt/local/lib/libgit2.dylib"))
(define _git_repository-ptr (_cpointer/null 'git_repository))
(define-libgit git_repository_init (_fun _git_repository-ptr _string _bool -> _int))
But then trying to use the function does not work:
-> (define new_repo _git_repository-ptr)
-> (git_repository_init new_repo "/tmp/..." #f)
; git_repository->C: argument is not `git_repository' pointer
; argument: #<ctype>
; [,bt for context]
libgit2 does not offer a function to initialize the pointer, as shown in the Racket FFI documentation example.
Is this the correct way to define a nullable pointer and initialize it to NULL?
git_repository, on the other hand, is a struct defined in the library. Am I supposed to use define-cstruct on the Racket side to get it correctly? This can be cumbersome, since the struct is defined in terms of other structs, with some levels of nesting. Are there any patterns to handle this situation?
The problem isn't that the pointer wasn't initialised to null; in fact, the git_repository_init documentation does not say that it has to be initialised to anything at all. The problem is that it's an out parameter, and your function declaration didn't specify so.
Here's some code I tested with (on Ubuntu 14.04) that worked for me:
> (require ffi/unsafe ffi/unsafe/define)
> (define-ffi-definer define-libgit (ffi-lib "libgit2"))
> (define _git_repository-ptr (_cpointer/null 'git_repository))
> (define-libgit git_repository_init (_fun (repo : (_ptr o _git_repository-ptr)) _string _bool -> (rc : _int) -> (values repo rc)))
> (git_repository_init "/tmp/repo" #f)
#<cpointer:git_repository>
0
See the documentation for _ptr on how to specify an out parameter. (You can use it to specify in/out parameters too, but those are generally rare.)
(Of course, you'll eventually want to massage your FFI so that instead of returning two values like in my example, you'll instead simply check the return code for an error, raising a proper Racket error as appropriate, and return just the repository pointer if successful.)

In common-lisp how can i override/change evaluation behaviour for a specific type of object?

In common-lisp, I want to implement a kind of reference system like this:
Suppose that I have:
(defclass reference () ((host) (port) (file)))
and also I have:
(defun fetch-remote-value (reference) ...) which fetches and deserializes a lisp object.
How could I intervene in the evaluation process so as whenever a reference object is being evaluated, the remote value gets fetched and re-evaluated again to produce the final result?
EDIT:
A more elaborate description of what I want to accomplish:
Using cl-store I serialize lisp objects and send them to a remote file(or db or anything) to be saved. Upon successful storage I keep the host,port and file in a reference object. I would like, whenever eval gets called on a reference object, to first retrieve the object, and then call eval on the retrieved value. Since a reference can be also serialized in other (parent) objects or aggregate types, I can get free recursive remote reference resolution by modyfing eval so i dont have to traverse and resolve the loaded object's child references myself.
EDIT:
Since objects always evaluate to themselves, my question is a bit wrongly posed. Essentially what I would like to do is:
I would like intercept the evaluation of symbols so that when their value is an object of type REFERENCE then instead of returning the object as the result of the symbol evaluation, to return the result of (fetch-remote-value object) ?
In short: you cannot do this, except by rewriting the function eval and modifying your Lisp's compiler. The rules of evaluation are fixed Lisp standard.
Edit After reading the augmented question, I don't think, that you can achieve full transperency for your references here. In a scenario like
(defclass foo () (reference :accessor ref))
(ref some-foo)
The result of the call to ref is simply a value; it will not be considered for evaluation regardless of its type.
Of course, you could define your accessors in a way, which does the resolution transparently:
(defmacro defresolver (name class slot)
`(defmethod ,name ((inst ,class))
(fetch-remote-reference (slot-value inst ',slot))))
(defresolver foo-reference foo reference)
Edit You can (sort of) hook into the symbol resolution mechanism of Common Lisp using symbol macros:
(defmacro let-with-resolution (bindings &body body)
`(symbol-macrolet ,(mapcar #'(lambda (form) (list (car form) `(fetch-aux ,(cadr form)))) bindings) ,#body))
(defmethod fetch-aux ((any t)) any)
(defmethod fetch-aux ((any reference)) (fetch-remote-reference any))
However, now things become pretty arcane; and the variables are no longer variables, but magic symbols, which merely look like variables. For example, modifying the content of a variable "bound" by this macro is not possible. The best you can do with this approach is to provide a setf expansion for fetch-aux, which modifies the original place.
Although libraries for lazy evaluatione and object persistence bring you part of the way, Common Lisp does not provide a portable way to implement fully transparent persistent values. Lazy or persistent values still have to be explicitly forced.
MOP can be used to implement lazy or persistent objects though, with the slot values transparently forced. It would take a change in the internals of the Common Lisp implementations to provide general transparency, so you could do e.g. (+ p 5) with p potentially holding a persistent or lazy value.
It is not possible to directly change the evaluation mechanisms. You would need to write a compiler for your code to something else. Kind of an embedded language.
On the CLOS level there are several ways to deal with it:
Two examples:
write functions that dispatch on the reference object:
(defmethod move ((object reference) position)
(move (dereference reference) position))
(defmethod move ((object automobile) position)
...))
This gets ugly and might be automated with a macro.
CHANGE-CLASS
CLOS objects already have an indirection, because they can change their class. Even though they may change their class, they keep their identity. CHANGE-CLASS is destructively modifying the instance.
So that would make it possible to pass around reference objects and at some point load the data, change the reference object to some other class and set the slots accordingly. This changing the class needs to be triggered somewhere in the code.
One way to have it automagically triggered might be an error handler that catches some kinds of errors involving reference object.
I would add a layer on top of your deserialize mechanism that dispatches based on the type of the incoming data.