typed-graph/graph.rkt

54 lines
1.7 KiB
Racket

;;; 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/LICENSE-2.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/racket-generic-graph-library-in-typed-racket
(module graph-wrapper racket
(require (prefix-in g: graph))
(provide graph?
directed-graph
has-vertex?
has-edge?
graphviz)
(struct graph (g))
(define (directed-graph es [ws #f])
(graph (g:directed-graph es ws)))
(define (has-vertex? g v)
(g:has-vertex? (graph-g g) v))
(define (has-edge? g u v)
(g:has-edge? (graph-g g) u v))
(define (graphviz g #:output [output #f] #:colors [colors #f])
(g:graphviz (graph-g g) #:output output #:colors colors)))
(require/typed 'graph-wrapper
[#:opaque Graph graph?]
[directed-graph (->* ((Listof (List Any Any))) ((Listof Any)) Graph)]
[has-vertex? (-> Graph Any Boolean)]
[has-edge? (-> Graph Any Any Boolean)]
[graphviz (->* (Graph)
(#:output Output-Port
#:colors (HashTable Any Natural))
String)])
(define g (directed-graph '((a b) (b c))))
(has-edge? g 'a 'c)
(has-vertex? g 'a)
(graphviz g)