unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Looking for graph library
@ 2022-10-08 10:29 Dmitry Polyakov
  2022-10-08 13:42 ` Maxime Devos
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Dmitry Polyakov @ 2022-10-08 10:29 UTC (permalink / raw)
  To: guile-user

Hello!

Can you recommend any library for working with graphs (I did not find it
in srfi and mailing list). Almost what I need to have in graphviz
bindings, but there are no predicate functions and the like.

-- 
sanity level in trouble



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Looking for graph library
  2022-10-08 10:29 Looking for graph library Dmitry Polyakov
@ 2022-10-08 13:42 ` Maxime Devos
  2022-10-08 14:07 ` Ricardo Wurmus
  2022-10-09  0:20 ` David Pirotte
  2 siblings, 0 replies; 12+ messages in thread
From: Maxime Devos @ 2022-10-08 13:42 UTC (permalink / raw)
  To: Dmitry Polyakov, guile-user


[-- Attachment #1.1.1: Type: text/plain, Size: 376 bytes --]



On 08-10-2022 12:29, Dmitry Polyakov wrote:
> Hello!
> 
> Can you recommend any library for working with graphs (I did not find it
> in srfi and mailing list). Almost what I need to have in graphviz
> bindings, but there are no predicate functions and the like.
> 

SRFI-234 is for linearizing a direct acyclic graph / topological sorting.

Greetings,
Maxime.

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 929 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Looking for graph library
  2022-10-08 10:29 Looking for graph library Dmitry Polyakov
  2022-10-08 13:42 ` Maxime Devos
@ 2022-10-08 14:07 ` Ricardo Wurmus
  2022-10-10 17:18   ` zimoun
  2022-10-09  0:20 ` David Pirotte
  2 siblings, 1 reply; 12+ messages in thread
From: Ricardo Wurmus @ 2022-10-08 14:07 UTC (permalink / raw)
  To: Dmitry Polyakov; +Cc: guile-user


Dmitry Polyakov <liltechdude@gmail.com> writes:

> Can you recommend any library for working with graphs (I did not find it
> in srfi and mailing list). Almost what I need to have in graphviz
> bindings, but there are no predicate functions and the like.

Here’s an implementation of inductive graphs:

https://github.com/rekado/minigrad/blob/main/minigrad/graph.scm


-- 
Ricardo



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Looking for graph library
  2022-10-08 10:29 Looking for graph library Dmitry Polyakov
  2022-10-08 13:42 ` Maxime Devos
  2022-10-08 14:07 ` Ricardo Wurmus
@ 2022-10-09  0:20 ` David Pirotte
  2022-10-09 10:51   ` Ricardo Wurmus
  2 siblings, 1 reply; 12+ messages in thread
From: David Pirotte @ 2022-10-09  0:20 UTC (permalink / raw)
  To: Dmitry Polyakov; +Cc: guile-user

[-- Attachment #1: Type: text/plain, Size: 1215 bytes --]

Hello,

	to all users: the most up-to-date list of guile s/w is here:

		http://sph.mn/foreign/guile-software.html

	[ and at the end of the list, it also offers 'searching links'
	[ to notabug, savannah, (give-up on) github and ML

> Can you recommend any library for working with graphs (I did not find
> it in srfi and mailing list). Almost what I need to have in graphviz
> bindings, but there are no predicate functions and the like.

In addition to the answers from Maxime and Ricardo ... I searched
because I thought guile has(had) a graphviz binding somewhere, here is
what I found

	https://reposcope.com/man/en/3guile/gv

Then I checked on debian, it has 

	libgv-guile	2.42.2-7
	Guile bindings for graphviz

	[ which seems up-to-date because it depends on the
	[ debian guile-3.0-libs 3.0.8-2

I also found this

	https://github.com/roelj/graphviz-guile

	which is 'old', but as it is merely a guile module definition to
	make the above lib available in guile 'proper', it should work
	or work with very little effort ...

I didn't try, short on time, but this should work or should work with
little effort, maybe just updating the graphviz-guile module ...

David

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Looking for graph library
  2022-10-09  0:20 ` David Pirotte
@ 2022-10-09 10:51   ` Ricardo Wurmus
  2022-10-09 18:47     ` David Pirotte
  0 siblings, 1 reply; 12+ messages in thread
From: Ricardo Wurmus @ 2022-10-09 10:51 UTC (permalink / raw)
  To: David Pirotte; +Cc: Dmitry Polyakov, guile-user


David Pirotte <david@altosw.be> writes:

>> Can you recommend any library for working with graphs (I did not find
>> it in srfi and mailing list). Almost what I need to have in graphviz
>> bindings, but there are no predicate functions and the like.
>
> In addition to the answers from Maxime and Ricardo ... I searched
> because I thought guile has(had) a graphviz binding somewhere, here is
> what I found
>
> 	https://reposcope.com/man/en/3guile/gv

Yes, graphviz comes with Guile bindings.  Since they don’t provide a
Guile module you need to define a module like this:

--8<---------------cut here---------------start------------->8---
(define-module (libgv)
  #:export (digraph node edge layout render setv))

(load-extension "libgv_guile" "SWIG_init")
--8<---------------cut here---------------end--------------->8---

These bindings are not very useful for anything beyond visualizing
graphs, though.

-- 
Ricardo



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Looking for graph library
  2022-10-09 10:51   ` Ricardo Wurmus
@ 2022-10-09 18:47     ` David Pirotte
  2022-10-09 23:00       ` Ricardo Wurmus
  0 siblings, 1 reply; 12+ messages in thread
From: David Pirotte @ 2022-10-09 18:47 UTC (permalink / raw)
  To: Ricardo Wurmus; +Cc: Dmitry Polyakov, guile-user

[-- Attachment #1: Type: text/plain, Size: 650 bytes --]

Hello Ricardo,

> > 	https://reposcope.com/man/en/3guile/gv  

> Yes, graphviz comes with Guile bindings.  Since they don’t provide a
> Guile module you need to define a module like this:

> --8<---------------cut here---------------start------------->8---
> (define-module (libgv)
>   #:export (digraph node edge layout render setv))
> 
> (load-extension "libgv_guile" "SWIG_init")
> --8<---------------cut here---------------end--------------->8---

Right, but the module I was looking at - but did not try - exports quite
a lot more functionality ...

	https://github.com/roelj/graphviz-guile/blob/master/graphviz.scm

David

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Looking for graph library
  2022-10-09 18:47     ` David Pirotte
@ 2022-10-09 23:00       ` Ricardo Wurmus
  2022-10-12  9:13         ` Dmitry Polyakov
  0 siblings, 1 reply; 12+ messages in thread
From: Ricardo Wurmus @ 2022-10-09 23:00 UTC (permalink / raw)
  To: David Pirotte; +Cc: Dmitry Polyakov, guile-user


David Pirotte <david@altosw.be> writes:

>> > 	https://reposcope.com/man/en/3guile/gv  
>
>> Yes, graphviz comes with Guile bindings.  Since they don’t provide a
>> Guile module you need to define a module like this:
>
>> --8<---------------cut here---------------start------------->8---
>> (define-module (libgv)
>>   #:export (digraph node edge layout render setv))
>> 
>> (load-extension "libgv_guile" "SWIG_init")
>> --8<---------------cut here---------------end--------------->8---
>
> Right, but the module I was looking at - but did not try - exports quite
> a lot more functionality ...
>
> 	https://github.com/roelj/graphviz-guile/blob/master/graphviz.scm

Yes, this is essentially the same.  My example above just shows the
pattern that needs to be used.  Roel’s module there exports *all* the
procedures.

The point, however, is — as Dmitry wrote earlier — that gv does not
provide any predicates or search algorithms.  gv only provides
constructors and accessors to the graphviz data structure, and the means
to render them.

-- 
Ricardo



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Looking for graph library
  2022-10-08 14:07 ` Ricardo Wurmus
@ 2022-10-10 17:18   ` zimoun
  0 siblings, 0 replies; 12+ messages in thread
From: zimoun @ 2022-10-10 17:18 UTC (permalink / raw)
  To: Ricardo Wurmus, Dmitry Polyakov; +Cc: guile-user

Hi Ricardo,

On sam., 08 oct. 2022 at 16:07, Ricardo Wurmus <rekado@elephly.net> wrote:

> Here’s an implementation of inductive graphs:
>
> https://github.com/rekado/minigrad/blob/main/minigrad/graph.scm

IIUC, mingrad is implementing a part of [1].  Some time ago, I have read
this interesting paper from Haskell folks:

    https://dl.acm.org/doi/pdf/10.1145/3122955.3122956

and maybe this kind of framework could be useful.

1: <https://web.engr.oregonstate.edu/~erwig/papers/InductiveGraphs_JFP01.pdf>


Cheers,
simon



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Looking for graph library
  2022-10-09 23:00       ` Ricardo Wurmus
@ 2022-10-12  9:13         ` Dmitry Polyakov
  2022-10-12 16:33           ` Ricardo Wurmus
  0 siblings, 1 reply; 12+ messages in thread
From: Dmitry Polyakov @ 2022-10-12  9:13 UTC (permalink / raw)
  To: Ricardo Wurmus, David Pirotte; +Cc: guile-user

Ricardo Wurmus <rekado@elephly.net> writes:

Could you provide examples of graph creation? It's not entirely obvious
to me how they need to be created via graph-cons.

I tried doing something like this:

(define test				;name
	(graph-cons (context 		; making context
		     "this-is-label"	;context label
		     (list (link "this-is-label-1" 1)) ;first link
		     (list (link "this-is-label-2" 1))) ;second link
		    graph-null))			;initial graph

This structure is created without problems, but when I want to draw it
through (draw test)

Then I get an error:

ice-9/boot-9.scm:1685:16: In procedure raise-exception:
In procedure struct-vtable: Wrong type argument in position 1 (expecting struct): 1

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.



-- 
sanity level in trouble



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Looking for graph library
  2022-10-12  9:13         ` Dmitry Polyakov
@ 2022-10-12 16:33           ` Ricardo Wurmus
  2022-10-12 19:35             ` Dmitry Polyakov
  2022-10-14 17:17             ` zimoun
  0 siblings, 2 replies; 12+ messages in thread
From: Ricardo Wurmus @ 2022-10-12 16:33 UTC (permalink / raw)
  To: Dmitry Polyakov; +Cc: David Pirotte, guile-user

[-- Attachment #1: Type: text/plain, Size: 949 bytes --]


Hi Dmitry,

> Ricardo Wurmus <rekado@elephly.net> writes:
>
> Could you provide examples of graph creation? It's not entirely obvious
> to me how they need to be created via graph-cons.
>
> I tried doing something like this:
>
> (define test				;name
> 	(graph-cons (context 		; making context
> 		     "this-is-label"	;context label
> 		     (list (link "this-is-label-1" 1)) ;first link
> 		     (list (link "this-is-label-2" 1))) ;second link
> 		    graph-null))			;initial graph
>
> This structure is created without problems, but when I want to draw it
> through (draw test)

You can’t use (minigrad dot)’s “draw” because it’s not a generic graph
drawing tool.  It’s used for minigrad expressions, which have a certain
structure.  (Minigrad is supposed to be a testbed for neural nets with
automatic backpropagation.)

Here’s a simple example with a generic “draw” procedure to get you
started:


[-- Attachment #2: graph-example.scm --]
[-- Type: application/octet-stream, Size: 2794 bytes --]

(use-modules (minigrad graph)
             (minigrad libgv)
             (srfi srfi-26)
             (ice-9 match))

(define* (draw graph #:optional (port (mkstemp "/tmp/minigrad-XXXXXX")))
  "Use Graphviz to draw the GRAPH as an SVG to FILENAME."
  (define g (digraph "whatever"))
  (setv g "rankdir" "LR")
  (let ((node-contexts (map (cut find-context <> graph)
                            (topo-sort graph)))
        (edges (ufold (match-lambda*
                        ((($ <context> source label in out) acc)
                         (append (map (match-lambda
                                        (($ <link> edge-label target)
                                         (list source target edge-label)))
                                      out)
                             (map (match-lambda
                                    (($ <link> edge-label target)
                                     (list target source edge-label)))
                                  in)
                           acc)))
                      '() graph)))
    ;; Add a node for each value.
    (for-each (match-lambda
                (($ <context> value label in out)
                 (let* ((uid (object->string value))
                        (g:node (node g uid)))
                   (setv g:node "shape" "record")
                   (setv g:node "label" label))))
              node-contexts)

    ;; Connect all edges by name
    (for-each (match-lambda
                ((source target edge-label)
                 (edge g
                       (object->string source)
                       (object->string target))))
              edges))

  (layout g "dot")

  (let ((filename (port-filename port)))
    (close port)
    (render g "svg" filename)

    ;; Let me see this in Emacs Geiser
    (format #false "#<Image: ~a>" filename)))

;; Construct a graph with graph-cons
(define my-graph/cons
  (let ((nodeA "A")
        (nodeB "B")
        (nodeC "C"))
    (graph-cons (context nodeC "C"
                         (list (link "C<-A" nodeA)
                               (link "C<-B" nodeB))
                         (list (link "C->A" nodeA)))
                (graph-cons (context nodeB "B" (list (link "B<-A" nodeA)) (list))
                            (graph-cons (context nodeA "A" (list) (list))
                                        graph-null)))))

;; Same thing with contexts->graph.
(define my-graph
  (let ((nodeA "A")
        (nodeB "B")
        (nodeC "C"))
    (contexts->graph
     (list (context nodeA "A" (list) (list))
           (context nodeB "B" (list (link "B<-A" nodeA)) (list))
           (context nodeC "C"
                    (list (link "C<-A" nodeA)
                          (link "C<-B" nodeB))
                    (list (link "C->A" nodeA)))))))

(draw my-graph)

[-- Attachment #3: Type: text/plain, Size: 745 bytes --]


As you can see every context links only to nodes that already exist in
the graph.  In the graph-cons example you see that node A cannot have
any links to other nodes, because none exist.  The edges between A and C
and A and B are added later when the contexts of these nodes are cons’d
onto the graph.  That’s the big idea behind inductive graphs.

To make the graphviz bindings work you need to set GUILE_EXTENSIONS_PATH
first.  I recommend this:

$ guix shell graphviz guile
$ [env] export GUILE_EXTENSIONS_PATH=$GUIX_ENVIRONMENT/lib/guile/3.0/extensions
$ [env] guile --listen

Then M-x connect-to-guile in a graphical Emacs and evaluate the code
above to see the graph visualization.

Hope this helps!

-- 
Ricardo

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Looking for graph library
  2022-10-12 16:33           ` Ricardo Wurmus
@ 2022-10-12 19:35             ` Dmitry Polyakov
  2022-10-14 17:17             ` zimoun
  1 sibling, 0 replies; 12+ messages in thread
From: Dmitry Polyakov @ 2022-10-12 19:35 UTC (permalink / raw)
  To: Ricardo Wurmus; +Cc: David Pirotte, guile-user

Ricardo Wurmus <rekado@elephly.net> writes:

> Hope this helps!

This help me a lot. Thank you for response >.< and for this library. May
be you have intention to pack this as ice-9 module and send patch to
guix? 

-- 
sanity level in trouble



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Looking for graph library
  2022-10-12 16:33           ` Ricardo Wurmus
  2022-10-12 19:35             ` Dmitry Polyakov
@ 2022-10-14 17:17             ` zimoun
  1 sibling, 0 replies; 12+ messages in thread
From: zimoun @ 2022-10-14 17:17 UTC (permalink / raw)
  To: Ricardo Wurmus, Dmitry Polyakov; +Cc: David Pirotte, guile-user

Hi Ricardo,

On mer., 12 oct. 2022 at 18:33, Ricardo Wurmus <rekado@elephly.net> wrote:

>             (Minigrad is supposed to be a testbed for neural nets with
> automatic backpropagation.)

Probably out of topic and much bigger than your initial intention, the
Julia programming language is taking an interesting approach,

    https://fluxml.ai/Zygote.jl/latest/
    https://arxiv.org/abs/1810.07951

which exploits the compiler.  It could be nice if Guile compiler could
be exploited for similar purposes. :-)


Cheers,
simon





^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2022-10-14 17:17 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-08 10:29 Looking for graph library Dmitry Polyakov
2022-10-08 13:42 ` Maxime Devos
2022-10-08 14:07 ` Ricardo Wurmus
2022-10-10 17:18   ` zimoun
2022-10-09  0:20 ` David Pirotte
2022-10-09 10:51   ` Ricardo Wurmus
2022-10-09 18:47     ` David Pirotte
2022-10-09 23:00       ` Ricardo Wurmus
2022-10-12  9:13         ` Dmitry Polyakov
2022-10-12 16:33           ` Ricardo Wurmus
2022-10-12 19:35             ` Dmitry Polyakov
2022-10-14 17:17             ` zimoun

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).