Re: getting to know the FFI ...
On 11/19/20 7:02 PM, Tim Meehan wrote: I figured that I would try and do something simple-ish to see how well I understood the FFI. I found this GTK tutorial, written in Chez Scheme: https://github.com/jhidding/lyonesse/blob/master/gtk-tutorial/window.scm I just tried to replace the Chez FFI calls with Guile FFI calls. I'm not sure how to tell GTK about a callback that is written in Guile. I'm not sure how to pass a string to GTK ... Cheers, ;; HELP: This obviously won't work. ;; How would I give GTK a Guile callback? (define (callback p) (let ([code (foreign-callable p (iptr iptr) void)]) (lock-object code) (foreign-callable-entry-point code))) try reading the manual section on procedure->pointer
Question about data structures
Hello Guile Users! I have a question about data structures. Recently I read a file and the lines in the file would become a list in my Guile program. The file was not super big or anything. However, I usually try to avoid having to use `append` or `reverse`, whenever possible, considering, that they are O(n) operations and in principle I do not want to write code, that uses lists in inefficient ways, when there is a more efficient way. That said, I hit a little snag: When I am reading a file and do not know how many lines there are in the file, I can use a normal list and construct it using recursive calls like `(cons line (iter ...))` where `iter` is the recursive call and. Could also be called `process-next-line` or simply `next`. Since I am building a recursive data structure, it is OK to have a non-tail position recursive call. Then I would return that list and work with it. However, what if I ever need to add a list entry and the order of list entry matters? I would either have to use `append`, to add it to the end, which would be O(n), or I would have to initially construct the list of lines in reversed order from initially, so that I can add by simply using `(cons new-entry lines)`. However, when I use the list in reverse and ever need to output the lines in the list in their original order, I would first need to `reverse` the list again. OK the whole reversing would not be a problem, if I used a vector to store the lines. However, then I would need to know the number of lines in the file ahead of time or look at the file once for counting the lines, then create the vector of that length and then store the lines in it. This seems inelegant again, because I look at the lines of the file twice. I could read it in as a list and then use `list->vector`, but that will also be an additional O(n) for converting every entry to vector element. If I now think about Python, then I have Python lists (actually arrays?) and those can be easily appended to in O(1) and random access is also O(1) according to https://wiki.python.org/moin/TimeComplexity. This means I do not need to think much about how I will use the lines of a file, when reading in the file. A Python list will be appropriate. So in Guile I sometimes feel some mental overhead of thinking about the choice of data structure, which I do not feel in Python. Generally I like Guile a lot more, so I would like to know how others deal with this. Here are some ideas for it: 1. I could create some abstraction layer for the "sequence of read lines", which I use inside the procedure, which reads in the lines and all other code, that works with the lines, so that I can rather easily later exchange the data structure. A data abstraction. However, that might only hide the complexities of some operations behind the abstraction and not reduce them. 2. Perhaps I want Guile's arrays? (Can they be expanded in O(1)? I do seem to remember reading, that Guile vectors are only a special case of Guile arrays, so that would mean they are not expandable in O(1).) 3. Just convert list to vector after reading in the file until I hit a problem, where that additional O(n) really becomes a problem. (In my current project it is not realistically a problem.) But this does not satisfy me. I should learn how to solve the problem in general and use the best way to do things. 4. Perhaps I need to write another data structure, that creates a new vector, when the previous one is full, managing the expansion myself. How do you approach this problem? Is it a problem at all? Best regards, Zelphir -- repositories: https://notabug.org/ZelphirKaltstahl
Re: eof-object? documentation
On 21.11.20 09:52, randomlooser wrote: > Il giorno dom, 15/11/2020 alle 13.16 +0100, Zelphir Kaltstahl ha > scritto: >> Nevermind … One line below the definition in the docs it says: >> >> "Note that unlike other procedures in this module, eof-object? is >> defined in the default environment. " >> >> But why would it be listed there then? > Maybe you could file an issue in the issue tracker ? Hi! I am not sure it is an issue to be honest, because it has that "repairing phrase" below it, saying, that the procedure is not in that module. It is also not a big problem, only something I noticed and that I found surprising or weird. I did not find the repository of the Guile reference manual yet. Can anyone point me to it? Then I could perhaps add examples, notes or remove the "?" from the page and that phrase other stuff via pull request. Best regards, Zelphir -- repositories: https://notabug.org/ZelphirKaltstahl
Re: Question about data structures
Hello Zelphir! Le dimanche 22 novembre 2020 à 19:48 +0100, Zelphir Kaltstahl a écrit : > However, when I use the list in > reverse and ever need to output the lines in the list in their > original > order, I would first need to `reverse` the list again. There is a "reverse" function; you could implement it yourself as a tail-recursive function if you wanted (it's currently implemented in C, so my guess is it's even more efficient). You don't need vectors for that. (define (my-reverse-aux accumulation list) (if (null? list) accumulation (my-reverse-aux (cons (car list) accumulation) (cdr list (define (my-reverse list) (my-reverse-aux '() list)) (my-reverse '(a b c d e f g h))
Re: Question about data structures
Hi divoplade! I know there is reverse and I think I did implement it before, when working through SICP exercises. Thanks for that implementation and input though! I think the point I wanted to make is rather to avoid `reverse` completely. If I had a vector, I could simply go by index backwards or forwards without adding any runtime complexity. But a vector is of defined length, not expandable like a list via cons, which means it might not be the best idea under some circumstances. Other circumstances make lists less ideal. If those circumstances appear together, then I probably should be using another data structure. Or pay the price in time complexity for some operations. I just read, that Python "lists" are actually implemented as https://en.wikipedia.org/wiki/Dynamic_array. So I guess a more specific but less generally useful question is: "What do I use in Guile, if I were using a dynamic array in Python?" I almost never find myself reversing a Python "list". Probably because it can be indexed in reverse order indices. Best regards, Zelphir On 11/22/20 8:45 PM, divoplade wrote: > Hello Zelphir! > > Le dimanche 22 novembre 2020 à 19:48 +0100, Zelphir Kaltstahl a écrit : >> However, when I use the list in >> reverse and ever need to output the lines in the list in their >> original >> order, I would first need to `reverse` the list again. > There is a "reverse" function; you could implement it yourself as a > tail-recursive function if you wanted (it's currently implemented in C, > so my guess is it's even more efficient). You don't need vectors for > that. > > (define (my-reverse-aux accumulation list) > (if (null? list) > accumulation > (my-reverse-aux (cons (car list) accumulation) (cdr list > > (define (my-reverse list) > (my-reverse-aux '() list)) > > (my-reverse '(a b c d e f g h)) > -- repositories: https://notabug.org/ZelphirKaltstahl
Re: Question about data structures
divoplade writes: > Hello Zelphir! > > Le dimanche 22 novembre 2020 à 19:48 +0100, Zelphir Kaltstahl a écrit : >> However, when I use the list in reverse and ever need >> to output the lines in the list in their original >> order, I would first need to `reverse` the list again. > There is a "reverse" function; you could implement it yourself as a > tail-recursive function if you wanted (it's currently implemented in C, > so my guess is it's even more efficient). You don't need vectors for > that. > > (define (my-reverse-aux accumulation list) > (if (null? list) > accumulation > (my-reverse-aux (cons (car list) accumulation) (cdr list > > (define (my-reverse list) > (my-reverse-aux '() list)) > > (my-reverse '(a b c d e f g h)) There is also a reverse! procedure. (reverse! '(a b c)) $1 = (c b a) I have not actually looked at the code, but I assume from the "!" in the name and a decent respect for competence of the programmers, that it uses the well-known algorithm to reverse a list by destructive update. This algorithm is still O(n), but who cares? It is at least O(n) to read n lines, no matter how how you read them. The cost is amortised and is only O(1) per line. The thing to avoid is "cons". The my-reverse procedure above still does O(n) times cons, which uses storage, and may call garbage collection. The destructive update algorithm does O(n) times update one storage location and a couple of registers, which is trivial compared to reading a line. Just remember not to save pointers into the original non-reversed list, because it gets smashed. (You can still save pointers to the _members_ of the list.) -- Keith, Programmer in Chief, Free Computer Shop, http://www.free-comp-shop.com/ Food, Shelter, Source code
Re: Question about data structures
Le dimanche 22 novembre 2020 à 21:24 +0100, Zelphir Kaltstahl a écrit : > If I had a vector, I could simply go by index backwards or > forwards without adding any runtime complexity. So, you would like to sometimes go forward, sometimes go backward? If it is sequential, the list is what you want. With the 2-variable function used earlier, you can go in one direction or the other, depending on which argument you decompose. > a more specific > but less generally useful question is: "What do I use in Guile, if I > were using a dynamic array in Python?" It depends. If you were using a dynamic array in Python because there was no good implementation of what guile calls lists or vectors, use lists or vectors ;-) If you were using a dynamic array because you wanted to grow lists while keeping random indexing, use VLists ( https://www.gnu.org/software/guile/manual/guile.html#VLists). Or hash tables (https://www.gnu.org/software/guile/manual/guile.html#VHashes, https://www.gnu.org/software/guile/manual/guile.html#Hash-Tables).
Re: eof-object? documentation
Il giorno dom, 22/11/2020 alle 19.57 +0100, Zelphir Kaltstahl ha scritto: > I did not find the repository of the Guile reference manual yet. Can > anyone point me to it? Then I could perhaps add examples, notes or > remove the "?" from the page and that phrase other stuff via pull > request. > > Best regards, > Zelphir > The Guile reference manual is in the same repository of Guile itself The manual is in the "doc" folder I built the manual without building the whole Guile myself, but I don't remember I asked on the irc channel, back then It was when Alex Sassmanhausen added a mention of guile-hall to the manual
Guile dynamic FFI, C function expecting pointer
I tried to boil this question down to the most simple thing that represented what I needed to understand. I have had luck getting C functions that expect arguments "by value," but "by reference" has been problematic. The failure mode is "Segmentation Fault," so I gather that I may not be using the right Guile call at all. The Guile user manual is usually quite excellent, but I seem to be missing something important. Thanks, ;;;; ;; C source for "libstuff.so": ;; file stuff.c, compiled as: ;; gcc stuff.c -o libstuff.so -fPIC -shared #| void int_ptr_example1(int *a) { *a = 5; } |# ;;;; ;; Test loading and using the library. (use-modules (system foreign)) (define libstuff (dynamic-link "./libstuff.so")) (define int-ptr-example1 (pointer->procedure void (dynamic-func "int_ptr_example1" libstuff) (list '*))) ;; Following: ;; https://nalaginrut.com/archives/2015/03/27/do-some-quick-and-dirty-with-guile-ffi (let ([a %null-pointer]) (int-ptr-example1 a) (display a) (newline)) ;;;; ;; Sadly, when it runs, I get a segmentation fault at the call to ;; int-ptr-example1 :(
Re: Question about data structures
On Sunday, 22 November 2020 19:48:24 CET Zelphir Kaltstahl wrote: > Hello Guile Users! > > I have a question about data structures. > > Recently I read a file and the lines in the file would become a list in > my Guile program. The file was not super big or anything. However, I > usually try to avoid having to use `append` or `reverse`, whenever > possible, considering, that they are O(n) operations and in principle I > do not want to write code, that uses lists in inefficient ways, when > there is a more efficient way. That said, I hit a little snag: > > When I am reading a file and do not know how many lines there are in the > file, I can use a normal list and construct it using recursive calls > like `(cons line (iter ...))` where `iter` is the recursive call and. > Could also be called `process-next-line` or simply `next`. Since I am > building a recursive data structure, it is OK to have a non-tail > position recursive call. Then I would return that list and work with it. > However, what if I ever need to add a list entry and the order of list > entry matters? I would either have to use `append`, to add it to the > end, which would be O(n), or I would have to initially construct the > list of lines in reversed order from initially, so that I can add by > simply using `(cons new-entry lines)`. However, when I use the list in > reverse and ever need to output the lines in the list in their original > order, I would first need to `reverse` the list again. > > OK the whole reversing would not be a problem, if I used a vector to > store the lines. However, then I would need to know the number of lines > in the file ahead of time or look at the file once for counting the > lines, then create the vector of that length and then store the lines in > it. This seems inelegant again, because I look at the lines of the file > twice. I could read it in as a list and then use `list->vector`, but > that will also be an additional O(n) for converting every entry to > vector element. > > If I now think about Python, then I have Python lists (actually arrays?) > and those can be easily appended to in O(1) and random access is also > O(1) according to https://wiki.python.org/moin/TimeComplexity. This > means I do not need to think much about how I will use the lines of a > file, when reading in the file. A Python list will be appropriate. > > So in Guile I sometimes feel some mental overhead of thinking about the > choice of data structure, which I do not feel in Python. Generally I > like Guile a lot more, so I would like to know how others deal with > this. Here are some ideas for it: > > 1. I could create some abstraction layer for the "sequence of read > lines", which I use inside the procedure, which reads in the lines and > all other code, that works with the lines, so that I can rather easily > later exchange the data structure. A data abstraction. However, that > might only hide the complexities of some operations behind the > abstraction and not reduce them. > > 2. Perhaps I want Guile's arrays? (Can they be expanded in O(1)? I do > seem to remember reading, that Guile vectors are only a special case of > Guile arrays, so that would mean they are not expandable in O(1).) > > 3. Just convert list to vector after reading in the file until I hit a > problem, where that additional O(n) really becomes a problem. (In my > current project it is not realistically a problem.) But this does not > satisfy me. I should learn how to solve the problem in general and use > the best way to do things. > > 4. Perhaps I need to write another data structure, that creates a new > vector, when the previous one is full, managing the expansion myself. > > How do you approach this problem? Is it a problem at all? > > Best regards, > Zelphir > > Hey Zelphir, If you want to use a FIFO data structure, you may want to check out queues. They're already in ice-9, under (ice-9 q). Mandatory manual reference: https://www.gnu.org/software/guile/manual/html_node/Queues.html#Queues Alternatively, if you want constant-time random access you could try using Vlists, although they aren't thread-safe. https://www.gnu.org/software/guile/manual/html_node/VLists.html#VLists Finally you could implement either a doubly-linked list or array list type depending on your needs. If I understand your requirements correctly I would recommend queues. They are easy to work with. Sincerely yours, - Tim signature.asc Description: This is a digitally signed message part.
broken link on "Learn" page
There is a broken link to the "Internet Scheme Repository" on the "Learn" page of the GNU Guile website: https://www.gnu.org/software/guile/learn/ I didn't see a contact email on the website, but I figured that someone here might know someone that ran the website ... if not, my apologies.
Re: Guile dynamic FFI, C function expecting pointer
On 11/22/20 2:50 PM, Tim Meehan wrote: I tried to boil this question down to the most simple thing that represented what I needed to understand. I have had luck getting C functions that expect arguments "by value," but "by reference" has been problematic. The failure mode is "Segmentation Fault," so I gather that I may not be using the right Guile call at all. The Guile user manual is usually quite excellent, but I seem to be missing something important. Thanks, ;;;; ;; C source for "libstuff.so": ;; file stuff.c, compiled as: ;; gcc stuff.c -o libstuff.so -fPIC -shared #| void int_ptr_example1(int *a) { *a = 5; } |# You'll need to make-bytevector a bytevector that holds sizeof(int) bytes. Then pass (bytevector->pointer ) as the argument. (let ((obj (make-bytevector (sizeof int (int-ptr-example (bytevector->pointer obj))) Now the 5 should be in the bytevector. You will need to extract it.
Re: Question about data structures
On 22.11.2020 19:48, Zelphir Kaltstahl wrote: Hello Guile Users! I have a question about data structures. [...] How do you approach this problem? Is it a problem at all? First of all, be cautious about premature optimization. In many cases it's best to just write the code the most straightforward way possible with the tools at hand, and not bother with optimization unless it actually proves to be an issue. Are you going to be processing files with millions of lines? Thousands of lines but on a very weak CPU? Does it matter if your program takes 0.1 seconds or 2 seconds to run? Now the actual answer, in case you need to optimize, or just want to learn more: All data structures that offer a sequential list of elements have to make some trade-offs between the performance of various operations, as well as the implementation complexity. Linked lists (i.e. "lists" in Scheme) are very simple, and a few operations are cheap as well, but they have the shortcomings you've described plus some more. Since your main concern seems to be appending, you could simply use a linked list where you keep a reference to the last cons pair (tail) of the list, so appending is simply a matter of a 'set-cdr!' operation on the tail. Python lists, JDK's ArrayList, and .NET ArrayList, among probably many other "list" or "array" data structures in popular languages nowadays use a relatively straightforward data structure that is backed by an actual array which can have empty slots (e.g. your Python list with 3 elements might be backed by an array of size 10), and is reallocated whenever there's no space left. This means that appending an element at the end is usually dirt cheap, until there's no space left, at which point the append operation is much heavier for one call, then the following calls are dirt cheap again, until it's full again... Inserting an element at the beginning or middle is also relatively expensive with those implementations, since all elements need to be shifted forward to make space for the new element. (Although this might be done with an operation like C's memcpy which is still actually very fast.) It's called a "dynamic array" by Wikipedia: https://en.wikipedia.org/wiki/Dynamic_array If you want to go on an adventure, you could implement a Scheme data structure called DVector that implements this strategy, using plain Scheme vectors for the backing array. The VList has also been mentioned in this thread, but from what I can tell it doesn't seem to offer a very efficient append operation. - Taylan
Re: Question about data structures
On Sun, Nov 22, 2020 at 10:43 PM Taylan Kammer wrote: Since your main concern seems to be appending, you could simply use a > linked list where you keep a reference to the last cons pair (tail) of > the list, so appending is simply a matter of a 'set-cdr!' operation on > the tail. > SRFI 117, List Queues, does exactly that. > Python lists, JDK's ArrayList, and .NET ArrayList, among probably many > other "list" or "array" data structures in popular languages nowadays > use a relatively straightforward data structure that is backed by an > actual array which can have empty slots (e.g. your Python list with 3 > elements might be backed by an array of size 10), and is reallocated > whenever there's no space left. This means that appending an element at > the end is usually dirt cheap, until there's no space left, at which > point the append operation is much heavier for one call, then the > following calls are dirt cheap again, until it's full again... > And the recent SRFI 214, Flexvectors, provides exactly this. Packaging these two SRFIs for Guile would be a Good Thing. John Cowan http://vrici.lojban.org/~cowanco...@ccil.org The present impossibility of giving a scientific explanation is no proof that there is no scientific explanation. The unexplained is not to be identified with the unexplainable, and the strange and extraordinary nature of a fact is not a justification for attributing it to powers above nature. --The Catholic Encyclopedia, s.v. "telepathy" (1913)