unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Ricardo Wurmus <rekado@elephly.net>
To: Dmitry Polyakov <liltechdude@gmail.com>
Cc: David Pirotte <david@altosw.be>, guile-user@gnu.org
Subject: Re: Looking for graph library
Date: Wed, 12 Oct 2022 18:33:22 +0200	[thread overview]
Message-ID: <87bkqhhtif.fsf@elephly.net> (raw)
In-Reply-To: <87edvdpfps.fsf@gmail.com>

[-- 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

  reply	other threads:[~2022-10-12 16:33 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2022-10-12 19:35             ` Dmitry Polyakov
2022-10-14 17:17             ` zimoun

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bkqhhtif.fsf@elephly.net \
    --to=rekado@elephly.net \
    --cc=david@altosw.be \
    --cc=guile-user@gnu.org \
    --cc=liltechdude@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).