@plylang/plylang 1.3.4 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +170 -115
  2. package/package.json +7 -7
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Ply is a concatenative language. A program is a sequence of words. A word takes values off the single stack and puts new ones on. The type of a word is that transformation. In stack notation the top is always on the right.
4
4
 
5
- The compiler translates a module into JavaScript. There is no separate virtual machine. The language version is 1. There are no subtypes: two types are compatible when they unify.
5
+ The compiler translates a module into JavaScript. There is no separate virtual machine. The language version is 2. Syntax is whitespace-independent and postfix: newlines and spaces are interchangeable everywhere except inside single-line `/` and `//` comments. There are no subtypes: two types are compatible when they unify.
6
6
 
7
7
  This text walks through the language once, with a short example of each feature.
8
8
 
@@ -13,16 +13,15 @@ The repository contains the `plylang` compiler and the `plylang-ls` server. They
13
13
  A signature says what a word takes and what it puts. The tail under that prefix stays in place.
14
14
 
15
15
  ```ply
16
- ~ "@plylang/prelude/Classes/Arithmetic" add
17
- ~ "@plylang/prelude/Data/Num"
16
+ "@plylang/prelude/Classes/Arithmetic" ( add ) ~
17
+ "@plylang/prelude/Data/Num" ~
18
18
 
19
- : double ( Num -- Num )
20
- dup add
19
+ .double ( Num -- Num ) [ dup add ] ::
21
20
  ```
22
21
 
23
22
  The body runs from left to right. A number is on the stack. `dup` copies the top. `add` takes both numbers and puts the sum.
24
23
 
25
- A word starts with `:`. Then `foreign` may appear, followed by the name and the scheme in parentheses. Spaces inside the parentheses do not matter. The body may continue on the same line: `: double ( Num -- Num ) dup add`.
24
+ A symbol starts with `.` and names a declaration, variable, class method, named constructor field, import rename, or module alias. A word ends with `::` and takes its name, its scheme in parentheses, and its body in `[ ... ]`: `.inc ( Num -- Num ) [ 1 add ] ::`. The quotation may also stand before the name: `[ 1 add ] .inc ( Num -- Num ) ::`. A word's quotation can even be built on the stack: `1 [ add ] cons .inc ( Num -- Num ) ::`. Spaces and line breaks inside or between forms do not matter. A foreign word has no body and ends with `**`: `.lte_num ( Num Num -- Bool ) **`.
26
25
 
27
26
  A name in a scheme is one slot. `Num Num` is two slots. `Maybe a` is also two: the constructor needs an argument, and that argument is written in parentheses, `(Maybe a)`. `(Num)` is the same type as `Num`.
28
27
 
@@ -53,20 +52,15 @@ Operations on these values are imported. This text does not list them.
53
52
  These words are built in. They need no import. On a stack `x y` the top is `y`:
54
53
 
55
54
  ```ply
56
- : copy ( a -- a a )
57
- dup
55
+ .copy ( a -- a a ) [ dup ] ::
58
56
 
59
- : gone ( a b -- a )
60
- drop
57
+ .gone ( a b -- a ) [ drop ] ::
61
58
 
62
- : flipped ( a b -- b a )
63
- swap
59
+ .flipped ( a b -- b a ) [ swap ] ::
64
60
 
65
- : top ( a b -- b )
66
- nip
61
+ .top ( a b -- b ) [ nip ] ::
67
62
 
68
- : again ( a b -- a b a )
69
- over
63
+ .again ( a b -- a b a ) [ over ] ::
70
64
  ```
71
65
 
72
66
  The same permutations as in Factor follow, with names made of letters: `dupd`, `swapd`, `tuck`, `pick`, `rot`, `unrot`, `dup2`, `drop2`, `nip2`, `swap2`, `over2`. A name does not start with a digit, so the pair is written `dup2`. On `x y z` with `z` on top, `rot` yields `y z x` and `unrot` yields `z x y`.
@@ -76,13 +70,13 @@ The same permutations as in Factor follow, with names made of letters: `dupd`, `
76
70
  `[ ... ]` is a value. It does not capture the stack outside, and it does not run by itself. Square brackets are a quotation. An array is written in angle brackets.
77
71
 
78
72
  ```text
79
- : call ( ..s (..s -- ..t) -- ..t )
80
- : dip ( ..s a (..s -- ..t) -- ..t a )
81
- : keep ( ..s a (..s a -- ..t) -- ..t a )
82
- : bi ( ..s a (a -- b) (a -- c) -- ..s b c )
83
- : if ( ..s Bool (..s -- ..t) (..s -- ..t) -- ..t )
84
- : quote ( ..s a -- ..s (-- a) )
85
- : cons ( ..r a (..s a -- ..t) -- ..r (..s -- ..t) )
73
+ .call ( ..s (..s -- ..t) -- ..t ) ::
74
+ .dip ( ..s a (..s -- ..t) -- ..t a ) ::
75
+ .keep ( ..s a (..s a -- ..t) -- ..t a ) ::
76
+ .bi ( ..s a (a -- b) (a -- c) -- ..s b c ) ::
77
+ .if ( ..s Bool (..s -- ..t) (..s -- ..t) -- ..t ) ::
78
+ .quote ( ..s a -- ..s (-- a) ) ::
79
+ .cons ( ..r a (..s a -- ..t) -- ..r (..s -- ..t) ) ::
86
80
  ```
87
81
 
88
82
  `call` takes a quotation and runs it on the rest. `dip` hides the value under the quotation, runs the quotation, and returns what it hid to the top. `dip2` and `dip3` hide two and three values. `keep` runs the quotation and returns the value it took. `keep2` returns two.
@@ -96,23 +90,24 @@ The same permutations as in Factor follow, with names made of letters: `dupd`, `
96
90
  `cons` pushes a value and then runs a quotation. The value sits under the quotation, so `5 [ 1 add ] cons` is a quotation of type `(-- Num)` that pushes five and then adds one. The call site does not name that value.
97
91
 
98
92
  ```ply
99
- ~ "@plylang/prelude/Classes/Setoid" equal
100
- ~ "@plylang/prelude/Classes/Subtractable" sub
101
- ~ "@plylang/prelude/Classes/Arithmetic" multiply
102
- ~ "@plylang/prelude/Data/Num"
93
+ "@plylang/prelude/Classes/Setoid" ( equal ) ~
94
+ "@plylang/prelude/Classes/Subtractable" ( sub ) ~
95
+ "@plylang/prelude/Classes/Arithmetic" ( multiply ) ~
96
+ "@plylang/prelude/Data/Num" ~
103
97
 
104
- : fact ( Num -- Num )
98
+ .fact ( Num -- Num ) [
105
99
  dup 0 equal [ drop 1 ] [ dup 1 sub fact multiply ] if
100
+ ] ::
106
101
  ```
107
102
 
108
103
  Zero is replaced by one. Otherwise `n` and `n - 1` remain on the stack, the recursive `fact` computes the second, and `multiply` multiplies.
109
104
 
110
- Names before `->` name stack values. The left name is the deeper value, the right name is the top. A quotation sees these names, and they are not part of the input. A clause that only renames the stack, and whose names do not appear in a quotation, is ordinary stack code written without `->`.
105
+ Names before `->` name stack values. The left name is the deeper value, the right name is the top. A nested quotation sees these names, and they are not part of its stack input. `[ x y -> x y add ]` is an ordinary quotation that names its two inputs.
111
106
 
112
107
  ```ply
113
- : curry ( a (a b -- c) -- (b -- c) )
114
- x q ->
115
- [ x swap q call ]
108
+ .curry ( a (a b -- c) -- (b -- c) ) [
109
+ x q -> [ x swap q call ]
110
+ ] ::
116
111
  ```
117
112
 
118
113
  This is an ordinary word of a module. It is not built into the compiler.
@@ -121,40 +116,49 @@ This is an ordinary word of a module. It is not built into the compiler.
121
116
 
122
117
  ## Pattern matching
123
118
 
124
- When the first line of a body is patterns and `->`, the whole body is equations. The right-hand pattern is the top. A name takes a slot. A literal is compared with the value and taken. `_` matches any value and leaves it. The first match wins. A value cannot be matched in the middle of a body: write a separate word for that, and make its whole body equations.
119
+ A quotation whose body opens with patterns and `->` is a match quotation. Arms are separated by `,`, with an optional trailing comma. Match quotations are first-class values: a match quotation can be the body of a word, passed to a higher-order word such as `map`, or run inline with `call`. The right-hand pattern is the top. A name takes a slot. A literal is compared with the value and taken. `_` matches any value and leaves it. The first match wins.
125
120
 
126
121
  ```ply
127
- : toString ( Bool -- Str )
128
- true -> "true"
122
+ .toString ( Bool -- Str ) [
123
+ true -> "true",
129
124
  false -> "false"
125
+ ] ::
130
126
  ```
131
127
 
132
- `true` together with `false` covers `Bool`. `unit` covers `Unit`. A literal of `Num`, `Int`, or `Str` does not. `Num ->`, `Int ->`, `Str ->`, `Bool ->`, `Array ->`, and `Unit ->` match a visible `Nullary` or `Unary` instance and leave the value. The test calls that instance's word. On a wider type they do not cover the slot, so a later name or `_` remains. The arm sees the word's signature with that shape filled in. The other arms do not. A `; foreign` type is matched the same way when its module defines `Nullary`, `Unary`, or `Binary`.
128
+ ```ply
129
+ / Inline match quotation passed to `map` or run with `call`.
130
+ [ (Just x) -> x inc, (Nothing ..) -> 0 ]
131
+ ```
132
+
133
+ `true` together with `false` covers `Bool`. `unit` covers `Unit`. A literal of `Num`, `Int`, or `Str` does not. `Num ->`, `Int ->`, `Str ->`, `Bool ->`, `Array ->`, and `Unit ->` match a visible `Nullary` or `Unary` instance and leave the value. The test calls that instance's word. On a wider type they do not cover the slot, so a later name or `_` remains. The arm sees the scheme with that shape filled in. The other arms do not. A foreign type declared with `*` is matched the same way when its module defines `Nullary`, `Unary`, or `Binary`.
133
134
 
134
135
  A bare constructor checks the tag and leaves the value. `(Ctor ..)` spills every field, last on top. `(Just x)` names the one field and does not put it until the name is mentioned. `(Person n _)` is positional: the first field is kept under `n` and the second stays off the stack. `(Person age name)` binds by name, in either order, when every name is a field. `(Person)` is an error. `(Some (Array Num) items)` checks that the field is an array of numbers and names that array. The check runs when the field's type is still open. `(Some (Num) ..)` checks and spills that number. `(Some (Num) _)` only checks. `(Array x)` does not bind `x`.
135
136
 
136
137
  An array in a pattern has a fixed length. `<>` matches only the empty array. One length does not cover every `Array`.
137
138
 
138
139
  ```ply
139
- : isEmpty ( (Array a) -- Bool )
140
- <> -> true
140
+ .isEmpty ( (Array a) -- Bool ) [
141
+ <> -> true,
141
142
  _ -> drop false
143
+ ] ::
142
144
  ```
143
145
 
144
146
  ## Data
145
147
 
146
- `;` in column 0 names an algebraic type. A constructor is a word. A type and a word with one name live in different spaces.
148
+ `;` declares an algebraic type: `.Name ( [params.] Ctors ) ;`. A constructor is a word. A type and a word with one name live in different spaces.
147
149
 
148
150
  ```ply
149
- ; Maybe a = Nothing | Just a
151
+ .Maybe ( a. Just a | Nothing ) ;
150
152
 
151
- : fromMaybe ( a (Maybe a) -- a )
152
- (Nothing ..) ->
153
+ .fromMaybe ( a (Maybe a) -- a ) [
154
+ (Nothing ..) ->,
153
155
  (Just ..) -> nip
156
+ ] ::
154
157
 
155
- : mapMaybe ( (Maybe a) (a -- b) -- (Maybe b) )
156
- (Nothing ..) q -> q drop Nothing
158
+ .mapMaybe ( (Maybe a) (a -- b) -- (Maybe b) ) [
159
+ (Nothing ..) q -> q drop Nothing,
157
160
  (Just x) q -> x q call Just
161
+ ] ::
158
162
  ```
159
163
 
160
164
  The empty body of the `(Nothing ..)` branch leaves the fallback value. For `Just`, `(Just ..)` puts the field above it, and `nip` drops what is underneath. In `mapMaybe` the quotation is the top: `q` names it, and `(Just x)` names the field. A bare `Just` would leave the `Maybe` in place.
@@ -162,24 +166,26 @@ The empty body of the `(Nothing ..)` branch leaves the fallback value. For `Just
162
166
  A list with its own structure is a separate type. The head sits in the constructor, and the tail is another value of that type.
163
167
 
164
168
  ```ply
165
- ; List a = Nil | Cons head : a, tail : (List a)
169
+ .List ( a. Nil | Cons .head a .tail (List a) ) ;
166
170
  ```
167
171
 
168
- A `|` may stand before the first constructor, so every arm starts in the same column.
172
+ A `|` may stand before the first constructor.
169
173
 
170
- Zero fields and one field have no names. `Nothing` stores nothing. `Just a` stores one value, and the equation names it. Two or more fields are `name : Type`, separated by commas. A second positional type is [PL0117](docs/Errors.md#pl0117). One named field is [PL0118](docs/Errors.md#pl0118).
174
+ Zero fields and one field have no names. `Nothing` stores nothing. `Just a` stores one value, and the equation names it. Two or more fields are `.name Type`, separated by whitespace. A second positional type is [PL0117](docs/Errors.md#pl0117). One named field is [PL0118](docs/Errors.md#pl0118).
171
175
 
172
176
  ```ply
173
- ; Pair a b = Pair frs : a, snd : b
177
+ .Person ( Person .name Str .age Num ) ;
178
+
179
+ .Pair ( a b. Pair .frs a .snd b ) ;
174
180
 
175
- : frs ( a b. (Pair a b) -- a )
176
- frs>>
181
+ .frs ( a b. (Pair a b) -- a ) [ frs>> ] ::
177
182
 
178
- : setSnd ( a b. (Pair a b) b -- (Pair a b) )
179
- >>snd
183
+ .setSnd ( a b. (Pair a b) b -- (Pair a b) ) [ >>snd ] ::
180
184
  ```
181
185
 
182
- `Pair` the word takes the fields in that order, last on top. `frs>>` consumes the pair and leaves the field. When the pair is still needed, copy it first: `dup frs>>`. `>>snd` takes the new field above the pair and returns a new pair. The previous pair is a separate value. A field on every constructor, with the same type, is `name>>`. A field of one constructor is read after that constructor's check: `Sent -> body>>`. `name>>` on `Maybe` is [PL0119](docs/Errors.md#pl0119). `frs>>` on `Int`, on a type variable, or on a synonym of `Num` is [PL0063](docs/Errors.md#pl0063). A missing field is [PL0120](docs/Errors.md#pl0120). `(Page)` is [PL0136](docs/Errors.md#pl0136).
186
+ `Pair` the word takes the fields in that order, last on top. `Pair>>` unpacks a `Pair` onto the stack, pushing its fields in declaration order with the last field on top. `Ctor>>` works on any single-constructor data type or newtype, and on a constructor of a sum type after checking its tag (`Just -> Just>>`). Unpacking a constructor of a multi-constructor type without checking its tag is [PL0145](docs/Errors.md#pl0145).
187
+
188
+ `frs>>` consumes the pair and leaves the field. When the pair is still needed, copy it first: `dup frs>>`. `>>snd` takes the new field above the pair and returns a new pair. The previous pair is a separate value. A field on every constructor, with the same type, is `name>>`. A field of one constructor is read after that constructor's check: `Sent -> body>>`. `name>>` on `Maybe` is [PL0119](docs/Errors.md#pl0119). `frs>>` on `Int`, on a type variable, or on a synonym of `Num` is [PL0063](docs/Errors.md#pl0063). A missing field is [PL0120](docs/Errors.md#pl0120). `(Page)` is [PL0136](docs/Errors.md#pl0136).
183
189
 
184
190
  In JavaScript a constructor with no fields is `{ $$tag: "Nothing" }`. One field is `(value) => ({ $$tag: "Just", value })`. Named fields are properties: `(v0, v1) => ({ $$tag: "Pair", frs: v0, snd: v1 })`. The key is `$$tag` so a field named `tag`, and a `tag` property from another system, stay separate. `$` cannot start a Ply name.
185
191
 
@@ -192,107 +198,154 @@ The text between the angles is a program. It starts from an empty stack and does
192
198
  A word that walks an array is imported. In the example the quotation sits on top. The name comes from one module, and the instance for the array comes from another.
193
199
 
194
200
  ```ply
195
- ~ "@plylang/prelude/Classes/Functor" map
196
- ~ "@plylang/prelude/Classes/Arithmetic" add
197
- ~ "@plylang/prelude/Data/Num"
198
- ~ "@plylang/prelude/Data/Array"
201
+ "@plylang/prelude/Classes/Functor" ( map ) ~
202
+ "@plylang/prelude/Classes/Arithmetic" ( add ) ~
203
+ "@plylang/prelude/Data/Num" ~
204
+ "@plylang/prelude/Data/Array" ~
199
205
 
200
- : shifted ( -- (Array Num) )
206
+ .shifted ( -- (Array Num) ) [
201
207
  < 1 2 3 > [ 1 add ] map
208
+ ] ::
202
209
  ```
203
210
 
204
211
  The bare import of `Data/Array` brings the instances of that module and brings no names. The result is `2`, `3`, `4`. `dup` of an array shares the JavaScript reference. A word that writes returns a new array, and the input stays.
205
212
 
206
213
  ## A name for a type
207
214
 
208
- `-` names a type that already exists. The right-hand side follows immediately. It is the same type, and the name has no constructor.
215
+ `-` names a type that already exists: `.Alias ( [params.] Type ) -`. It is the same type, and the name has no constructor.
209
216
 
210
217
  ```ply
211
- ; Person = Person name : Str, age : Num
212
- - Human Person
218
+ .Person ( Person .name Str .age Num ) ;
219
+ .Human ( Person ) -
213
220
 
214
- : older ( Human -- Human )
215
- 37 >>age
221
+ .older ( Human -- Human ) [ 37 >>age ] ::
216
222
  ```
217
223
 
218
- Parameters are written as they are on a type: `- Twin a (Pair a a)`. In a slot a synonym is saturated, `(Twin Num)`.
224
+ Parameters stand before a dot inside the parentheses: `.PairOf ( a. Pair a a ) -`. In a slot a synonym is saturated, `(PairOf Num)`.
219
225
 
220
226
  ## Wrapper
221
227
 
222
- `;` with `==` is a distinct type with one constructor and one field. It does not unify with the field. In JavaScript the constructor returns its argument as it is.
228
+ `==` declares a distinct newtype with one constructor and one field: `.Name ( [params.] Ctor Field ) ==`. It does not unify with the field. In JavaScript the constructor returns its argument as it is, and `Ctor>>` unpacks it with zero overhead.
223
229
 
224
230
  ```ply
225
- ~ "@plylang/prelude/Classes/Arithmetic" add
226
- ~ "@plylang/prelude/Data/Num"
231
+ "@plylang/prelude/Classes/Arithmetic" ( add ) ~
232
+ "@plylang/prelude/Data/Num" ~
227
233
 
228
- ; Age == Age Num
234
+ .Sum ( a. Sum a ) ==
229
235
 
230
- : older ( Age -- Age )
231
- (Age ..) -> 1 add Age
236
+ .addSum ( (Sum Num) (Sum Num) -- (Sum Num) ) [
237
+ [ Sum>> ] dip Sum>> add Sum
238
+ ] ::
232
239
  ```
233
240
 
234
- Zero fields, two fields, or two constructors do not fit `==`. A type with `=` is for those.
241
+ Zero fields, two fields, or two constructors do not fit `==`. A data declaration `;` is for those.
235
242
 
236
243
  ## Dictionary
237
244
 
238
245
  `(StrMap a)` comes from `@plylang/prelude/Data/StrMap`. The key is `Str`. The program chooses the key when it runs, and the type does not name that key. There is no literal. An empty dictionary in JavaScript is `Object.create(null)`.
239
246
 
240
247
  ```ply
241
- ~ "@plylang/prelude/Data/StrMap" StrMap, emptyStrMap, insertStrMap
248
+ "@plylang/prelude/Data/StrMap" ( StrMap, emptyStrMap, insertStrMap ) ~
242
249
 
243
- : ages ( -- (StrMap Num) )
250
+ .ages ( -- (StrMap Num) ) [
244
251
  emptyStrMap "Ada" 36 insertStrMap
252
+ ] ::
245
253
  ```
246
254
 
247
255
  The order in the example is the dictionary, the key, and the value on top.
248
256
 
257
+ ## Variables and local declarations
258
+
259
+ `:` pops the top value and declares a mutable variable. An explicit type may appear in parentheses before `:`. `=` pops the top value and updates that variable.
260
+
261
+ ```ply
262
+ 0 .counter :
263
+ Nothing .opt ( Maybe Num ) :
264
+
265
+ .inc ( -- Num ) [
266
+ counter 1 add dup .counter =
267
+ ] ::
268
+ ```
269
+
270
+ Reading `counter` pushes a snapshot of its current value `( -- Num )`: a later update does not change a value already on the stack. `1 .counter =` updates `counter` `( Num -- )`. A variable imported under a module alias is updated as `10 .Mod:counter =`. Variable names are unique within their scope. Module-level variables are initialized when the module loads, may be listed in `@`, and may be imported by other modules.
271
+
272
+ Inside a word or quotation body, local declarations introduce bindings scoped to that block: local words with `::` (including recursive helpers and stack-computed words), local variables with `:` and `=`, and local types with `;`, `==`, and `-`. Local variables may be captured and updated by inner quotations. A local type cannot escape its scope ([PL0149](docs/Errors.md#pl0149)).
273
+
274
+ ```ply
275
+ .factLocal ( Num -- Num ) [
276
+ .loop ( Num Num -- Num ) [
277
+ over 1 lte
278
+ [ nip ]
279
+ [ over multiply [ 1 sub ] dip loop ]
280
+ if
281
+ ] ::
282
+ 1 loop
283
+ ] ::
284
+ ```
285
+
249
286
  ## Classes
250
287
 
251
- `&` declares a class. A method is written without a colon. A constraint is not written in the class text: the compiler adds it. `#` declares an instance. The head is the type after the class name. Superclasses stand to the left of `=>`. A subclass constraint brings a superclass method when that method's name is imported. Several constraints in a scheme are separated by a comma.
288
+ `&` declares a class: `.Name ( params. [Super =>] .method ( scheme ) ... ) &`. A constraint on the class itself is not written in a method signature: the compiler adds it. Superclasses stand to the left of `=>`. A subclass constraint brings a superclass method when that method's name is imported. Several constraints in a scheme are separated by a comma.
252
289
 
253
290
  ```ply
254
- & Equal a
255
- equal ( a a -- Bool )
291
+ .Setoid ( a. .equal ( a a -- Bool ) ) &
256
292
 
257
- & Equal a => Ordered a
258
- before ( a a -- Bool )
293
+ .Ord ( a. Setoid a => .lte ( a a -- Bool ) ) &
259
294
  ```
260
295
 
261
296
  ```ply
262
- ~ "@plylang/prelude/Classes/Setoid" Setoid, equal
263
- ~ "@plylang/prelude/Data/Num"
297
+ "@plylang/prelude/Classes/Setoid" ( Setoid, equal ) ~
298
+ "@plylang/prelude/Data/Num" ~
264
299
 
265
- : same ( Setoid a => a a -- Bool )
266
- equal
300
+ .same ( Setoid a => a a -- Bool ) [ equal ] ::
267
301
  ```
268
302
 
269
303
  The instance for numbers is declared in `@plylang/prelude/Data/Num`. The bare import brings the instances of that module and brings no names. That import gives `same` that instance.
270
304
 
271
- A hand-written instance is a word for each method. A module may write one when it defines the class, the type, or the wrapper in the head, or when the head contains a built-in constructor `Int`, `Num`, `Bool`, `Str`, `Unit`, or `Array`.
305
+ `#` declares an instance. The header in parentheses holds any context before `=>`, one or more class names separated by commas, and the head type. A module may write an instance when it defines the class, the type, or the wrapper in the head, or when the head contains a built-in constructor `Int`, `Num`, `Bool`, `Str`, `Unit`, or `Array`.
306
+
307
+ Each method in `[ ... ]` binds `.method` either to a word name or directly to an inline quotation:
272
308
 
273
309
  ```ply
274
- & Equal a
275
- equal ( a a -- Bool )
310
+ .Equal ( a. .equal ( a a -- Bool ) ) &
276
311
 
277
- ; Box a = Mk a
312
+ .Box ( a. Mk a ) ;
278
313
 
279
- : eqBox ( Equal a => (Box a) (Box a) -- Bool )
314
+ .eqBox ( Equal a => (Box a) (Box a) -- Bool ) [
280
315
  (Mk x) (Mk y) -> x y equal
316
+ ] ::
317
+
318
+ ( Equal a => Equal (Box a) ) [ .equal eqBox ] #
319
+
320
+ ( Equal Bool ) [
321
+ .equal [
322
+ true true -> true,
323
+ false false -> true,
324
+ _ _ -> drop drop false
325
+ ]
326
+ ] #
327
+ ```
328
+
329
+ Multiple classes sharing one head are listed with commas:
281
330
 
282
- # Equal (Box a)
283
- equal = eqBox
331
+ ```ply
332
+ ( Number a => Semigroup, Monoid (Sum a) ) [
333
+ .concat concat_sum
334
+ .empty empty_sum
335
+ ] #
284
336
  ```
285
337
 
338
+ A class with no methods takes an empty block or omits it: `( Monad Maybe ) [] #` or `( Monad Maybe ) #`. A newtype can forward an existing instance from its underlying type with `== #`, with no method block and zero wrapper overhead: `( Eq a => Eq (Sum a) ) == #`.
339
+
286
340
  A parameter of kind `Type -> Type` writes the constructor in the head.
287
341
 
288
342
  ```ply
289
- & Mapped f
290
- map ( (f a) (a -- b) -- (f b) )
343
+ .Mapped ( f. .map ( (f a) (a -- b) -- (f b) ) ) &
291
344
  ```
292
345
 
293
- An instance of such a class begins with `# Mapped Box`.
346
+ An instance of such a class is `( Mapped Box ) [ .map mapBox ] #`.
294
347
 
295
- Two classes with one head are written with a comma. An instance is not a name. Any import carries the instances of that module, and those instances pass along the next import. The word on the right of `=` stays in the file that writes `#` unless that name is also in the `@` list. The caller writes the class method. A module may write only instances. Its `@` list can be empty, and the build still includes that module.
348
+ An instance is not a name. Any import carries the instances of that module, and those instances pass along the next import. A named word bound in `[ ... ]` stays in the file that writes `#` unless that name is also in the `@` list. The caller writes the class method. A module may write only instances. Its `@` list can be `() @`, and the build still includes that module.
296
349
 
297
350
  When an exported word has a constraint in its scheme, the method dictionary becomes the first argument in JavaScript.
298
351
 
@@ -300,24 +353,23 @@ When an exported word has a constraint in its scheme, the method dictionary beco
300
353
 
301
354
  One `*.ply` file is one module. The file itself has no name. The path from the source directory gives it: `src/Data/Maybe.ply` is called `Data.Maybe`. Each segment starts with a capital Latin letter.
302
355
 
303
- A module names what it publishes with one `@` list. The list may stand anywhere in the file. A name in the list leaves the file. Any other name stays in the file. The same list holds words, types, constructors, synonyms, classes, methods, and names brought in by an import. A type and its constructor share one entry when they share a name. Names are separated by commas. A bare `@` publishes no names. A module that writes an instance an importer can see is still built. A second `@` is a duplicate definition.
356
+ A module names what it publishes with one `@` list: `( Maybe, Just, Nothing ) @`. The list may stand anywhere in the file. A name in the list leaves the file. Any other name stays in the file. The same list holds words, variables, types, constructors, synonyms, classes, methods, and names brought in by an import. A type and its constructor share one entry when they share a name. Names are separated by commas. `() @` publishes no names. A module that writes an instance an importer can see is still built. A second `@` is a duplicate definition.
304
357
 
305
358
  ```ply
306
- ~ "Data/Maybe" Just, Nothing, fromMaybe
307
- ~ "@algosail/example/Num" add
359
+ "Data/Maybe" ( Maybe, fromMaybe .unwrap ) ~
360
+ "Data/StrMap" .M ~
361
+ "@algosail/example/Num" ( add ) ~
308
362
 
309
- @ Maybe, Nothing, Just, fromMaybe
363
+ ( Maybe, unwrap ) @
310
364
  ```
311
365
 
312
- Imports stay together and come before the first `:`, `;`, `-`, `&`, `#`, `?`, or `=`. A path inside this package is written from the source directory. A path into another package is a package specifier. The names after the path are what this file uses. `: Alias` brings every published name as `Alias:name`, and a call is `Alias:send`. `send : busSend` brings `send` as `busSend`. A bare path brings instances and no names. Writing the local name again in the `@` list publishes it. A colon in column 0 still starts a word.
313
-
314
- A word `main` with scheme `( -- Unit )` may stay off the list. Node still runs it when it starts that file.
366
+ Imports end with `~`. A path inside this package is written from the source directory. A path into another package is a package specifier. A parenthesized list imports those names, with an optional `.local` rename (`fromMaybe .unwrap`). A symbol alias (`"Data/Maybe" .M ~`) brings every published name as `M:name`, and a call is `M:fromMaybe` (updating an aliased variable is `10 .M:counter =`). A bare `"Data/Maybe" ~` brings instances and no names. Writing a local name again in the `@` list publishes it.
315
367
 
316
- A comment runs from `.` to the end of the line, when the dot is not inside `()`. `..` in column 0, directly above `:`, `;`, `-`, `&`, `?`, or `=`, is the text of that declaration. One space after `..` is not part of the text. A line that is only `..` keeps a paragraph break. A type has one text for all of its constructors. The lines above `&` are the text of the class. A `..` line at the start of an indented line, directly above a method, is the text of that method. A method without that line keeps the class text in the hover. A `..` line above `#` does not attach to the declaration.
368
+ A comment starts with `/` and runs to the end of the line. It is the only newline-terminated construct in Ply. `//` at the first non-space of a line, directly above a declaration with no blank line between them, is the doc comment of that declaration. One space after `//` is not part of the text. A line that is only `//` keeps a paragraph break. A leading `//` block separated by a blank line at the top of a file is the module's description. A type has one text for all of its constructors. The lines above `&` are the text of the class. A `//` block inside a class directly above `.method` is the text of that method; a method without one keeps the class text in hover.
317
369
 
318
- A declaration stands in column 0 and starts with `@`, `:`, `;`, `-`, `&`, `#`, `?`, `~`, or `=`.
370
+ Each top-level declaration ends with its rune: `@` exports, `~` import, `;` data type, `==` newtype, `-` type synonym, `*` foreign type, `&` class, `#` instance, `::` word, `**` foreign word, `:` variable declaration, and `?` check.
319
371
 
320
- A place is one value the module keeps off the stack. `= base Num` declares it. The program under that line has scheme `( -- Num )` and runs once, when the module loads. After that, `base` reads the current frame `( -- Num )`. `=base` writes the top value `( Num -- )`. `=>base` runs the quotation under a new frame and restores the previous frame afterwards, including when the quotation throws: `( ..s Num ( ..s -- ..t ) -- ..t )`. The stored type is monomorphic. Importing `base` also opens `=base` and `=>base`. The program that fills the place does not read or write it.
372
+ Executable terms may also stand at the top level of a module. They run in source order when the module loads, starting and ending with an empty stack. Top-level code initializes module-level variables and top-level stack-computed words, and acts as the program of an entry module.
321
373
 
322
374
  A package is described by `package.json`.
323
375
 
@@ -325,7 +377,7 @@ A package is described by `package.json`.
325
377
  {
326
378
  "name": "@algosail/example",
327
379
  "type": "module",
328
- "ply": { "source": "src", "foreign": "foreign", "out": "dist", "language": 1 },
380
+ "ply": { "source": "src", "foreign": "foreign", "out": "dist", "language": 2 },
329
381
  "exports": {
330
382
  "./Data/Maybe": {
331
383
  "ply": "./dist/Data/Maybe.d.ply",
@@ -335,13 +387,16 @@ A package is described by `package.json`.
335
387
  }
336
388
  ```
337
389
 
338
- A `foreign` word has no body. Its implementation lives in the foreign directory, and the number of JavaScript parameters matches the input length. Leave it out of the `@` list when it stays in this file. A foreign word that receives an array or a `StrMap` does not change that value. It copies, then writes. `dup` shares the reference. `Cell` is the type of a shared cell.
390
+ A foreign word ends with `**` and has no body. Its implementation lives in the foreign directory, and the number of JavaScript parameters matches the input length. Leave it out of the `@` list when it stays in this file. A foreign word that receives an array or a `StrMap` does not change that value. It copies, then writes. `dup` shares the reference. `Cell` is the type of a shared cell. A foreign type has no constructors and ends with `*`:
339
391
 
340
392
  ```ply
341
- : foreign length ( (Array a) -- Num )
393
+ .Hub ( a. ) *
394
+ .Gate () *
395
+
396
+ .length ( (Array a) -- Num ) **
342
397
  ```
343
398
 
344
- The foreign directory is `foreign` when that directory exists, or the string `ply.foreign` when the field is set. It is required when the package has such a word. It lies inside the package and is distinct from the source directory and the output directory. The generated `.js` imports the implementation from there. Those files are not copied into `dist`. A new package writes `ply.language` and leaves `source`, `foreign`, and `out` unset: those directories are `src`, `foreign`, and `dist`. `ply.entry` names the module a pathless package import opens, and the `exports` key `.`. `plylang compile` writes one classic script for a page. Without `--out` that file is `script.js` in the output directory. `--entry`, `--out`, and `--foreign` override the entry module, the script path, and the foreign directory for that one run. They are not written back. The entry module's `main` of scheme `( -- Unit )` is the root, and the script calls `main()`. A module with no `main` and at least one place roots the script at those places and does not call `main`. The bindings that root reaches are inlined.
399
+ The foreign directory is `foreign` when that directory exists, or the string `ply.foreign` when the field is set. It is required when the package has a `**` word. It lies inside the package and is distinct from the source directory and the output directory. The generated `.js` imports the implementation from there. Those files are not copied into `dist`. A new package writes `ply.language` (`2`) and leaves `source`, `foreign`, and `out` unset: those directories are `src`, `foreign`, and `dist`. `ply.entry` names the module a pathless package import opens, and the `exports` key `.`. `plylang compile` writes one classic script for a page. Without `--out` that file is `script.js` in the output directory. `--entry`, `--out`, and `--foreign` override the entry module, the script path, and the foreign directory for that one run. They are not written back. The entry module's top-level executable code is the root of the script, and the bindings it reaches are inlined.
345
400
 
346
401
  ```text
347
402
  plylang check [dir]
@@ -353,26 +408,26 @@ plylang check --parse <file>
353
408
  plylang check --js <file>
354
409
  ```
355
410
 
356
- `plylang check` checks every file under the source directory, the imports, `foreign`, and each `?` check. It writes no files. The default directory is the current one. `plylang build` checks the modules and writes them into the output directory. It leaves each `?` check out of that typecheck and out of both files. An import is written when the built program uses that name, when `@` re-exports it, or when an instance in this file binds that imported word. A `.js` file is written when the module has a JavaScript export, `main`, a place, or is `ply.entry`. A `.d.ply` file is written when `@` names something, or when the file writes an instance an importer can see. A file with an empty `@`, no such instance, and no `main`, place, or entry writes nothing, and a file already at that path is removed. A `.js` or `.d.ply` under the output directory is removed when its path is a module path and that source file is gone. Any other file stays, including the page script `script.js`. An empty directory under the output directory is removed. The output directory itself stays. `build` does not change `exports`. `plylang build --exports` refreshes the `ply` and `import` paths of modules already listed in `exports`. A source file is added when its name is passed, as in `plylang build --exports Data/Sheet`, or when `--all` is passed. `--all` adds each source module whose `@` names something, and each source module that writes an instance an importer can see. Several names may follow `--exports`. A name that is not a source file is an error, and this run writes nothing. Names that stay out of the list are printed under `not exported`. `plylang build --exports --check` prints that report and leaves `package.json` unchanged. The modules are still written. The exit code is 1 when a path would change, an entry names a module that is gone, or `--all` would add a module whose `@` names something or that writes an instance an importer can see. `not exported` by itself leaves the exit code at 0. `--all` together with a module name is rejected. `--watch` cannot be combined with `--exports`. A module whose file is gone is removed when its entry has a `ply` field. A key that does not name a module of this package stays as written. On a module that stays, the `ply` and `import` strings are updated and the other fields stay. A module with no `.js` keeps `ply` and has no `import`. `.` follows `ply.entry` from the file on disk when that module is listed or is being added. With no `ply.entry`, a hand-written `.` stays. Each written path uses `ply.out` from that file. `--entry`, `--out`, and `--foreign` apply to this run and stay out of `package.json`. A module of this package is imported by its path under the source directory, whether or not `exports` lists it. Another package finds the module through the `exports` entry.
411
+ `plylang check` checks every file under the source directory, the imports, foreign words, and each `?` check. It writes no files. The default directory is the current one. `plylang build` checks the modules and writes them into the output directory. It leaves each `?` check out of that typecheck and out of both files. An import is written when the built program uses that name, when `@` re-exports it, or when an instance in this file binds that imported word. A `.js` file is written when the module has a JavaScript export, top-level executable code, or is `ply.entry`. A `.d.ply` file begins with `language 2` and is written when `@` names something, or when the file writes an instance an importer can see. A file with `() @` (or no `@`), no such instance, no top-level code, and not `ply.entry` writes nothing, and a file already at that path is removed. A `.js` or `.d.ply` under the output directory is removed when its path is a module path and that source file is gone. Any other file stays, including the page script `script.js`. An empty directory under the output directory is removed. The output directory itself stays. `build` does not change `exports`. `plylang build --exports` refreshes the `ply` and `import` paths of modules already listed in `exports`. A source file is added when its name is passed, as in `plylang build --exports Data/Sheet`, or when `--all` is passed. `--all` adds each source module whose `@` names something, and each source module that writes an instance an importer can see. Several names may follow `--exports`. A name that is not a source file is an error, and this run writes nothing. Names that stay out of the list are printed under `not exported`. `plylang build --exports --check` prints that report and leaves `package.json` unchanged. The modules are still written. The exit code is 1 when a path would change, an entry names a module that is gone, or `--all` would add a module whose `@` names something or that writes an instance an importer can see. `not exported` by itself leaves the exit code at 0. `--all` together with a module name is rejected. `--watch` cannot be combined with `--exports`. A module whose file is gone is removed when its entry has a `ply` field. A key that does not name a module of this package stays as written. On a module that stays, the `ply` and `import` strings are updated and the other fields stay. A module with no `.js` keeps `ply` and has no `import`. `.` follows `ply.entry` from the file on disk when that module is listed or is being added. With no `ply.entry`, a hand-written `.` stays. Each written path uses `ply.out` from that file. `--entry`, `--out`, and `--foreign` apply to this run and stay out of `package.json`. A module of this package is imported by its path under the source directory, whether or not `exports` lists it. Another package finds the module through the `exports` entry.
357
412
 
358
- A check stands in column 0.
413
+ A check (probe) ends with `?`:
359
414
 
360
415
  ```ply
361
- ? "seed stays" < 0 > [
416
+ "seed stays" < 0 > [
362
417
  < 2 10 Occurrence > streamFrom
363
418
  0 stepper
364
419
  0 runAt
365
420
  1 valueAt
366
- ]
421
+ ] ?
367
422
  ```
368
423
 
369
424
  The name is a string literal. The picture is the stack from the bottom upward. The quotation is the program, and it starts from an empty stack. `< 1 2 >` is two values. `<>` is an empty stack. The quotation calls words of this file, including a word that stays off the `@` list. A word the check alone needs is written inside the quotation. The check is not a published name.
370
425
 
371
- `plylang test` builds the package, then finds `?` in source files that match one pattern. The pattern is relative to the source directory. The default is `**/*.ply`. `*` matches inside one path segment. `**` matches across directories. Quote the pattern so the shell does not expand `*`. A file with no `?` is left out. The runner typechecks each check and runs it. The comparison is structural. A failure prints the name, `file:line`, and both stack pictures. One failed check still lets the other checks in that file run. A throw is a failure of that named check. One file that does not compile still lets the other files run. The runner writes the check into a temporary directory inside the package and deletes that directory when the run ends, including when a check fails to compile. The modules in the output directory stay, and they contain no check. A source file cannot import a package that is only a `devDependency`. A failed check, a compile error, or a throw exits 1. No file matched exits 1 with `no tests matched`. Color is used when stdout is a terminal and `NO_COLOR` is unset.
426
+ `plylang test` builds the package, then finds `?` checks in source files that match one pattern. The pattern is relative to the source directory. The default is `**/*.ply`. `*` matches inside one path segment. `**` matches across directories. Quote the pattern so the shell does not expand `*`. A file with no `?` is left out. The runner typechecks each check and runs it. The comparison is structural. A failure prints the name, `file:line`, and both stack pictures. One failed check still lets the other checks in that file run. A throw is a failure of that named check. One file that does not compile still lets the other files run. The runner writes the check into a temporary directory inside the package and deletes that directory when the run ends, including when a check fails to compile. The modules in the output directory stay, and they contain no check. A source file cannot import a package that is only a `devDependency`. A failed check, a compile error, or a throw exits 1. No file matched exits 1 with `no tests matched`. Color is used when stdout is a terminal and `NO_COLOR` is unset.
372
427
 
373
- A word named `main` whose scheme is `( -- Unit )` is the program of that module. It may stay off the `@` list. `build` calls it at the bottom of that module's `.js`, and only when Node started that file. `node dist/App.js` runs the program. An import of the module does not. A `package.json` script can name that file. Several modules may each have a `main`. A module without one is a library. Any other scheme on the name `main` is an error. `plylang compile` also writes those modules, then the page script. `--out` names that file. Otherwise it is `script.js` beside the modules. `--entry` names the module for this run, instead of `ply.entry`. The root is `main` when that module defines `main` of scheme `( -- Unit )`, and the script calls `main()`. A module with no `main` and at least one place roots the script at those places, and the script does not call `main`. A module with neither is an error. A binding is inlined when the root can call it. The modules themselves still carry the Node guard. `compile` does not rewrite `exports`. `--foreign` names the foreign directory for this run. A relative `--out` or `--foreign` stays inside the package.
428
+ A module with top-level executable code runs that code when loaded, so `node dist/App.js` runs the program directly and a `package.json` script can name that file. A module without top-level code is a pure library. `plylang compile` also writes the modules, then one classic page script. `--out` names that file; otherwise it is `script.js` beside the modules. `--entry` names the module for this run, instead of `ply.entry`. The entry module must contain top-level executable code, which roots the page script, and bindings reachable from it are inlined. `compile` does not rewrite `exports`. `--foreign` names the foreign directory for this run. A relative `--out` or `--foreign` stays inside the package.
374
429
 
375
- `plylang docs` writes `DOCS.md` at the package root. `--out` names another file. A module heading is the string used to import it: the package name and the path, or only the path when there is no name. The prose comes from `..`. Inside a module the sections are `Types`, `Classes`, `Instances`, and `Words`. An empty section is left out. The name of a type, a class, or a word is a heading under its section. A method is a heading after its class. The class heading carries the class text. The method heading carries that method's text. `Instances` lists each class written in that file, one class on a line. A line has the class and the type, with no comment and no method bindings.
430
+ `plylang docs` writes `DOCS.md` at the package root. `--out` names another file. A module heading is the string used to import it: the package name and the path, or only the path when there is no name. The prose comes from `//`. Inside a module the sections are `Types`, `Classes`, `Instances`, and `Words`. An empty section is left out. The name of a type, a class, or a word is a heading under its section. A method is a heading after its class. The class heading carries the class text. The method heading carries that method's text. `Instances` lists each class written in that file, one class on a line. A line has the class and the type, with no comment and no method bindings.
376
431
 
377
432
  `plylang check --parse` prints how one file was parsed, and it does not check types. `plylang check --js` prints the JavaScript that `build` would write for that file. Outside a package that is the JavaScript of the one file, and imports are not resolved.
378
433
 
@@ -390,7 +445,7 @@ cargo build --release
390
445
 
391
446
  `target/release/plylang` and `target/release/plylang-ls` are the compiler and the server. The server speaks LSP over stdio. In a package with a `ply` field it resolves imports the same way `plylang build` does.
392
447
 
393
- `@plylang/plylang` installs the `plylang` command. `@plylang/language-server` installs `plylang-ls` and depends on the same version of the compiler. Publish the compiler and its six platform packages before the server package and its six platform packages. `.github/workflows/publish.yml` does that when a GitHub release is published. Version `1.x` is language `1`. A minor or a patch of the compiler does not change the language number. `PLYLANG_BIN` and `PLYLANG_LS_BIN` supply a binary of their own.
448
+ `@plylang/plylang` installs the `plylang` command. `@plylang/language-server` installs `plylang-ls` and depends on the same version of the compiler. Publish the compiler and its six platform packages before the server package and its six platform packages. `.github/workflows/publish.yml` does that when a GitHub release is published. Version `2.x` is language `2`. A minor or a patch of the compiler does not change the language number. `PLYLANG_BIN` and `PLYLANG_LS_BIN` supply a binary of their own.
394
449
 
395
450
  `node script/stage-native.js <platform> <binary-dir> <out-dir> plylang` writes one compiler platform package. The same command with `plylang-ls` writes `@plylang/language-server-<os>-<arch>`. Each archive holds one binary.
396
451
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plylang/plylang",
3
- "version": "1.3.4",
3
+ "version": "2.0.1",
4
4
  "description": "Ply compiler",
5
5
  "license": "MIT",
6
6
  "author": "Algosail",
@@ -30,12 +30,12 @@
30
30
  "bin/native.js"
31
31
  ],
32
32
  "optionalDependencies": {
33
- "@plylang/plylang-darwin-arm64": "1.3.4",
34
- "@plylang/plylang-darwin-x64": "1.3.4",
35
- "@plylang/plylang-linux-arm64": "1.3.4",
36
- "@plylang/plylang-linux-x64": "1.3.4",
37
- "@plylang/plylang-win32-arm64": "1.3.4",
38
- "@plylang/plylang-win32-x64": "1.3.4"
33
+ "@plylang/plylang-darwin-arm64": "2.0.1",
34
+ "@plylang/plylang-darwin-x64": "2.0.1",
35
+ "@plylang/plylang-linux-arm64": "2.0.1",
36
+ "@plylang/plylang-linux-x64": "2.0.1",
37
+ "@plylang/plylang-win32-arm64": "2.0.1",
38
+ "@plylang/plylang-win32-x64": "2.0.1"
39
39
  },
40
40
  "scripts": {
41
41
  "build": "cargo build --release",