[racket-users] Using brag parser library with Typed Racket

2017-02-04 Thread Sourav Datta
Hi everyone!

I was taking a look at the brag library (http://docs.racket-lang.org/brag/) for 
AST generation and thought of using it in one of my projects which is in TR. I 
started by requiring brag/support with type annotations like this:

#lang typed/racket

(require/typed brag/support
   [#:opaque Token token-struct?]
   [token (->* ((U String Symbol))
   (Any
#:line Positive-Integer
#:column Natural
#:position Positive-Integer
#:span Natural
#:skip? Boolean)
   Token)])

This worked. So far so good. But when trying to import a file which has #lang 
brag in it I encountered this problem.

(require/typed "grammar1.rkt"
  [parse (->* (Listof Token)
  (Any)
  Syntax)])

Type Checker: Type (->* (Listof Token) (Any) Syntax) could not be converted to 
a contract: required a flat contract but generated a chaperone contract in: 
(->* (Listof Token) (Any) Syntax)

I am not sure what this means exactly but is this because Racket does not see 
the automatically exported function parse (or parse-tree) from a #lang brag 
module? And, is there a way to correct this error so that I can call the parse 
function from a TR module? 

Note: I found a github issue that could be related to this here: 
https://github.com/racket/typed-racket/issues/338

Thanks, 
Sourav

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


Re: [racket-users] Using brag parser library with Typed Racket

2017-02-04 Thread Matthew Butterick

> On Feb 4, 2017, at 8:18 AM, Sourav Datta  wrote:
> 
> (require/typed "grammar1.rkt"
>  [parse (->* (Listof Token)
>  (Any)
>  Syntax)])
> 
> Type Checker: Type (->* (Listof Token) (Any) Syntax) could not be converted 
> to a contract: required a flat contract but generated a chaperone contract 
> in: (->* (Listof Token) (Any) Syntax)
> 
> I am not sure what this means exactly but is this because Racket does not see 
> the automatically exported function parse (or parse-tree) from a #lang brag 
> module? And, is there a way to correct this error so that I can call the 
> parse function from a TR module? 

1) That type declaration for `parse` seems both too strict (in terms of the 
input `parse` will accept), and not quite accurate in terms of order of 
arguments. I might do it this way:

(require/typed brag/support
   [#:opaque Token token-struct?]
   [token (->* ((U String Symbol))
   (Any
#:line Positive-Integer
#:column Natural
#:position Positive-Integer
#:span Natural
#:skip? Boolean)
   Token)])

(define-type Tok (U String Symbol Token Void))

(require/typed "grammar1.rkt"
   [parse (case-> ((U (Listof Tok) (-> Tok)) . -> . Any)
  (Any (U (Listof Tok) (-> Tok)) . -> . Any))])


2) If I use `Any` rather than `Syntax` as the output type, then `parse` works. 

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