[racket] TypedRacket Exec Structures

From: Ray Racine (ray.racine at gmail.com)
Date: Sun Jun 3 08:29:20 EDT 2012

One option is to avoid the struct: approach and go with the following Lens
definition.

(define-type Lens (All (A B) (case->  (A -> B) (B A -> A))))

Then along the lines below works.

(: mod (All (A B) (Lens A B) (B -> B) A -> A))
(define (mod lens f a)
  (lens (f (lens a)) a))


On Sat, Jun 2, 2012 at 7:27 PM, Ray Racine <ray.racine at gmail.com> wrote:

> I'd like to create a Lens structure which is a procedure structure.  The
> structures exec procedure would be a case-lambda parameterized by A, B.
>
> (define-struct/exec:  (A B) Lens ([getter : (A -> B)][setter : (B A ->
> A)]) [fn : (case-> (A -> B) (B A -> A))])
>
> where fn would be the getter and setter detemined by arity at the call
> site.
>
> Looks like currently a define-struct/exec: cannot be parameterized.  Is
> this something that might be available down the road?
>
> The goal is to say
>
> (int-lens 3 (int-lens 2))
>
> as opposed to say
>
> (set int-lens 3 (get int-lens 2))
>
>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> ;; Below is current effort.  Want to do a define-struct/exec
>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>
> (struct: (A B) Lens ([getter : (A -> B)]
>                      [setter : (B A -> A)]))
>
> (: get (All (A B) (Lens A B) A -> B))
> (define (get lens a)
>   ((Lens-getter lens) a))
>
> (: set (All (A B) (Lens A B) B A -> A))
> (define (set lens b a)
>   ((Lens-setter lens) b a))
>
> (: mod (All (A B) (Lens A B) (B -> B) A -> A))
> (define (mod lens f a)
>   (set lens (f (get lens a)) a))
>
> (: and-then (All (A B C) (Lens A B) (Lens B C) -> (Lens A C)))
> (define (and-then lens-ab lens-bc)
>   (Lens (λ: ((a : A))
>           ((Lens-getter lens-bc) ((Lens-getter lens-ab) a)))
>         (λ: ((c : C) (a : A))
>           (mod lens-ab (λ: ((b : B)) ((Lens-setter lens-bc) c b)) a))))
>
> (: compose (All (A B C) (Lens B C) (Lens A B) -> (Lens A C)))
> (define (compose lens-ab lens-ca)
>   (and-then lens-ca lens-ab))
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120603/dcb8df9f/attachment-0001.html>

Posted on the users mailing list.