Racket FFI: initialize pointer to NULL - racket

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.)

Related

VSCode Problem: If `( * )` has no mutable arguments partially applied then the lambda can be removed

The following line causes F# Linter to list the problem in the title (not an error, but suggestion) in VSCode, pointing to the lambda:
let product = [1;5;4;9;3] |> List.reduce (fun a b -> a * b)
Can this be simplified? I don't know of any other way to write this multiply function inline without the lambda. Also, I couldn't find this function in the System.Math class or an F# equivalent function.
Or is F# Linter wrong in this case?
reduce expects a function of 2 arguments and operator of multiplication * is a function of 2 arguments, that you can pass directly without wrapping it in a fun.
So lint suggests you to write it this way:
let product = [1;5;4;9;3] |> List.reduce (*)

Where does a make- function come from?

This code works:
(define list-of-events
(for/list ([(date code)
(in-query odc "select date, code from attendance
where student_id = ? and term_code = ?"
"12345" "654321")])
(make-attendance-event date code)))
However, when I try to duplicate the behavior for another table, the parallel item to make-attendance-event complains about it being an "unbound identifier".
Now, where does make-attendance-event come from?
The identifier make-attendance-event came from a (define-struct attendance-event (...)).
A structure definition such as
(define-struct foo (a b))
will expand into multiple definitions.
make-foo will construct foo-structures
foo-a, foo-b field accessors
foo? a predicate that can determine whether a value is a foo
In the Advanced language you also get:
set-foo-a!, set-foo-b! to mutate the respective fields.
See more here: http://docs.racket-lang.org/htdp-langs/advanced.html?q=define-struct#%28form._%28%28lib._lang%2Fhtdp-advanced..rkt%29._define-struct%29%29
Note that you can hover over the identifier make-attendance-event in DrRacket, right click and choose "Jump to Binding Occurrence" to see where an identifier is defined.
make-attendance-event is a function defined somewhere else in your Racket file. It's not a library function.

Why does REPL treat clojure.core/doc as a var?

I'm trying to get documentation using the Clojure doc function, but can't get it recognized from the REPL (I'm using Emacs and SLIME). The following sequence describes what's going on (error message follows immediately after each line):
gaidica.core> (doc first)
; Evaluation aborted.
Unable to resolve symbol: doc in this context
[Thrown class java.lang.Exception]
gaidica.core> (clojure.core/doc first)
; Evaluation aborted.
No such var: clojure.core/doc
[Thrown class java.lang.Exception]
user> (clojure.core/doc first)
; Evaluation aborted.
No such var: clojure.core/doc
[Thrown class java.lang.Exception]
user> (doc first)
-------------------------
clojure.core/first
([coll])
Returns the first item in the collection. Calls seq on its
argument. If coll is nil, returns nil.
nil
user>
How do I refer to the doc function and get it recognized as a function, not a variable?
ADDENDUM, 6/22/11, 9 hours after question posted
#kotarak made the most relevant comment: "Note, that clojure.core/doc is 1.2 and earlier. clojure.repl/doc is 1.3 and later." Sure enough, the following worked:
user> (clojure.repl/doc first)
-------------------------
clojure.core/first
([coll])
Returns the first item in the collection. Calls seq on its
argument. If coll is nil, returns nil.
nil
user>
I was able to confirm that Clojure 1.3 was active:
user> *clojure-version*
{:interim true, :major 1, :minor 3, :incremental 0, :qualifier "master"}
user>
But this too was confusing--my Leiningen project.clj had specified Clojure 1.2!
In my own experience, I once noticed that the SLIME-based REPL "hung onto" a Java classpath value even after I changed the contents of relevant directories. The solution then was to exit Emacs and lein swank, then re-enter both and try again. I tried the same and got the following result:
user> *clojure-version*
{:major 1, :minor 2, :incremental 0, :qualifier ""}
user>
The only conclusion I can make is that my previous REPL had been using Clojure 1.3. The project that I worked on previous to this had used a Clojure 1.3 snapshot, so I'm guessing that the REPL had "hung onto" Clojure 1.3 somehow.
Problem solved, lesson learned, etc. For bonus points, can anybody explain the reason for what happened (with Clojure 1.2 vs. 1.3)?
Thanks to all who contributed.
Couple of corrections. First, doc is a macro not a function. Also functions and macros can be stored in a var. Second, in clojure 1.3, which is likely the version you are using, doc is stored in the var clojure.repl/doc, not clojure.core/doc (as it is in 1.2). In the namespace user, doc is "used", aka there is an implicit "(use [clojure.repl :only [doc])". When you goto a new namespace, or even create one with ns, clojure.repl/doc is not automatically added, unlike it is with 'user.
To be more explicit about the questions:
Why does REPL treat clojure.core/doc as a var?
Functions, macros and values in clojure are either stored in vars or bound to a symbol such as in a let or function. clojure.core/doc is the var that holds the macro that does what doc does.
How do I refer to the doc function and get it recognized as a function, not a variable?
Like in all lisps, to do a call, whether a function or a macro, you must put the function/macro in the first position of a list.
(<fn/macro> *args)
So to call the macro doc on itself, you would do:
(doc doc)
The doc for doc shows its a macro so expanding it with macroexpand shows that is
expecting the name of a var containging a function. so the very short answer to your question would be "functions in a namespace are contained in vars".
in situations like this macroexpand-1 can be a good place to start:
(macroexpand-1 '(doc doc))
(clojure.core/print-doc (var doc))
sso-config.core> (doc doc)
-------------------------
clojure.core/doc
([name])
Macro
Prints documentation for a var or special form given its name

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.

lisp file pointers in classes

I'm running up against a problem in understanding the CLOS way of handling file access within a class. In c++ I would be able to do this:
class Foo {
Foo (string filename); // opens the file (my_file) requested by the filename
~Foo (); // close the file
FILE * my_file; // a persistent file-handle
DataStruct my_data; // some data
void ParseData (); // will perform some function on the file and populate my_data
DataStruct * GetData () { return &my_data; } // accessor to the data
};
What I'd like to point out is that PraseData() will be called multiple times, and each time a new block of data will be parsed from the file and my_data will be altered.
I'm trying to perform the same trick in CLOS - create all the generic methods to parse the data, load the file, read headers, etc. as well as the class definition which I have as:
(defclass data-file ()
((filename :initarg :filename :accessor filename)
(file :accessor file)
(frame :accessor frame)))
In the "constructor" (i.e. initialize-instance) I open the file just as my c++ idiom. Then I have access to the data and I can parse the data as before. However, I'm told that using a "destructor" or (finalize) method to close the file is not idiomatic CLOS for handling this type of situation where I need the file to be around so I can access it outside of my data-file methods.
I'm going to define a function that loads a data-file, and then performs a series of analyses with its data, and then hopefully close it. What's a way to go about doing this? (I'm assuming a macro or some type of closure would work in here, but I'm not familiar enough with the lisp way to decide what is needed or how to implement it).
One option is to have the stream as a slot instead of the filename, and then scope it with WITH-OPEN-FILE:
(with-open-file (stream file)
(let ((foo (make-instance 'foo :stream stream)))
(frob foo)
(...other processing of foo...)))
Then your stream will be closed automatically.
I think I would lean towards making classes only to store complete authoritative data (what you call DataStruct?).
You don't really need a special class for "loading + storage of another class". Plus, that way has the unspoken invariant that my_data holds the data of my_file up to the current seek position, which seems a bit strange to my eye.
Put another way: what does Foo do? Given a filename, it loads data, and gives you a DataStruct. That sounds like a function to me. If you need to be able to run it in a thread, or fire events between loading records, a class is the natural way to do it in C++, but you don't need a class for those things in Lisp.
Also, remember that you don't need to use DEFCLASS in order to use generic methods in CLOS.
I don't know what the structure of your data is, but in similar situations I've made a parse-one-chunk function that takes a stream and returns one record, and then create a complete Foo instance inside a loop in a with-open-file. If the stream is never needed outside the scope of a with-open-file expansion, you never need to worry about closing it.