Re: [racket-users] Best way to destruct objects allocated with "atomic interior" malloc

2017-10-03 Thread Eric Dobson
I've actually simplified my program that causes the issues and it doesn't
need the callback or the foreign functions at all it just needs structs
with substructs.

This shows that if garbage is collected after making the substruct is
complete then the finalizer gets run, which trashes the memory I'm about to
read. The interior memory will-registration would solve this, but given
that the docs say that it shouldn't be used for small objects I don't want
to use it for every slice object in my program. I think I need to come up
with a different method for handling garbage collection of structs, or
possible judicious use of the recently added void/reference-sink once the
new release is out.

#lang racket/base

(require
  ffi/unsafe
  ffi/unsafe/alloc)

(define-cstruct _grpc-slice-refcounted
  ([bytes _pointer]
   [length _size]))

(define-cstruct _grpc-slice-inlined
  ([length _uint8]
   [bytes (_array _uint8 (+ (ctype-sizeof _size) (ctype-sizeof _pointer)
-1))]))

(define-cstruct _grpc-slice/ffi
  ([refcount _pointer]
   [data (_union _grpc-slice-refcounted _grpc-slice-inlined)]))

;; Struct that hides the raw pointer from the exposed api
(struct grpc-slice (pointer))

(define (grpc-slice-length slice)
  (define ffi-slice (grpc-slice-pointer slice))
  (if (grpc-slice/ffi-refcount ffi-slice)
  (grpc-slice-refcounted-length (union-ref (grpc-slice/ffi-data
ffi-slice) 0))
  (grpc-slice-inlined-length
(begin0
  (union-ref (grpc-slice/ffi-data ffi-slice) 1)
  (collect-garbage)

(define (make-grpc-slice)
  (define p (ptr-ref (malloc _grpc-slice/ffi) _grpc-slice/ffi))
  (memset p #x00 1 _grpc-slice/ffi)
  ;; Would call slice-unref which might free the memory in the real code.
  (register-finalizer p (lambda (p) (memset p #xFF 1 _grpc-slice/ffi)))
  (grpc-slice p))


(define s (make-grpc-slice))
(grpc-slice-length s)
(grpc-slice-length (begin0 s (set! s #f)))




On Tue, Oct 3, 2017 at 7:55 PM, Eric Dobson  wrote:

> Parsing it out in atomic mode probably will work, I'll look at that. The
> callback method I described is actually not how it works, and it is a bit
> more complicated (involving a separate place and blocking foreign calls),
> but I can do all of the decomposing work in atomic mode. I think I'm
> avoiding atomic mode because in a multithreaded system it would be somewhat
> expensive operation, but that might not hold in Racket.
>
> The other option I'm looking at is to construct the racket child struct
> reference before handing it off to the foreign library, and having them be
> responsible for deallocating their fields and then decrementing a ref count
> on the parent whose will will only be responsible for freeing the direct
> memory.
>
> The will functionality I'm describing is different than current wills as
> current wills are ready when the cpointer is unreachable not when the
> memory pointed to by the cpointer is collectable.
>
> #lang racket
> (require ffi/unsafe)
>
>
> (define p1 (malloc 'atomic-interior 100))
>
> (define port (current-output-port))
> (register-finalizer p1 (lambda (p1) (displayln 'dead port)))
> (define p2 (ptr-add p1 10))
> (set! p1 #f)
> (collect-garbage)
> (sleep 4)
>
> In this example it wouldn't print "dead" because p2 is still holding the
> memory alive even though p1 is dead. But if p2 was cleared then it would
> get printed.
>
>
>
> On Tue, Oct 3, 2017 at 7:04 PM, Matthew Flatt  wrote:
>
>> Is the work to parse out the values short enough that you can do it
>> atomically in response to the notification that says the data is ready?
>> If so, it's probably best to parse and free in atomic mode. Or can you
>> at least atomically separate out references to children, where
>> finalizers can sensibly be attached to the individual references?
>>
>> If not, then I don't have a better idea than the approach you describe.
>> I don't quite follow why you'd need new functionality from wills, since
>> a will procedure on a cpointer already receives the cpointer back when
>> there are otherwise no references to the cpointer.
>>
>> At Tue, 3 Oct 2017 18:39:04 -0700, Eric Dobson wrote:
>> > I'm dealing with some foreign apis that want to be passed long lived
>> output
>> > pointers to structs. The apis eventually call back indicating that the
>> > struct has been filled in appropriately and then I want to read out the
>> > values and deallocate the structs. I'm using atomic interior memory for
>> > these structs as they don't need to hold GCable values themselves and
>> they
>> > need to not be moved while the foreign library has a pointer to them.
>> Once
>> > they are back I nee

Re: [racket-users] Best way to destruct objects allocated with "atomic interior" malloc

2017-10-03 Thread Eric Dobson
Parsing it out in atomic mode probably will work, I'll look at that. The
callback method I described is actually not how it works, and it is a bit
more complicated (involving a separate place and blocking foreign calls),
but I can do all of the decomposing work in atomic mode. I think I'm
avoiding atomic mode because in a multithreaded system it would be somewhat
expensive operation, but that might not hold in Racket.

The other option I'm looking at is to construct the racket child struct
reference before handing it off to the foreign library, and having them be
responsible for deallocating their fields and then decrementing a ref count
on the parent whose will will only be responsible for freeing the direct
memory.

The will functionality I'm describing is different than current wills as
current wills are ready when the cpointer is unreachable not when the
memory pointed to by the cpointer is collectable.

#lang racket
(require ffi/unsafe)


(define p1 (malloc 'atomic-interior 100))

(define port (current-output-port))
(register-finalizer p1 (lambda (p1) (displayln 'dead port)))
(define p2 (ptr-add p1 10))
(set! p1 #f)
(collect-garbage)
(sleep 4)

In this example it wouldn't print "dead" because p2 is still holding the
memory alive even though p1 is dead. But if p2 was cleared then it would
get printed.



On Tue, Oct 3, 2017 at 7:04 PM, Matthew Flatt  wrote:

> Is the work to parse out the values short enough that you can do it
> atomically in response to the notification that says the data is ready?
> If so, it's probably best to parse and free in atomic mode. Or can you
> at least atomically separate out references to children, where
> finalizers can sensibly be attached to the individual references?
>
> If not, then I don't have a better idea than the approach you describe.
> I don't quite follow why you'd need new functionality from wills, since
> a will procedure on a cpointer already receives the cpointer back when
> there are otherwise no references to the cpointer.
>
> At Tue, 3 Oct 2017 18:39:04 -0700, Eric Dobson wrote:
> > I'm dealing with some foreign apis that want to be passed long lived
> output
> > pointers to structs. The apis eventually call back indicating that the
> > struct has been filled in appropriately and then I want to read out the
> > values and deallocate the structs. I'm using atomic interior memory for
> > these structs as they don't need to hold GCable values themselves and
> they
> > need to not be moved while the foreign library has a pointer to them.
> Once
> > they are back I need to construct derived pointers to the sub structs and
> > parse out all the different parameters.
> >
> > The issue I'm running into is reliably destroying the struct when I'm
> done
> > parsing out all the values. If it was as simple as freeing the memory, it
> > would be easy as I can rely on the GC to do that. But it is not as the
> > struct has pointer fields that may have been initialized by the foreign
> > library and thus I need to actively run destructor code to free those
> > subparts before freeing the struct. This seems like a good use for
> > will-executors, but the issue with those is that they are attached to
> > racket values not the underlying memory object. And in this case because
> > the memory is allocated with 'atomic-interior', it is possible for the
> > original pointer to no longer be needed and only derived pointers needed.
> >
> > I'm thinking there may be clever ways with making sure that every derived
> > pointer either maintains an explicit reference or increments a reference
> > count on the original pointer, but I'm worried that is very complicated
> and
> > likely to be broken. The easiest solution for me would be to have
> something
> > like a will that could be attached to a 'cpointer?' value and would be
> > called back with a fresh 'cpointer?' value that pointed at the same
> address
> > once there were only weak references to the object. Is this possible or
> > does anyone see a better solution?
> >
> >
> > Example:
> >
> > ;; Child corresponds to a byte array
> > (define-cstruct _child ([ptr _pointer] [len _int])
> > ;; Parent has two inlined children
> > (define-cstruct _parent ([child1 _child] [child2 _child]))
> >
> > ;; When the parent is to be cleaned up I need to ensure that the two
> > children have their 'ptr' fields passed to free.
> >
> > ;; The issue is in a function that wants to decompose the parent into
> > children pointers and use their values:
> > (define (child->bytes c) )
> > (

[racket-users] Best way to destruct objects allocated with "atomic interior" malloc

2017-10-03 Thread Eric Dobson
I'm dealing with some foreign apis that want to be passed long lived output
pointers to structs. The apis eventually call back indicating that the
struct has been filled in appropriately and then I want to read out the
values and deallocate the structs. I'm using atomic interior memory for
these structs as they don't need to hold GCable values themselves and they
need to not be moved while the foreign library has a pointer to them. Once
they are back I need to construct derived pointers to the sub structs and
parse out all the different parameters.

The issue I'm running into is reliably destroying the struct when I'm done
parsing out all the values. If it was as simple as freeing the memory, it
would be easy as I can rely on the GC to do that. But it is not as the
struct has pointer fields that may have been initialized by the foreign
library and thus I need to actively run destructor code to free those
subparts before freeing the struct. This seems like a good use for
will-executors, but the issue with those is that they are attached to
racket values not the underlying memory object. And in this case because
the memory is allocated with 'atomic-interior', it is possible for the
original pointer to no longer be needed and only derived pointers needed.

I'm thinking there may be clever ways with making sure that every derived
pointer either maintains an explicit reference or increments a reference
count on the original pointer, but I'm worried that is very complicated and
likely to be broken. The easiest solution for me would be to have something
like a will that could be attached to a 'cpointer?' value and would be
called back with a fresh 'cpointer?' value that pointed at the same address
once there were only weak references to the object. Is this possible or
does anyone see a better solution?


Example:

;; Child corresponds to a byte array
(define-cstruct _child ([ptr _pointer] [len _int])
;; Parent has two inlined children
(define-cstruct _parent ([child1 _child] [child2 _child]))

;; When the parent is to be cleaned up I need to ensure that the two
children have their 'ptr' fields passed to free.

;; The issue is in a function that wants to decompose the parent into
children pointers and use their values:
(define (child->bytes c) )
(define (parent->bytes p)
  (bytes-append (child->bytes (parent-child1 p)) #"." (child->bytes
(parent-child2 p

;; In this after the parent-child2 call, there is no strong reference to p
so if this was the last reference to it, GC could happen before
child->bytes was called and determine that p was unreachable and so any
wills would become ready. Thus I cannot free the underlying memory in a
will.

-- 
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] Safely allocating memory in places enabled world

2017-10-02 Thread Eric Dobson
George: I don't see that invariant for java in anything that I search for
on Java finalizers. Do you have a reference? In particular java only has
one finalizer per object (the finalize method) and across objects the
references I found seem to imply that there is no guaranteed order?

https://en.wikipedia.org/wiki/Finalization#Connection_with_finally seems to
imply no ordering across finalizers.


Matthew: Ok, I'll just file a bug to track the issue. I'm trying to do the
best available solution following standard practices, and not actually that
worried about the memory leak for my program.

On Mon, Oct 2, 2017 at 1:48 PM, George Neuner  wrote:

>
> On 10/2/2017 2:52 PM, Matthew Flatt wrote:
>
> If we change `ffi/unsafe/alloc` so that a custodian shutdown runs all
> deallocators, that would work in many cases. It would create a problem,
> however, if there are objects to deallocate where the deallocation
> function references other objects that themselves must be deallocated.
> That is, an allocated value A might depend on an allocated value B up
> until the point that A has been deallocated. That dependency can be
> expressed to `ffi/unsafe/alloc` (via the garbage collector) by
> including a reference to B in the deallocation closure for A. Setting
> up dependencies that way is not common, but I'm pretty sure I've used
> that pattern once or twice. Meanwhile, there's no similar way to order
> callbacks that are registered with a custodian.
>
>
> IIRC, the JVM executes finalizers in reverse order of registration -
> expecting that, in your example, A would have been allocated before B.  Is
> that something that would work here?  [He asks naively.]
>
> I'm tempted to say that `ffi/unsafe/alloc` should only be used for
> objects where the deallocation order doesn't matter, so that
> deallocators can be triggered by a custodian shutdown. But an
> additional constraint is needed: values to be deallocated cannot be
> used by any other custodian-shutdown callbacks. That additional
> constraint seems too limiting.
>
> Maybe we need to add some sort of ordering constraints to custodians,
> but I'm not immediately sure of the right way to do that.
>
>
> George
>
> --
> 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.


[racket-users] Safely allocating memory in places enabled world

2017-09-30 Thread Eric Dobson
I'm trying to write some racket code which interfaces with a foreign
library and provides a safe interface. Some of the functions I'm calling
allocate memory and need to have this explicitly freed by the caller. The
'allocator' binding from ffi/unsafe/alloc seems to solve this problem, but
I'm running into issues using it from places other than the main one. The
issue is that if a place gets shut down then the finalizer that was
registered don't seem to run, and I can observe the leaked memory at the
process level.

I took a look at ffi/unsafe/custodian and it seems that
register-custodian-shutdown would allow this to be implemented. Is there a
reason that such allocator doesn't do this by default?

Code:
https://gist.github.com/endobson/6efdca16885162b51b76ab385e9afa5b

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


[racket-users] Problems connecting to mirror.racket-lang.org

2016-12-22 Thread Eric Dobson
I have been downloading racket in my continuous builds for a while from

http://mirror.racket-lang.org/installers/6.6/racket-minimal-6.6-x86_64-macosx.tgz

but today it stopped working. Is this just a transient error with the
server or should I change my urls? I noticed that the new urls on the page
are

https://download.racket-lang.org/releases/6.6/installers/racket-minimal-6.6-x86_64-macosx.tgz

Last successful build was 21 hours ago:
https://travis-ci.org/endobson/yaspl2/builds/186010189
But haven't attempted any builds till now.



Al it seems that the download all versions page is the old css style.

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


[racket-users] Slow expansion. (Likely of match)

2016-03-23 Thread Eric Dobson
I'm seeing slow expansion (on the order of hundreds of milliseconds) for
the following program:
https://gist.github.com/endobson/e165edf224b4028db48d

It is a single function of generated code. Similar functions that don't use
match as extensively are much faster < 10 milliseconds.

I'm trying to understand what is likely the cause of the slowness here so
that I can avoid triggering it in my code generation.

And also what speed I can expect from racket expansion here. My current
timing is ~2.5-3 seconds for ~600 definitions of similar size or smaller.
I'm trying to get that as fast as possible hopefully under 1s as it is in
the critical path of my run/test cycle. Is that a reasonable goal?

-- 
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] time_t and size_t ffi types

2015-06-20 Thread Eric Dobson
That seems to be defined as _uintptr which could be different than 'size_t'
in C, but is probably a better choice than '_int64'.

On Tue, Jun 16, 2015 at 9:55 AM, Marc Burns  wrote:

> What about _size in ffi/unsafe ?
>
> On Jun 14, 2015, at 1:39 PM, Eric Dobson  wrote:
>
> I'm currently on working on bindings to a foreign library that has
> elements in structs that are defined as time_t and size_t. I couldn't find
> anything in the core ffi types that corresponded to these, so currently I
> am using int64_t as that was what I found was correct for the system I'm
> currently working on (OS X), which might not work when I try to port this
> to other OS's (Linux).
>
> What is the best practice for using such types, is there any core c-type I
> should use or should I just roll my own?
>
> --
> 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.


[racket-users] time_t and size_t ffi types

2015-06-16 Thread Eric Dobson
I'm currently on working on bindings to a foreign library that has elements
in structs that are defined as time_t and size_t. I couldn't find anything
in the core ffi types that corresponded to these, so currently I am using
int64_t as that was what I found was correct for the system I'm currently
working on (OS X), which might not work when I try to port this to other
OS's (Linux).

What is the best practice for using such types, is there any core c-type I
should use or should I just roll my own?

-- 
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] source locations of syntax objects in a required file

2015-03-08 Thread Eric Dobson
For an example match does this:
https://github.com/plt/racket/blob/master/racket/collects/racket/match/gen-match.rkt#L36

Added in this commit
https://github.com/plt/racket/commit/fc8ed9772a701062dff2b928fb99d90e01b7f177


On Sun, Mar 8, 2015 at 2:01 PM, Robby Findler 
wrote:

> Syntax object constants, when compiled, have their source location
> information discarded. So you're running in drracket, I guess, with
> automatic compilation on. The usual way around this is to write a
> macro that explicitly tracks the source locations that you care about
> and use it (instead of using quote-syntax).
>
> Robby
>
>
> On Sun, Mar 8, 2015 at 1:19 PM, Alexander D. Knauth
>  wrote:
> > If I have these files:
> > test.rkt:
> > #lang racket
> > (define stx #'here)
> > (syntax-source stx)
> > (syntax-line stx)
> > (syntax-column stx)
> > (syntax-position stx)
> > (syntax-span stx)
> > require-test.rkt:
> > #lang racket
> > (require "test.rkt")
> >
> > Then when I run test.rkt, it prints:
> > #
> > 2
> > 14
> > 28
> > 4
> > As expected.  But when I run require-test.rkt, it prints:
> > #f
> > #f
> > #f
> > #f
> > 0
> >
> > What happened to the source locations?  Why is it doing this?
> >
> > 
> >   Racket Users list:
> >   http://lists.racket-lang.org/users
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] FFI and C header defines

2015-02-06 Thread Eric Dobson
I wrote something similar for my LLVM bindings, after running into many
issues with Dave's code. It follows the c spec not c++, and doesn't support
gcc extensions which many libaries use.

It is no where near production quality, but may give you a starting point:
https://github.com/shekari/racket-llvm/blob/master/private/llvm-headers.rkt

On Fri, Feb 6, 2015 at 2:26 PM, Bartosz Przygoda 
wrote:

> Hello again,
>
> Looks like this
> 
>  (
> http://docs.racket-lang.org/inside/Writing_Racket_Extensions.html?q=#%28part._.Declaring_a_.Module_in_an_.Extension%29)
> solves it the way I wanted (and it's quite easy to build, thanks to
> documentation). So at worst, I could use an extension (would be very
> similar to the way python does it). I said 'at worst', cause it's gonna
> involve lots of tedious typing on the C side.
>
> I'm gonna check out those c parsing libs for that reason.
>
> Thanks!
>
> On Fri, Feb 6, 2015 at 2:01 AM, Anthony Carrico 
> wrote:
>
>> On 02/05/2015 05:01 PM, Jens Axel Søgaard wrote:
>> > Maybe you can find something useful here:
>> > http://pkg-build.racket-lang.org/doc/c-utils/index.html
>>
>> Hey, this is pretty cool. I wonder if Dave Herman has thought of bolting
>> this to Clang.
>>
>> --
>> Anthony Carrico
>>
>>
>>
>> 
>>   Racket Users list:
>>   http://lists.racket-lang.org/users
>>
>>
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
>

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Starting a place with breaks disabled

2014-11-07 Thread Eric Dobson
I'm trying to figure out how to reliably kill a place silently. The current
code I have starts up a place and nearly immediately sets a break handler
that quietly exits, the issue is that there is a fairly long gap where the
place is started and still doing import resolution. If the place revieves a
break during this it logs output which is not what I want. It seems like it
should technically be possible to start a place with breaks disabled, but I
cannot seem to find a way. Is there one? Is there a better way to
accomplish this?

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Deadlock with FIFO files

2014-10-26 Thread Eric Dobson
I attempted reviewing the attached file, and it looks fine and I can
replicate the buggy behavior, but I'm not that familiar with kqueue.

Thanks for the quick turnaround/fix.

On Sun, Oct 26, 2014 at 8:14 AM, Matthew Flatt  wrote:
> It looks like kqueue() is broken for FIFOs on Mac OS X. See the
> enclosed "kqueue_fifo.c". Unless I'm confused (a review of the enclosed
> program would be welcome), adding a kqueue event to watch for FIFO
> input disables an event that watches for FIFO output space, or
> something like that.
>
> I enabled the use of kqueue() on FIFOs only in June. I'll disable it
> again.
>
> At Sat, 25 Oct 2014 21:06:54 -0700, Eric Dobson wrote:
>> I'm attempting to use FIFOs as a simple IPC mechanism and I'm getting
>> into a deadlock. I've simplified my program to the following and am
>> still getting deadlocks. I believe that this shouldn't have an issues,
>> but I might be wrong about that. Am I doing something wrong, or might
>> this be a bug in the IO system? I'm running on OS X 10.9.5 if that is
>> relevant.
>>
>> It usually gets stuck at around 10-11 thousand.
>>
>>
>> #lang racket
>>
>> (define the-fifo-path "the-fifo")
>>
>> (when (file-exists? the-fifo-path)
>>   (delete-file the-fifo-path))
>>
>> (system "mkfifo the-fifo")
>>
>> (thread
>>   (lambda ()
>> (call-with-output-file* the-fifo-path #:exists 'append
>>   (lambda (port)
>> (file-stream-buffer-mode port 'none)
>> (let loop ([i 0])
>>   (printf "Writing ~a~n" i)
>>   (write i port)
>>   (newline port)
>>   (loop (add1 i)))
>>
>> (call-with-input-file* the-fifo-path
>>   (lambda (port)
>> (file-stream-buffer-mode port 'none)
>> (let loop ()
>>   (displayln "Reading")
>>   (define v (read-line port))
>>   (unless (eof-object? v)
>> (printf "Read ~a~n" v)
>> (loop)
>> 
>>   Racket Users list:
>>   http://lists.racket-lang.org/users

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Deadlock with FIFO files

2014-10-25 Thread Eric Dobson
I'm attempting to use FIFOs as a simple IPC mechanism and I'm getting
into a deadlock. I've simplified my program to the following and am
still getting deadlocks. I believe that this shouldn't have an issues,
but I might be wrong about that. Am I doing something wrong, or might
this be a bug in the IO system? I'm running on OS X 10.9.5 if that is
relevant.

It usually gets stuck at around 10-11 thousand.


#lang racket

(define the-fifo-path "the-fifo")

(when (file-exists? the-fifo-path)
  (delete-file the-fifo-path))

(system "mkfifo the-fifo")

(thread
  (lambda ()
(call-with-output-file* the-fifo-path #:exists 'append
  (lambda (port)
(file-stream-buffer-mode port 'none)
(let loop ([i 0])
  (printf "Writing ~a~n" i)
  (write i port)
  (newline port)
  (loop (add1 i)))

(call-with-input-file* the-fifo-path
  (lambda (port)
(file-stream-buffer-mode port 'none)
(let loop ()
  (displayln "Reading")
  (define v (read-line port))
  (unless (eof-object? v)
(printf "Read ~a~n" v)
(loop)

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Help debugging a ffi crash

2014-09-28 Thread Eric Dobson
That is almost surely it, thanks for the second pair of eyes. I had
similar issues with a different type that I thought was a pointer but
was actually a struct, but that one I couldn't even get a single call
to work so it was much more obvious something was up.

On Sun, Sep 28, 2014 at 11:08 AM, Matthew Flatt  wrote:
> Looking at
>
>   http://clang.llvm.org/doxygen/CXString_8h_source.html
>
> it seems that CXString as returned by clang_getCursorSpelling() is not
> a pointer:
>
>  typedef struct {
>const void *data;
>unsigned private_flags;
>  } CXString;
>
> If that's right, I'm a little surprised that `cursor-spelling` works
> --- but when you get representation wrong, strange things can happen,
> including something working when it shouldn't.
>
> Am I looking at the right library/definitions?
>
> At Sun, 28 Sep 2014 10:48:06 -0700, Eric Dobson wrote:
>> I'm trying to debug an FFI crash that I'm seeing, and because it is
>> dealing with C code the error just presents as a segfault. I believe I
>> have tracked down what is causing the problem, but don't understand
>> how it could be doing so.
>>
>> I have two racket functions which take a "cursor" (the foreign
>> libraries object) and return a string representation of it, which I'm
>> trying to use for debugging.
>>
>> (define raw-clang-get-cstring
>>   (get-ffi-obj "clang_getCString" lib-clang
>> (_fun _pointer -> _string)))
>>
>> (define raw-cursor-spelling
>>  (get-ffi-obj "clang_getCursorSpelling" lib-clang
>>(_fun _CXCursor -> _pointer)))
>>
>> (define (cursor-spelling c)
>>   (raw-clang-get-cstring (raw-cursor-spelling c)))
>>
>> (define cursor-spelling2
>>  (get-ffi-obj "clang_getCursorSpelling" lib-clang
>>(_fun _CXCursor -> (make-ctype _pointer values (λ (v)
>> (raw-clang-get-cstring v))
>>
>> If I use cursor-spelling, I have not been able to trigger a crash. But
>> if I use cursor-spelling2 I can reliably trigger a crash.
>>
>> Is there anything obvious on how these functions are different?
>> Because they look to me like they should be doing the same thing. If
>> it would be helpful I can try to get my code in a portable enough
>> shape so that it will work/crash on another machine.
>>
>> 
>>   Racket Users list:
>>   http://lists.racket-lang.org/users


  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Help debugging a ffi crash

2014-09-28 Thread Eric Dobson
I'm trying to debug an FFI crash that I'm seeing, and because it is
dealing with C code the error just presents as a segfault. I believe I
have tracked down what is causing the problem, but don't understand
how it could be doing so.

I have two racket functions which take a "cursor" (the foreign
libraries object) and return a string representation of it, which I'm
trying to use for debugging.

(define raw-clang-get-cstring
  (get-ffi-obj "clang_getCString" lib-clang
(_fun _pointer -> _string)))

(define raw-cursor-spelling
 (get-ffi-obj "clang_getCursorSpelling" lib-clang
   (_fun _CXCursor -> _pointer)))

(define (cursor-spelling c)
  (raw-clang-get-cstring (raw-cursor-spelling c)))

(define cursor-spelling2
 (get-ffi-obj "clang_getCursorSpelling" lib-clang
   (_fun _CXCursor -> (make-ctype _pointer values (λ (v)
(raw-clang-get-cstring v))

If I use cursor-spelling, I have not been able to trigger a crash. But
if I use cursor-spelling2 I can reliably trigger a crash.

Is there anything obvious on how these functions are different?
Because they look to me like they should be doing the same thing. If
it would be helpful I can try to get my code in a portable enough
shape so that it will work/crash on another machine.


  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] on reversing a list by way of sort

2014-09-15 Thread Eric Dobson
On Mon, Sep 15, 2014 at 8:50 PM, Asumu Takikawa  wrote:
> On 2014-09-15 18:57:51 -0700, Matthew Butterick wrote:
>> Mike Bostock's visual demonstration of why naive sorting functions are false
>> friends. As he and Henglein point out, transitivity of the comparator is
>> essential.
>>
>> http://bost.ocks.org/mike/shuffle/compare.html
>
> Thanks, that's really interesting. Also means that our implementation of
> `shuffle` in Racket stdlib is not great:
>
>   (define (shuffle l)
> (sort l < #:key (λ(_) (random)) #:cache-keys? #t))
>
> (more prose on the algorithm here: 
> http://bost.ocks.org/mike/algorithms/#shuffling)

This is different than linked algorithm. This one assigns a random
number from [0,1) to each element where the linked algorithm assigns a
random pairwise ordering to each comparison.

I don't think there is anything wrong from a correctness standpoint on
the stdlib's algorithm.

>
> Cheers,
> Asumu
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users


  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] the type of range

2014-07-15 Thread Eric Dobson
The issue with random testing is that it can only catch for bugs in
programs that can be generated. We currently have a small set of
primitives as allowed functions, and they are only numeric ones. Thus
we cannot currently generate programs with lists which would be
required for adding range. This is likely easy to do but just hasn't
been done yet.

On Tue, Jul 15, 2014 at 6:45 PM, Robby Findler
 wrote:
> Shouldn't random testing have found at least some of these?
>
> Robby
>
> On Tue, Jul 15, 2014 at 8:37 PM, Alexander D. Knauth
>  wrote:
>> The type of range is this:
>>> (case->
>>>  (-> Nonpositive-Integer Null)
>> Why isn’t this (-> Nonpositive-Real Null) ?
>>>  (-> One (List One))
>> This is wrong, because (range 1) produces ‘(0), not ‘(1).
>> It should be (-> One (List Zero)).
>>>  (-> Byte (Listof Byte))
>>>  (-> Index (Listof Index))
>>>  (-> Fixnum (Listof Fixnum))
>> Why isn’t this (-> Fixnum (Listof Nonnegative-Fixnum)) ?
>> And why isn’t there a (-> Integer (Listof Nonnegative-Integer)) case in here?
>>>  (-> Real (Listof Integer))
>> Why isn’t this (-> Real (Listof Nonnegative-Integer)) ?
>>
>>>  (->* (Positive-Integer Byte) (Integer) (Listof Positive-Byte))
>>>  (->* (Nonnegative-Integer Byte) (Integer) (Listof Byte))
>>>  (->* (Positive-Integer Index) (Integer) (Listof Positive-Index))
>>>  (->* (Nonnegative-Integer Index) (Integer) (Listof Index))
>>>  (->* (Nonnegative-Integer Nonnegative-Fixnum)
>>>   (Integer)
>>>   (Listof Nonnegative-Fixnum))
>>>  (->* (Positive-Integer Fixnum) (Nonnegative-Integer) (Listof 
>>> Positive-Fixnum))
>>>  (->* (Nonnegative-Integer Fixnum)
>>>   (Nonnegative-Integer)
>>>   (Listof Nonnegative-Fixnum))
>>>  (->* (Nonnegative-Integer Nonnegative-Integer)
>>>   (Integer)
>>>   (Listof Nonnegative-Integer))
>>>  (->* (Positive-Integer Integer)
>>>   (Nonnegative-Integer)
>>>   (Listof Positive-Integer))
>>>  (->* (Nonnegative-Integer Integer)
>>>   (Nonnegative-Integer)
>>>   (Listof Nonnegative-Integer))
>>>  (->* (Integer Real) (Integer) (Listof Integer))
>>>  (->* (Exact-Rational Real) (Exact-Rational) (Listof Exact-Rational))
>>>  (->* (Flonum Real) (Flonum) (Listof Flonum))
>>>  (->* (Single-Flonum Real) (Single-Flonum) (Listof Single-Flonum))
>>>  (->* (Inexact-Real Real) (Inexact-Real) (Listof Inexact-Real))
>>>  (->* (Real Real) (Real) (Listof Real)))
>>
>>
>> 
>>   Racket Users list:
>>   http://lists.racket-lang.org/users
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users


  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Syntax objects and pairs

2014-07-06 Thread Eric Dobson
What should the following program return?

#lang racket
(syntax? (cdr (syntax-e #'(2 . ()

If I just run the program with 'racket' I get #t. If I compile it with
'raco make' and run it again I get #f.

I'm trying to figure out how to type this in TR, and thought I fully
understood how pairs and syntax objects interacted but this change (by
the serialization?) violates my understanding.

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] FW: q about code for partitions

2014-06-29 Thread Eric Dobson
Vincent: exact-zero? is defined through 'make-predicate' which uses
contract machinery to generate the function. I filed a couple of bugs
tracking some of the slowness issues. There is no `contract` form
though so I doubt that the coach will find it.

http://bugs.racket-lang.org/query/?cmd=view%20audit-trail&database=default&pr=14610
http://bugs.racket-lang.org/query/?cmd=view%20audit-trail&database=default&pr=14611

On Sun, Jun 29, 2014 at 11:53 AM, Jos Koot  wrote:
> Great work, Jens. I am glad my approach as been adopted (and much improved
> without deviating from the original idea of simpler recurrence). When can we
> expect it in the next nightly build?
>
> Thanks, Jos
>
>> -Original Message-
>> From: jensaxelsoega...@gmail.com
>> [mailto:jensaxelsoega...@gmail.com] On Behalf Of Jens Axel Søgaard
>> Sent: domingo, 29 de junio de 2014 12:48
>> To: Matthew Flatt
>> Cc: Jos Koot; Racket Users List
>> Subject: Re: [racket] FW: q about code for partitions
>>
>> I have made a new vector version using zero? instead of exact-zero?.
>>
>> To give users a chance to remove the cache after doing
>> partitions calculations,
>> I have added set-partitions-cache.
>>
>>Code:
>>
>> https://github.com/soegaard/racket/blob/patch-14/pkgs/math-pkg
> s/math-lib/math/private/number-theory/partitions.rkt
>>
>>Discussion:
>> https://github.com/plt/racket/pull/697
>>
>> /Jens Axel
>>
>>
>> 2014-06-29 12:44 GMT+02:00 Jens Axel Søgaard :
>> > 2014-06-29 8:47 GMT+02:00 Matthew Flatt :
>> >> It looks like "partitions2.rkt" ends up calling a contract-wrapped
>> >> variant of `exact-zero?`.
>> >
>> > That explains why Eric saw an improvement, when the used #f
>> instead of
>> > 0 as the not-cached-yet value.
>> >
>> > /Jens Axel
>>
>>
>>
>> --
>> --
>> Jens Axel Søgaard
>
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users


  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] FW: q about code for partitions

2014-06-28 Thread Eric Dobson
I took a look and for a while nothing I did sped it up, (likely
already racket was doing the optimizations I was doing by hand). But I
got a factor of 4 speed up on the vector code over the hash code by
not checking for exact 0, but instead using #f as the sentinal value.
Growing the vector by a a factor of 2 had some improvement as well but
didn't get the vector code faster than the hash code.

https://gist.github.com/shekari/3bccee6e0d5948181f1a

On Sat, Jun 28, 2014 at 8:43 AM, Jos Koot  wrote:
> When calling partitions once only the vector grows once only. Another case is 
> when partitions is called many times with increasing argument. In that case 
> the vector has to be copied every time. Therefore I would prefer the mutable 
> hash.
>
> I have thought of combining the two seperate loops for negative and positive 
> k into one loop, but as it is not certain that the loops have the same 
> length, this complicates the loop with an extra if-clause cq cond-clause.
>
> Nevertheless, I think the main gain is obtained by using a recurrent formula 
> for the computation of
> (- n (* k (add1 (* 3 k and
> (- n (* k (sub1 (* 3 k and
> more importantly avoiding to compute
> (/ (+ 1.0 (flsqrt (+ 1.0 (* 24.0 n 6.0)
> for every instance the partitions count has not yet been memorized.
> It where the inexact operations that made me think.
>
> Another thing that could be tried is to sepatare both loops for even and odd 
> k such as to avoid the test on the partity of k. I did not (yet) try that. 
> This would lead to a total of 4 loops.
>
> Anyway, code should be readable and fast (in that order). Therefore 
> complicating the code much for a slight gain of speed may be the wrong thing 
> to do. MHO
>
> Jos
>
>
>> -Original Message-
>> From: jensaxelsoega...@gmail.com
>> [mailto:jensaxelsoega...@gmail.com] On Behalf Of Jens Axel Søgaard
>> Sent: sábado, 28 de junio de 2014 16:51
>> To: Neil Toronto
>> Cc: Jos Koot; Racket Users List
>> Subject: Re: [racket] FW: q about code for partitions
>>
>> The vector grows only once, so that's not it.
>>
>> Below is a version where vector-ref! was removed.
>> This brings the timings closer to each other.
>> It seems that the hash version still is slightly faster though.
>>
>> Is there anything else that can be improved?
>>
>> #lang typed/racket/base
>> (provide partitions reset-partitions-cache)
>> (require math/private/number-theory/types)
>>
>> ;;; Partitions are computed using Euler's algorithm:
>>
>> ;kk(3k+1)  k(3k-1)
>> ; p(n) = sum (-1)   [ p( n - - ) + p( n - - ) ]
>> ;k>=122
>>
>> ; http://en.wikipedia.org/wiki/Partition_(number_theory)
>>
>> (define cache-size 1)
>> (: cache (Vectorof Integer))
>> (define cache (make-vector cache-size 0))
>> (vector-set! cache 0 1)
>>
>> (: reset-partitions-cache : -> Void)
>> (define (reset-partitions-cache)
>>   (set! cache-size 1)
>>   (make-vector cache-size 0)
>>   (set! cache (make-vector cache-size 0))
>>   (vector-set! cache 0 1))
>>
>> (: grow-cache : Natural -> Void)
>> (define (grow-cache n)
>>   (cond [(> cache-size n) (void)]
>> [else (define n+1 (+ n 1))
>>   (define new-cache (make-vector n+1 0))
>>   (vector-copy! new-cache 0 cache)
>>   (set! cache-size n+1)
>>   (set! cache new-cache)]))
>>
>> (: loop1 : Integer Integer Integer -> Integer)
>> (define (loop1 k m s)
>>   (cond [(< m 0) s]
>> [else (loop1 (+ k 1)
>>  (- m (+ (* 3 k) 1))
>>  (if (odd? k) (+ s (p m)) (- s (p m]))
>>
>> (: loop2 : Integer Integer Integer -> Integer)
>> (define (loop2 k m s)
>>   (cond [(< m 0) s]
>> [else   (loop2 (- k 1)
>>(+ m (* 3 k) -2)
>>(if (odd? k) (+ s (p m)) (- s (p m]))
>>
>> (: p : Integer -> Integer)
>> (define (p n)
>>   (define cached (vector-ref cache n))
>>   (cond [(exact-zero? cached)
>>  (define pn (+ (loop1  1 (- n 1) 0)
>>(loop2 -1 (- n 2) 0)))
>>  (vector-set! cache n pn)
>>  pn]
>> [else cached]))
>>
>> (: partitions : Integer -> Integer)
>> (define (partitions n)
>>   (cond [(< n 0) 0]
>> [else (grow-cache n)
>>   (p n)]))
>>
>>
>>
>>
>>
>> 2014-06-28 16:26 GMT+02:00 Neil Toronto :
>> > Possible culprit: growing the vector by 1 instead of by powers of 2.
>> >
>> > Also, vector-ref! may not be especially fast. In
>> particular, it always applies a higher-order function, but
>> hash-ref! only does that when an entry is missing.
>> >
>> > Neil
>> >
>> > Sent from my iPhone
>> >
>> >> On Jun 28, 2014, at 7:04 AM, "Jos Koot"  wrote:
>> >>
>> >> Thanks for the conversion. I hope the code can be useful.
>> >>
>> >> Strange timing differences. I would expect a vector to be
>> faster, but I am no expert on the inside of Racket. Looking
>> at profiles with (partit

[racket] Difference in speed accessing fields of a struct with gen:equal+hash

2014-06-23 Thread Eric Dobson
I added gen:equal+hash to a struct type and noticed that accessing
fields of the struct got slower, even though I never used equal? or
hash functions. Is this expected? And if so what is the cause of it?

#lang racket

(struct foo (x) #:transparent)
(struct bar (x) #:transparent
#:methods gen:equal+hash
[(define (equal-proc x y recur)
   (recur (bar-x x) (bar-x y)))
 (define (hash-proc x recur) (recur (bar-x x)))
 (define (hash2-proc x recur) (recur (bar-x x)))])






(define N 1000)
(define f (foo 3))
(define b (bar 3))
(for ((j 5))
  (time
(for ((i (in-range N)))
  (equal? (foo-x f) (foo-x f
  (time
(for ((i (in-range N)))
  (equal? (bar-x b) (bar-x b)

My timings showing a 10% slowdown.

cpu time: 296 real time: 296 gc time: 0

cpu time: 334 real time: 334 gc time: 0

cpu time: 297 real time: 297 gc time: 0

cpu time: 340 real time: 340 gc time: 0

cpu time: 303 real time: 302 gc time: 0

cpu time: 340 real time: 339 gc time: 0

cpu time: 292 real time: 292 gc time: 0

cpu time: 327 real time: 326 gc time: 0

cpu time: 291 real time: 291 gc time: 0

cpu time: 323 real time: 322 gc time: 0

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] 3D Plot example doesn't work in Typed Racket

2014-05-31 Thread Eric Dobson
This should now be fixed at HEAD.

On Fri, May 30, 2014 at 8:25 PM, Greg Hendershott
 wrote:
> Thanks, Neil and Alexander!
>
> (At one point I actually did try `list` instead of `vector`, but that
> uncovered an additional typecheck problem that I conflated with this.
> All set now.)
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] 3D Plot example doesn't work in Typed Racket

2014-05-30 Thread Eric Dobson
I know what the bug is and it should be pretty simple to fix, but
please file a bug for tracking purposes.

On Fri, May 30, 2014 at 1:44 PM, Neil Toronto  wrote:
> This is one case in which Typed Racket could do better inference. I'm not
> sure what the difference is between Listof and Sequenceof that makes it
> treat them differently, but it does:
>
> #lang typed/racket
>
> ;; Typechecks
> (ann (list (vector 1 2 3))
>  (Listof (Vector Any Any (U Real False
>
> ;; Fails
> (ann (list (vector 1 2 3))
>  (Sequenceof (Vector Any Any (U Real False
>
> ;; Typechecks
> (ann (list (ann (vector 1 2 3)
> (Vector Any Any (U Real False
>  (Sequenceof (Vector Any Any (U Real False
>
>
> If there shouldn't be a difference, it's an error that needs a bug report.
>
> In the meantime, use lists. This works fine:
>
>
> #lang typed/racket   ;<- typed
> (require plot/typed) ;<- typed
> (plot-new-window? #t)
> (plot3d (discrete-histogram3d '((a a 1) (a b 2) (b b 3))
>
>   #:label "Missing (b,a)"
>   #:color 4 #:line-color 4))
>
>
> Neil ⊥
>
>
> On 05/30/2014 01:50 PM, Alexander D. Knauth wrote:
>>
>> The problem isn’t with List vs. Sequenceof, the problem is that (Vector
>> Symbol Symbol Integer) isn’t a subtype of (Vector Any Any (U Real False
>> ivl)).
>> #lang typed/racket
>> (require plot/typed)
>> (ann (cast 0 (Vector Symbol Symbol Integer)) (Vector Any Any (U Real
>> False ivl)))
>>
>> Also (Vector Symbol) isn’t a subtype of (Vector Any), either.
>>
>> You can solve this problem by doing this:
>> #lang typed/racket   ;<- typed
>> (require plot/typed) ;<- typed
>> (plot-new-window? #t)
>> (plot3d (discrete-histogram3d (ann '(#(a a 1) #(a b 2) #(b b 3))
>> (List (Vector Any Any (U Real False
>> ivl))
>>   (Vector Any Any (U Real False
>> ivl))
>>   (Vector Any Any (U Real False
>> ivl
>>#:label "Missing (b,a)"
>>#:color 4 #:line-color 4))
>>
>> The type (Vector Symbol Symbol Integer) can’t be a subtype of (Vector
>> Any Any (U Real False ivl)) because Vectors are mutable.
>>  From the perspective of an accessor it should be a subtype, but from
>> the perspective of a mutator it should be the other way around.
>>
>> At one point I submitted a bug report about this, but it turns out I was
>> wrong.
>>
>> http://bugs.racket-lang.org/query/?cmd=view%20audit-trail&database=default&pr=14506&return_url=http%3A%2F%2Fbugs.racket-lang.org%2Fquery%2F%3Fdatabase%3Ddefault%3Bdebug%3D%3BState%3Dany%3BSynopsis%3DVector%3Bmultitext%3D%3Bcolumns%3DState%3Bcolumns%3DSynopsis%3Bcolumns%3DCategory%3Bcolumns%3DLast-Modified%3Bcolumns%3DRelease%3Bcmd%3Dsubmit%2520query%3Bsortby%3DNumber
>>
>> On May 30, 2014, at 10:17 AM, Greg Hendershott
>> mailto:greghendersh...@gmail.com>> wrote:
>>
>>> p.s. I happened to paste the error message from 5.3.5. The TR error
>>> message format in recent versions of Racket is much nicer; kudos!
>>>
>>> ; /tmp/tr.rkt:4:8: Type Checker: type mismatch
>>> ;   expected: (Sequenceof
>>> ;  (U (Vector Any Any (U Real False ivl))
>>> ; (List Any Any (U Real False ivl
>>> ;   given: (List
>>> ;   (Vector Symbol Symbol Integer)
>>> ;   (Vector Symbol Symbol Integer)
>>> ;   (Vector Symbol Symbol Integer))
>>> ;   in: (discrete-histogram3d (quote (#(a a 1) #(a b 2) #(b b 3)))
>>> #:label "Missing (b,a)" #:color 4 #:line-color 4)
>>>
>>>
>>> On Fri, May 30, 2014 at 10:13 AM, Greg Hendershott
>>> mailto:greghendersh...@gmail.com>> wrote:

 This example from the plot docs works great:

#lang racket
(require plot)
(plot-new-window? #t)
(plot3d (discrete-histogram3d '(#(a a 1) #(a b 2) #(b b 3))
#:label "Missing (b,a)"
#:color 4 #:line-color 4))

 But not with Typed Racket:

#lang typed/racket   ;<- typed
(require plot/typed) ;<- typed
(plot-new-window? #t)
(plot3d (discrete-histogram3d '(#(a a 1) #(a b 2) #(b b 3))
#:label "Missing (b,a)"
#:color 4 #:line-color 4))
; /tmp/tr.rkt:7:8: Type Checker: Expected (Sequenceof (U (Vector
 Any Any (U Real False ivl)) (List Any Any (U Real False ivl, but
 got (List (Vector Symbol Symbol Integer) (Vector Symbol Symbol
 Integer) (Vector Symbol Symbol Integer))
;   in: (discrete-histogram3d (quote (#(a a 1) #(a b 2) #(b b 3)))
 #:label "Missing (b,a)" #:color 4 #:line-color 4)

 It fails to typecheck on 6.0.1.10, but also back on 5.3.5.

 I don't understand the error; a list is a sequence, no?
>>>
>>> 
>>>  Racket Users list:
>>> http://lists.r

Re: [racket] occurrence typing for case?

2014-05-11 Thread Eric Dobson
Are you using head from the last day or so. I recently pushed fixes for
multiple annotations on the same form. If so can you file a minimal test
case as a bug?
On May 11, 2014 6:15 PM, "Alexander D. Knauth"  wrote:

> I tried this:
> (define-syntax-rule
>   (case/type x-expr
> [t body ...] ...)
>   (let ([x x-expr])
> (cond [((ann (make-predicate t) (Any -> Boolean : #:+ (t @ x-expr) #:-
> (! t @ x-expr)))
> x)
>body ...]
>   ...))
>   )
> But it gave me these errors:
> . Type Checker: type mismatch
>   expected: (-> Any Boolean)
>   given: (-> Any Boolean : String) in: (make-predicate t)
> . Type Checker: type mismatch
>   expected: (-> Any Boolean)
>   given: (-> Any Boolean : (Listof Any)) in: (make-predicate t)
> Even though I put the filters in, and if it’s ignoring my filters, then
> shouldn’t (-> Any Boolean : Type) be a subtype of (-> Any Boolean)?
>
> On May 11, 2014, at 8:54 PM, Alexander D. Knauth 
> wrote:
>
>
> On May 11, 2014, at 7:41 PM, Eric Dobson  wrote:
>
> The issue is that there is a temporary variable that has the same
> value as x and TR cannot see that they point to the same location.
> Your implementation of case/type has the same problem. I thought there
> was a bug filed for it but couldn't find it so just filed one.
> http://bugs.racket-lang.org/query/?cmd=view&pr=14502
>
>
> Use this:
> #lang typed/racket
> (require typed/rackunit)
>
> (define-syntax-rule
>  (case/type y x-expr
>[t1 body ...] ...)
>  (let ([y x-expr])
>(cond [((make-predicate t1) y) body ...] ...))
>  )
>
> This worked:
> #lang typed/racket
> (require typed/rackunit)
> (require syntax/parse/define)
>
> (define-simple-macro
>   (case/type x:id
> [t:expr body:expr ...+] ...)
>   (cond [((make-predicate t) x) body ...] ...)
>   )
>
> (define (my-length x)
>   (case/type x
> [String (string-length x)]
> [(Listof Any) ((inst length Any) x)]
> ;[VectorTop (vector-length x)]
> ))
> (check-equal? (my-length "12345") 5)
> (check-equal? (my-length '(1 2 3 4 5)) 5)
> ;(check-equal? (my-length #(1 2 3 4 5)) 5)
>
> Would something like that work for the typed version of case?
>
> 
>  Racket Users list:
>  http://lists.racket-lang.org/users
>
>
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] occurrence typing for case?

2014-05-11 Thread Eric Dobson
I'm not sure what you mean by the typed version of case. The version
of case in TR is the same as the one in untyped racket, so we cannot
change it to only require an identifier. The real solution is to fix
PR 14502, and while it is non-trivial (> 1 hour of work) we mostly
know what needs to be done to fix it.

On Sun, May 11, 2014 at 5:54 PM, Alexander D. Knauth
 wrote:
>
> On May 11, 2014, at 7:41 PM, Eric Dobson  wrote:
>
> The issue is that there is a temporary variable that has the same
> value as x and TR cannot see that they point to the same location.
> Your implementation of case/type has the same problem. I thought there
> was a bug filed for it but couldn't find it so just filed one.
> http://bugs.racket-lang.org/query/?cmd=view&pr=14502
>
>
> Use this:
> #lang typed/racket
> (require typed/rackunit)
>
> (define-syntax-rule
>  (case/type y x-expr
>[t1 body ...] ...)
>  (let ([y x-expr])
>(cond [((make-predicate t1) y) body ...] ...))
>  )
>
> This worked:
> #lang typed/racket
> (require typed/rackunit)
> (require syntax/parse/define)
>
> (define-simple-macro
>   (case/type x:id
> [t:expr body:expr ...+] ...)
>   (cond [((make-predicate t) x) body ...] ...)
>   )
>
> (define (my-length x)
>   (case/type x
> [String (string-length x)]
> [(Listof Any) ((inst length Any) x)]
> ;[VectorTop (vector-length x)]
> ))
> (check-equal? (my-length "12345") 5)
> (check-equal? (my-length '(1 2 3 4 5)) 5)
> ;(check-equal? (my-length #(1 2 3 4 5)) 5)
>
> Would something like that work for the typed version of case?
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] occurrence typing for case?

2014-05-11 Thread Eric Dobson
On Sun, May 11, 2014 at 4:10 PM, Alexander D. Knauth
 wrote:
> It seems like case doesn’t do occurrence typing, when it seems like it would
> be easiest for case, because of singleton types.
> For example:
> #lang typed/racket
>
> (define (f x)
>   (case x
> [(thing) (g x)]
> ))
>
> (: g ('thing -> 'thing))
> (define (g x) x)
>
> Gives me this:
> . Type Checker: type mismatch
>   expected: 'thing
>   given: Any in: x
>
> This isn’t much of a problem (I can easily just do a cast), but It just
> seems like it should be really easy for typed racket to figure out that x
> has the type ‘thing if it gets into that clause.
The issue is that there is a temporary variable that has the same
value as x and TR cannot see that they point to the same location.
Your implementation of case/type has the same problem. I thought there
was a bug filed for it but couldn't find it so just filed one.
http://bugs.racket-lang.org/query/?cmd=view&pr=14502

>
> By the way is there something like case/type that would be something like
> this?:
> (case/type x
>   [Type-1 body …]
>   [Type-2 body …]
>   …
>   )
> And would use (make-predicate Type) to do it so that it would do occurrence
> typing?  Maybe something like this:
> (define-syntax-rule
>   (case/type x-expr
> [t body …] …)
>   (let ([x x-expr])
> (cond [((make-predicate t) x) body …] …))
>   )
>
> I tried it, but it didn’t work like I expected it to do with occurrence
> typing:
> #lang typed/racket
> (require typed/rackunit)
>
> (define-syntax-rule
>   (case/type x-expr
> [t1 body ...] ...)
>   (let ([x x-expr])
> (cond [((make-predicate t1) x) body ...] ...))
>   )
>
> (define (my-length x)
>   (case/type x
> [String (string-length x)]
> [(Listof Any) ((inst length Any) x)]
> [VectorTop (vector-length x)]
> ))
> (check-equal? (my-length "12345") 5)
> (check-equal? (my-length '(1 2 3 4 5)) 5)
> (check-equal? (my-length #(1 2 3 4 5)) 5)
>
> It gave me these errors:
> . Type Checker: type mismatch
>   expected: String
>   given: Any in: x
> . Type Checker: type mismatch
>   expected: (Listof Any)
>   given: Any in: x
> . Type Checker: type mismatch
>   expected: VectorTop
>   given: Any in: x
>
> So I guess it didn’t do occurrence typing for that either, even though it
> uses make-predicate.  Is there anything I can do to help it do occurrence
> typing for this?

Use this:
#lang typed/racket
(require typed/rackunit)

(define-syntax-rule
  (case/type y x-expr
[t1 body ...] ...)
  (let ([y x-expr])
(cond [((make-predicate t1) y) body ...] ...))
  )

(define (my-length x)
  (case/type y x
[String (string-length y)]
[(Listof Any) (length y)]
#;
[VectorTop (vector-length y)]
))
(check-equal? (my-length "12345") 5)
(check-equal? (my-length '(1 2 3 4 5)) 5)
#;
(check-equal? (my-length #(1 2 3 4 5)) 5)

There is currently an issue with generating the predicate for VectorTop.

>
> By the way why is length polymorphic?
Not sure if there is any reason, most list functions are so likely
because the original author was just writing a bunch of them at once.


>
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>


  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] math/matrix

2014-05-11 Thread Eric Dobson
Where is the time spent in the algorithm? I assume that most of it is
in the matrix manipulation work not the orchestration of finding a
pivot and reducing based on that. I.e. `elim-rows!` is the expensive
part. Given that you only specialized the outer part of the loop, I
wouldn't expect huge performance changes.

On Sun, May 11, 2014 at 2:13 PM, Jens Axel Søgaard
 wrote:
> I tried restricting the matrix-solve and matrix-gauss-elim to (Matrix Flonum).
> I can't observe a change in the timings.
>
> #lang typed/racket
> (require math/matrix
>  math/array
>  math/private/matrix/utils
>  math/private/vector/vector-mutate
>  math/private/unsafe
>  (only-in racket/unsafe/ops unsafe-fl/)
>  racket/fixnum
>  racket/list)
>
> (define-type Pivoting (U 'first 'partial))
>
> (: flonum-matrix-gauss-elim
>(case-> ((Matrix Flonum) -> (Values (Matrix Flonum) (Listof Index)))
>((Matrix Flonum) Any -> (Values (Matrix Flonum) (Listof Index)))
>((Matrix Flonum) Any Any -> (Values (Matrix Flonum) (Listof 
> Index)))
>((Matrix Flonum) Any Any Pivoting -> (Values (Matrix
> Flonum) (Listof Index)
> (define (flonum-matrix-gauss-elim M [jordan? #f] [unitize-pivot? #f]
> [pivoting 'partial])
>   (define-values (m n) (matrix-shape M))
>   (define rows (matrix->vector* M))
>   (let loop ([#{i : Nonnegative-Fixnum} 0]
>  [#{j : Nonnegative-Fixnum} 0]
>  [#{without-pivot : (Listof Index)} empty])
> (cond
>   [(j . fx>= . n)
>(values (vector*->matrix rows)
>(reverse without-pivot))]
>   [(i . fx>= . m)
>(values (vector*->matrix rows)
>;; None of the rest of the columns can have pivots
>(let loop ([#{j : Nonnegative-Fixnum} j] [without-pivot
> without-pivot])
>  (cond [(j . fx< . n)  (loop (fx+ j 1) (cons j 
> without-pivot))]
>[else  (reverse without-pivot)])))]
>   [else
>(define-values (p pivot)
>  (case pivoting
>[(partial)  (find-partial-pivot rows m i j)]
>[(first)(find-first-pivot rows m i j)]))
>(cond
>  [(zero? pivot)  (loop i (fx+ j 1) (cons j without-pivot))]
>  [else
>   ;; Swap pivot row with current
>   (vector-swap! rows i p)
>   ;; Possibly unitize the new current row
>   (let ([pivot  (if unitize-pivot?
> (begin (vector-scale! (unsafe-vector-ref rows i)
>   (unsafe-fl/ 1. pivot))
>(unsafe-fl/ pivot pivot))
> pivot)])
> (elim-rows! rows m i j pivot (if jordan? 0 (fx+ i 1)))
> (loop (fx+ i 1) (fx+ j 1) without-pivot))])])))
>
> (: flonum-matrix-solve
>(All (A) (case->
>  ((Matrix Flonum) (Matrix Flonum)-> (Matrix Flonum))
>  ((Matrix Flonum) (Matrix Flonum) (-> A) -> (U A (Matrix
> Flonum))
> (define flonum-matrix-solve
>   (case-lambda
> [(M B)  (flonum-matrix-solve
>  M B (λ () (raise-argument-error 'flonum-matrix-solve
> "matrix-invertible?" 0 M B)))]
> [(M B fail)
>  (define m (square-matrix-size M))
>  (define-values (s t) (matrix-shape B))
>  (cond [(= m s)
> (define-values (IX wps)
>   (parameterize ([array-strictness #f])
> (flonum-matrix-gauss-elim (matrix-augment (list M B)) #t #t)))
> (cond [(and (not (empty? wps)) (= (first wps) m))
>(submatrix IX (::) (:: m #f))]
>   [else  (fail)])]
>[else
> (error 'flonum-matrix-solve
>"matrices must have the same number of rows; given ~e and 
> ~e"
>M B)])]))
>
> (: mx Index)
> (define mx 600)
>
> (: r (Index Index -> Flonum))
> (define (r i j) (random))
>
> (: A : (Matrix Flonum))
> (define A (build-matrix mx mx r))
>
> (: sum : Integer Integer -> Flonum)
> (define (sum i n)
>   (let loop ((j 0) (acc 0.0))
> (if (>= j mx) acc
> (loop (+ j 1) (+ acc (matrix-ref A i j))) )))
>
> (: b : (Matrix Flonum))
> (define b (build-matrix mx 1 sum))
>
> (time
>  (let [(m (flonum-matrix-solve A b))]
>   (matrix-ref m 0 0)))
> (time
> (let [(m (matrix-solve A b))]
>   (matrix-ref m 0 0)))
>
> (time
>  (let [(m (flonum-matrix-solve A b))]
>   (matrix-ref m 0 0)))
> (time
> (let [(m (matrix-solve A b))]
>   (matrix-ref m 0 0)))
>
> (time
>  (let [(m (flonum-matrix-solve A b))]
>   (matrix-ref m 0 0)))
> (time
> (let [(m (matrix-solve A b))]
>   (matrix-ref m 0 0)))
>
> 2014-05-11 21:48 GMT+02:00 Neil Toronto :
>> The garbage collection time is probably from cleaning up boxed flonums, and
>> possibly intermediate vectors. If so, a separate implementation of Gaussian
>> elimination for the FlArray type would cut the GC time to nearly zero.
>>
>> Neil ⊥
>>
>>
>> 

Re: [racket] math/matrix

2014-05-11 Thread Eric Dobson
Where are you seeing the slowness in that code? For me the majority of
the time is spent in matrix-solve, and since that is another module it
doesn't matter what you write your module in.

In untyped racket, (set the first line to #lang
typed/racket/no-check), it is much slower. 36 seconds to create the
matrix instead 70 milliseconds.

Also as a side not you can run 'raco make matrix.rkt', and this will
typecheck and compile the code so that 'racket matrix.rkt' will do
less work at startup.


On Sat, May 10, 2014 at 9:09 PM, Eduardo Costa  wrote:
> The documentation says that one should expect typed/racket to be faster than
> racket. I tested the math/matrix library and it seems to be almost as slow
> in typed/racket as in racket. Here is the program in typed racket:
>
> #lang typed/racket ; file: matrix.rkt
> (require math/matrix)
>
> (: mx Index)
> (define mx 600)
>
> (: r (Index Index -> Flonum))
> (define (r i j) (random))
>
> (: A : (Matrix Flonum))
> (define A (build-matrix mx mx r))
>
> (: sum : Integer Integer -> Flonum)
> (define (sum i n)
>   (let loop ((j 0) (acc 0.0))
> (if (>= j mx) acc
> (loop (+ j 1) (+ acc (matrix-ref A i j))) )))
>
> (: b : (Matrix Flonum))
> (define b (build-matrix mx 1 sum))
>
> (let [(m (matrix-solve A b))]
>   (displayln (matrix-ref m 0 0)))
>
> You can run it thus:
>
> time racket matrix.rkt
>
> What should I do to speedup the above code?
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] filters in typed racket

2014-05-10 Thread Eric Dobson
Well as it turns out it looks like they are broken. In my
experimenting it seems that they only work for functions declared in
the base type environment.

I'm not sure of a solution that will work for you currently.

On Sat, May 10, 2014 at 10:09 AM, Alexander D. Knauth
 wrote:
> I tried this:
> (: string-with-length-1? (Any -> Boolean : #:+ (String @ 0)))
> (define (string-with-length-1? x)
>   (if (string? x)
>   (one? (string-length x))
>   #f))
> (declare-refinement string-with-length-1?)
> (define-type String-With-Length-1 (Refinement string-with-length-1?))
> And it gave me this error:
> . Type Checker: parse error in type;
>  expected a predicate for argument to Refinement
>   given: Nothing in: (Refinement string-with-length-1?)
> Even though I declared that string-with-length-1? has the type (Any ->
> Boolean : #:+ (String @ 0)).
>
> On May 10, 2014, at 12:40 PM, Eric Dobson  wrote:
>
> The point of Opaque types are that they are opaque and you cannot see
> into them. Thus making them subtypes is against what they are supposed
> to do.
>
> You want Refinement types which are similar, but get you subtyping.
> They are unsound, and thus are still in experimental. The unsoundness
> is very hard to fix, and thus they likely are not going to leave
> experimental for a while.
>
> http://www.cs.utah.edu/plt/snapshots/current/doc/ts-reference/Experimental_Features.html
>
>
>
> On Sat, May 10, 2014 at 9:20 AM, Alexander D. Knauth
>  wrote:
>
> Then is it possible to make (Opaque string-with-length-1?) a subtype of
> String, if string-with-length-1? has the type (Any -> Boolean : #:+ (String
> @ 0))?
>
> On May 10, 2014, at 11:45 AM, Asumu Takikawa  wrote:
>
> On 2014-05-10 11:30:58 -0400, Alexander D. Knauth wrote:
>
>  After trying some stuff and guessing stuff from error messages, and just
>  to try it and see what happened, I tried this:
>
>  (: string-with-length-1? (Any -> Boolean : #:+ (String @ x) #:- ((U String
>  Any) @ x)))
>  (define (string-with-length-1? x)
>(if (string? x)
>(one? (string-length x))
>#f))
>
>  And it gave me this weird error message:
>  . Type Checker: type mismatch;
>   mismatch in filter
>expected: ((String @ x) | Top)
>given: ((String @ x) | Top) in: (if (string? x) (one? (string-length x))
>  #f)
>  What?  If it*s expecting ((String @ x) | Top), and I*m giving it ((String
>  @ x) | Top),  then what*s the problem?
>
>
> This works for me if I use the following type declaration instead:
>
> (: string-with-length-1? (Any -> Boolean : #:+ (String @ 0) #:- ((U String
> Any) @ 0)))
>
> Note that I've switched `x` with `0` in the filters.
>
> Basically, the formal parameter `x` is not in scope when you write a
> type declaration like this *outside* of the function. If you have a type
> inside the function, it should be in scope.
>
> The `0` means that you're referring to the 1st parameter, which is `x`.
>
>  And is there any documentation anywhere about this?
>
>
> There is now, but I just added it a few days ago and it's only in the
> pre-release docs (the syntax may change by v6.0.2):
> http://www.cs.utah.edu/plt/snapshots/current/doc/ts-reference/type-ref.html?q=#%28form._%28%28lib._typed-racket%2Fbase-env%2Fbase-types-extra..rkt%29._-~3e%29%29
>
> In the pre-release, you can also write this instead:
>
> (: string-with-length-1? (Any -> Boolean : #:+ String))
>
> which is a shorthand for the type above. It'll also print like that.
>
> Cheers,
> Asumu
>
>
>
> 
>  Racket Users list:
>  http://lists.racket-lang.org/users
>
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] filters in typed racket

2014-05-10 Thread Eric Dobson
The point of Opaque types are that they are opaque and you cannot see
into them. Thus making them subtypes is against what they are supposed
to do.

You want Refinement types which are similar, but get you subtyping.
They are unsound, and thus are still in experimental. The unsoundness
is very hard to fix, and thus they likely are not going to leave
experimental for a while.

http://www.cs.utah.edu/plt/snapshots/current/doc/ts-reference/Experimental_Features.html



On Sat, May 10, 2014 at 9:20 AM, Alexander D. Knauth
 wrote:
> Then is it possible to make (Opaque string-with-length-1?) a subtype of 
> String, if string-with-length-1? has the type (Any -> Boolean : #:+ (String @ 
> 0))?
>
> On May 10, 2014, at 11:45 AM, Asumu Takikawa  wrote:
>
>> On 2014-05-10 11:30:58 -0400, Alexander D. Knauth wrote:
>>>   After trying some stuff and guessing stuff from error messages, and just
>>>   to try it and see what happened, I tried this:
>>>
>>>   (: string-with-length-1? (Any -> Boolean : #:+ (String @ x) #:- ((U String
>>>   Any) @ x)))
>>>   (define (string-with-length-1? x)
>>> (if (string? x)
>>> (one? (string-length x))
>>> #f))
>>>
>>>   And it gave me this weird error message:
>>>   . Type Checker: type mismatch;
>>>mismatch in filter
>>> expected: ((String @ x) | Top)
>>> given: ((String @ x) | Top) in: (if (string? x) (one? (string-length x))
>>>   #f)
>>>   What?  If it*s expecting ((String @ x) | Top), and I*m giving it ((String
>>>   @ x) | Top),  then what*s the problem?
>>
>> This works for me if I use the following type declaration instead:
>>
>>  (: string-with-length-1? (Any -> Boolean : #:+ (String @ 0) #:- ((U String 
>> Any) @ 0)))
>>
>> Note that I've switched `x` with `0` in the filters.
>>
>> Basically, the formal parameter `x` is not in scope when you write a
>> type declaration like this *outside* of the function. If you have a type
>> inside the function, it should be in scope.
>>
>> The `0` means that you're referring to the 1st parameter, which is `x`.
>>
>>>   And is there any documentation anywhere about this?
>>
>> There is now, but I just added it a few days ago and it's only in the
>> pre-release docs (the syntax may change by v6.0.2):
>>  
>> http://www.cs.utah.edu/plt/snapshots/current/doc/ts-reference/type-ref.html?q=#%28form._%28%28lib._typed-racket%2Fbase-env%2Fbase-types-extra..rkt%29._-~3e%29%29
>>
>> In the pre-release, you can also write this instead:
>>
>>  (: string-with-length-1? (Any -> Boolean : #:+ String))
>>
>> which is a shorthand for the type above. It'll also print like that.
>>
>> Cheers,
>> Asumu
>
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] filters in typed racket

2014-05-10 Thread Eric Dobson
I have been working on fixing the feature that does the checking if a
certain type meets the expectations, and there are tons of bugs in it
for the edge cases. I'm guessing you are just triggering one of those
right now, so I would file a bug.

In terms of docs, they were just added and so are visible on the pre
release page:
http://www.cs.utah.edu/plt/snapshots/current/doc/ts-reference/type-ref.html#%28part._.Other_.Type_.Constructors%29

On Sat, May 10, 2014 at 8:30 AM, Alexander D. Knauth
 wrote:
> Is there a way of specifying filters in typed racket so that if it returns
> true, it knows it’s a certain type, but if it returns false, it doesn’t mean
> that it’s not that type (or the other way around).
>
> For example:
> (: string-with-length-1? (Any -> Boolean : #:if-true String #:if-false (U
> String Any)))
> (define (string-with-length-1? x)
>   (if (string? x)
>   (one? (string-length x))
>   #f))
>
> I know there’s something like this somewhere.
>
> When I tried it, it gave me an error message (I expected that), but that
> error message said something about #:+ and #:-
>
> After trying some stuff and guessing stuff from error messages, and just to
> try it and see what happened, I tried this:
> (: string-with-length-1? (Any -> Boolean : #:+ (String @ x) #:- ((U String
> Any) @ x)))
> (define (string-with-length-1? x)
>   (if (string? x)
>   (one? (string-length x))
>   #f))
> And it gave me this weird error message:
> . Type Checker: type mismatch;
>  mismatch in filter
>   expected: ((String @ x) | Top)
>   given: ((String @ x) | Top) in: (if (string? x) (one? (string-length x))
> #f)
>
> What?  If it’s expecting ((String @ x) | Top), and I’m giving it ((String @
> x) | Top),  then what’s the problem?
>
> And is there any documentation anywhere about this?
>
>
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>


  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] typed racket error

2014-04-20 Thread Eric Dobson
On Sun, Apr 20, 2014 at 10:39 AM, Alexander D. Knauth
 wrote:
> I’m trying to make a multiplication function that can take either all
> numbers or all numbers and one vector, and have it not matter what order
> they come in.  To do this, I’m using match with list-no-order:
> #lang typed/racket
>
> (require plot/typed/utils)
>
> (define listof-number?
>   (make-predicate (Listof Number)))
>
> (: my* (case->
> (-> One)
> (Number Number * -> Number)
> ((Vectorof Real) Real * -> (Vectorof Real))
> (Real (Vectorof Real) Real * -> (Vectorof Real))
> (Real Real (Vectorof Real) Real * -> (Vectorof Real))
> (Real Real Real (Vectorof Real) Real * -> (Vectorof Real))
> (Real Real Real Real (Vectorof Real) Real * -> (Vectorof Real
> ; if I could, I would do this:
> ; (Real * (Vectorof Real) Real * -> (Vectorof Real))
>
> (define my*
>   (case-lambda
> [() 1]
> [args (match args
> [(list-no-order (? vector? v) (? real? ns) ...)
>  (v* v (apply * ns))]
> [(? listof-number? ns)
>  (apply * ns)])]))
>
> It’s giving me these errors:
> . Type Checker: Expected One, but got Number in: (apply * ns) ; why is it
> expecting One there?
This looks like a bug.
> . Type Checker: Expected (Vectorof Real), but got Any in: v ; the type
> shouldn’t be Any, it should at least
> ; be VectorTop,
> and with the type annotation
> ; for my* should
> tell it that all of the
> ; vectors are
> (Vectorof Real), right?
That is assuming a simple expansion in match (that TR can follow)
which isn't a valid assumption.
> . Type Checker: Bad arguments to function in apply: ; shouldn’t it be able
> to tell that ns is a
> Domains: Number *   ; (Listof Number),
> especially since
> Arguments: (Listof Any) *   ; listof-number? was
> defined with
>  in: (apply * ns)   ; make-predicate?
> . Type Checker: Expected One, but got (Vectorof Real) in: (v* v (apply *
> ns)) ; why is it expecting One?
Same bug as above.
> . Type Checker: Expected Number, but got (Vectorof Real) in: (v* v (apply *
> ns)) ;why is it expecting Number?
Because it is typechecking the case where there are no vectors and
doesn't know that this branch is dead code.
> . Type Checker: Expected (Vectorof Real), but got Number in: (apply *
> ns);whyis it expecting (Vectorof Real)?
Reverse of the above.
>
>
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>


  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] another weird typed racket error

2014-04-20 Thread Eric Dobson
The following annotation makes it work for me.

#lang typed/racket

(require plot/typed plot/typed/utils)

(: vsum ((Listof (Vectorof Real)) -> (Vectorof Real)))
(define (vsum vs)
(apply (ann vector-map (-> (-> Real * Real) (Vectorof Real) *
(Vectorof Real))) + vs))

I'm not sure why apply is giving this error message.

On Sat, Apr 19, 2014 at 8:34 PM, Alexander D. Knauth
 wrote:
>
>
> Here’s the program:
> #lang typed/racket
>
> (require plot/typed plot/typed/utils)
>
> (: vsum ((Listof (Vectorof Real)) -> (Vectorof Real)))
> (define (vsum vs)
>   (apply vector-map + vs))
>
> It’s giving me this error:
> . Type Checker: Bad arguments to polymorphic function in apply:
> Domain: (a b ... b -> c) (Vectorof a) (Vectorof b) ... b
> Arguments: (case-> (-> Zero) (Zero Zero -> Zero) (One Zero -> One) (Zero One
> -> One) (Positive-Byte Zero -> Positive-Byte) (Zero Positive-Byte ->
> Positive-Byte) (Byte Zero -> Byte) (Zero Byte -> Byte) (Positive-Index Zero
> -> Positive-Index) (Zero Positive-Index -> Positive-Index) (Index Zero ->
> Index) (Zero Index -> Index) (Positive-Fixnum Zero -> Positive-Fixnum) (Zero
> Positive-Fixnum -> Positive-Fixnum) (Nonnegative-Fixnum Zero ->
> Nonnegative-Fixnum) (Zero Nonnegative-Fixnum -> Nonnegative-Fixnum)
> (Negative-Fixnum Zero -> Negative-Fixnum) (Zero Negative-Fixnum ->
> Negative-Fixnum) (Nonpositive-Fixnum Zero -> Nonpositive-Fixnum) (Zero
> Nonpositive-Fixnum -> Nonpositive-Fixnum) (Fixnum Zero -> Fixnum) (Zero
> Fixnum -> Fixnum) (Positive-Byte Positive-Byte -> Positive-Index) (Byte Byte
> -> Index) (Positive-Byte Positive-Byte Positive-Byte -> Positive-Index)
> (Byte Byte Byte -> Index) (Positive-Index Index -> Positive-Fixnum) (Index
> Positive-Index -> Positive-Fixnum) (Positive-Index Index Index ->
> Positive-Fixnum) (Index Positive-Index Index -> Positive-Fixnum) (Index
> Index Positive-Index -> Positive-Fixnum) (Index Index -> Nonnegative-Fixnum)
> (Index Index Inde in: (apply vector-map + vs)
>
>
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>


  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] macro for define-values?

2014-02-24 Thread Eric Dobson
Does this do what you want? (Note its untested.)

(define-syntax-rule (ref name ...)
  (begin
(define name (void)) ...))

Using define-values is tricky as you saw, because you need to refer to
name in the body to get the right duplication but then not use it.

On Mon, Feb 24, 2014 at 10:23 PM, Kevin Forchione  wrote:
> I’m trying to code a macro that will define a variable number of identifiers. 
> What I want is something like:
>
> (ref foo bar baz ...)
>
> and assign them an initial value of #. The kludge I’ve come up with 
> works, but there must be a better way to do this:
>
> (define-syntax-rule (ref name ...)
>   (define-values (name …) (values ((λ (name) (void)) #f) ...)))
>
>>(ref foo bar baz)
>>foo
> #f
>
> Thanks!
>
> -Kevin
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users


  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] typed/racket and contract boundaries

2014-01-27 Thread Eric Dobson
This is likely a bug, but I cannot reproduce using the following
program. Do you have a small test case?

#lang racket/load

(module typed typed/racket
  (provide foo)
  (: foo (Number -> Number))
  (define (foo x) (add1 x)))

(module untyped racket
  (require 'typed)
  (foo 4))

(require 'untyped)

On Mon, Jan 27, 2014 at 8:25 AM, Spencer Florence  wrote:
> I have a typed module being required by a normal #lang racket module.
>
> When I call a function from the typed module I get this error:
>
> default-blame-format: contract violation
>   expected: a blame object with a non-#f positive field
>   given: #
>
>
> Can someone give me some insight into what is going on?
>
> --spencer
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Getting an expression to type

2014-01-26 Thread Eric Dobson
In that case you can use cast until we fix the bug.

(cast p (All (A B) ((A -> B) -> (A -> B

On Sun, Jan 26, 2014 at 11:34 AM, Spencer Florence  wrote:
> Unfortunately that was a simplified example. The in the program I'm writing,
> the type isn't Number but (All (A B) ((A -> B) -> (A -> B))), which I can't
> check for with a predicate as far as I know.
>
>
> On Sun, Jan 26, 2014 at 2:31 PM, Eric Dobson 
> wrote:
>>
>> That is definitely a bug, not sure exactly what is going wrong though.
>> Can you file a bug for this?
>>
>> In terms of getting your program to run, if you replace '(not
>> (parameter? p))' with 'number?' it should work.
>>
>> On Sun, Jan 26, 2014 at 10:08 AM, Spencer Florence 
>> wrote:
>> > I'm having difficulty getting the following code to type:
>> >
>> > #lang typed/racket
>> > (: test : ((U Number (Parameterof Number)) -> Number))
>> > (define (test p)
>> >   (if (not (parameter? p))
>> >   p
>> >   (p)))
>> >
>> > But I get the error:
>> >> Type Checker: Expected Number, but got (U Complex (Parameterof Number))
>> >> in: p
>> > On the then branch of the if. Is there a way to convince this to type?
>> >
>> > --Spencer
>> >
>> > 
>> >   Racket Users list:
>> >   http://lists.racket-lang.org/users
>> >
>
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Getting an expression to type

2014-01-26 Thread Eric Dobson
That is definitely a bug, not sure exactly what is going wrong though.
Can you file a bug for this?

In terms of getting your program to run, if you replace '(not
(parameter? p))' with 'number?' it should work.

On Sun, Jan 26, 2014 at 10:08 AM, Spencer Florence  wrote:
> I'm having difficulty getting the following code to type:
>
> #lang typed/racket
> (: test : ((U Number (Parameterof Number)) -> Number))
> (define (test p)
>   (if (not (parameter? p))
>   p
>   (p)))
>
> But I get the error:
>> Type Checker: Expected Number, but got (U Complex (Parameterof Number))
>> in: p
> On the then branch of the if. Is there a way to convince this to type?
>
> --Spencer
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Generating comparable symbols

2014-01-14 Thread Eric Dobson
I'm not sure what you mean by random, but does gensym do what you want?

http://docs.racket-lang.org/reference/symbols.html?q=gensym#%28def._%28%28quote._~23~25kernel%29._gensym%29%29

On Tue, Jan 14, 2014 at 11:18 PM, Marco Morazan  wrote:
> Is there a built-in function to generate (random) symbols that one can
> compare using eq?  ?
>
> Thanks,
>
> Marco
>
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Detecting that a place channel is unreachable

2013-12-07 Thread Eric Dobson
Ok, I'll file a low priority feature request then and just make sure I
manually turn it off.

On Sat, Dec 7, 2013 at 9:45 AM, Matthew Flatt  wrote:
> You're right: the implementation has that kind of information (for
> non-distributed channels), but it's significant work to add a trigger.
>
> At Fri, 6 Dec 2013 23:53:04 -0800, Eric Dobson wrote:
>> Is there a way to determine if a place channel is unreachable? My
>> experiments with will executors seem to show that they trigger even if
>> the place channel is reachable from another place. Is there a way to
>> determine that all other places don't have access to the value, I
>> assume that it has to be possible because otherwise memory would leak,
>> but maybe getting triggers based of of it would be hard.
>>
>> My goal is to have two places sharing a channel, and one stop sending
>> messages across if the other one is provable not listening. I can make
>> it so that it can turn it off manual, but I'm looking to see if I can
>> get it to work if the listener just lets the channel get gc'd.
>>
>>
>> Testing program:
>> #lang racket/base
>>
>> (require racket/place)
>>
>> (define (holder)
>>   (place chan
>> (let loop ([v #f])
>>   (define v2 (sync chan))
>>   (place-channel-put chan v)
>>   (collect-garbage)
>>   (loop v2
>>
>>
>> (module+ main
>>   (define other (holder))
>>   (define exec (make-will-executor))
>>   (define exec-thread
>> (thread
>>   (lambda ()
>> (let loop ()
>>   (will-execute exec)
>>   (loop)
>>   (define in
>> (let-values ([(in out) (place-channel)])
>>   (place-channel-put other out)
>>   (sync other)
>>   (will-register exec out
>> (lambda (v) (printf "~a was deleted~n" v)))
>>   in))
>>   (collect-garbage)
>>   ;; Let will execute
>>   (sleep 1))
>> 
>>   Racket Users list:
>>   http://lists.racket-lang.org/users

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Detecting that a place channel is unreachable

2013-12-06 Thread Eric Dobson
Is there a way to determine if a place channel is unreachable? My
experiments with will executors seem to show that they trigger even if
the place channel is reachable from another place. Is there a way to
determine that all other places don't have access to the value, I
assume that it has to be possible because otherwise memory would leak,
but maybe getting triggers based of of it would be hard.

My goal is to have two places sharing a channel, and one stop sending
messages across if the other one is provable not listening. I can make
it so that it can turn it off manual, but I'm looking to see if I can
get it to work if the listener just lets the channel get gc'd.


Testing program:
#lang racket/base

(require racket/place)

(define (holder)
  (place chan
(let loop ([v #f])
  (define v2 (sync chan))
  (place-channel-put chan v)
  (collect-garbage)
  (loop v2


(module+ main
  (define other (holder))
  (define exec (make-will-executor))
  (define exec-thread
(thread
  (lambda ()
(let loop ()
  (will-execute exec)
  (loop)
  (define in
(let-values ([(in out) (place-channel)])
  (place-channel-put other out)
  (sync other)
  (will-register exec out
(lambda (v) (printf "~a was deleted~n" v)))
  in))
  (collect-garbage)
  ;; Let will execute
  (sleep 1))

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Test for async-channel being full

2013-12-03 Thread Eric Dobson
I just ended up using a pipe and having my other thread read from that
in addition to other inputs, as custom output ports are too much of a
pain to actually use.

I lost ordering of events, (i.e. cannot tell whether output to the
pipe came before or after a message on another channel), but was able
to mostly hack around that.


On Tue, Dec 3, 2013 at 5:44 AM, Robby Findler
 wrote:
> Oh, right: so you probably don't want 'get' 'put' and 'is-full?' but you
> want 'get' and 'try-put' that fails only when the channel is full, returning
> a boolean to indicate success.
>
> Robby
>
>
> On Mon, Dec 2, 2013 at 11:24 PM, David T. Pierson  wrote:
>>
>> On Fri, Nov 29, 2013 at 12:23:42PM -0800, Eric Dobson wrote:
>> > I'm trying to make a custom output port that is backed by a
>> > async-channel. One of the operations that ports need to provide is an
>> > event that is ready when progress on the event can be made. But since
>> > I cannot tell if the async-channel is full and put would not block
>> > without actually sending a value, I cannot provide such an event. Is
>> > it possible to provide such an API or does the implementation of
>> > async-channels prevent this?
>>
>> I don't know much about creating custom ports, so I looked up progress
>> events in order to understand your question better.  But now I'm
>> confused, because progress events seem to be specific to input ports,
>> whereas it sounds like you are creating an output port.  What am I
>> missing?
>>
>> Regarding your actual question: I think the reason a non-side-effecting
>> async-channel-put-evt is not provided is that using such an event could
>> lead to race conditions.  A previous thread [1] on this list touched on
>> this with regard to places and channels as events; I'm extrapolating to
>> extend that answer to async-channel-put-evt.
>>
>> David
>>
>> [1] http://www.mail-archive.com/users@racket-lang.org/msg15630.html
>> 
>>   Racket Users list:
>>   http://lists.racket-lang.org/users
>
>

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Timing of parallel compile

2013-12-03 Thread Eric Dobson
I'm trying to understand why my compiles are as slow as they are and
see if there are any easy gains to make them faster.

If I just want a serial compile then I can use 'managed-compile-zo'
and look at the compile/cm logger to get information.

But if I want to do a parallel compile, then the manager-trace-handler
is overridden and doesn't log the current-inexact-milliseconds
(instead it just prints things when very-verbose is set).
https://github.com/plt/racket/blob/master/racket/collects/setup/parallel-build.rkt#L280

Is there a way to get this information from a parallel compile?

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Test for async-channel being full

2013-11-29 Thread Eric Dobson
I'm trying to make a custom output port that is backed by a
async-channel. One of the operations that ports need to provide is an
event that is ready when progress on the event can be made. But since
I cannot tell if the async-channel is full and put would not block
without actually sending a value, I cannot provide such an event. Is
it possible to provide such an API or does the implementation of
async-channels prevent this?

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Discrepency in Commandline Documentation

2013-11-25 Thread Eric Dobson
The documention for the commandline racket implies that the following
two command lines should have the same behavior.

racket -v -e "(read-eval-print-loop)"
racket -i -q

But on my machine I'm seeing different behavior with regards to
terminal settings. Without interactive mode I don't get the echo of
control characters and I don't get a newline when the program exits.
I'm trying to make a program which when run behaves the same as the
standard interactive racket (or close enough to not bother me). So I
want to understand this distinction so that I can replicate it.

endobson@yggdrasil () % racket -v -e "(read-eval-print-loop)"
Welcome to Racket v6.0.0.1.
> %
endobson@yggdrasil () % racket -i -q
Welcome to Racket v6.0.0.1.
> ^D

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Changing the pretty print columns when running raco expand

2013-11-07 Thread Eric Dobson
I know about the macro stepper and it doesn't work for my use case. I
don't have a simple macro, I have the entire TR typechecker/optimizer.
Thus splitting it out into its own module is not feasible. Also
because of how TR works there is one major macro application and the
macro stepper cannot see into that.

The use case I have is, Take TR program that is being misoptimized,
look at the fully expanded syntax and see what went wrong. Thus the
thing I need is a good visualization of the fully expanded syntax. The
macro stepper may give a slightly more annotated version than 'raco
expand' but those annotations are unnecessary for what I need. TR's
optimizer is good at using generate-temporary so that you don't need
the mark numbers to follow bindings. Also using the macro stepper
requires using DrRacket, which has issues when I change its
dependencies (TR) out from underneath it and is less keyboard/screen
realestate friendly then my terminal.



On Thu, Nov 7, 2013 at 9:28 AM, Konrad Hinsen
 wrote:
> Neil Van Dyke writes:
>
>  > "raco expand"?  If you've not yet tried the Macro Stepper feature of
>  > DrRacket, you are in for a treat.
>
> And for Emacs fans, Geiser provides macro-expansion-at-a-keypress.
>
> Konrad.
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Changing the pretty print columns when running raco expand

2013-11-07 Thread Eric Dobson
I use raco expand a bunch when debugging macros to see the final
output and see why it doesn't match up with my expectations. One issue
I have is that the pretty printer decides to only use 80ish columns
when my terminal is 200 wide, this makes the output really ugly and
hard to read. If this was a racket command I could use -e
"(pretty-print-colums 200)", but I don't see a way to do that with
raco. Is there an easy way to set up printing parameters before the
expansion code is run?

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Why does rackunit show the stack trace of check failures?

2013-10-08 Thread Eric Dobson
I'm trying to clean up the output of some test cases so they are
easier to debug when the go wrong. One of the issues is that the
rackunit text ui always wants to print the stack trace of check
failures. I never find these useful since they just show frames of the
test code, not the code under test.

The code is here:
https://github.com/plt/racket/blob/master/pkgs/rackunit-pkgs/rackunit-lib/rackunit/text-ui.rkt#L105
Added here:
https://github.com/plt/racket/commit/f6b73f01e174337534ed11e7ff78d095f16d6088

It is guarded by a (when #t ...), could we change it so that is
instead a (when verbose? ...)? verbose? is already affecting other
parts of the output in this function.

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Lifting submodules

2013-09-13 Thread Eric Dobson
Which lifting function? All of the ones I found didn't do what I
needed when called from an expression context, as the lifted syntax
was also in expression context. (syntax-local-lift-expression, and
syntax-local-lift-module-end-declaration)

On Fri, Sep 13, 2013 at 1:53 PM, Matthias Felleisen
 wrote:
>
> Lift the module definition to top and require in the expression position?
>
>
> On Sep 13, 2013, at 12:08 PM, Eric Dobson wrote:
>
>> I want to write a macro which generates submodules and then possibly
>> requires them. This is so that can easily use another language (TR)
>> for the expression.
>>
>> If all uses of the macro are at the top-level/module-level this is
>> easy and I can expand out to something like:
>> (begin
>>  (module new-mod typed/racket )
>>  (require 'new-mod)
>>  )
>>
>> But I don't see how I can do something like this when the use is in an
>> expression context or an internal-definition context. Is there a way
>> to do this?
>> 
>>  Racket Users list:
>>  http://lists.racket-lang.org/users
>

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Lifting submodules

2013-09-13 Thread Eric Dobson
I want to write a macro which generates submodules and then possibly
requires them. This is so that can easily use another language (TR)
for the expression.

If all uses of the macro are at the top-level/module-level this is
easy and I can expand out to something like:
(begin
  (module new-mod typed/racket )
  (require 'new-mod)
  )

But I don't see how I can do something like this when the use is in an
expression context or an internal-definition context. Is there a way
to do this?

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] property pict-convertible in typed racket

2013-09-13 Thread Eric Dobson
Currently we do not have a safe way of checking #:property options in
TR, so writing the struct in R and requiring it is the only solution
right now.

On Fri, Sep 13, 2013 at 12:42 AM, Daniele Capo  wrote:
> Hello,
>
> in a personal project I'm working on I've some structure that has to be
> printed 'graphically'.
> I've used prop:pict-convertible to do this, now I'd like to try to rewrite
> the code in typed racket, but it seems that I can't use the #:property
> options in typed struct:
>
> The only way I've found to do the same thing was to define the struct in an
> untyped module than use require/typed in a typed module.
> Is there another way to define  structure that can be printed as a shape?
>
> thanks,
> Daniele
>
> --
> daniele capo
> viale iv novembre, 8/a
> 01100 viterbo
>
> via mazzetta, 10
> 01100 viterbo
>
> tel. (+39) 328.30.51.751
>
> my (new) website: http://www.danielecapo.com
> daniele’s flickr page: http://www.flickr.com/photos/danielecapo/
> twitter
> http://twitter.com/danielecapo
>
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>


  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Typed Racket and require/typed for polymorphic structs

2013-08-07 Thread Eric Dobson
That doesn't solve the issue. The issue is not in struct/c and thus
using struct/dc has the same problem. A immutable field cannot have a
impersonator contract applied to it because that would be equivalent
to mutating the field.

On Wed, Aug 7, 2013 at 8:49 AM, Asumu Takikawa  wrote:
> On 2013-08-07 08:42:53 -0700, Eric Dobson wrote:
>> What is (posn X Y)? If you mean the obvious (struct/c posn X Y) you
>> will run into issues with the fact that posn is immutable and
>> therefore the contracts on its fields need to be flat or chaperone
>> contracts, and parametric contracts are not.
>
> How about something like:
>   (struct/dc posn [x () #:impersonator X]
>   [y () #:impersonator Y])
>
> Cheers,
> Asumu

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Typed Racket and require/typed for polymorphic structs

2013-08-07 Thread Eric Dobson
What is (posn X Y)? If you mean the obvious (struct/c posn X Y) you
will run into issues with the fact that posn is immutable and
therefore the contracts on its fields need to be flat or chaperone
contracts, and parametric contracts are not.


On Wed, Aug 7, 2013 at 8:32 AM, Asumu Takikawa  wrote:
> On 2013-08-07 08:18:41 -0700, Eric Dobson wrote:
>> Ok, now I'm not so sure it is possible. Can you give what you think
>> the contracts should be for your posn example? The issue that I see is
>> that the parametricity is shared across different functions and I
>> don't know how that is represented with racket contracts.
>
> The contracts should be something like
>   make-posn  -->  (-> X Y (posn X Y))
>   posn-x -->  (-> (posn X Y) X)
>   posn-y -->  (-> (posn X Y) Y)
>
>   where X = (new-∀/c 'X)
> Y = (new-∀/c 'Y)
>
> This is similar to how `contract-out` works with the #:exists and
> #:forall keywords.
>
> Cheers,
> Asumu


  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Typed Racket and require/typed for polymorphic structs

2013-08-07 Thread Eric Dobson
Ok, now I'm not so sure it is possible. Can you give what you think
the contracts should be for your posn example? The issue that I see is
that the parametricity is shared across different functions and I
don't know how that is represented with racket contracts.

On Wed, Aug 7, 2013 at 6:13 AM, Asumu Takikawa  wrote:
> On 2013-08-06 21:53:14 -0700, Eric Dobson wrote:
>> I'm assuming you mean parametric contracts instead of polymorphic. But
>> not sure why those would be the correct solution, I think any/c would
>> work, I'm not seeing a case where wrapping the value would protect
>> anything.
>
> Yes, I mean parameteric contracts. I think you want parameteric
> contracts so that you know, for example, that a struct accessor is
> actually giving you the value you put in a struct instead of some random
> unrelated value.
>
> Cheers,
> Asumu

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Typed Racket and require/typed for polymorphic structs

2013-08-06 Thread Eric Dobson
I'm assuming you mean parametric contracts instead of polymorphic. But
not sure why those would be the correct solution, I think any/c would
work, I'm not seeing a case where wrapping the value would protect
anything.

I don't see a fundamental limitation to doing this either.

On Tue, Aug 6, 2013 at 2:56 PM, Asumu Takikawa  wrote:
> Hi all,
>
> Does anyone know why Typed Racket does not support importing polymorphic
> structs using the #:struct keyword in `require/typed`? Is this a
> fundamental limitation or just a "small matter of programming"?
>
> i.e., I want to do something like
>   (require/typed lang/posn
> [#:struct posn (A) ([x : A] [y: A])])
>
> This seems like it could be supported by the contract system in most
> cases (with polymorphic contracts on accessors and constructors). The
> case in which it wouldn't work is if you wanted to export a struct
> instance with a polymorphic type from the untyped module.
>
> Cheers,
> Asumu
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Best Way to Really Understand the Compilation and Execution Model?

2013-08-05 Thread Eric Dobson
I don't think expanding into a define and a define-for-syntax will
work. If I understand Nick correctly then Arc doesn't have phases and
state should be shared across the uses in macros and uses in the
program. I.e.

(define cdr/only-once
  (let ((state #f))
(lambda (list)
  (if state
  list
  (begin
(set! state #t)
(cdr list))

Should only ever take the cdr once even across expansion/runtime.


On Mon, Aug 5, 2013 at 10:12 AM, Vincent St-Amour  wrote:
> For that, you could have Arc's `define' expand into both a Racket
> `define' and a `define-for-syntax', which I believe is what Jens was
> suggesting. You may want to do something similar for `require', too.
>
> Vincent
>
>
>
> At Mon, 5 Aug 2013 10:06:36 -0700,
> Nick Sivo wrote:
>>
>> I'm aware of defmacro and even understand its implementation (cute
>> trick!), but I need to eventually get something like this to work:
>>
>> (require compatibility/defmacro)
>>
>> (define (my-cdr lst)
>>   (cdr lst))
>>
>> (defmacro drop-first-form args
>>   `(begin ,@(my-cdr args)))
>>
>> (drop-first-form
>>  (displayln 1)
>>  (displayln 2))
>>
>> Where the above just works and displays 2.
>>
>> I'll check out the latest research papers too.
>>
>> Thanks,
>> Nick
>>
>> On Mon, Aug 5, 2013 at 1:40 AM, Noel Welsh  wrote:
>> > Does defmacro do what you want?
>> >
>> > http://docs.racket-lang.org/compatibility/defmacro.html?q=defmacro#%28form._%28%28lib._compatibility%2Fdefmacro..rkt%29._defmacro%29%29
>> >
>> > In terms of grokking the macro system you probably want to read some of the
>> > research papers, which you can find from links here:
>> >
>> > http://racket-lang.org/people.html
>> >
>> > Racket's macro system is really cutting edge, so it doesn't have a large
>> > body of text explaining it.
>> >
>> > HTH,
>> > N.
>> >
>> >
>> >
>> > On Sun, Aug 4, 2013 at 9:50 PM, Nick Sivo  wrote:
>> >>
>> >> Hello everyone,
>> >>
>> >> I work on Hacker News[1], which is written in Arc[2], and am tasked
>> >> with making it faster.  The Arc compiler targets Racket[3], but
>> >> doesn't do so in such a way that function names or source locations
>> >> are preserved.  This makes output from the sampling profiler and error
>> >> messages mostly useless.
>> >>
>> >> I'd like to make Arc work as a Racket language (and in the process
>> >> preserve srclocs and names), and have made some progress[4].  However,
>> >> I'm hitting a wall trying to implement Arc's macros.  Specifically, I
>> >> need to find a way for the macros themselves to use previously defined
>> >> Arc functions as part of their processing. I understand that there are
>> >> good reasons for Racket's hygiene and syntax phases, but I'm
>> >> implementing an existing language that has its own strong opinions.
>> >> I'm almost certain that accomplishing my goal is possible with the
>> >> right understanding, but despite reading much of the Racket Reference,
>> >> I don't truly get it yet.
>> >>
>> >> With that motivation in mind, what's the best way for me to become
>> >> intimately familiar with Racket's runtime, execution and compilation
>> >> model? Should I read through and make sure I understand the debugger?
>> >> The macro stepper?  Is there any kind of guide to Racket Internals?
>> >>
>> >> Thanks for any advice.
>> >>
>> >> Best,
>> >> Nick
>> >>
>> >> [1] https://news.ycombinator.com
>> >> [2] http://arclanguage.org
>> >> [3] Well, really mzscheme
>> >> [4] https://github.com/kogir/rark
>> >> 
>> >>   Racket Users list:
>> >>   http://lists.racket-lang.org/users
>> >
>> >
>> 
>>   Racket Users list:
>>   http://lists.racket-lang.org/users
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Syntax parse and not enough forms

2013-07-20 Thread Eric Dobson
I'm trying to improve the error messages for some macros and it seems
like in some cases syntax-parse cannot give a good error message and
just returns "bad syntax". The main issue I'm dealing with is when the
use does not have as many subforms as the macro requires. I think I
understand why it is hard for syntax-parse to automatically generate
the error message because it doesn't want to make error messages be
made from compound parts. But I'm not sure as a macro writer how I
tell syntax-parse the info it doesn't want to synthesize so that I can
have nice error messages when there are not enough arguments. Is this
easy to do/even possible?

My Program:

#lang racket

(require syntax/parse)

(define (parser stx)
  (with-handlers ([exn:fail? exn-message])
(syntax-parse stx
  [(_ arg1:id arg2:id) "Matched"])))

(parser #'(foo a1)) ;; Bad error message and I want to change this
(parser #'(foo a1 a2))
(parser #'(foo a1 a2 a3))


(define (parser2 stx)
  (with-handlers ([exn:fail? exn-message])
(syntax-parse stx
  [(_ arg1:id . arg2:id) "Matched"])))

(parser2 #'(foo a1))

Output:
/Users/endobson/tmp/tmp2.rkt:10:10: foo: bad syntax\n  in: (foo a1)"
"Matched"
"/Users/endobson/tmp/tmp2.rkt:12:21: foo: unexpected term\n  at: a3\n
in: (foo a1 a2 a3)"
"/Users/endobson/tmp/tmp2.rkt:20:11: foo: expected identifier\n  at:
()\n  in: (foo a1)"

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Lazy syntax class attributes

2013-06-09 Thread Eric Dobson
Confirmed that it is internal to syntax parse.

#lang racket

(begin-for-syntax
  (require syntax/parse racket/promise)
  (define-syntax-class foo
(pattern x:id
  #:attr y (delay (displayln (syntax->datum #'x)) #''x


(define-syntax bar
  (syntax-parser
((_ a:foo b:foo c:foo d:foo e:foo f:foo g:foo)
 #'(list a.y b.y c.y d.y e.y f.y g.y

(bar a b c d e f g)

This program sometimes changes the output (racket vs. raco make,
seemed the best way to trigger it)

endobson@yggdrasil () ~/proj/racket/plt % racket ~/tmp/tmp.rkt (0)
d
b
c
e
f
g
a
'(a b c d e f g)
endobson@yggdrasil () ~/proj/racket/plt % raco make ~/tmp/tmp.rkt (0)
c
d
e
f
g
a
b

What does the syntax-parse API guarantee about this?

On Sun, Jun 9, 2013 at 10:55 AM, Eric Dobson  wrote:
> I'm seeing something which looks like on different runs of my program
> different attributes are forced in different orders. The change that
> triggers this is changing something unrelated in the syntax pattern.
> I'm still working at understanding what is triggering this so that I
> can reproduce a small test case.
>
> The basic code is that #'(op.unsafe e1.opt e2.opt) seems to force
> e1.opt first sometimes and sometimes e2.opt is forced first. Could
> this be a hash iteration order issue in the internals of syntax/parse?
>
> On Wed, Jun 5, 2013 at 4:58 PM, Eric Dobson  wrote:
>> I started working with this last night, and it simplified the code a
>> lot. I was able to build syntax classes on top of other ones with a
>> lot less worrying about when the slow path would be run. (And
>> uncovered a couple of optimizer bugs in the process).
>>
>>
>> On Wed, Jun 5, 2013 at 2:05 PM, Ryan Culpepper  wrote:
>>> I've implemented what I described earlier, as well as the error on 3D values
>>> in a #:with clause or ~parse right-hand side.
>>>
>>> Pattern variables that come from syntax-parse already acted differently; if
>>> they weren't syntax-valued, they raised errors. All I've done now is turn
>>> some error cases into non-error cases.
>>>
>>> Ryan
>>>
>>>
>>>
>>> On 06/01/2013 11:55 AM, Eric Dobson wrote:
>>>>
>>>> I would say not to implement this just on my behalf. I think it would
>>>> be weird for pattern variables to act differently if they came from
>>>> syntax-parse versus from with-syntax.
>>>>
>>>> On Fri, May 31, 2013 at 2:17 PM, Sam Tobin-Hochstadt 
>>>> wrote:
>>>>>
>>>>> On Fri, May 31, 2013 at 4:42 PM, Ryan Culpepper 
>>>>> wrote:
>>>>>>
>>>>>>
>>>>>> Note, however, that the syntax class now uses #:attr instead of #:with.
>>>>>> That's the main usability issue I'm worried about with this change.
>>>>>> Following with-syntax's lead, a #:with clause automatically converts its
>>>>>> right-hand side to syntax---even if the result is "3D". That means that
>>>>>> if
>>>>>> you forget that step of the conversion to laziness, you'll probably get
>>>>>> bizarre 3D syntax. I could change #:with to raise an error in some/all
>>>>>> 3D
>>>>>> cases, but that might break existing programs.
>>>>>>
>>>>>> Is anyone out there using syntax-parse to make 3D syntax?
>>>>>
>>>>>
>>>>> I'm sure all the places where I do this are bugs (and it has happened
>>>>> to me), so I'd welcome this error.
>>>>>
>>>>> Sam
>>>
>>>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Lazy syntax class attributes

2013-06-09 Thread Eric Dobson
I'm seeing something which looks like on different runs of my program
different attributes are forced in different orders. The change that
triggers this is changing something unrelated in the syntax pattern.
I'm still working at understanding what is triggering this so that I
can reproduce a small test case.

The basic code is that #'(op.unsafe e1.opt e2.opt) seems to force
e1.opt first sometimes and sometimes e2.opt is forced first. Could
this be a hash iteration order issue in the internals of syntax/parse?

On Wed, Jun 5, 2013 at 4:58 PM, Eric Dobson  wrote:
> I started working with this last night, and it simplified the code a
> lot. I was able to build syntax classes on top of other ones with a
> lot less worrying about when the slow path would be run. (And
> uncovered a couple of optimizer bugs in the process).
>
>
> On Wed, Jun 5, 2013 at 2:05 PM, Ryan Culpepper  wrote:
>> I've implemented what I described earlier, as well as the error on 3D values
>> in a #:with clause or ~parse right-hand side.
>>
>> Pattern variables that come from syntax-parse already acted differently; if
>> they weren't syntax-valued, they raised errors. All I've done now is turn
>> some error cases into non-error cases.
>>
>> Ryan
>>
>>
>>
>> On 06/01/2013 11:55 AM, Eric Dobson wrote:
>>>
>>> I would say not to implement this just on my behalf. I think it would
>>> be weird for pattern variables to act differently if they came from
>>> syntax-parse versus from with-syntax.
>>>
>>> On Fri, May 31, 2013 at 2:17 PM, Sam Tobin-Hochstadt 
>>> wrote:
>>>>
>>>> On Fri, May 31, 2013 at 4:42 PM, Ryan Culpepper 
>>>> wrote:
>>>>>
>>>>>
>>>>> Note, however, that the syntax class now uses #:attr instead of #:with.
>>>>> That's the main usability issue I'm worried about with this change.
>>>>> Following with-syntax's lead, a #:with clause automatically converts its
>>>>> right-hand side to syntax---even if the result is "3D". That means that
>>>>> if
>>>>> you forget that step of the conversion to laziness, you'll probably get
>>>>> bizarre 3D syntax. I could change #:with to raise an error in some/all
>>>>> 3D
>>>>> cases, but that might break existing programs.
>>>>>
>>>>> Is anyone out there using syntax-parse to make 3D syntax?
>>>>
>>>>
>>>> I'm sure all the places where I do this are bugs (and it has happened
>>>> to me), so I'd welcome this error.
>>>>
>>>> Sam
>>
>>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Lazy syntax class attributes

2013-06-05 Thread Eric Dobson
I started working with this last night, and it simplified the code a
lot. I was able to build syntax classes on top of other ones with a
lot less worrying about when the slow path would be run. (And
uncovered a couple of optimizer bugs in the process).


On Wed, Jun 5, 2013 at 2:05 PM, Ryan Culpepper  wrote:
> I've implemented what I described earlier, as well as the error on 3D values
> in a #:with clause or ~parse right-hand side.
>
> Pattern variables that come from syntax-parse already acted differently; if
> they weren't syntax-valued, they raised errors. All I've done now is turn
> some error cases into non-error cases.
>
> Ryan
>
>
>
> On 06/01/2013 11:55 AM, Eric Dobson wrote:
>>
>> I would say not to implement this just on my behalf. I think it would
>> be weird for pattern variables to act differently if they came from
>> syntax-parse versus from with-syntax.
>>
>> On Fri, May 31, 2013 at 2:17 PM, Sam Tobin-Hochstadt 
>> wrote:
>>>
>>> On Fri, May 31, 2013 at 4:42 PM, Ryan Culpepper 
>>> wrote:
>>>>
>>>>
>>>> Note, however, that the syntax class now uses #:attr instead of #:with.
>>>> That's the main usability issue I'm worried about with this change.
>>>> Following with-syntax's lead, a #:with clause automatically converts its
>>>> right-hand side to syntax---even if the result is "3D". That means that
>>>> if
>>>> you forget that step of the conversion to laziness, you'll probably get
>>>> bizarre 3D syntax. I could change #:with to raise an error in some/all
>>>> 3D
>>>> cases, but that might break existing programs.
>>>>
>>>> Is anyone out there using syntax-parse to make 3D syntax?
>>>
>>>
>>> I'm sure all the places where I do this are bugs (and it has happened
>>> to me), so I'd welcome this error.
>>>
>>> Sam
>
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] syntax-parse, macros, and literal-sets

2013-06-01 Thread Eric Dobson
It explained the problem, but it didn't explain why a literal set is
different than a literal with an explicit phase. I understand why the
problem cannot be solved without an explicit phase

From the docs:
If the #:phase option is given, then the literal is compared at phase
phase-expr. Specifically, the binding of the literal-id at phase
phase-expr must match the input’s binding at phase phase-expr.

Why cannot it be checked statically that the literal-id has a binding
at phase-expr, but it can be done when constructing a literal set?

On Fri, May 31, 2013 at 11:47 AM, Ryan Culpepper  wrote:
> This might answer some of your questions:
> http://macrologist.blogspot.com/2011/09/syntax-parse-and-literals.html
>
> Ryan
>
>
>
> On 05/31/2013 02:30 AM, Eric Dobson wrote:
>>
>> Ok given the complexities of literal-sets and hygiene, I think I will
>> avoid them if I can. The issue is that I cannot seem to get the same
>> behaviour from literal lists as I do from literal-sets. Literal sets
>> will give an error an expansion time if there is not a binding for a
>> specified literal, but literal lists do not do this even when I
>> specify a phase for the binding.
>>
>> #lang racket
>>
>> (require syntax/parse)
>> (require (for-template (only-in racket/base list)))
>>
>> (syntax-parse #'list
>> #:literals ((list list #:phase -1))
>> (list #f))
>>
>> ;; Raises the error at runtime
>> (syntax-parse #'list
>> #:literals ((map map #:phase -1))
>> (map #f))
>>
>> (define-literal-set literals1 #:for-template (list))
>>
>> ;; Gives the error at compile time if uncommented
>> ;(define-literal-set literals2 #:for-template (map))
>>
>> I'm not sure I understand the reason for this limitation.
>>
>>
>> On Thu, May 30, 2013 at 9:43 AM, Carl Eastlund  wrote:
>>>
>>> On Thu, May 30, 2013 at 12:25 PM, Eric Dobson 
>>> wrote:
>>>>
>>>>
>>>> Why do literal-sets have to be unhygienic? I expected them to work
>>>> like literal lists which you say are hygienic. Also literal-sets are
>>>> not documented as being unhygienic, which was the confusing part.
>>>
>>>
>>>
>>> Literal sets are hygienic in the informal sense of "follow a disciplined
>>> notion of binding structure".  They are not hygienic in the formal sense
>>> of
>>> Scheme and Racket expansion wherein names derive their scope from where
>>> they
>>> appear in the source.  Specifically, when you use a literal set, you do
>>> not
>>> write down the names in the set, yet they are scoped as if you wrote them
>>> right there in your program.  Which is what you want -- to use those
>>> names
>>> in the current scope -- but is not hygienic, by the strict definition.
>>> Anything where you "store" names and pull them out later -- module
>>> exports,
>>> unit signatures, literal sets, and other similar tools -- is necessarily
>>> unhygienic.  Using them in surface syntax is relatively straightforward;
>>> using them in macros is tricky, because these tools have to introduce the
>>> stored names into some lexical context for you to use, and with a macro
>>> that
>>> could be either the macro's context or the macro user's context.
>>>
>>>>
>>>> In my example 2 both the literals in the literal-list and the ids used
>>>> to match the syntax have the same marks, so it is very unintuitive
>>>> that the lexical context of the literal-set name should matter. Also
>>>> can literal sets be used to abstract over identifiers with different
>>>> lexical contexts, intuitively since other forms can do that I would
>>>> expect that they should be able to, but given that its unhygienic I'm
>>>> not so sure now.
>>>
>>>
>>>
>>> Because literal-sets can be used in any context, the names used in the
>>> literal set are used in the context of the reference to the literal set.
>>> So
>>> if a macro injects the name of the literal set to a syntax-parse
>>> expression,
>>> the macro needs to determine whether that literal set is used in the
>>> macro's
>>> context or the macro user's context.  Otherwise it could be difficult to
>>> use
>>> the names in the macro itself.  Hence the #:at option, which lets macros
>>> specify the context "at" which the literal set is used, if it's different
>>&g

Re: [racket] Lazy syntax class attributes

2013-06-01 Thread Eric Dobson
I would say not to implement this just on my behalf. I think it would
be weird for pattern variables to act differently if they came from
syntax-parse versus from with-syntax.

On Fri, May 31, 2013 at 2:17 PM, Sam Tobin-Hochstadt  wrote:
> On Fri, May 31, 2013 at 4:42 PM, Ryan Culpepper  wrote:
>>
>> Note, however, that the syntax class now uses #:attr instead of #:with.
>> That's the main usability issue I'm worried about with this change.
>> Following with-syntax's lead, a #:with clause automatically converts its
>> right-hand side to syntax---even if the result is "3D". That means that if
>> you forget that step of the conversion to laziness, you'll probably get
>> bizarre 3D syntax. I could change #:with to raise an error in some/all 3D
>> cases, but that might break existing programs.
>>
>> Is anyone out there using syntax-parse to make 3D syntax?
>
> I'm sure all the places where I do this are bugs (and it has happened
> to me), so I'd welcome this error.
>
> Sam

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Lazy syntax class attributes

2013-05-31 Thread Eric Dobson
That seems like more syntax in the use than just #`(complicated
#,((attribute e.v))), which I can get with just thunking the
computation. The thunk it self can manipulate a promise to make sure
the effects only happen once.

I'm pretty sure I could use a rename transformer struct property and a
chaperoned struct to have any effects I want on pattern variable
lookup (as it tries to lookup the slot in the struct), but I still
need to return the original pattern variable id from the transformer,
so that doesn't let me change the final value of the lookup. Note that
this is untested.

On Fri, May 31, 2013 at 8:25 AM, J. Ian Johnson  wrote:
> You could also make a compile-time macro that will introduce several 
> identifier macros to your syntax-class pattern right-hand-sides like
>
> (define-lazy-syntax-class blah
>   (pattern pat #:lazy-wrap [complicated (e.v ...)] rhs ...) ...)
>
> =>
> ;; generate ((force-e.v ...) ...)
> (define-syntax-class blah
>   (pattern pat #:do [(define-syntax (force-e.v stx)
>   (syntax-case stx (e.v)
> [e.v #`(complicated #,(force (attribute e.v))])) ...]
> rhs ...) ...)
>
> This way you can make the attributes promises (at compile time) to later 
> force when using them in rhs ...
>
> Too much?
> -Ian
> - Original Message -
> From: "Eric Dobson" 
> To: "Matthias Felleisen" 
> Cc: users@racket-lang.org
> Sent: Friday, May 31, 2013 11:00:13 AM GMT -05:00 US/Canada Eastern
> Subject: Re: [racket] Lazy syntax class attributes
>
> That seems like a a workable amount of overhead, but I'm not sure what
> you are suggesting the v attribute to be bound as. It might have not
> been obvious from my my example that its value depends on the slow
> computation, so unless pattern variables can be mutated I'm not sure
> how force-all can change what e.v resolves to.
>
> On Fri, May 31, 2013 at 5:18 AM, Matthias Felleisen
>  wrote:
>>
>> Can you prefix the #'(complicated ..) expression with a form
>> that forces all lazy computations so that complicated itself
>> remains the same?
>>
>>   (let () (force-all) #'(complicated (form e.v)))
>>
>> where force-all is defined via a begin in the slow syntax class?
>>
>> -- Matthias
>>
>>
>> On May 31, 2013, at 2:47 AM, Eric Dobson wrote:
>>
>>> I'm working on code (TR optimizer) that needs to match some
>>> expressions and then compute an attribute based on the expression. I
>>> would like to abstract this out as a syntax class. Example is below:
>>>
>>> #lang racket
>>> (require syntax/parse)
>>>
>>> (define-syntax-class slow
>>>  (pattern e:expr
>>>   #:with v (begin (displayln 'slow) #''e)))
>>>
>>> (syntax-parse #'(x 2)
>>>  ((e:slow 1) #'(complicated (form e.v)))
>>>  ((e:slow 2) #'(complicated (form e.v
>>>
>>> The issue is that computing the attribute is slow/expensive/effectful
>>> and so I only want to do the calculation in the case that I use the
>>> result. One solution is to attach a thunk as the attribute value, but
>>> this requires changing #'(complicated (form e.v)) to #`(complicated
>>> (form #,((attribute e.v which quickly becomes less readable with
>>> many such attributes in the final syntax object. Is there a way to do
>>> this in a lazy manner that does not complicate the use of the syntax
>>> class? I'm fine adding complexity to the creation of the syntax class
>>> because it is a one time cost versus the many places where the syntax
>>> class would be used.
>>> 
>>>  Racket Users list:
>>>  http://lists.racket-lang.org/users
>>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Lazy syntax class attributes

2013-05-31 Thread Eric Dobson
That seems like a a workable amount of overhead, but I'm not sure what
you are suggesting the v attribute to be bound as. It might have not
been obvious from my my example that its value depends on the slow
computation, so unless pattern variables can be mutated I'm not sure
how force-all can change what e.v resolves to.

On Fri, May 31, 2013 at 5:18 AM, Matthias Felleisen
 wrote:
>
> Can you prefix the #'(complicated ..) expression with a form
> that forces all lazy computations so that complicated itself
> remains the same?
>
>   (let () (force-all) #'(complicated (form e.v)))
>
> where force-all is defined via a begin in the slow syntax class?
>
> -- Matthias
>
>
> On May 31, 2013, at 2:47 AM, Eric Dobson wrote:
>
>> I'm working on code (TR optimizer) that needs to match some
>> expressions and then compute an attribute based on the expression. I
>> would like to abstract this out as a syntax class. Example is below:
>>
>> #lang racket
>> (require syntax/parse)
>>
>> (define-syntax-class slow
>>  (pattern e:expr
>>   #:with v (begin (displayln 'slow) #''e)))
>>
>> (syntax-parse #'(x 2)
>>  ((e:slow 1) #'(complicated (form e.v)))
>>  ((e:slow 2) #'(complicated (form e.v
>>
>> The issue is that computing the attribute is slow/expensive/effectful
>> and so I only want to do the calculation in the case that I use the
>> result. One solution is to attach a thunk as the attribute value, but
>> this requires changing #'(complicated (form e.v)) to #`(complicated
>> (form #,((attribute e.v which quickly becomes less readable with
>> many such attributes in the final syntax object. Is there a way to do
>> this in a lazy manner that does not complicate the use of the syntax
>> class? I'm fine adding complexity to the creation of the syntax class
>> because it is a one time cost versus the many places where the syntax
>> class would be used.
>> 
>>  Racket Users list:
>>  http://lists.racket-lang.org/users
>

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Lazy syntax class attributes

2013-05-30 Thread Eric Dobson
I'm working on code (TR optimizer) that needs to match some
expressions and then compute an attribute based on the expression. I
would like to abstract this out as a syntax class. Example is below:

#lang racket
(require syntax/parse)

(define-syntax-class slow
  (pattern e:expr
   #:with v (begin (displayln 'slow) #''e)))

(syntax-parse #'(x 2)
  ((e:slow 1) #'(complicated (form e.v)))
  ((e:slow 2) #'(complicated (form e.v

The issue is that computing the attribute is slow/expensive/effectful
and so I only want to do the calculation in the case that I use the
result. One solution is to attach a thunk as the attribute value, but
this requires changing #'(complicated (form e.v)) to #`(complicated
(form #,((attribute e.v which quickly becomes less readable with
many such attributes in the final syntax object. Is there a way to do
this in a lazy manner that does not complicate the use of the syntax
class? I'm fine adding complexity to the creation of the syntax class
because it is a one time cost versus the many places where the syntax
class would be used.

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] syntax-parse, macros, and literal-sets

2013-05-30 Thread Eric Dobson
Ok given the complexities of literal-sets and hygiene, I think I will
avoid them if I can. The issue is that I cannot seem to get the same
behaviour from literal lists as I do from literal-sets. Literal sets
will give an error an expansion time if there is not a binding for a
specified literal, but literal lists do not do this even when I
specify a phase for the binding.

#lang racket

(require syntax/parse)
(require (for-template (only-in racket/base list)))

(syntax-parse #'list
   #:literals ((list list #:phase -1))
   (list #f))

;; Raises the error at runtime
(syntax-parse #'list
   #:literals ((map map #:phase -1))
   (map #f))

(define-literal-set literals1 #:for-template (list))

;; Gives the error at compile time if uncommented
;(define-literal-set literals2 #:for-template (map))

I'm not sure I understand the reason for this limitation.


On Thu, May 30, 2013 at 9:43 AM, Carl Eastlund  wrote:
> On Thu, May 30, 2013 at 12:25 PM, Eric Dobson 
> wrote:
>>
>> Why do literal-sets have to be unhygienic? I expected them to work
>> like literal lists which you say are hygienic. Also literal-sets are
>> not documented as being unhygienic, which was the confusing part.
>
>
> Literal sets are hygienic in the informal sense of "follow a disciplined
> notion of binding structure".  They are not hygienic in the formal sense of
> Scheme and Racket expansion wherein names derive their scope from where they
> appear in the source.  Specifically, when you use a literal set, you do not
> write down the names in the set, yet they are scoped as if you wrote them
> right there in your program.  Which is what you want -- to use those names
> in the current scope -- but is not hygienic, by the strict definition.
> Anything where you "store" names and pull them out later -- module exports,
> unit signatures, literal sets, and other similar tools -- is necessarily
> unhygienic.  Using them in surface syntax is relatively straightforward;
> using them in macros is tricky, because these tools have to introduce the
> stored names into some lexical context for you to use, and with a macro that
> could be either the macro's context or the macro user's context.
>
>>
>> In my example 2 both the literals in the literal-list and the ids used
>> to match the syntax have the same marks, so it is very unintuitive
>> that the lexical context of the literal-set name should matter. Also
>> can literal sets be used to abstract over identifiers with different
>> lexical contexts, intuitively since other forms can do that I would
>> expect that they should be able to, but given that its unhygienic I'm
>> not so sure now.
>
>
> Because literal-sets can be used in any context, the names used in the
> literal set are used in the context of the reference to the literal set.  So
> if a macro injects the name of the literal set to a syntax-parse expression,
> the macro needs to determine whether that literal set is used in the macro's
> context or the macro user's context.  Otherwise it could be difficult to use
> the names in the macro itself.  Hence the #:at option, which lets macros
> specify the context "at" which the literal set is used, if it's different
> from the context naming the literal set.
>
> The short, short version: use the #:at option if your macro uses literal
> sets "on behalf of" the user.
>
> The syntax-parse documentation could probably be more explicit about how
> literals in literal sets derive their context; I do not envy Ryan the task
> of writing that up, because I'm not sure how to make it 100% clear myself,
> as my ramblings above may show.
>
>>
>> On Thu, May 30, 2013 at 3:49 AM, Ryan Culpepper  wrote:
>> > On 05/29/2013 03:30 AM, Eric Dobson wrote:
>> >>
>> >> I was writing a macro that generated a literal-set and ran into some
>> >> confusing behavior which I have distilled to the following program.
>> >>
>> >> #lang racket
>> >> (require syntax/parse
>> >>   (for-syntax syntax/parse))
>> >>
>> >>
>> >> (define-syntax (define-ls1 stx)
>> >>(syntax-parse stx
>> >>  ((_ name:id (lit:id ...))
>> >>   #'(define-literal-set name (lit ...)
>> >>
>> >> (define-ls1 ls1 (+))
>> >> (define-syntax-class sc1
>> >>#:literal-sets (ls1)
>> >>(pattern +))
>> >>
>> >> (for/list ((x (list #'+ #'*)))
>> >>(syntax-parse x
>> >>  (x:sc1 #t)
>> >>  (_ #f)))
>> >>
>> >>
>> >

Re: [racket] syntax-parse, macros, and literal-sets

2013-05-30 Thread Eric Dobson
Why do literal-sets have to be unhygienic? I expected them to work
like literal lists which you say are hygienic. Also literal-sets are
not documented as being unhygienic, which was the confusing part.

In my example 2 both the literals in the literal-list and the ids used
to match the syntax have the same marks, so it is very unintuitive
that the lexical context of the literal-set name should matter. Also
can literal sets be used to abstract over identifiers with different
lexical contexts, intuitively since other forms can do that I would
expect that they should be able to, but given that its unhygienic I'm
not so sure now.

On Thu, May 30, 2013 at 3:49 AM, Ryan Culpepper  wrote:
> On 05/29/2013 03:30 AM, Eric Dobson wrote:
>>
>> I was writing a macro that generated a literal-set and ran into some
>> confusing behavior which I have distilled to the following program.
>>
>> #lang racket
>> (require syntax/parse
>>   (for-syntax syntax/parse))
>>
>>
>> (define-syntax (define-ls1 stx)
>>(syntax-parse stx
>>  ((_ name:id (lit:id ...))
>>   #'(define-literal-set name (lit ...)
>>
>> (define-ls1 ls1 (+))
>> (define-syntax-class sc1
>>#:literal-sets (ls1)
>>(pattern +))
>>
>> (for/list ((x (list #'+ #'*)))
>>(syntax-parse x
>>  (x:sc1 #t)
>>  (_ #f)))
>>
>>
>> (define-syntax (define-sc2 stx)
>>(syntax-parse stx
>>  ((_ name:id (lit:id ...))
>>   #'(begin
>>   (define-literal-set inner (lit ...))
>>   (define-syntax-class name
>> #:literal-sets (inner)
>> (pattern lit) ...)
>>
>> (define-sc2 sc2 (+))
>> (for/list ((x (list #'+ #'*)))
>>(syntax-parse x
>>  (x:sc2 #t)
>>  (_ #f)))
>>
>> (define-syntax (define-sc3 stx)
>>(syntax-parse stx
>>  ((_ name:id inner:id (lit:id ...))
>>   #'(begin
>>   (define-literal-set inner (lit ...))
>>   (define-syntax-class name
>> #:literal-sets (inner)
>> (pattern lit) ...)
>>
>> (define-sc3 sc3 inner3 (+))
>> (for/list ((x (list #'+ #'*)))
>>(syntax-parse x
>>  (x:sc3 #t)
>>  (_ #f)))
>>
>>
>> (define-syntax (define-sc4 stx)
>>(syntax-parse stx
>>  ((_ name:id (lit:id ...))
>>   #'(begin
>>   (define-literal-set inner (lit ...))
>>   (define-syntax-class name
>> #:literal-sets ((inner #:at name))
>> (pattern lit) ...)
>>
>> (define-sc4 sc4 (+))
>> (for/list ((x (list #'+ #'*)))
>>(syntax-parse x
>>  (x:sc4 #t)
>>  (_ #f)))
>>
>> This produces the output:
>> '(#t #f)
>> '(#t #t)
>> '(#t #f)
>> '(#t #f)
>>
>> I would have expected the second one to return '(#t #f) like the first
>> but it doesn't.
>
>
> The issue is how syntax-parse decides whether an identifier in a pattern is
> a pattern variable or a literal. Let's take the simple case, where we have
> just a literals list. From the standpoint of hygiene, the literals list is
> "binding-like" because it sets the interpretation for "references" in the
> patterns. That means the relevant comparison is bound-identifier=? (like all
> binding forms), and that means that at a minimum if you want an identifier
> in a pattern to be considered a literal, it must have the same marks as the
> corresponding identifier in the literals list. Consider the following
> program:
>
> (define-syntax-rule
>   (define-syntax-rule/arith (macro . pattern) template)
>   (define-syntax macro
> (syntax-rules (+ - * /)
>   [(macro . pattern) template])))
>
> (define-syntax-rule/arith (sum-left-arg (+ x y)) x)
>
> One might expect sum-left-arg to raise a syntax error if given a term with
> something other than + in operator position. But in fact:
>
> (sum-left-arg (* 1 2))
> ;; => 1
>
> The reason is because the expansion of define-syntax-rule/arith puts marks
> on the + in the literals list. So only +'s with the same mark in the pattern
> are considered literals; all others are pattern variables. In particular,
> the + in the pattern in the definition of sum-left-arg is unmarked, so it's
> a pattern variable.
>
> Now back to literal sets. A #:literal-sets clause is an essentially like an
> unhygienic binding form. The identifiers it "binds" must be given some
> lexical context; it defaults to

[racket] syntax-parse, macros, and literal-sets

2013-05-29 Thread Eric Dobson
I was writing a macro that generated a literal-set and ran into some
confusing behavior which I have distilled to the following program.

#lang racket
(require syntax/parse
 (for-syntax syntax/parse))


(define-syntax (define-ls1 stx)
  (syntax-parse stx
((_ name:id (lit:id ...))
 #'(define-literal-set name (lit ...)

(define-ls1 ls1 (+))
(define-syntax-class sc1
  #:literal-sets (ls1)
  (pattern +))

(for/list ((x (list #'+ #'*)))
  (syntax-parse x
(x:sc1 #t)
(_ #f)))


(define-syntax (define-sc2 stx)
  (syntax-parse stx
((_ name:id (lit:id ...))
 #'(begin
 (define-literal-set inner (lit ...))
 (define-syntax-class name
   #:literal-sets (inner)
   (pattern lit) ...)

(define-sc2 sc2 (+))
(for/list ((x (list #'+ #'*)))
  (syntax-parse x
(x:sc2 #t)
(_ #f)))

(define-syntax (define-sc3 stx)
  (syntax-parse stx
((_ name:id inner:id (lit:id ...))
 #'(begin
 (define-literal-set inner (lit ...))
 (define-syntax-class name
   #:literal-sets (inner)
   (pattern lit) ...)

(define-sc3 sc3 inner3 (+))
(for/list ((x (list #'+ #'*)))
  (syntax-parse x
(x:sc3 #t)
(_ #f)))


(define-syntax (define-sc4 stx)
  (syntax-parse stx
((_ name:id (lit:id ...))
 #'(begin
 (define-literal-set inner (lit ...))
 (define-syntax-class name
   #:literal-sets ((inner #:at name))
   (pattern lit) ...)

(define-sc4 sc4 (+))
(for/list ((x (list #'+ #'*)))
  (syntax-parse x
(x:sc4 #t)
(_ #f)))

This produces the output:
'(#t #f)
'(#t #t)
'(#t #f)
'(#t #f)

I would have expected the second one to return '(#t #f) like the first
but it doesn't. The third and fourth are ways to get it to, but I'm
not sure why they work. The third seems to show that the meaning of
how a literal set binds variables depends on the context of the
literal-set name and not the actual literals. The fourth way uses #:at
which is documented as being useful to macros, but not much else. Can
someone explain how literal-sets are supposed to determine whether or
not a variable in the pattern is treated as a literal or as a pattern
variable?

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] disappeared-use syntax property

2013-05-28 Thread Eric Dobson
Is it 'wrong' to add a disappeared-use property onto a syntax object
even if the use wasn't actually disappeared? Its not very obvious what
semantics these syntax properties are supposed to have.

The situation I'm in is that the code that determines examines the
bindings is somewhat separate from the code that finally makes the
decision on whether or not to remove the use. So if it was ok to
unconditionally add any identifier whose binding matched the first
step, that would be useful.

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Thought about namespaces

2013-05-26 Thread Eric Dobson
Try using make-empty-namespace instead of make-base-empty-namespace.
It won't work but will explain why the add1 procedures are the same
when you do get it to work, with the change below.

(define-namespace-anchor anchor)

(define (get-from-fresh-namespace var)
 (let ((namespace (make-empty-namespace)))
  (parameterize ((current-namespace namespace))
(namespace-attach-module (namespace-anchor->namespace anchor)
'racket
;''#%builtin
)
   (namespace-require 'racket)
   (namespace-variable-value var #t (λ () 'error)

You are using the the same builtin module instance in your example but
not the same racket instance. If you use the same racket instance then
you get the same answer for both values. If you could somehow get a
different '#%builtin module then the add1's might be different but I
don't think that is possible.


On Sun, May 26, 2013 at 10:43 AM, Jos Koot  wrote:
> #| Consider: |#
>
> #lang racket
>
> (define (get-from-fresh-namespace var)
>  (let ((namespace (make-base-empty-namespace)))
>   (parameterize ((current-namespace namespace))
>(namespace-require 'racket)
>(namespace-variable-value var #t (λ () 'error)
>
> (eq?
>  (get-from-fresh-namespace 'add1)
>  (get-from-fresh-namespace 'add1)) ; -> #t
>
> (eq?
>  (get-from-fresh-namespace 'force)
>  (get-from-fresh-namespace 'force)) ; -> #f
>
> #|
> It is clear to me why the last form produces #f. Procedure force is a
> predicate of a struct and is exported by module
> .../collects/racket/promise.rkt. For each fresh empty base-namespace the
> form (namespace-require 'racket) uses a distinct instance of this module.
> Each instance defines the promise-struct freshly and provides distinct
> variable- and syntax-bindings related to promises. Is my observation
> correct?
>
> It is little bit confusing that procedure get-from-fresh-namespace, when
> called with the same variable-name, in some cases returns identical values
> and in others does not.
>
> I think it is not easy to make Racket such as to make it procedure
> get-from-fresh-namespace always to return distinct objects (not eq?) or
> always to return identical objects (eq?) when called with the same
> variable-name.
>
> I know, I am comparing procedures, but as
>
> |# (let ((a add1)) (eq? a add1)) #|
>
> is guaranteed to return #t, I wonder what you folks think about to make
> modules such as always provide the same instance of a module when required
> within the same Racket or DrRacket session. Is it possible? Is it desirable?
> What when a module produces side effects (e.g. displayed output)?
> |#
>
>
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>


  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] [Typed Racket] define-predicate for union with functions

2013-05-20 Thread Eric Dobson
No, you cannot define predicates for function types. If you explain
the problem you have, we might be able to explain another solution
that will work.

On Mon, May 20, 2013 at 1:32 AM, Paul Leger  wrote:
> Hi all,
>   Maybe, this question is very easy. In the following code, I try
> defining a predicate for T2, but I cannot because T1 it is a function.
>
> (define-type T1 (Symbol -> Any) )
> (define-type T2 (U Symbol Number T1))
>
> ;(define-predicate T1? Procedure) ;this line is useless
>
> (define-predicate T2? T2)
>
>> Type Checker: Type T2 could not be converted to a contract. in: T2
>
> My unsuccessful answer is:
>   (define-type T2 (U Symbol Number Procedure))
>
> I do not like it because I lose the relationship between T1 and T2. Are
> there some possibility
>
> Thank in advance,
> Paul
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] [Typed Racket] "define-predicate" in Racket 5.3.4

2013-05-09 Thread Eric Dobson
That definitely looks like a bug. Can you file one either using the
bug report tool in the menus or at bugs.racket-lang.org.

No you cannot define a predicate like that, because you cannot tell a
functions type with a first order check. But you can use cast, so
(cast  (Boolean -> Boolean)) should work.

On Thu, May 9, 2013 at 7:57 PM, Paul Leger  wrote:
> Hi all,
> In the Racket 5.3.4, I defined the following predicate
>>(define-predicate A? Procedure)
>
>and later, I tried using as follow:
>>(A? 'a)
>
>   But I have the following error:
>   >recursive-contract: contract violation
>   >expected: flat-contract?
>   >given: #<|chaperone-case->|>
>
> BTW, in this version, Is it possible to define predicate with a specific
> function?
> (define-type A (Boolean -> Boolean))
> (define-predicate A? A)
>
> Best,
> Paul
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] TR: struct: #:prefab option

2013-05-06 Thread Eric Dobson
Couldn't this be solved by protecting the struct on export?

#lang racket/load

(module foo racket
 (struct foo (f) #:prefab)
 (define x (foo (lambda (x) (* 3.3 x
 (provide/contract
   (x (struct/c foo (-> number? number?)
(module bar racket
  (require 'foo)
  (struct foo (f) #:prefab)
  ((foo-f x) "foo"))
(require 'bar)

On Mon, May 6, 2013 at 7:27 AM, Asumu Takikawa  wrote:
> On 2013-05-06 10:09:06 -0400, Ray Racine wrote:
>>The below is now failing for me.
>>(struct: Stuff ([this : String][that : Natural]) #:prefab)
>>
>> [...]
>>
>>If there is a typing issue with the use of #:prefab and its banishment
>>permanent, is there an alternate best practice mechanism for easy struct:
>>serialization/de-serialization?
>
> If you're using the Racket pre-release, then that was probably my commit
> that broke that. Sorry for breaking compatibility here, but it's unsound
> for TR to admit #:prefab as is. Here's an example:
>
>   $ racket
>   Welcome to Racket v5.3.
>   -> (module foo typed/racket
>(struct: foo ([f : (Float -> Float)]) #:prefab)
>(define x (foo (lambda: ([x : Float]) (* 3.3 x
>(provide x))
>   -> (module bar racket
>(require 'foo)
>(struct foo (f) #:prefab)
>((foo-f x) "foo"))
>   -> (require 'bar)
>   2.293305645053357e-309
>
> The first module exports a #:prefab struct instance containing an
> optimized (Float -> Float) function. Since it's a prefab, the second
> module (which is untyped), can freely access the function stored inside.
>
> Since the function is unprotected, it does an unsafe multiplication on a
> string and no contract error is raised.
>
> I'm not sure what's a good alternative for serialization in TR off the
> top of my head.
>
> Cheers,
> Asumu
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] syntax-parse and matching ordered optional keyworded expressions

2013-05-05 Thread Eric Dobson
Version 1 was what I meant by not duplicating work, and version 2
seems much too complicated.

It looks like syntax/parse/experimental/eh, does what I need.

On Sun, May 5, 2013 at 1:11 PM, J. Ian Johnson  wrote:
> One way is to have your ~or form in an ~and with a catch-all where you can 
> observe the ordering.
> That is,
> (~and (~seq forms ...) (~seq (~or (~optional (~seq #:kw0 kw0)) ...others...) 
> ...))
> Then walk through (forms ...) when you're outputting your changed keyword 
> arguments to determine which to output. Say, build a table of keyword to 
> output for that keyword.
> You could also trust that syntax-parse will parse your dotted ~or in order 
> and have ~do forms that populate a data structure via mutation and bump up a 
> counter each time.
>
> -Ian
> ----- Original Message -
> From: "Eric Dobson" 
> To: users@racket-lang.org
> Sent: Sunday, May 5, 2013 3:58:31 PM GMT -05:00 US/Canada Eastern
> Subject: [racket] syntax-parse and matching ordered optional keyworded 
> expressions
>
> I'm trying to improve the struct: form in TR, and one of the things I
> need to do is match a sequence of keyworded expressions, add
> annotations on the expressions, and then put them back into the same
> order.
>
> I can either do a large ~or clause and have optional constraints on
> each clause, but If I do this then each kind of clause needs a
> different name and then putting them back in the same order is
> difficult. If I make a syntax class with each kind of clause then I
> don't know how to enforce the at most one constraint.
>
> Is there a way to do this with out duplicating the parsing work?
>
> Method 1:
> (~or
>  (~optional guard:guard-form)
>  (~optional mutable:mutable-form)
>  property:struct-property-form) ...
> and
> #'(guard.contracted mutable.contracted property.contracted ...))
>
> Method 2:
> (define-splicing-syntax-class any-form
>  (pattern :guard-form)
>  (pattern :mutable-form)
>  (pattern :struct-property-form))
> (form:any-form ...)
> and
> #'(form.contracted ...)
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] syntax-parse and matching ordered optional keyworded expressions

2013-05-05 Thread Eric Dobson
I'm trying to improve the struct: form in TR, and one of the things I
need to do is match a sequence of keyworded expressions, add
annotations on the expressions, and then put them back into the same
order.

I can either do a large ~or clause and have optional constraints on
each clause, but If I do this then each kind of clause needs a
different name and then putting them back in the same order is
difficult. If I make a syntax class with each kind of clause then I
don't know how to enforce the at most one constraint.

Is there a way to do this with out duplicating the parsing work?

Method 1:
(~or
 (~optional guard:guard-form)
 (~optional mutable:mutable-form)
 property:struct-property-form) ...
and
#'(guard.contracted mutable.contracted property.contracted ...))

Method 2:
(define-splicing-syntax-class any-form
 (pattern :guard-form)
 (pattern :mutable-form)
 (pattern :struct-property-form))
(form:any-form ...)
and
#'(form.contracted ...)

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] fingertree / nested datatype

2013-04-13 Thread Eric Dobson
I believe this is the issue that the the check is trying to prevent.

Sam, can you explain how the indirection is supposed to solve this?

What is currently happening is we ask:
(subtype (Deep Number) (Deep Any)), which computes
(subtype (Indirect (List Number)) (Indirect (List Any))), which then computes
(subtype (Deep (List Number)) (Deep (List Any))), and then repeats.

On Fri, Apr 12, 2013 at 5:40 PM, Anthony Carrico  wrote:
> On 04/11/2013 08:51 AM, Sam Tobin-Hochstadt wrote:
>> Yes, an explicit struct involves an explicit indirection, and thus
>> produces a regular tree.
>
> Using Eric's example:
>
> #lang typed/racket
>
> (struct: (a) Indirect ((v : (Deep a
> (struct: (a) Deep ((spine : (Indirect (List a)
> ;(struct: (a) Deep ((spine : (Deep (List a)
>
> This checks fine, but try to use it:
>
> (: test ((Deep Number) -> Boolean))
> (define (test ft) (Deep? ft))
>
> And it hangs. So maybe indirection just pushes the problem somewhere else?
>
> --
> Anthony Carrico
>
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] fingertree / nested datatype

2013-04-11 Thread Eric Dobson
Sam: Currently all the fail tests for TR pass if this check is
removed, can you add one that exposes the issues around subtyping?

On Thu, Apr 11, 2013 at 6:33 AM, Anthony Carrico  wrote:
> On 04/11/2013 09:26 AM, Sam Tobin-Hochstadt wrote:
>> This is indeed something we've run into before. See section 4.2 of
>> this paper: http://www.ccs.neu.edu/racket/pubs/sfp10-kth.pdf
>>
>> We could probably also introduce something like Haskell's `newtype` to
>> address this, as you suggest, but it's not obvious to me how that
>> would work in interoperation with untyped code, for example.  There
>> are a lot of open questions here, and I think we can eventually give
>> better answers to them than we currently have, but we don't have them
>> yet.
>
> Cool. It is great have the opportunity to learn about types a familiar
> environment. Thanks.
>
> --
> Anthony Carrico
>
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] fingertree / nested datatype

2013-04-10 Thread Eric Dobson
So it looks like that you need an indirection struct, here is a simple example.

#lang typed/racket


(struct: (a) Indirect ((v : (Deep a
(struct: (a) Deep ((spine : (Indirect (List a)
;(struct: (a) Deep ((spine : (Deep (List a)

The uncommented version typechecks, but the commented one does not. I
understand why this works in the implementation, but don't know the
theoretical reason why the implementation prevents it, since Indirect
and Deep should be isomorphic.

On Wed, Apr 10, 2013 at 9:43 PM, Eric Dobson  wrote:
> Quick answer is to replace
> (define-type (Fingertree a) (U Empty (Single a) (Deep a)))
> With
> (struct: (a) Fingertree ((v : (U Empty (Single a) (Deep a)
>
> Still looking into understanding what is going on, but I believe you
> will need to introduce a Rec somewhere to get it how you want.
>
> On Wed, Apr 10, 2013 at 6:27 PM, Anthony Carrico  
> wrote:
>> I've been reading about fingertrees, and I figure the best way to
>> understand is to implement. This is my first experience with typed
>> racket. Are nested datatypes supported?
>>
>> This is my first try:
>>
>> typed-fingertree.rkt:19:48: Type Checker: Structure type constructor
>> Deep applied to non-regular arguments ((U (Node2 a) (Node3 a)))
>>
>> #lang typed/racket
>>
>> (struct: (a) Digit1 ((v0 : a)))
>> (struct: (a) Digit2 ((v0 : a) (v1 : a)))
>> (struct: (a) Digit3 ((v0 : a) (v1 : a) (v2 : a)))
>> (struct: (a) Digit4 ((v0 : a) (v1 : a) (v2 : a) (v3 : a)))
>> (define-type (Digit a) (U (Digit1 a) (Digit2 a) (Digit3 a) (Digit4 a)))
>>
>> (struct: (a) Node2 ((v0 : a) (v1 : a)))
>> (struct: (a) Node3 ((v0 : a) (v1 : a) (v2 : a)))
>> (define-type (Node a) (U (Node2 a) (Node3 a)))
>>
>> (struct: Empty ())
>> (struct: (a) Single ((v : a)))
>> (struct: (a) Deep ((left : (Digit a))
>>(spine : (Fingertree (Node a)))
>>(right : (Digit a
>>
>> (define-type (Fingertree a) (U Empty (Single a) (Deep a)))
>>
>> Thank you!
>>
>> --
>> Anthony Carrico
>>
>>
>> 
>>   Racket Users list:
>>   http://lists.racket-lang.org/users
>>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] fingertree / nested datatype

2013-04-10 Thread Eric Dobson
Quick answer is to replace
(define-type (Fingertree a) (U Empty (Single a) (Deep a)))
With
(struct: (a) Fingertree ((v : (U Empty (Single a) (Deep a)

Still looking into understanding what is going on, but I believe you
will need to introduce a Rec somewhere to get it how you want.

On Wed, Apr 10, 2013 at 6:27 PM, Anthony Carrico  wrote:
> I've been reading about fingertrees, and I figure the best way to
> understand is to implement. This is my first experience with typed
> racket. Are nested datatypes supported?
>
> This is my first try:
>
> typed-fingertree.rkt:19:48: Type Checker: Structure type constructor
> Deep applied to non-regular arguments ((U (Node2 a) (Node3 a)))
>
> #lang typed/racket
>
> (struct: (a) Digit1 ((v0 : a)))
> (struct: (a) Digit2 ((v0 : a) (v1 : a)))
> (struct: (a) Digit3 ((v0 : a) (v1 : a) (v2 : a)))
> (struct: (a) Digit4 ((v0 : a) (v1 : a) (v2 : a) (v3 : a)))
> (define-type (Digit a) (U (Digit1 a) (Digit2 a) (Digit3 a) (Digit4 a)))
>
> (struct: (a) Node2 ((v0 : a) (v1 : a)))
> (struct: (a) Node3 ((v0 : a) (v1 : a) (v2 : a)))
> (define-type (Node a) (U (Node2 a) (Node3 a)))
>
> (struct: Empty ())
> (struct: (a) Single ((v : a)))
> (struct: (a) Deep ((left : (Digit a))
>(spine : (Fingertree (Node a)))
>(right : (Digit a
>
> (define-type (Fingertree a) (U Empty (Single a) (Deep a)))
>
> Thank you!
>
> --
> Anthony Carrico
>
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Using syntax/parse, how to accept keyword arguments in any order yet not allow duplicates?

2013-04-07 Thread Eric Dobson
You want ~once. See
http://docs.racket-lang.org/syntax/stxparse-patterns.html?q=syntax-parse#%28form._%28%28lib._syntax%2Fparse..rkt%29._~7eonce%29%29
and 
http://docs.racket-lang.org/syntax/More_Keyword_Arguments.html?q=syntax-parse.

On Sun, Apr 7, 2013 at 5:23 PM, Scott Klarenbach  wrote:
> Hi there,
>
> I'd like to create a macro that accepts non-optional keyword arguments in
> any order.  It should however, ensure that all the arguments are provided.
> Finally, it should not allow duplicates.
>
> So far I've only been able to solve for my first criteria.
>
> As a test, I have this:
>
> (syntax-parse stx
>   [(a:id b:id (~or (~seq #:key1 c:id) (~seq #:key2 d:id)) ...) #t])
>
> Now, that matches for:
> #'(a b #:key1 hey #:key2 there)
> #'(a b #:key2 there #:key1 hey)
>
> Which is what I want.  But unfortunately it also matches:
> #'(a b #:key2 there)
>
> And worse:
> #'(a b #:key2 one #:key2 two #:key2 three)
>
> The reasons are obvious.  I'm just wondering if there's a more robust way of
> dealing with these types of scenarios that I'm missing?  I thought of
> combining an (~or (~and clause but that becomes very tedious, and leaves the
> duplicate problem unsolved.
>
> Thanks.
>
> --
> Talk to you soon,
>
> Scott Klarenbach
>
> PointyHat Software Corp.
> www.pointyhat.ca
> p 604-568-4280
> e sc...@pointyhat.ca
> 200-1575 W. Georgia
> Vancouver, BC V6G2V3
>
> ___
> To iterate is human; to recur, divine
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Typed Racket generalization and inference for containers

2013-04-07 Thread Eric Dobson
This is already fixed at HEAD,
https://github.com/plt/racket/commit/1334e8dcc77854ac826306d3f6a36150cb0bf0c1
has the fix.

On Sun, Apr 7, 2013 at 7:47 AM, Sam Tobin-Hochstadt  wrote:
> On Sun, Apr 7, 2013 at 10:18 AM, Andrey Larionov  wrote:
>> Hello, Racket community.
>> I'm a newbie and just started study Racket and Typed Racket and a little
>> confused with generalization and inference.
>> I found what inference works different for different containers type. For
>> example:
>> Set:
>>> (set 0 195 196)
>> - : (Setof Byte)
>> (set 0 195 196)
>> List:
>>> '((195 . 1) (196 . 2))
>> - : (Listof (Pairof Positive-Byte Positive-Byte)) [generalized from (List
>> (Pairof Positive-Byte One) (Pairof Positive-Byte Positive-Byte))]
>> '((195 . 1) (196 . 2))
>> HashTable:
>>> #hash((195 . 1) (196 . 2) (197 . 3))
>> - : (HashTable Integer Integer)
>> '#hash((195 . 1) (197 . 3) (196 . 2))
>
> This is because sets and lists are immutable, while hash tables are
> (potentially) mutable.  Typed Racket generates more general types for
> mutable data because otherwise the lack of subtyping would make such
> data very hard to use.
>
> In fact, this hash table is immutable, but Typed Racket doesn't
> distinguish between mutable and immutable hash tables.  If it did,
> then you'd get the result you'd expect here.
>
>> Actually i'm writing library for msgpack protocol and decide to implement it
>> in Typed Racket. Here is my code which did't type check and broke all other
>> program:
>> (define-type Atom (U False True Null))
>> (: atoms (HashTable Byte Atom))
>> (define atoms #hash((#xc0 . ())
>> (#xc3 . #t)
>> (#xc2 . #f)))
>> Error: Type Checker: Expected (HashTable Byte Atom), but got (HashTable
>> Integer Atom) in: #hash((195 . #t) (194 . #f) (192 . ()))
>
> This is a bug. You may be able to work around it by using the `hash`
> function, instead of the literal data constructor.
>
> Sam
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Testing macro helpers

2013-04-07 Thread Eric Dobson
I have already done that in the example, the issue is testing the
syntax it produces when called at phase 0.

On Sun, Apr 7, 2013 at 5:04 AM, Robby Findler
 wrote:
> You can put your helper code in a submodule that you require from the test
> module at phase 0 but from the actual module at phase 1.
>
> Robby
>
>
> On Sun, Apr 7, 2013 at 1:47 AM, Eric Dobson  wrote:
>>
>> The issue with that is that it runs the code (compute) at phase1, when
>> I need to run that code at phase 0, otherwise compiling the module
>> runs the tests. I'm willing to muck with eval and namespaces, so I
>> believe what I want should be possible if difficult.
>>
>>
>> On Sat, Apr 6, 2013 at 11:34 PM, Ryan Culpepper  wrote:
>> > On 04/07/2013 01:24 AM, Eric Dobson wrote:
>> >>
>> >> I am trying to test a helper to a macro. It generates a syntax object
>> >> with bindings at phase-1, this is then returned by the macro and it
>> >> correctly evaluates. Is there a way to not go through the macro, but
>> >> still evaluate the syntax-object with those bindings it has at phase-1
>> >> relative to the helper and not phase 0 relative?
>> >>
>> >> Example code:
>> >> #lang racket/load
>> >>
>> >> (module t racket
>> >>(provide (all-defined-out))
>> >>(define temp 7))
>> >>
>> >> (module stx-compute racket
>> >>(provide (all-defined-out))
>> >>(require (for-template 't))
>> >>;(require 't)
>> >>(define (compute) #'temp))
>> >>
>> >>
>> >> (module stx racket
>> >>(provide (all-defined-out))
>> >>(require (for-syntax 'stx-compute))
>> >>(define-syntax (use-compute stx)
>> >>  (compute)))
>> >>
>> >> (require 'stx)
>> >> (displayln (use-compute))
>> >> (require 'stx-compute)
>> >> (displayln (eval-syntax (compute)))
>> >>
>> >> This fails on the eval-syntax, but succeds if I uncomment the (require
>> >> 't).
>> >
>> >
>> > You might find phase1-eval from unstable/macro-testing helpful.
>> >
>> > (require (for-syntax 'stx-compute)
>> >  unstable/macro-testing))
>> >
>> > (phase1-eval (compute))
>> > ;; => 'temp
>> >
>> > (phase1-eval (compute) #:quote quote-syntax)
>> > ;; => #
>> >
>> > (eval-syntax (phase1-eval (compute) #:quote quote-syntax))
>> > ;; => 7
>> >
>> > Ryan
>> >
>> 
>>   Racket Users list:
>>   http://lists.racket-lang.org/users
>
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Testing macro helpers

2013-04-06 Thread Eric Dobson
The issue with that is that it runs the code (compute) at phase1, when
I need to run that code at phase 0, otherwise compiling the module
runs the tests. I'm willing to muck with eval and namespaces, so I
believe what I want should be possible if difficult.


On Sat, Apr 6, 2013 at 11:34 PM, Ryan Culpepper  wrote:
> On 04/07/2013 01:24 AM, Eric Dobson wrote:
>>
>> I am trying to test a helper to a macro. It generates a syntax object
>> with bindings at phase-1, this is then returned by the macro and it
>> correctly evaluates. Is there a way to not go through the macro, but
>> still evaluate the syntax-object with those bindings it has at phase-1
>> relative to the helper and not phase 0 relative?
>>
>> Example code:
>> #lang racket/load
>>
>> (module t racket
>>(provide (all-defined-out))
>>(define temp 7))
>>
>> (module stx-compute racket
>>(provide (all-defined-out))
>>(require (for-template 't))
>>;(require 't)
>>(define (compute) #'temp))
>>
>>
>> (module stx racket
>>(provide (all-defined-out))
>>(require (for-syntax 'stx-compute))
>>(define-syntax (use-compute stx)
>>  (compute)))
>>
>> (require 'stx)
>> (displayln (use-compute))
>> (require 'stx-compute)
>> (displayln (eval-syntax (compute)))
>>
>> This fails on the eval-syntax, but succeds if I uncomment the (require
>> 't).
>
>
> You might find phase1-eval from unstable/macro-testing helpful.
>
> (require (for-syntax 'stx-compute)
>  unstable/macro-testing))
>
> (phase1-eval (compute))
> ;; => 'temp
>
> (phase1-eval (compute) #:quote quote-syntax)
> ;; => #
>
> (eval-syntax (phase1-eval (compute) #:quote quote-syntax))
> ;; => 7
>
> Ryan
>

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Testing macro helpers

2013-04-06 Thread Eric Dobson
I am trying to test a helper to a macro. It generates a syntax object
with bindings at phase-1, this is then returned by the macro and it
correctly evaluates. Is there a way to not go through the macro, but
still evaluate the syntax-object with those bindings it has at phase-1
relative to the helper and not phase 0 relative?

Example code:
#lang racket/load

(module t racket
  (provide (all-defined-out))
  (define temp 7))

(module stx-compute racket
  (provide (all-defined-out))
  (require (for-template 't))
  ;(require 't)
  (define (compute) #'temp))


(module stx racket
  (provide (all-defined-out))
  (require (for-syntax 'stx-compute))
  (define-syntax (use-compute stx)
(compute)))

(require 'stx)
(displayln (use-compute))
(require 'stx-compute)
(displayln (eval-syntax (compute)))

This fails on the eval-syntax, but succeds if I uncomment the (require 't).

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] C++ FFI

2013-04-04 Thread Eric Dobson
Write a C wrapper. Thats what llvm does, and how my racket bindings use it.

On Wed, Apr 3, 2013 at 8:16 PM, Vehm Stark  wrote:
> This was discussed on the PLT Scheme list back in 2006 but I wonder if
> anyone has any new advice for doing FFI with a C++ library.
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Understanding raco make and its correctness guarantees

2013-03-21 Thread Eric Dobson
I am under the assumption that the compiled directory is just a cache
of the byte code version of the compiled source code, and that racket
should use the source code when ever that cache is invalid. This is
currently not true, as my examples show, yet racket does work to make
it mostly true. So my question is why doesn't racket do the same check
that raco make does?

No I don't want racket to run raco make before requiring the file.
That would update the compiled directories, (which likely isn't
expensive, but not the feature I'm asking about).

On Thu, Mar 21, 2013 at 8:34 AM, Tobias Hammer  wrote:
> You can use a little 'preload' script:
>
> wrap-compile.rkt
> #lang racket
> (require compiler/cm)
> (current-load/use-compiled
>  (make-compilation-manager-load/use-compiled-handler))
>
> and run with racket -t wrap-compile.rkt 
> I would really love to see this functionality as a command line option to
> the console racket
>
> Tobias
>
>
>
>
>
> On Thu, 21 Mar 2013 16:31:51 +0100, Eric Dobson 
> wrote:
>
>> So anyone who just uses the command line tools is out of luck? I like
>> my build system to be correct, and it seems weird that there is work
>> to make sure the right zo file is matched with the source file but
>> that it is incorrect in some simple cases.
>>
>> On Thu, Mar 21, 2013 at 8:19 AM, Robby Findler
>>  wrote:
>>>
>>> Or use DrRacket and turn on the auto compilation feature. Or set up
>>> compiler/cm yourself to do that.
>>>
>>> Robby
>>>
>>>
>>> On Thu, Mar 21, 2013 at 10:16 AM, Eric Dobson 
>>> wrote:
>>>>
>>>>
>>>> That doesn't explain why I can get the same behavior as the macro with
>>>> a function call, probably inlining is responsible for that though.
>>>>
>>>> So the take away is that if I want my running system to represent the
>>>> current state of the source files I either need to run raco make every
>>>> single time, or never run raco make. That seems very counter
>>>> intuitive.
>>>>
>>>> On Thu, Mar 21, 2013 at 1:51 AM, Tobias Hammer 
>>>> wrote:
>>>> > The difference lies in the method how racket and raco make check for
>>>> > changes.
>>>> > - racket only looks at each individual file's timestamp source and .zo
>>>> > timestamp and uses whichever is never.
>>>> > - raco make always checks if the file has changed or any of its
>>>> > dependencies
>>>> > has changed to decide if it has to recompile the file.
>>>> >
>>>> > Whats happens in version 1 is that the macro is already expanded in
>>>> > a.zo
>>>> > and
>>>> > a.zo is still never than a.rkt. Therefore racket loads a.zo (but
>>>> > b.rkt)
>>>> > and
>>>> > prints 'version1.
>>>> > In version 2 raco make (or more correctly compiler/cm) checks a.rkt,
>>>> > finds
>>>> > the dependency to b.rkt, notices the change in b.rkt (by calculating a
>>>> > checksum over the whole dependency tree if i remember correct) and
>>>> > therefore
>>>> > recompiles a.rkt AND b.rkt.
>>>> >
>>>> > Version 3 should as far as i understand never differ, assumed raco
>>>> > make
>>>> > works.
>>>> >
>>>> > Tobias
>>>> >
>>>> >
>>>> >
>>>> > On Thu, 21 Mar 2013 06:23:24 +0100, Eric Dobson
>>>> > 
>>>> > wrote:
>>>> >
>>>> >> I'm trying to understand what are the guarantees that raco make is
>>>> >> meant to provide. I'm going to limit this to simple programs, no
>>>> >> fancy
>>>> >> dynamic requires, or trying to trick the compiler.
>>>> >>
>>>> >> In the following scenario:
>>>> >> 1. Edit files
>>>> >> 2. run 'raco make '
>>>> >> 3. Change files
>>>> >> I expect all of these to have the same effect during the running of
>>>> >> phase 0 when running racket.
>>>> >> 4. racket 
>>>> >> or
>>>> >> 4. raco make 
>>>> >> 5. racket 
>>>> >> or
>>>> >> 4. rm -rf compiled/
>>>> >> 5. raco make 
>>>> >> 6. racket 
>>>>

Re: [racket] Understanding raco make and its correctness guarantees

2013-03-21 Thread Eric Dobson
So anyone who just uses the command line tools is out of luck? I like
my build system to be correct, and it seems weird that there is work
to make sure the right zo file is matched with the source file but
that it is incorrect in some simple cases.

On Thu, Mar 21, 2013 at 8:19 AM, Robby Findler
 wrote:
> Or use DrRacket and turn on the auto compilation feature. Or set up
> compiler/cm yourself to do that.
>
> Robby
>
>
> On Thu, Mar 21, 2013 at 10:16 AM, Eric Dobson 
> wrote:
>>
>> That doesn't explain why I can get the same behavior as the macro with
>> a function call, probably inlining is responsible for that though.
>>
>> So the take away is that if I want my running system to represent the
>> current state of the source files I either need to run raco make every
>> single time, or never run raco make. That seems very counter
>> intuitive.
>>
>> On Thu, Mar 21, 2013 at 1:51 AM, Tobias Hammer 
>> wrote:
>> > The difference lies in the method how racket and raco make check for
>> > changes.
>> > - racket only looks at each individual file's timestamp source and .zo
>> > timestamp and uses whichever is never.
>> > - raco make always checks if the file has changed or any of its
>> > dependencies
>> > has changed to decide if it has to recompile the file.
>> >
>> > Whats happens in version 1 is that the macro is already expanded in a.zo
>> > and
>> > a.zo is still never than a.rkt. Therefore racket loads a.zo (but b.rkt)
>> > and
>> > prints 'version1.
>> > In version 2 raco make (or more correctly compiler/cm) checks a.rkt,
>> > finds
>> > the dependency to b.rkt, notices the change in b.rkt (by calculating a
>> > checksum over the whole dependency tree if i remember correct) and
>> > therefore
>> > recompiles a.rkt AND b.rkt.
>> >
>> > Version 3 should as far as i understand never differ, assumed raco make
>> > works.
>> >
>> > Tobias
>> >
>> >
>> >
>> > On Thu, 21 Mar 2013 06:23:24 +0100, Eric Dobson
>> > 
>> > wrote:
>> >
>> >> I'm trying to understand what are the guarantees that raco make is
>> >> meant to provide. I'm going to limit this to simple programs, no fancy
>> >> dynamic requires, or trying to trick the compiler.
>> >>
>> >> In the following scenario:
>> >> 1. Edit files
>> >> 2. run 'raco make '
>> >> 3. Change files
>> >> I expect all of these to have the same effect during the running of
>> >> phase 0 when running racket.
>> >> 4. racket 
>> >> or
>> >> 4. raco make 
>> >> 5. racket 
>> >> or
>> >> 4. rm -rf compiled/
>> >> 5. raco make 
>> >> 6. racket 
>> >>
>> >> I can make version 1 and 2 differ with the following program:
>> >> a.rkt
>> >> #lang racket
>> >> (require "b.rkt")
>> >> (macro)
>> >>
>> >> #lang racket
>> >> (provide maco)
>> >> (define-syntax (macro stx)
>> >>   #''version1)
>> >>
>> >> Where the edit is changing version1 to version2. I can also replicate
>> >> this with a function and not a macro.
>> >>
>> >> I thought I could make version 2 and version 3 differ, but cannot seem
>> >> to replicate it now, but it would be helpful to know if I see
>> >> something that is fixed by clearing the compiled directories.
>> >>
>> >> My understanding was that racket used the same logic as raco make on
>> >> whether or not to use the compiled versions versus recompiling, and
>> >> this seems to refute that. Can someone give some insight on what I
>> >> should be expecting?
>> >> 
>> >>   Racket Users list:
>> >>   http://lists.racket-lang.org/users
>> >
>> >
>> >
>> > --
>> > -
>> > Tobias Hammer
>> > DLR / Robotics and Mechatronics Center (RMC)
>> > Muenchner Str. 20, D-82234 Wessling
>> > Tel.: 08153/28-1487
>> > Mail: tobias.ham...@dlr.de
>> 
>>   Racket Users list:
>>   http://lists.racket-lang.org/users
>
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Understanding raco make and its correctness guarantees

2013-03-21 Thread Eric Dobson
That doesn't explain why I can get the same behavior as the macro with
a function call, probably inlining is responsible for that though.

So the take away is that if I want my running system to represent the
current state of the source files I either need to run raco make every
single time, or never run raco make. That seems very counter
intuitive.

On Thu, Mar 21, 2013 at 1:51 AM, Tobias Hammer  wrote:
> The difference lies in the method how racket and raco make check for
> changes.
> - racket only looks at each individual file's timestamp source and .zo
> timestamp and uses whichever is never.
> - raco make always checks if the file has changed or any of its dependencies
> has changed to decide if it has to recompile the file.
>
> Whats happens in version 1 is that the macro is already expanded in a.zo and
> a.zo is still never than a.rkt. Therefore racket loads a.zo (but b.rkt) and
> prints 'version1.
> In version 2 raco make (or more correctly compiler/cm) checks a.rkt, finds
> the dependency to b.rkt, notices the change in b.rkt (by calculating a
> checksum over the whole dependency tree if i remember correct) and therefore
> recompiles a.rkt AND b.rkt.
>
> Version 3 should as far as i understand never differ, assumed raco make
> works.
>
> Tobias
>
>
>
> On Thu, 21 Mar 2013 06:23:24 +0100, Eric Dobson 
> wrote:
>
>> I'm trying to understand what are the guarantees that raco make is
>> meant to provide. I'm going to limit this to simple programs, no fancy
>> dynamic requires, or trying to trick the compiler.
>>
>> In the following scenario:
>> 1. Edit files
>> 2. run 'raco make '
>> 3. Change files
>> I expect all of these to have the same effect during the running of
>> phase 0 when running racket.
>> 4. racket 
>> or
>> 4. raco make 
>> 5. racket 
>> or
>> 4. rm -rf compiled/
>> 5. raco make 
>> 6. racket 
>>
>> I can make version 1 and 2 differ with the following program:
>> a.rkt
>> #lang racket
>> (require "b.rkt")
>> (macro)
>>
>> #lang racket
>> (provide maco)
>> (define-syntax (macro stx)
>>   #''version1)
>>
>> Where the edit is changing version1 to version2. I can also replicate
>> this with a function and not a macro.
>>
>> I thought I could make version 2 and version 3 differ, but cannot seem
>> to replicate it now, but it would be helpful to know if I see
>> something that is fixed by clearing the compiled directories.
>>
>> My understanding was that racket used the same logic as raco make on
>> whether or not to use the compiled versions versus recompiling, and
>> this seems to refute that. Can someone give some insight on what I
>> should be expecting?
>> 
>>   Racket Users list:
>>   http://lists.racket-lang.org/users
>
>
>
> --
> -
> Tobias Hammer
> DLR / Robotics and Mechatronics Center (RMC)
> Muenchner Str. 20, D-82234 Wessling
> Tel.: 08153/28-1487
> Mail: tobias.ham...@dlr.de

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Understanding raco make and its correctness guarantees

2013-03-20 Thread Eric Dobson
I'm trying to understand what are the guarantees that raco make is
meant to provide. I'm going to limit this to simple programs, no fancy
dynamic requires, or trying to trick the compiler.

In the following scenario:
1. Edit files
2. run 'raco make '
3. Change files
I expect all of these to have the same effect during the running of
phase 0 when running racket.
4. racket 
or
4. raco make 
5. racket 
or
4. rm -rf compiled/
5. raco make 
6. racket 

I can make version 1 and 2 differ with the following program:
a.rkt
#lang racket
(require "b.rkt")
(macro)

#lang racket
(provide maco)
(define-syntax (macro stx)
  #''version1)

Where the edit is changing version1 to version2. I can also replicate
this with a function and not a macro.

I thought I could make version 2 and version 3 differ, but cannot seem
to replicate it now, but it would be helpful to know if I see
something that is fixed by clearing the compiled directories.

My understanding was that racket used the same logic as raco make on
whether or not to use the compiled versions versus recompiling, and
this seems to refute that. Can someone give some insight on what I
should be expecting?

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Marking dependencies on dynamically required modules.

2013-03-20 Thread Eric Dobson
It looks like these get turned into collects paths automatically (for
paths in collects dir), and other modules that are required normally
have the complete path already in deps and this is no different. So
cm-accomplice will solve my issue.

On Tue, Mar 19, 2013 at 10:29 PM, Eric Dobson  wrote:
> I have two files a.rkt and b.rkt, a.rkt dynamically requires b.rkt at
> syntax time.
>
> a.rkt:
> #lang racket
> (require (for-syntax compiler/cm-accomplice))
>
> (define-syntax (go stx)
>   (dynamic-require "b.rkt" #f)
>   #''success)
>
> (go)
>
> b.rkt:
> #lang racket
>
> (when #f
>   (error 'this-might-break))
>
> If I compile a.rkt with raco make, and then change b.rkt's #f to #t,
> and run a.rkt it prints success. If I then delete a.rkt's compiled
> version and recompile, it raises the error.
>
> I want to be able to declare that a.rkt depends on b.rkt.
>
> It looks like compiler/cm-accomplice was meant to solve this, but it
> requires complete paths, and it seems like that would prevent the
> bytecode from being machine independent. Is this actually a problem,
> or do the paths only affect the .dep files and so distributing
> compiled versions wouldn't be affected?

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Marking dependencies on dynamically required modules.

2013-03-19 Thread Eric Dobson
I have two files a.rkt and b.rkt, a.rkt dynamically requires b.rkt at
syntax time.

a.rkt:
#lang racket
(require (for-syntax compiler/cm-accomplice))

(define-syntax (go stx)
  (dynamic-require "b.rkt" #f)
  #''success)

(go)

b.rkt:
#lang racket

(when #f
  (error 'this-might-break))

If I compile a.rkt with raco make, and then change b.rkt's #f to #t,
and run a.rkt it prints success. If I then delete a.rkt's compiled
version and recompile, it raises the error.

I want to be able to declare that a.rkt depends on b.rkt.

It looks like compiler/cm-accomplice was meant to solve this, but it
requires complete paths, and it seems like that would prevent the
bytecode from being machine independent. Is this actually a problem,
or do the paths only affect the .dep files and so distributing
compiled versions wouldn't be affected?

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Rounding

2013-03-16 Thread Eric Dobson
The docs say that it is round to even, which is the standard for
floating point numbers [1]. You can write your own rounding operator
that rounds .5 up if need be.

;; Untested
(define (my-round n)
  (let ((r (round n))) (if (= .5 (- n r)) (add1 r) r)))

[1] http://en.wikipedia.org/wiki/Rounding#Round_half_to_even

On Sat, Mar 16, 2013 at 7:21 AM, Eric Nadeau  wrote:
> Hi,
>
> I was having problems with a mathematical application I'm writing and 
> narrowed it down to:
>
> (round 0.5)
> (round (/ 1 2))
> (round 1.5)
> (round (/ 3 2))
>
> giving me
>
> 0.0
> 0
> 2.0
> 2
>
> Since when is round 0.5 not giving 1?!
>
> Cheers,
> Eric
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] code review request for LRU code

2013-03-12 Thread Eric Dobson
Why cannot that contract be checked? It seems that since the or/c is
the unwrapping of the parametric contracts it should be possible,
since the first order checks should differentiate them.

On Tue, Mar 12, 2013 at 8:46 PM, Asumu Takikawa  wrote:
> On 2013-03-12 13:16:02 -0700, Danny Yoo wrote:
>> slab:lru dyoo$ racket untyped-client.rkt
>> untyped-client.rkt:10:0: Type Checker: The type of lru-ref cannot be
>> converted to a contract
>>   in: (lru-ref l "greeting")
>>   context...:
>>/Applications/Racket v5.3.2/collects/racket/private/modbeg.rkt:46:4
>>standard-module-name-resolver
>
> I think this boils down to the fact that the following does not work:
>
>   #lang racket
>
>   (module b typed/racket
> (: f (All (A B) (A B -> (U A B
> (define (f x y) x)
> (provide f))
>
>   (require 'b)
>   f
>
> The reason this doesn't work is because this contract does not work:
>
>   #lang racket
>
>   (define/contract (f x y)
> (parametric->/c (A B) (-> A B (or/c A B)))
> x)
>
>   (f 1 "foo")
>
>   ;;;
>
>   f: broke its contract
>two of the clauses in the or/c might both match: B and A
>produced: #
>in: the range of
>...
> (parametric->/c (A B) ...)
>contract from: (function f)
>blaming: (function f)
>at: unsaved-editor160271:3.18
>
> Cheers,
> Asumu
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Typed Racket, Generics Support

2013-03-08 Thread Eric Dobson
I cannot speak for the other TR devs but this is nowhere on my radar,
I'm currently dealing with all the open bugs (and there are a lot of
these).

On Fri, Mar 8, 2013 at 8:31 AM, Ray Racine  wrote:
> The possibility(s) of TR support for Generics is salivating.  Any current
> activity in this direction?  Any ballpark estimate when "experimental"
> availability may drop into Racket?
>
> Thanks,
>
>
> Ray
>
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Racket without thread-local storage?

2013-02-25 Thread Eric Dobson
Racket's thread cells are local to 'Racket Threads', so I doubt that they
map to OS level thread local storage, and thus would work fine.


On Mon, Feb 25, 2013 at 8:07 PM, Neil Toronto wrote:

> On 02/25/2013 06:17 PM, Matthew Flatt wrote:
>
>> At Tue, 26 Feb 2013 01:30:18 +0100, Juan Francisco Cantero Hurtado wrote:
>>
>>> Hi. I have this issue when I compile racket with GCC 4.2 on OpenBSD:
>>>
>>> cd ../foreign; make all
>>> make foreign.lo
>>> /usr/bin/libtool --mode=compile --tag=CC cc -O2 -pipe -g
>>> -I/usr/local/include -I/usr/X11R6/include -DMZ_USE_JIT_SSE
>>> -I/usr/local/include -pthread-DMZ_USES_SHARED_LIB  -I./../racket
>>> -I/usr/ports/pobj/racket-5.3.**3/racket-5.3.3/src/foreign/../**
>>> racket/include
>>> -I/usr/ports/pobj/racket-5.3.**3/racket-5.3.3/src/foreign/../**
>>> racket/src
>>> -c /usr/ports/pobj/racket-5.3.3/**racket-5.3.3/src/foreign/**foreign.c
>>> -o
>>> foreign.lo
>>> cc -O2 -pipe -g -I/usr/local/include -I/usr/X11R6/include
>>> -DMZ_USE_JIT_SSE -I/usr/local/include -pthread -DMZ_USES_SHARED_LIB
>>> -I./../racket
>>> -I/usr/ports/pobj/racket-5.3.**3/racket-5.3.3/src/foreign/../**
>>> racket/include
>>> -I/usr/ports/pobj/racket-5.3.**3/racket-5.3.3/src/foreign/../**
>>> racket/src
>>> -c /usr/ports/pobj/racket-5.3.3/**racket-5.3.3/src/foreign/**foreign.c
>>> -fPIC
>>> -DPIC -o .libs/foreign.o
>>> In file included from
>>> /usr/ports/pobj/racket-5.3.3/**racket-5.3.3/src/foreign/../**
>>> racket/include/scheme.
>>> h:1169,
>>>from
>>> /usr/ports/pobj/racket-5.3.3/**racket-5.3.3/src/foreign/../**
>>> racket/src/schpriv.h:2
>>> 2,
>>>from
>>> /usr/ports/pobj/racket-5.3.3/**racket-5.3.3/src/foreign/**foreign.c:10:
>>> /usr/ports/pobj/racket-5.3.3/**racket-5.3.3/src/foreign/../**
>>> racket/include/schthre
>>> ad.h:483:
>>> error: thread-local storage not supported for this target
>>> Error while executing cc -O2 -pipe -g -I/usr/local/include
>>> -I/usr/X11R6/include -DMZ_USE_JIT_SSE -I/usr/local/include -pthread
>>> -DMZ_USES_SHARED_LIB -I./../racket
>>> -I/usr/ports/pobj/racket-5.3.**3/racket-5.3.3/src/foreign/../**
>>> racket/include
>>> -I/usr/ports/pobj/racket-5.3.**3/racket-5.3.3/src/foreign/../**
>>> racket/src
>>> -c /usr/ports/pobj/racket-5.3.3/**racket-5.3.3/src/foreign/**foreign.c
>>> -fPIC
>>> -DPIC -o .libs/foreign.o
>>> *** Error 2 in foreign (Makefile:43 'foreign.lo')
>>> *** Error 1 in foreign (Makefile:18 'all')
>>> *** Error 1 in racket (Makefile:130 'foreign-stuff')
>>> *** Error 1 in racket (Makefile:76 'common')
>>> *** Error 1 in racket (Makefile:79 'cgc')
>>> *** Error 1 in racket (Makefile:85 '3m')
>>> *** Error 1 in . (Makefile:40 '3m')
>>> *** Error 1 in /usr/ports/pobj/racket-5.3.3/**build-amd64 (Makefile:37
>>> 'all')
>>>
>>>
>>> I know the problem is the lack of support for TLS on OpenBSD/GCC. Now I
>>> compile racket with GCC 4.6 because this version has TLS support
>>> emulated but I think this workaround is adding other bugs to racket. Is
>>> it possible to compile racket in a OS/compiler without thread-local
>>> storage?.
>>>
>>> My configure options: --enable-libffi --enable-gracket --enable-jit
>>> --enable-foreign --enable-places --enable-float --enable-docs
>>> --enable-pthread --enable-lt=/usr/bin/libtool --enable-futures
>>>
>>
>> Thread-local storage is needed for `--enable-places' and
>> `--enable-futures', so you just have to leave those out to compile
>> without it.
>>
>
> What do Racket's thread cells do when Racket is compiled without TLS?
>
> Neil ⊥
>
>
> 
>  Racket Users list:
>  http://lists.racket-lang.org/**users 
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] require/typed to a struct type

2013-02-08 Thread Eric Dobson
Its the same reason that this program gives an error.

#lang racket

(define/contract (f x)
  (my-struct? . -> . number?)
  1)

(struct my-struct ())

(f (my-struct))

Do you expect this to work?

We could replace uses of my-struct? with (lambda (x) (my-struct? x)) but I
wonder if the optimizer will see through that or not (Given that it
actually is delaying the resolution of the variable). Also that doesn't
return true to struct-predicate-procedure? and something might depend on
that for optimizations.


On Fri, Feb 8, 2013 at 8:20 PM, Joe Gibbs Politz  wrote:

> When using require/typed with a struct type in the signature, it seems
> to matter if the require comes before or after the struct definition:
>
> typed.rkt
> =
> #lang typed/racket
>
> (require/typed "untyped.rkt"
>  [number-me (my-struct -> Number)])
>
> (struct: my-struct ())
>
> (number-me (my-struct))
>
> untyped.rkt
> =
> #lang racket/base
>
> (provide number-me)
> (define (number-me _) 52)
>
>
> Running typed.rkt yields:
>
> my-struct?: undefined;
>  cannot reference an identifier before its definition
>
> But, if I change typed.rkt to
>
> typed2.rkt
> =
> #lang typed/racket
>
> (struct: my-struct ())
>
> (require/typed "untyped.rkt"
>  [number-me (my-struct -> Number)])
>
> (number-me (my-struct))
>
>
> There's no error and the call succeeds.
>
> Any reason for the ordering dependency?  It's quite inconvenient to be
> shuffling require statements around as soon as you want to put a
> struct in their type, so I'm assuming this wasn't intended.
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Module Availability

2013-02-08 Thread Eric Dobson
Thats what I thought it was, but I cannot replicate with the following
program.

secret.rkt
#lang racket/base
(provide (all-defined-out))
(define secret 1)

secret-stx.rkt:
#lang racket/base

(require (for-template "secret.rkt"))
(provide use-secret)

(define (use-secret) #'secret)

secret-user.rkt
#lang racket/base
(require (for-syntax racket/base))

(define-syntax (go stx)
  (define use-secret (dynamic-require "secret-stx.rkt" 'use-secret))
  (use-secret))

(go)

The resulting program from compiling secret-user shouldn't have any
dependency on secret, yet it works.



On Fri, Feb 8, 2013 at 9:35 AM, Sam Tobin-Hochstadt wrote:

> On Fri, Feb 8, 2013 at 12:26 PM, Eric Dobson 
> wrote:
> > I'm trying to understand why TR requires a module that it doesn't
> actually
> > use in the file that requires it.
> >
> > The requirement is unstable/contract in typed-racket/type-racket.rkt.
> > Without it tests/typed-racket/succeed/succed-cnt.rkt fails. But when I
> try
> > to reduce the test case to not use TR, I cannot replicate it.
>
> This require is there so that the residual program is correct.  The
> basic issue is that Typed Racket dynamically loads large parts of its
> implementation, some of which is used to produce the residual code in
> the expansion of a TR module (such as contracts, which can use
> `unstable/contract`).  However, the resulting program must contain a
> dependency on `unstable/contract`, otherwise Racket can't figure out
> how to evaluate the reference. Therefore, the part of TR that is *not*
> dynamically loaded needs to declare a static dependency on
> `unstable/contract`.
>
> Sam
>

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Module Availability

2013-02-08 Thread Eric Dobson
I'm trying to understand why TR requires a module that it doesn't actually
use in the file that requires it.

The requirement is unstable/contract in typed-racket/type-racket.rkt.
Without it tests/typed-racket/succeed/succed-cnt.rkt fails. But when I try
to reduce the test case to not use TR, I cannot replicate it.

The error message:

link: namespace mismatch;
 reference to a module that is not available
  reference phase: 0
  referenced module:
"/Users/endobson/proj/racket/plt/collects/unstable/contract.rkt"
  referenced phase level: 0
  reference in module:
"/Users/endobson/proj/racket/plt/collects/tests/typed-racket/succeed/succeed-cnt2.rkt"
  in: provide/contract-contract-id-sequence/c.24

Can someone explain what this error means/how to write a simple program
which triggers it?

  Racket Users list:
  http://lists.racket-lang.org/users


Re: [racket] Typed Racket procedure wrapping?

2013-02-05 Thread Eric Dobson
I don't get why TR should use a custom contract instead of case-> providing
better error messages.

You get the same error message with:

#lang racket

(define/contract (f) (case->) 2)
(f 2)
;(f)


On Tue, Feb 5, 2013 at 8:10 PM, Matthias Felleisen wrote:

>
> On Feb 5, 2013, at 4:17 PM, Asumu Takikawa wrote:
>
> >>
> >>   In 5.3.2, when running untyped.rkt, I get:
> >>
> >>   #: arity mismatch;
> >>the expected number of arguments does not match the given number
> >> given: 1
> >> arguments...:
> >>  5
> >
> > I agree that this error message is bad. It's a result of changing the
> > type `Procedure` to use the contract `(case->)`. Maybe we should use a
> > custom contract here instead that produces a better error message.
>
>
> AMEN!
> 
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

  Racket Users list:
  http://lists.racket-lang.org/users


[racket] Understanding future syncronization

2012-12-31 Thread Eric Dobson
I am playing around with futures currently, and am trying to understand how
to make my programs faster. The future visualizer is wonderful in
understanding whats going on, but it lead me down a track which made my
program slower.

The issue is that void primitive needs to synchronize with the main thread.
So when I was using it to provide the return value from my function it was
synchronizing. I thought that not calling a function and just referencing a
module-level constant would solve this synchronization issue. And it did,
but it made the program slower.

What is the rule of thumb for understanding how expensive synchronization
is, I assumed it was at least the cost of a couple atomic operations? Also
can someone explain why void needs to synchronize with the main thread?


My (reduced) program:
#lang racket

(define void-t (void))
#;
(define-syntax-rule (my-void arg)
  (begin
arg
void-t))
(define my-void void)


(define (run)
  (define N 100)
  (define v (box #f))
  (define (f)
(future
  (thunk
(for ((i (in-range N)))
  (my-void (box-cas! v (unbox v) i))
  (define f1 (f))
  (define f2 (f))
  (touch f1)
  (touch f2)
  (values))



(define (time-it)
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run))
  (collect-garbage)
  (time (run)))

#|
(time-it)
|#


(require future-visualizer)
(visualize-futures
  (run)
  #f)

  Racket Users list:
  http://lists.racket-lang.org/users


  1   2   >