You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('') and can be up to 35 characters long.
78 lines
2.3 KiB
78 lines
2.3 KiB
;;; Copyright 2021 Sergiu Ivanov <sivanov@colimite.fr>


;;;


;;; Licensed under the Apache License, Version 2.0 (the "License");


;;; you may not use this file except in compliance with the License.


;;; You may obtain a copy of the License at


;;;


;;; http://www.apache.org/licenses/LICENSE2.0


;;;


;;; Unless required by applicable law or agreed to in writing, software


;;; distributed under the License is distributed on an "AS IS" BASIS,


;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.


;;; See the License for the specific language governing permissions and


;;; limitations under the License.




#lang typed/racket




;;; This file implements Alex Knauth's solution presented here:


;;;


;;; https://stackoverflow.com/questions/65386334/racketgenericgraphlibraryintypedracket




(module graphwrapper racket


(require (prefixin g: graph))


(provide graph? hasvertex? hasedge?




directedgraph




graphviz)




;; Wrap the opaque graph structure coming from the generic


;; graph library.


(struct graph (g))


(define gg graphg)




(define (hasvertex? g v)


(g:hasvertex? (gg g) v))


(define (hasedge? g u v)


(g:hasedge? (gg g) u v))




;; 2 Graph constructors


;; 2.2 Weighted graphs


(define (directedgraph es [ws #f])


(graph (g:directedgraph es ws)))




;; 10 Graphviz


(define (graphviz g #:output [output #f] #:colors [colors #f])


(g:graphviz (gg g) #:output output #:colors colors)))






(require/typed/provide 'graphwrapper


[#:opaque Graph graph?]


[hasvertex? (> Graph Any Boolean)]


[hasedge? (> Graph Any Any Boolean)]




;; 2 Graph constructors


;; 2.2 Weighted graphs


[directedgraph (>* ((Listof (List Any Any))) ((Listof Any)) Graph)]




;; 10 Graphviz


[graphviz (>* (Graph)


(#:output OutputPort


#:colors (HashTable Any Natural))


String)])






(module+ test


;; The goal of the tests is to check that all of the provided


;; functions can be invoked without errors. The tests do not check


;; whether the results make sense.


(require typed/rackunit)




(testcase "1 Generic Graph Interface"


(define g (directedgraph '((a b) (b c))))


(checktrue (hasedge? g 'a 'c))


(checktrue (hasvertex? g 'a)))




(testcase "10 Graphviz"


(define g (directedgraph '((a b) (b c))))


(graphviz g)))
