require/typed and opaque structures

31 views
Skip to first unread message

unlimitedscolobb

unread,
Dec 23, 2020, 5:59:16 PM12/23/20
to Racket Users
Hello,

Again, a question out of curiosity, because I think I found a solution, but I'd like to have something more elegant.

I'm trying to use the (untyped) Racket generic graph library from my Typed Racket code:

#lang typed/racket

(require/typed graph
  [#:opaque Graph graph?]
  [directed-graph (->* ((Listof (List Any Any))) ((Listof Any)) Graph)])

(directed-graph '((a b) (b c)))

When I load this code in the REPL I get

graph?: contract violation
  any-wrap/c: Unable to protect opaque value passed as `Any`
  value: #<unweighted-graph>
  This warning will become an error in a future release.
  in: the 1st argument of
      a part of the or/c of
      (or/c
       struct-predicate-procedure?/c
       (-> Any boolean?))
  contract from: (interface for graph?)
  blaming: <pkgs>/dds/tmp.rkt
   (assuming the contract is correct)
  at: <pkgs>/dds/tmp.rkt:4.18
#<unweighted-graph>

I found this solution by Alex Knauth: https://stackoverflow.com/questions/65386334/racket-generic-graph-library-in-typed-racket , which essentially consists in wrapping the opaque graph struct into another struct.  Alex says that the problem comes from the fact that graph? essentially encompasses a couple different internal representations the graph library uses.

While this solution should fit me, I was wondering whether anybody here has encountered this issue and has a different solution.

-
Sergiu

unlimitedscolobb

unread,
Dec 25, 2020, 5:28:41 PM12/25/20
to Racket Users
Since apparently I am not the only one wanting to use graphs in Typed Racket, I will prepare a typed interface to Stephen Chang's graph library: typed-graph.  I am planning to upload it to the main package catalog.

If you have any advice or ideas, or want to contribute to the effort, do tell me.

-
Sergiu
Reply all
Reply to author
Forward
0 new messages