Empty records

This commit is contained in:
Tony Garnock-Jones 2023-11-01 11:20:33 +01:00
parent 1784024952
commit 63c124307b
1 changed files with 18 additions and 7 deletions

View File

@ -3,7 +3,7 @@ title: "P-expressions"
--- ---
Tony Garnock-Jones <tonyg@leastfixedpoint.com> Tony Garnock-Jones <tonyg@leastfixedpoint.com>
October 2023. Version 0.1.1. October 2023. Version 0.1.2.
This document defines a grammar called *Preserves Expressions* This document defines a grammar called *Preserves Expressions*
(*P-expressions*, *pexprs*) that includes [ordinary Preserves text (*P-expressions*, *pexprs*) that includes [ordinary Preserves text
@ -53,6 +53,10 @@ with `Block` everywhere it is mentioned.
Block = "{" *Value ws "}" Block = "{" *Value ws "}"
Syntax for `Record` is loosened to allow empty angle brackets.
Record = "<" *Value ws ">"
New syntax for explicit uninterpreted grouping of sequences of values is New syntax for explicit uninterpreted grouping of sequences of values is
introduced, and added to class `Value`. introduced, and added to class `Value`.
@ -86,7 +90,7 @@ While the ordinary text syntax forbids comments in these positions,
P-expressions allow them: P-expressions allow them:
Document =/ *Value Trailer ws Document =/ *Value Trailer ws
Record =/ "<" Value *Value Trailer ws ">" Record =/ "<" *Value Trailer ws ">"
Sequence =/ "[" *Value Trailer ws "]" Sequence =/ "[" *Value Trailer ws "]"
Set =/ "#{" *Value Trailer ws "}" Set =/ "#{" *Value Trailer ws "}"
Block =/ "{" *Value Trailer ws "}" Block =/ "{" *Value Trailer ws "}"
@ -101,8 +105,8 @@ We write ⌜*p*⌝ for the encoding into Preserves of P-expression *p*.
| ⌜·⌝ : **P-expression** | ⟶ | **Preserves** | | ⌜·⌝ : **P-expression** | ⟶ | **Preserves** |
Aside from the special classes `Group`, `Block`, `Comma`, `Semicolon`, Aside from the special classes `Group`, `Block`, `Comma`, `Semicolon`,
`Colons`, or `Trailer`, P-expressions are encoded directly as Preserves `Colons`, `Trailer`, or empty `Record`, P-expressions are encoded
data. directly as Preserves data.
{:.pseudocode.equations} {:.pseudocode.equations}
| ⌜`[`*p* ...`]`⌝ | = | `[`⌜*p*⌝ ...`]` | | ⌜`[`*p* ...`]`⌝ | = | `[`⌜*p*⌝ ...`]` |
@ -112,7 +116,7 @@ data.
| ⌜`@`*p* *q*⌝ | = | `@`⌜*p*⌝ ⌜*q*⌝ | | ⌜`@`*p* *q*⌝ | = | `@`⌜*p*⌝ ⌜*q*⌝ |
| ⌜*p*⌝ | = | *p* when *p***Atom** | | ⌜*p*⌝ | = | *p* when *p***Atom** |
All members of the special classes are encoded as Preserves Everything else is encoded as Preserves
dictionaries[^encoding-rationale]. dictionaries[^encoding-rationale].
[^encoding-rationale]: In principle, it would be nice to use *records* [^encoding-rationale]: In principle, it would be nice to use *records*
@ -120,6 +124,7 @@ dictionaries[^encoding-rationale].
usages of records! usages of records!
{:.pseudocode.equations} {:.pseudocode.equations}
| ⌜`<>`⌝ | = | `{r:[]}` |
| ⌜`(`*p* ...`)`⌝ | = | `{g:[`⌜*p*⌝ ...`]}` | | ⌜`(`*p* ...`)`⌝ | = | `{g:[`⌜*p*⌝ ...`]}` |
| ⌜`{`*p* ...`}`⌝ | = | `{b:[`⌜*p*⌝ ...`]}` | | ⌜`{`*p* ...`}`⌝ | = | `{b:[`⌜*p*⌝ ...`]}` |
| ⌜`,`⌝ | = | `{s:|,|}` | | ⌜`,`⌝ | = | `{s:|,|}` |
@ -153,8 +158,9 @@ classes mentioned above.
1. Every `Group` or `Semicolon` that appears is an error. 1. Every `Group` or `Semicolon` that appears is an error.
2. Every `Colons` with two or more colons in it is an error. 2. Every `Colons` with two or more colons in it is an error.
3. Every `Comma` that appears is discarded. 3. Every `Comma` that appears is discarded.
3. Every `Trailer` that appears is an error.[^discard-trailers-instead-of-error] 4. Every `Trailer` that appears is an error.[^discard-trailers-instead-of-error]
4. Every `Block` must contain triplets of `Value`, `Colons` (with a 5. Every `Record` with no values in it is an error.
6. Every `Block` must contain triplets of `Value`, `Colons` (with a
single colon), `Value`. Any `Block` not following this pattern is an single colon), `Value`. Any `Block` not following this pattern is an
error. Each `Block` following the pattern is translated to a error. Each `Block` following the pattern is translated to a
`Dictionary` containing a key/value pair for each triplet. `Dictionary` containing a key/value pair for each triplet.
@ -176,6 +182,11 @@ text-syntax encodings.
= <date 1821 {g:[lookup-month "February"]} 3> = <date 1821 {g:[lookup-month "February"]} 3>
``` ```
```preserves
<>⌝
= {r:[]}
```
```preserves ```preserves
⌜(begin (println! (+ 1 2)) (+ 3 4))⌝ ⌜(begin (println! (+ 1 2)) (+ 3 4))⌝
= {g:[begin {g:[println! {g:[+ 1 2]}]} {g:[+ 3 4]}]} = {g:[begin {g:[println! {g:[+ 1 2]}]} {g:[+ 3 4]}]}