Re: What is a form, exactly? + Are there any guarantees about the return types of quote and syntax-quote?

2010-02-16 Thread Garth Sheldon-Coulson
Dear Meikel and Michal,

Thanks for your replies.

Meikel is right in observing that my question makes very little sense out of
context.

The reason I'm asking which Java classes can hold Clojure expressions is
that Clojuratica includes a Clojure s-expression reader, and I'm trying to
refine it. The reader allows the user to express Mathematica expressions in
Clojure syntax; for instance, the user can write the Mathematica expression
Apply[Plus, {1, 2}] as (Apply Plus [1 2]), and Clojuratica will evaluate
that expression in Mathematica even though the rest of the program is
written in Clojure. My question arises because I allow the user to unquote
*Clojure* data structures within the Clojure-syntax Mathematica expressions.
For instance, if x is the Clojure vector [1 2], I allow the user to write
(Apply Plus ~x), which evaluates to the same thing as the expression above.
But now suppose x is not a vector but a list (1 2). When the user writes
(Apply Plus ~x), I have to decide whether the list should be treated as an
expression (function 1, argument 2) or a data structure. I want the user to
be able to achieve either goal: unquote the expression (1 2) (like a macro),
or unquote the data (1 2). Hence I was trying to use the underlying Java
type to differentiate between x's that are expressions and x's that are
data: perhaps data if it's a lazy-seq, and expression if it's a cons or a
list.

However, your replies have made me realize that this approach is a mistake.
Instead, I need to decide whether x is an expression or data on the basis of
its context within the larger expression, not on the basis of its type.
That's the way Clojure does it: the seq (1 2) is always an expression,
regardless of its concrete type, unless it's quoted. So, I need to be more
precise about quoting and unquoting in Clojuratica. What I think I've
decided on is something like the following:

(def x '(1 2))

;in Clojuratica:
(Apply Plus ~x)   ; unquotes x as an expression: 1 is a function, 2 is the
argument

(Apply Plus ~~x)   ; unquotes x as data: treats x as the list (1 2)

or something along those lines. I have to try to make it congruent with
Clojure's quoting and unquoting conventions.

Thanks for your input. Your criticism of my premises made me rethink my
approach in a very constructive way.

Garth



On Mon, Feb 15, 2010 at 3:40 PM, Meikel Brandmeyer m...@kotka.de wrote:

 Hi,

 On Sun, Feb 14, 2010 at 08:58:56PM -0500, Garth Sheldon-Coulson wrote:

  (Revised version: What characterizes an expression? Is it correct to say
  that an expression is always something for which seq? returns true and
 never
  something for which seq? returns false?)

 I don't think so. {:a :b} is just an expression (which evaluates to a
 map) as is 5 (evaluating to 5) as is x (evaluating to whatever x is
 bound to) as is (inc 1) (evaluating to 2).

  And please consider the other questions revised as appropriate. In
  particular, please restrict my question about quote and syntax-quote to
  situations where they quote a form demarcated by parens. I would still
 like
  to know whether there is anything certain about the types returned by
 quote
  and syntax-quote when they are called on a paren-demacated form.

 As Clojure is based on abstractions everything relying on specific types
 should be questioned. Especially since in the future reify may kill that
 idea completely.

 My (certainly limited) experience with this kind of questions is: Why do
 you need to know? There may be perfectly valid reasons. Then I would go
 with seq?.

 Sincerely
 Meikel

 --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.comclojure%2bunsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en


-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: What is a form, exactly? + Are there any guarantees about the return types of quote and syntax-quote?

2010-02-16 Thread Meikel Brandmeyer
Hi,

On Feb 16, 11:26 pm, Garth Sheldon-Coulson g...@mit.edu wrote:


 The reason I'm asking which Java classes can hold Clojure expressions is
 --context snipped---
 list.

Ok. I understand your problem. You have maction - a mix between macro
and function - and you are not sure when to evaluate something
unquoted (function behaviour) and when not (macro behaviour). You
problem arises because you want to allow something like this (silly
example):

(def x '(Apply Plus [1 2]))
(Apply Plus ~x)

I suppose, the Apply call is contained in some wrapper macro?
(mathematica (Apply Plus ~x))? You could provide a mathematica*
function which does the work. Then the user can choose:

(def x '(Apply Plus [1 29])
(def y (list 1 2))

(mathematica (Apply Plus ~(mathematica* x) ~y))

 What I think I've decided on is something like the following:

 (def x '(1 2))

 ;in Clojuratica:
 (Apply Plus ~x)   ; unquotes x as an expression: 1 is a function, 2 is the
 argument

 (Apply Plus ~~x)   ; unquotes x as data: treats x as the list (1 2)

 or something along those lines. I have to try to make it congruent with
 Clojure's quoting and unquoting conventions.

This hijacks a little bit the unquoting. I don't know in how far this
will be of relevance, but if you use something like this in a macro,
you might run into trouble because suddenly the unquoting doesn't
match anymore. However this is only a rather fuzzy fear. You should do
some tests.

Basically it's the same idea as I proposed above: let the user specify
what he wants.

 Thanks for your input. Your criticism of my premises made me rethink my
 approach in a very constructive way.

You are welcome. :)

Sincerely
Meikel

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: What is a form, exactly? + Are there any guarantees about the return types of quote and syntax-quote?

2010-02-15 Thread Meikel Brandmeyer
Hi,

On Sun, Feb 14, 2010 at 08:58:56PM -0500, Garth Sheldon-Coulson wrote:

 (Revised version: What characterizes an expression? Is it correct to say
 that an expression is always something for which seq? returns true and never
 something for which seq? returns false?)

I don't think so. {:a :b} is just an expression (which evaluates to a
map) as is 5 (evaluating to 5) as is x (evaluating to whatever x is
bound to) as is (inc 1) (evaluating to 2).

 And please consider the other questions revised as appropriate. In
 particular, please restrict my question about quote and syntax-quote to
 situations where they quote a form demarcated by parens. I would still like
 to know whether there is anything certain about the types returned by quote
 and syntax-quote when they are called on a paren-demacated form.

As Clojure is based on abstractions everything relying on specific types
should be questioned. Especially since in the future reify may kill that
idea completely.

My (certainly limited) experience with this kind of questions is: Why do
you need to know? There may be perfectly valid reasons. Then I would go
with seq?.

Sincerely
Meikel

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


What is a form, exactly? + Are there any guarantees about the return types of quote and syntax-quote?

2010-02-14 Thread Garth Sheldon-Coulson
Hi Everyone,

The set of classes for which seq? returns true is rather large. It contains
Cons, LazySeq, PersistentList, and many others. Experimentation reveals that
any of these types can be used to hold Clojure code for evaluation by eval.

Is a form just anything for which seq? returns true, by definition? Or is
there a more accurate definition of a form?

If it's correct to say that a form is always something for which seq?
returns true and never something for which seq? returns false, then any
ISeq-implementing type can be used to hold the return values of quote and
syntax-quote, in principle.

Currently,

user= (class '(f arg1 arg2))
clojure.lang.PersistentList

user= (class `(f arg1 arg2))
clojure.lang.Cons

But those particular classes might be just implementation details. In
principle, in a future release, one or both of those calls might return
ChunkedCons or LazySeq, or any of the other ISeq-implementing types.

I am wondering whether someone could guarantee that this potentiality will
*not* come to pass. To put the question differently, is there a subset of
the ISeq-implementing classes that is guaranteed never to be used to hold
the output of quote, syntax-quote, or any other core code-generation
facilities? In particular, can it be guaranteed that quote and syntax-quote
will never return a lazy-seq? (I can't see why it would ever be desired that
they should, but I am wondering whether a guarantee is available.)

Perhaps no such guarantee is available because it is an implementation
detail that quote and syntax-quote return Java objects in the first place.
In another release they might return deftypes or CLR objects. In that case,
is there any internal-to-Clojure facility I can use to differentiate between
seqs generally, on one hand, and the return values of quote and
syntax-quote, on the other hand? Is there any way I can generate a
clojure-generated-form? predicate that is more restrictive than seq??

Thanks,
Garth

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: What is a form, exactly? + Are there any guarantees about the return types of quote and syntax-quote?

2010-02-14 Thread Michał Marczyk
On 15 February 2010 02:02, Garth Sheldon-Coulson g...@mit.edu wrote:
 If it's correct to say that a form is always something for which seq?
 returns true and never something for which seq? returns false, [...]

The traditional Lisp view would be that *anything* that can be passed
to eval is a form, cf. Common Lisp the Language, 2 ed., section 5.1.
Forms:

http://www.cs.cmu.edu/afs/cs.cmu.edu/project/ai-repository/ai/html/cltl/clm/node56.html#SECTION0091

Note that a form as defined there is a data structure, not a piece
of concrete syntax. The meaning of the word seems to be extended
frequently to cover any self-delimited expression in Lisp's concrete
syntax, which becomes a slightly more involved notion in the presence
of reader macros (#^{:foo bar} [1 2 3], say, would be a single
concrete form).

As far as I can tell, Clojure's eval will accept basically any sort of
object as a form, with things for which no special behaviour is
defined taken to be self-evaluating. That seems to be the usual
behaviour for a Lisp dialect.

Sincerely,
Michał

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: What is a form, exactly? + Are there any guarantees about the return types of quote and syntax-quote?

2010-02-14 Thread Michał Marczyk
A post scriptum:

On 15 February 2010 02:31, Michał Marczyk michal.marc...@gmail.com wrote:
 As far as I can tell, Clojure's eval will accept basically any sort of
 object as a form, with things for which no special behaviour is
 defined taken to be self-evaluating. That seems to be the usual
 behaviour for a Lisp dialect.

Actually I should have left out that last sentence, as CL
implementations might not behave in this way and I used CL
documentation to support the definition of form I've proposed... In
fact that's what it says on the very page I've linked to. At any rate,
(some implementations of) Scheme and Clojure seem exhibit this
behaviour.

The main point is that it would be very unusual to restrict the
meaning of the word form to things which test positive with seq?.
And if I'm right in believing the Clojure eval to return unchanged any
arguments for which it has no special handling prepared, then every
piece of data that can be handled in Clojure is a Clojure form.

Sincerely,
Michał

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: What is a form, exactly? + Are there any guarantees about the return types of quote and syntax-quote?

2010-02-14 Thread Garth Sheldon-Coulson
Okay, Michal, I see what you mean.

Let me revise my first question, then, to replace form with expression,
where by expression I mean a form whose first element will be called as a
function when the form is passed to eval.

(Revised version: What characterizes an expression? Is it correct to say
that an expression is always something for which seq? returns true and never
something for which seq? returns false?)

And please consider the other questions revised as appropriate. In
particular, please restrict my question about quote and syntax-quote to
situations where they quote a form demarcated by parens. I would still like
to know whether there is anything certain about the types returned by quote
and syntax-quote when they are called on a paren-demacated form.

Thanks.

Garth

On Sun, Feb 14, 2010 at 8:31 PM, Michał Marczyk michal.marc...@gmail.comwrote:

 On 15 February 2010 02:02, Garth Sheldon-Coulson g...@mit.edu wrote:
  If it's correct to say that a form is always something for which seq?
  returns true and never something for which seq? returns false, [...]

 The traditional Lisp view would be that *anything* that can be passed
 to eval is a form, cf. Common Lisp the Language, 2 ed., section 5.1.
 Forms:


 http://www.cs.cmu.edu/afs/cs.cmu.edu/project/ai-repository/ai/html/cltl/clm/node56.html#SECTION0091

 Note that a form as defined there is a data structure, not a piece
 of concrete syntax. The meaning of the word seems to be extended
 frequently to cover any self-delimited expression in Lisp's concrete
 syntax, which becomes a slightly more involved notion in the presence
 of reader macros (#^{:foo bar} [1 2 3], say, would be a single
 concrete form).

 As far as I can tell, Clojure's eval will accept basically any sort of
 object as a form, with things for which no special behaviour is
 defined taken to be self-evaluating. That seems to be the usual
 behaviour for a Lisp dialect.

 Sincerely,
 Michał

 --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.comclojure%2bunsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en


-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en