Re: [racket-users] ask the type of a value (partial solution)

2020-08-03 Thread Alexis King
> On Aug 3, 2020, at 11:48, Hendrik Boom  wrote:
> 
> Still, it would be nice to find out how to get this information more 
> directly instead of relying on functions that do much more than 
> what I was asking for.

In general, there aren’t any. The ability of struct->vector to extract a 
symbolic name for any value really is specific to struct->vector (and it’s only 
really useful for debugging, for the reason Ryan points out).

If you only care about bona fide structs, not primitive values like strings and 
ports, you can do a little better using struct-info:

(define (type-name v)
  (match/values (struct-info v)
[(#f _) #f]
[((app struct-type-info name _ _ _ _ _ _ _) _) name]))

(struct point (x y) #:transparent)
(type-name (point 1 2)) ; => 'point

But this doesn’t work for non-transparent structs unless you own a sufficiently 
powerful inspector:

(let ()
  (struct point (x y))
  (type-name (point 1 2))) ; => #f

(let ()
  (struct point (x y)
#:inspector (make-inspector))
  (type-name (point 1 2))) ; => 'point

But again, this just gets you a symbolic name, which is subject to the same 
issue Ryan points out. The value you get back from struct-info is truly unique 
to that particular type, so it’s a more useful value than the symbolic name. 
But again, it’s not clear to me why you’d want to do this (except for 
debugging/instrumentation), since you still can’t possibly do anything useful 
with a value of a truly unknown type.

On the other hand, if it *is* for debugging, then meddling with the inspector 
is a valid thing to do (and is in fact one of the main reasons inspectors 
exist). You can change current-inspector to a weaker inspector during the 
instantiation of the modules you want to debug, and then you can use 
struct-info to get information about any values of the struct types they create.

Alexis

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/5CAB018A-6F47-4FBA-A249-6A7472D8A896%40gmail.com.


Re: [racket-users] ask the type of a value

2020-08-03 Thread Ryan Culpepper
but beware, for

  > (for-each
 (lambda (x) (printf "~a: ~a\n" (~v x #:min-width 28) (struct->vector
x)))
 (list "hello" (let () (struct string (n)) (string 5
  "hello" : #(struct:string ...)
  #   : #(struct:string ...)

Ryan


On Mon, Aug 3, 2020 at 6:07 PM Laurent  wrote:

> and yet:
>
> > (for-each
>(λ (x) (printf "~a: ~a\n" (~v x #:min-width 28) (struct->vector x)))
>`(hello "hello" 43110 #f #(h e l l o) #"hello" (h e l l o)
>,(new frame% [label ""])))
>
> hello   : #(struct:symbol ...)
> "hello" : #(struct:string ...)
> 43110   : #(struct:fixnum-integer ...)
> #f  : #(struct:false ...)
> #(h e l l o): #(struct:vector ...)
> #"hello": #(struct:byte-string ...)
> (h e l l o) : #(struct:pair ...)
> #(struct:object:frame% ...) : #(struct:object:frame% ...)
>
> ;)
>
>
> On Mon, Aug 3, 2020 at 4:20 PM Alexis King  wrote:
>
>> In general, the answer is “no,” mostly because it’s not clear in Racket
>> what “the type of a value” means. It’s difficult to come up with a
>> definition that would be useful enough to satisfy your criterion of not
>> “having to guess it first.” Consider: suppose you had a hypothetical
>> `type-of` operation that returned a value’s type:
>>
>> (type-of 'hello) ; => 'symbol
>> (type-of "hello") ; => 'string
>>
>> Now suppose you apply type-of to a value you know nothing about and get
>> back 'widget. Well, you don’t know what a widget is, so that didn’t
>> actually get you any further from where you started: you still don’t know
>> what to do with the value.
>>
>> Furthermore, types are not actually disjoint. You can write a struct
>> definition with properties like prop:procedure and prop:evt, and if a
>> struct has both, should type-of return the struct’s name, 'procedure, or
>> 'evt? It’s not really clear. You might be able to come up with some more
>> sophisticated system with (potentially multiple) inheritance that could
>> capture these relationships, but I can say with confidence that no such
>> system currently exists in Racket.
>>
>> Alexis
>>
>> > On Aug 3, 2020, at 09:19, Hendrik Boom  wrote:
>> >
>> > In plain, untyped Racket:
>> >
>> > It is possible to ask if a value is a symbol with symbol?
>> > It is possible to ask if a value is a string with string?
>> > etc.
>> >
>> > Is there a way to ask the type of a value without having to guess it
>> first?
>> >
>> > -- hendrik
>>
>> --
>> 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.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/racket-users/C561F2A9-0C78-4EDA-A401-4FB067D79849%40gmail.com
>> .
>>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/CABNTSaGUiZWoU9mZbqMjB04FSu_kFOGrgyv0riU8TuKtffB_dg%40mail.gmail.com
> 
> .
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CANy33qnV3CNH3eGpVZ59qx%2BZQfjmaJ%3D9gSBfk_TY6aSx5F4k1w%40mail.gmail.com.


Re: [racket-users] ask the type of a value (partial solution)

2020-08-03 Thread Hendrik Boom
On Mon, Aug 03, 2020 at 05:06:51PM +0100, Laurent wrote:
> and yet:
> 
> > (for-each
>(λ (x) (printf "~a: ~a\n" (~v x #:min-width 28) (struct->vector x)))
>`(hello "hello" 43110 #f #(h e l l o) #"hello" (h e l l o)
>,(new frame% [label ""])))
> 
> hello   : #(struct:symbol ...)
> "hello" : #(struct:string ...)
> 43110   : #(struct:fixnum-integer ...)
> #f  : #(struct:false ...)
> #(h e l l o): #(struct:vector ...)
> #"hello": #(struct:byte-string ...)
> (h e l l o) : #(struct:pair ...)
> #(struct:object:frame% ...) : #(struct:object:frame% ...)
> 
> ;)

Thank you.

That itself is already useful.
struct->vector combined with printf handles my immediate application.
It told me #(struct:object:image-snip% ...)
As I've been warned, it doesn't tell me much, but the result is a 
useful clue for further investigation.

It turns out to react positively to image?

Still, it would be nice to find out how to get this information more 
directly instead of relying on functions that do much more than 
what I was asking for.

-- hendrik

> 
> 
> On Mon, Aug 3, 2020 at 4:20 PM Alexis King  wrote:
> 
> > In general, the answer is “no,” mostly because it’s not clear in Racket
> > what “the type of a value” means. It’s difficult to come up with a
> > definition that would be useful enough to satisfy your criterion of not
> > “having to guess it first.” Consider: suppose you had a hypothetical
> > `type-of` operation that returned a value’s type:
> >
> > (type-of 'hello) ; => 'symbol
> > (type-of "hello") ; => 'string
> >
> > Now suppose you apply type-of to a value you know nothing about and get
> > back 'widget. Well, you don’t know what a widget is, so that didn’t
> > actually get you any further from where you started: you still don’t know
> > what to do with the value.
> >
> > Furthermore, types are not actually disjoint. You can write a struct
> > definition with properties like prop:procedure and prop:evt, and if a
> > struct has both, should type-of return the struct’s name, 'procedure, or
> > 'evt? It’s not really clear. You might be able to come up with some more
> > sophisticated system with (potentially multiple) inheritance that could
> > capture these relationships, but I can say with confidence that no such
> > system currently exists in Racket.
> >
> > Alexis
> >
> > > On Aug 3, 2020, at 09:19, Hendrik Boom  wrote:
> > >
> > > In plain, untyped Racket:
> > >
> > > It is possible to ask if a value is a symbol with symbol?
> > > It is possible to ask if a value is a string with string?
> > > etc.
> > >
> > > Is there a way to ask the type of a value without having to guess it
> > first?
> > >
> > > -- hendrik
> >
> > --
> > 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.
> > To view this discussion on the web visit
> > https://groups.google.com/d/msgid/racket-users/C561F2A9-0C78-4EDA-A401-4FB067D79849%40gmail.com
> > .
> >

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/20200803164843.if7wggbketi2ziyl%40topoi.pooq.com.


Re: [racket-users] ask the type of a value

2020-08-03 Thread Laurent
and yet:

> (for-each
   (λ (x) (printf "~a: ~a\n" (~v x #:min-width 28) (struct->vector x)))
   `(hello "hello" 43110 #f #(h e l l o) #"hello" (h e l l o)
   ,(new frame% [label ""])))

hello   : #(struct:symbol ...)
"hello" : #(struct:string ...)
43110   : #(struct:fixnum-integer ...)
#f  : #(struct:false ...)
#(h e l l o): #(struct:vector ...)
#"hello": #(struct:byte-string ...)
(h e l l o) : #(struct:pair ...)
#(struct:object:frame% ...) : #(struct:object:frame% ...)

;)


On Mon, Aug 3, 2020 at 4:20 PM Alexis King  wrote:

> In general, the answer is “no,” mostly because it’s not clear in Racket
> what “the type of a value” means. It’s difficult to come up with a
> definition that would be useful enough to satisfy your criterion of not
> “having to guess it first.” Consider: suppose you had a hypothetical
> `type-of` operation that returned a value’s type:
>
> (type-of 'hello) ; => 'symbol
> (type-of "hello") ; => 'string
>
> Now suppose you apply type-of to a value you know nothing about and get
> back 'widget. Well, you don’t know what a widget is, so that didn’t
> actually get you any further from where you started: you still don’t know
> what to do with the value.
>
> Furthermore, types are not actually disjoint. You can write a struct
> definition with properties like prop:procedure and prop:evt, and if a
> struct has both, should type-of return the struct’s name, 'procedure, or
> 'evt? It’s not really clear. You might be able to come up with some more
> sophisticated system with (potentially multiple) inheritance that could
> capture these relationships, but I can say with confidence that no such
> system currently exists in Racket.
>
> Alexis
>
> > On Aug 3, 2020, at 09:19, Hendrik Boom  wrote:
> >
> > In plain, untyped Racket:
> >
> > It is possible to ask if a value is a symbol with symbol?
> > It is possible to ask if a value is a string with string?
> > etc.
> >
> > Is there a way to ask the type of a value without having to guess it
> first?
> >
> > -- hendrik
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/C561F2A9-0C78-4EDA-A401-4FB067D79849%40gmail.com
> .
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CABNTSaGUiZWoU9mZbqMjB04FSu_kFOGrgyv0riU8TuKtffB_dg%40mail.gmail.com.


Re: [racket-users] ask the type of a value

2020-08-03 Thread Alexis King
In general, the answer is “no,” mostly because it’s not clear in Racket what 
“the type of a value” means. It’s difficult to come up with a definition that 
would be useful enough to satisfy your criterion of not “having to guess it 
first.” Consider: suppose you had a hypothetical `type-of` operation that 
returned a value’s type:

(type-of 'hello) ; => 'symbol
(type-of "hello") ; => 'string

Now suppose you apply type-of to a value you know nothing about and get back 
'widget. Well, you don’t know what a widget is, so that didn’t actually get you 
any further from where you started: you still don’t know what to do with the 
value.

Furthermore, types are not actually disjoint. You can write a struct definition 
with properties like prop:procedure and prop:evt, and if a struct has both, 
should type-of return the struct’s name, 'procedure, or 'evt? It’s not really 
clear. You might be able to come up with some more sophisticated system with 
(potentially multiple) inheritance that could capture these relationships, but 
I can say with confidence that no such system currently exists in Racket.

Alexis

> On Aug 3, 2020, at 09:19, Hendrik Boom  wrote:
> 
> In plain, untyped Racket:
> 
> It is possible to ask if a value is a symbol with symbol?
> It is possible to ask if a value is a string with string?
> etc.
> 
> Is there a way to ask the type of a value without having to guess it first?
> 
> -- hendrik

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/C561F2A9-0C78-4EDA-A401-4FB067D79849%40gmail.com.


Re: [racket-users] ask the type of a value

2020-08-03 Thread Laurent
Maybe try `struct->vector`, which works on any value iirc (from mobile,
can't test)

On Mon, Aug 3, 2020, 15:19 Hendrik Boom  wrote:

> In plain, untyped Racket:
>
> It is possible to ask if a value is a symbol with symbol?
> It is possible to ask if a value is a string with string?
> etc.
>
> Is there a way to ask the type of a value without having to guess it first?
>
> -- hendrik
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/20200803141931.t4v4nc2e5d4afx6f%40topoi.pooq.com
> .
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CABNTSaEcetXOrk77n1RdFKDteQm_sp-8w8VN7qB54qpi-jMs5A%40mail.gmail.com.