Re: [racket-users] Struct general question

2018-03-30 Thread Alexander McLin
As an aside, vectors are intended to be a constant-time access data 
structure. Lists aren't so it won't make sense to build vectors on top of 
lists which would make for non-constant slot access times.


On Monday, March 26, 2018 at 4:13:02 PM UTC-4, lysseus wrote:
>
>
>
> > On Mar 26, 2018, at 11:03 AM, Eric Griffis  > wrote: 
> > 
> > The `struct` form is defined in `struct.rkt` [1]. As you can see, 
> `struct` wraps `define-struct/derived` [2], which uses many things exported 
> from `struct.c` [3]. The "Inside: Racket C API" doc [5] describes some of 
> these functions -- see section 16. 
> > 
> > On the matter of structs being essentially vectors: Again in `struct.c` 
> [3], the C function `scheme_make_struct_instance` (line 2388) instantiates 
> a `Scheme_Structure`, which is declared in `schpriv.h` [4], line 1113. The 
> field values go into a `slots` array, which points directly to the 
> `Scheme_Object`s (i.e. arbitrary Racket values) supplied to 
> `scheme_make_struct_instance`. Assuming Racket vectors are essentially C 
> arrays, Racket structs are like vectors in this way. 
> > 
> > Looking at `Scheme_Struct_Type`, defined just above `Scheme_Structure` 
> in `schpriv.h` [4], it's clear there's nowhere to store field names. We can 
> confirm this by looking at `_make_struct_type` in `struct.c` [3], starting 
> at line 4778, as this is the function used (indirectly) by 
> `define-struct/derived`. 
> > 
> > Eric 
>
> Thanks, Eric! So maybe the solution for that would not be rewriting the 
> struct definition altogether, but providing that additional information in 
> something equivalent to a le over lambda, which would contain that meta 
> information? 
>
> I ran into an issue that felt similar to this when I was trying to pass 
> method names around in Racket objects. I’ve no idea, but I would think the 
> “solution” to both issues would be similar. Admittedly, I’ve not dug down 
> into either structs or objects deep enough. 
>
> Kevin 
>
>

-- 
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] Struct general question

2018-03-26 Thread Kevin Forchione


> On Mar 26, 2018, at 11:03 AM, Eric Griffis  wrote:
> 
> The `struct` form is defined in `struct.rkt` [1]. As you can see, `struct` 
> wraps `define-struct/derived` [2], which uses many things exported from 
> `struct.c` [3]. The "Inside: Racket C API" doc [5] describes some of these 
> functions -- see section 16.
> 
> On the matter of structs being essentially vectors: Again in `struct.c` [3], 
> the C function `scheme_make_struct_instance` (line 2388) instantiates a 
> `Scheme_Structure`, which is declared in `schpriv.h` [4], line 1113. The 
> field values go into a `slots` array, which points directly to the 
> `Scheme_Object`s (i.e. arbitrary Racket values) supplied to 
> `scheme_make_struct_instance`. Assuming Racket vectors are essentially C 
> arrays, Racket structs are like vectors in this way.
> 
> Looking at `Scheme_Struct_Type`, defined just above `Scheme_Structure` in 
> `schpriv.h` [4], it's clear there's nowhere to store field names. We can 
> confirm this by looking at `_make_struct_type` in `struct.c` [3], starting at 
> line 4778, as this is the function used (indirectly) by 
> `define-struct/derived`.
> 
> Eric

Thanks, Eric! So maybe the solution for that would not be rewriting the struct 
definition altogether, but providing that additional information in something 
equivalent to a le over lambda, which would contain that meta information? 

I ran into an issue that felt similar to this when I was trying to pass method 
names around in Racket objects. I’ve no idea, but I would think the “solution” 
to both issues would be similar. Admittedly, I’ve not dug down into either 
structs or objects deep enough. 

Kevin

-- 
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] Struct general question

2018-03-26 Thread Kevin Forchione


> On Mar 26, 2018, at 9:36 AM, Jens Axel Søgaard  wrote:
> 
> 2018-03-26 17:58 GMT+02:00 Kevin Forchione  >:
> In another thread on structs it was confirmed that structs are in essence a 
> vector with some fancy bindings associated. But there must be more going on, 
> for instance, the definition (struct foo (A B)) creates a function foo that 
> when applied does not print like a vector. Is there any documentation 
> instructs that does into the details of what they are and are doing under the 
> hood?
> 
> FWIW here is my way of implementing structs in JavaScript. I assume the C 
> implementation 
> of structs is similar.
> 
> https://github.com/soegaard/urlang/blob/master/compiler-rjs/runtime.rkt#L1730 
> 
> 
> The most difficult part to implement is structs with supers.
> 
> The C implementation is here:
> 
> https://github.com/racket/racket/blob/master/racket/src/racket/src/struct.c 
> 
> 
> 
> /Jens Axel
> 
Thanks! That’s a fascinating artifact. I hadn’t realized structs went down to C 
code. I’d assumed they were built from Racket vectors, which in  turn were 
built on Racket lists, built on Racket pairs, with pairs maybe in C. LOL. 
Probably not the efficient way to do it, which is why it’s in C? 

Kevin

-- 
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] Struct general question

2018-03-26 Thread Jens Axel Søgaard
And here is the racketscript implemenation of structs (also in JavaScript).

https://github.com/vishesh/racketscript/blob/master/racketscript-compiler/racketscript/compiler/runtime/core/struct.js

2018-03-26 18:36 GMT+02:00 Jens Axel Søgaard :

> 2018-03-26 17:58 GMT+02:00 Kevin Forchione :
>
>> In another thread on structs it was confirmed that structs are in essence
>> a vector with some fancy bindings associated. But there must be more going
>> on, for instance, the definition (struct foo (A B)) creates a function foo
>> that when applied does not print like a vector. Is there any documentation
>> instructs that does into the details of what they are and are doing under
>> the hood?
>>
>
> FWIW here is my way of implementing structs in JavaScript. I assume the C
> implementation
> of structs is similar.
>
> https://github.com/soegaard/urlang/blob/master/compiler-
> rjs/runtime.rkt#L1730
>
> The most difficult part to implement is structs with supers.
>
> The C implementation is here:
>
> https://github.com/racket/racket/blob/master/racket/src/
> racket/src/struct.c
>
>
> /Jens Axel
>
>


-- 
-- 
Jens Axel Søgaard

-- 
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] Struct general question

2018-03-26 Thread Jens Axel Søgaard
2018-03-26 17:58 GMT+02:00 Kevin Forchione :

> In another thread on structs it was confirmed that structs are in essence
> a vector with some fancy bindings associated. But there must be more going
> on, for instance, the definition (struct foo (A B)) creates a function foo
> that when applied does not print like a vector. Is there any documentation
> instructs that does into the details of what they are and are doing under
> the hood?
>

FWIW here is my way of implementing structs in JavaScript. I assume the C
implementation
of structs is similar.

https://github.com/soegaard/urlang/blob/master/compiler-rjs/runtime.rkt#L1730

The most difficult part to implement is structs with supers.

The C implementation is here:

https://github.com/racket/racket/blob/master/racket/src/racket/src/struct.c


/Jens Axel

-- 
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] Struct general question

2018-03-26 Thread David Storrs
There's this: 
https://docs.racket-lang.org/reference/printing.html#%28part._print-structure%29
  Here's an example:

#lang racket
(struct foo (A B) #:transparent)(define transp (foo 7 8))
(struct bar (A B) #:prefab)(define pref  (bar 7 8))
(struct baz (A B))  (define normal (baz 7 8))

(parameterize ([print-struct #t]) ; default
  (for ([f (list displayln println writeln)])
(display (~a (object-name f) ", normal: ")) (f normal)
(display (~a (object-name f) ", transparent: ")) (f transp)
(display (~a (object-name f) ", prefab : ")) (f pref)))
(displayln "\n--- print-struct becomes #f")
(parameterize ([print-struct #f])
  (for ([f (list displayln println writeln)])
(display (~a (object-name f) ", normal: ")) (f normal)
(display (~a (object-name f) ", transparent: ")) (f transp)
(display (~a (object-name f) ", prefab : ")) (f pref)))



And the output:

displayln, normal: #
displayln, transparent: #(struct:foo 7 8)
displayln, prefab : #s(bar 7 8)
println, normal: #
println, transparent: (foo 7 8)
println, prefab : '#s(bar 7 8)
writeln, normal: #
writeln, transparent: #(struct:foo 7 8)
writeln, prefab : #s(bar 7 8)

--- print-struct becomes #f
displayln, normal: #
displayln, transparent: #
displayln, prefab : #
println, normal: #
println, transparent: #
println, prefab : #
writeln, normal: #
writeln, transparent: #
writeln, prefab : #

On Mon, Mar 26, 2018 at 11:58 AM, Kevin Forchione  wrote:
> In another thread on structs it was confirmed that structs are in essence a 
> vector with some fancy bindings associated. But there must be more going on, 
> for instance, the definition (struct foo (A B)) creates a function foo that 
> when applied does not print like a vector. Is there any documentation 
> instructs that does into the details of what they are and are doing under the 
> hood?
>
> Kevin
>
> --
> 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.