-
-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Square notation #172
Comments
What if you switched the roles of FYI, I changed the parser in the package implementing #163, because I think the pop quiz here pointed to a mismatch between the prose spec and the implementation. The point stands that the result is not obvious. |
Assuming a parsed representation that uses tags, neither of these deeply bothers me but I think they run counter to the most common associations of each delimiter. |
I think that Mathematica use a similar system, where scare brackets are used for function definition and application, and parenthesis are used only in math expressions. |
I'm trying to figure out how essential it is to equate a group with one item and that item. It seems like the sort of representation choice that usually goes wrong in subtle ways. For example, suppose a macro It looks like the area several bugs in the parsing of
|
Thanks for the repair to The problem with dropping parens is interesting. The goal of normalizing parens was to avoid situations where macros break due to extra parens that are used to disambiguate. For example, writing
That way the The idea of |
Thanks for the clarification. I can appreciate the goal of allowing parentheses to group without interfering with matching. I expected that kind of interference with shrubbery parentheses, but for what it's worth, it hasn't turned out to be a problem. Implementing parentheses as a specific expression, binding, etc., form has worked well. I also consider it a feature that A similar grouping idea might be applied to shrubbery notation by removing the ability of |
In that case, the parenthesis-dropping idea isn't essential. I'm not really sure I like
parsing the same as
anyway. So if we set that aside, as well as the goal of having orthogonal grouping and sequencing forms and its big tradeoffs, I think there are still two ideas in here I'd like to make sure come across:
or as another grouping / sequencing form like
I think a syntax very similar to Shrubberies (especially if you drop
If you keep I would find that description much easier to follow. |
I like the idea of completely separating The main use of I tried going entirely without a delimiting form for shrubbery blocks, but then there's no way to render some shrubberies that are naturally constructed through templates. For example,
|
Hmm. Now that I better understand the situations where |
These do seem like situations where Square's idea of having extra parentheses be insignificant works well, however. In Square I think the first two examples would be:
I can see how in expansion it would be useful to manipulate blocks and alts separately from their surrounding group. Perhaps Square should make |
Square notation
This proposal is an attempt to design a syntax similar to Shrubbery notation (#122), but where each syntactic element has a single role that can be described and understood separately.
A key syntax in Shrubbery notation has multiple roles: parentheses. Parentheses are used both as list-like syntax for function application, tuples, etc., and to indicate grouping in arithmetic expressions.
The central design decision in this proposal is to use parentheses only to disambiguate grouping and to use square brackets for all list-like structures, such as function argument lists. Thus the name.
Here are some examples in square notation:
Basic syntax
Items include atoms, groups, sequences, blocks, and alternative groups.
Atoms
This proposal concerns the grouping forms, so ignores the syntax of atoms. Let's assume atoms similar to Shrubberies. Note that it is not necessary to separate operators from other identifiers, however.
Groups
A sequence of whitespace-separated items on a line forms a group. Here's a group with five elements:
Pairs of
(
and)
form a nested group that can span lines. The following is a group with three elements, where the last element is a group with five elements.All elements of a group must begin at the same or greater column as the first element, but the first element may be at an earlier column than any introductory
(
. This is legal:This is not:
Sequences
Sequences are formed by pairs of
[
and]
, and items within are separated into groups by,
. Extra or trailing,
are disallowed. Line breaks are allowed within groups in a sequence.This is a sequence with two groups:
All lines beginning with elements of a sequence must begin at the same column as the first element, but multiple
,
separated elements may appear on a line, elements that are groups may span multiple lines, and the first element may be at an earlier column than the introductory[
.This is legal:
Blocks
Blocks start with a
:
following an item, optionally followed by an indent and newline. These examples have a block containing one group:Blocks may be nested on the same line:
Without use of parentheses to disambiguate, the elements following the last
:
belong to the innermost block.Within a block, groups are separated by
;
or newline.All lines of a block must begin on at the same column, but there may be multiple
;
separated elements on a line. These are legal:but these are not:
Continuing a group across lines in a block requires parentheses:
A block within a group is always the last element of a group. Where it ends depends on the surrounding context: a block nested within an outer block ends at a dedent; a block in a sequence ends at
,
or]
; a block in a parenthesized group ends at)
. Some examples:Because an item is required before the
:
, a block is never the first element of a group.Alternative groups
An alternative group is opened by a
|
following an item, either on the same line or a new line. Subsequent alternatives in the group are opened with|
. The contents of each alternative in an alternative group are parsed as a block.Note that because an alternative group must directly follow an item, a
:
preceeding an alternative group is not allowed.All lines of an alternative group must begin on the same line, but there may be multiple
|
separated elements on a line. If an alternative's block contains an un-parenthesized:
block, the alternative must appear beginning on its own line.These are legal:
These are not:
Like a block, an alternative group will always be the last element of a surrounding group and will always be preceeded by at least one other item.
Parsed representation
The parsed representation uses syntax objects with paren-shape annotations. Atoms represent themselves;
(item ...)
represents a multi-element group;[item ...]
represents a sequence, and{item ...}
represents a block.Because groups cannot begin with a block, blocks cannot occur directly nested. Thus
{{item ...} ...}
is available to unambiguously represent alternative groups. This is probably too cute. Maybe tags as in the Shrubbery proposal are a better idea, but I'll offer this as an alternative.Parentheses are only used for disambiguation, so extra parentheses are removed at read-time. The parsed representation of single element groups has no parentheses, and that for multiple element groups has a single set of parentheses. So the following all have the same parsed representation:
of
Here are the parsed representations of the examples from the earlier sections of this document:
More examples
miniKanren
Parsed:
OMeta
Adapated from here
Parsed:
Tradeoffs
Advantages
Because the syntaxes are more orthogonal, I think Square notation can be described more simply than Shrubberies. Though of course this current document is much less complete than the Shrubbery proposal.
Square notation is potentially easy to represent, print, and match with S-expressions without an extra layer of tagging. Unless people find the
{{item ...} ...}
representation is too awkward for alternative groups.Conceptually separate syntaxes for groups, sequences, blocks, and alternatives allow separate line-continuation rules for each context that avoid potential confusion. Pop quiz: how does this Shrubbery parse?
can be written
Both parse as:
Lack of
{}
means there is only one syntax for blocks, rather than two.Allowing line breaks in lists and parenthesized groups obviates
\
.Disadvantages:
Use of square brackets for function calls is less conventional, and violates one of @mflatt's original desiderata for Rhombus.
There is no special syntax for common data literals, such as lists, hashes, or sets, as there is only one sequence form:
[item, ...]
@samth's desire for convenient use of
|>
remains unsupported.And probably many I haven't noticed.
Implementation
None.
The text was updated successfully, but these errors were encountered: