After some poking around, here is what I can offer:

#lang racket

(module predicates racket
  (provide discrete-dist-thing?)
  
  ;; ---------------------------------
  ;; dependencies 
  (require math/distributions)

  ;; ---------------------------------
  ;; implementation 
  (define (discrete-dist-thing? x)
    (define is-it-discrete-dist-values
      (with-handlers ([exn:fail? (lambda (xn)
                                   (define msg (exn-message xn))
                                   (if (regexp-match? #rx"expected: 
discrete-dist-struct" msg)
                                       #false
                                       (raise xn)))])
        (discrete-dist-values x)))
    (and is-it-discrete-dist-values
         (andmap symbol? is-it-discrete-dist-values)
         (let ([probs (discrete-dist-probs x)])
           (and (andmap flonum? probs) (andmap positive? probs))))))

(module server racket
  (require (submod ".." predicates))
  (provide
   (contract-out
    [d discrete-dist-thing?]))

  ;; ---------------------------------
  ;; dependencies 
  (require math/distributions)

  ;; ---------------------------------
  ;; implementation 
  (define d (discrete-dist '(one two))))

(require 'server)
d


Watch for symbol?. You want to abstract over that. 



> On Jul 28, 2017, at 12:16 PM, Matthias Felleisen <matth...@ccs.neu.edu> wrote:
> 
>> 
>> On Jul 28, 2017, at 11:23 AM, James Geddes <jged...@turing.ac.uk> wrote:
>> 
>> This is likely a typed-untyped interface newbie question.
>> 
>> Briefly, how can I write a contract that asserts that some value has a type 
>> that is defined in an imported typed/racket library?
>> 
>> In more detail, I am using the math/distributions library, which is written 
>> in typed/racket. My module is written in untyped Racket, and I'm figuring 
>> out how to use contacts:
>> 
>> #lang racket
>> (require math/distributions) ;; this is a typed/racket library
>> 
>> (define my-dist (discrete-dist '(thing-one thing-two)))
>> 
>> (provide (contract-out [my-dist ???]))
>> 
>> 
>> The question is: what should ??? be?. There is a distribution? predicate but 
>> I'd quite like to be more specific: namely, that my-dist is a discrete 
>> distribution. In the source of math/distributions, the following type is 
>> defined:
>> 
>>      (define-type (Discrete-Dist A) (discrete-dist-struct A A))
>> 
>> but I don't know how this might translate into my untyped module.
>> 
>> 
>> Any help much appreciated!
> 
> 
> This is a great question. It suggests we should provide these generated 
> contracts (or grant access them) via a modicum of reflection. 
> 
> In the meantime, you want to look at the result type of discrete-dist, which 
> is the type (Discrete-Dist A). This is turn is a subtype of (dist A A), which 
> is opaque/undocumented/I can’t find it in the code base (on the fly). It’s 
> not exported. So at the moment, your will have to take the fall if someone 
> abuses it. 
> 
> — Matthias
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to