Skip to content

Commit 906f1bd

Browse files
committed
language improvements
- Arithmetic operators: - Division operators can't have a wrapping or saturating version - Use `%` prefix for wrapping version (instead of `@`) - Removing skipping version of the operators - New extending type of operators - Changing `=?`, `=!`, `>=?` `<=?` to `=?=`, `=/=`, `>?=`, `<?=` - Leaving `_ |? _ |: _` for ternary operator and changing pattern matching to `>| _ => _` - Clarifying syntax for generic closure function arguments with `fx _: F: a -> b => a, F -> b`
1 parent 1983fb9 commit 906f1bd

File tree

6 files changed

+90
-40
lines changed

6 files changed

+90
-40
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Changelog
2+
3+
## 1st Nov 2024
4+
5+
- Arithmetic operators:
6+
- Division operators can't have a wrapping or saturating version
7+
- Use `%` prefix for wrapping version (instead of `@`)
8+
- Removing skipping version of the operators
9+
- New extending type of operators
10+
- Changing `=?`, `=!`, `>=?` `<=?` to `=?=`, `=/=`, `>?=`, `<?=`
11+
- Leaving `_ |? _ |: _` for ternary operator and changing pattern matching to `>| _ => _`
12+
- Clarifying syntax for generic closure function arguments with `fx _: F: a -> b => a, F -> b`

content/category.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ val projA := new.a
134134
val projB := new.b
135135
136136
-- testing that the result is the same as the original values:
137-
projA =? valueA && projB =? valueB !! mustBeEqual
137+
projA =?= valueA && projB =?= valueB !! mustBeEqual
138138
```
139139

140140
<aside>

content/design.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,22 @@ components and pattern-match against them. In other words, injection operators a
190190
they are the way how *if-else* statements and pattern matching is handled in the language.
191191

192192
There are several ways of doing branching constructions using injection operators. First, one can use `value >|` in
193-
combination with `pattern |?` expressions to match the value against a set of patterns and handle a default variant
194-
with `|:`:
193+
combination with `pattern => code` expressions to match the value against a set of patterns and handle a default variant
194+
with `_ => code`:
195+
196+
<aside>
197+
<p>We use `=>` operator to signify that each pattern is in fact a natural transformation</p>
198+
</aside>
195199

196200
```
197201
data WorldDirection: north | south | west | east
198202
199203
val sample: WorldDirection
200204
201205
sample >| – match sample {
202-
north |? doSomething1 – north => {}
203-
south |? doSomething2 – south => {}
204-
|: doSomething3 – _ => {}
206+
north => doSomething1 – north => {}
207+
south => doSomething2 – south => {}
208+
_ => doSomething3 – _ => {}
205209
– }
206210
```
207211

@@ -221,11 +225,11 @@ condition. Let's assume we have an expression `test` which results in a boolean
221225
</aside>
222226

223227
```
224-
sample =? north – if sample == north
228+
sample =?= north – if sample == north
225229
|? doSomething1
226230
|: doSomething2 – else
227231
228-
sample =? north – if sample == north
232+
sample =?= north – if sample == north
229233
|? doSomething1
230234
|: sample =? south – else if sample == south
231235
|? doSomething2
@@ -549,22 +553,22 @@ class ord
549553
550554
@final
551555
infx `<?`: a Self, b Self -> Bool
552-
~ a >? b
556+
~ (a >? b)
553557
554558
class eq: ord
555-
infx `=?`: Self, Self -> Bool
559+
infx `=?=`: Self, Self -> Bool
556560
557561
@final
558-
infx `=!`: a Self, b Self -> Bool
562+
infx `=/=`: a Self, b Self -> Bool
559563
~ a =? b
560564
561565
@final
562-
infx `>=?`: a Self, b Self -> Bool
563-
a =? b |? true |: a >? b
566+
infx `>?=`: a Self, b Self -> Bool
567+
a =?= b |? true |: a >? b
564568
565569
@final
566-
infx `<=?`: a Self, b Self -> Bool
567-
a =? b |? true |: a <? b
570+
infx `<?=`: a Self, b Self -> Bool
571+
a =?= b |? true |: a <? b
568572
```
569573

570574
<aside>

content/examples.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ class iter: item any
9898
fx next: _ -> item?
9999
100100
infx findFirst: V eq, I iter(item eq) => iter I, value V -> U64?
101-
enumerate iter |> fst =? value |? $ := lst _
101+
enumerate iter |> fst =?= value |? $ := lst _
102102
103103
infx hasUnique: V eq, I iter(item eq) => iter I, value V -> Bool
104104
val index := iter findFirst value !! false
105-
(iter[index..] |> =? value |? $ := false) ?? true
105+
(iter[index..] |> =?= value |? $ := false) ?? true
106106
107107
[0, 1, 2, 3, 4] hasUnique 1 -- true
108108
```
@@ -136,18 +136,18 @@ class ord
136136
~ a >? b
137137
138138
class eq: ord
139-
infx `=?`: Self, Self -> Bool
139+
infx `=?=`: Self, Self -> Bool
140140
141141
@final
142-
infx `=!`: a Self, b Self -> Bool
143-
~ a =? b
142+
infx `=/=`: a Self, b Self -> Bool
143+
~ (a =?= b)
144144
145145
@final
146-
infx `>=?`: a Self, b Self -> Bool
147-
a =? b |? true |: a >? b
146+
infx `>?=`: a Self, b Self -> Bool
147+
a =?= b |? true |: a >? b
148148
149149
@final
150-
infx `<=?`: a Self, b Self -> Bool
151-
a =? b |? true |: a <? b
150+
infx `<?=`: a Self, b Self -> Bool
151+
a =?= b |? true |: a <? b
152152
```
153153

content/reference.md

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,22 @@ Call of an infix function `infn` is `arg0 infn arg1, .., argLast`. It can be rev
168168

169169
#### Injections
170170

171-
Constructs with `>|`, `|?`, `|:` symbols are called branching operators.
172-
They correspond to `match { .. -> .., .. -> .. }` construction.
171+
Injections are branching operators, which allow code execution to branch execution basing on a value of a co-product
172+
type. They are either pattern matching (an equivalent of Rust `match` and Haskell `case`), or a boolean-test based.
173173

174-
Operator `.. |? .. |: ..` is also the ternary operator, corresponding to
175-
`if .. then .. else` construction. It also has a form of
176-
`condition1 |? statement1 |: condition2 |? statement2 |: statementElse`,
177-
corresponding to
174+
Pattern matching is done with `>|` infix operator, which takes a value of a co-product type on the left side, and a
175+
set of natural transformations matching each of the injections on the right:
176+
177+
```idris
178+
value >|
179+
pattern1 => code
180+
pattern2 => code
181+
_ => code -- default match
182+
```
183+
184+
Branching based on the boolean conditions uses `.. |? .. |: ..` operator, which is also a ternary operator,
185+
corresponding to `if .. then .. else` construction. It also has a form of
186+
`condition1 |? statement1 |: condition2 |? statement2 |: statementElse`, corresponding to
178187
`if condition1 then statement1 elseIf condition2 then statement2 else statementElse`.
179188

180189
#### Annotations
@@ -322,9 +331,9 @@ postfixed with a symbol representing the way of handling overflow, underflow or
322331
- `?` for converting the result of the operation into `Maybe` monad;
323332
- `!` for converting the result of the operation into `Result::error` monad variant with details on specific condition
324333
which has occurred;
325-
- `@` for wrapping a value in case of overflow;
334+
- `%` for wrapping a value in case of overflow (modulo-arithmetic) and returning `ArRes` monad;
326335
- `^` for saturating a value with a maximum possible value in case of overflow;
327-
- `|` for skipping the operation as whole.
336+
- with no postfix for extending the result type to the next bit dimension so it always fits.
328337

329338
Additionally, Cation allows <dfn>native arithmetic operations</dfn> when an operation itself and the resulting type
330339
guarantees impossibility of overflow or other exceptional conditions. These operations are:
@@ -341,14 +350,14 @@ guarantees impossibility of overflow or other exceptional conditions. These oper
341350

342351
Thus, the resulting table of the arithmetic operations is the following:
343352

344-
| Operation | Native | Maybe | Result | Wrapping | Saturating | Skipping |
345-
|-----------------|--------|-------|--------|----------|------------|----------|
346-
| Addition | `+` | `+?` | `+!` | `+@` | `+^` | `+`\| |
347-
| Subtraction | `-` | `-?` | `-!` | `-@` | `-^` | `-`\| |
348-
| Multiplication | `*` | `*?` | `*!` | `*@` | `*^` | `*`\| |
349-
| Division | `/` | `/?` | `/!` | `/@` | `/^` | `/`\| |
350-
| Modulo division | `%` | `%?` | `%!` | `%@` | `%^` | `%`\| |
351-
| Potentiation | `^` | `^?` | `^!` | `^@` | `^^` | `^`\| |
353+
| Operation | Native | Maybe | Result | Wrapping | Saturating | Extending |
354+
|-----------------|--------|-------|--------|----------|------------|-----------|
355+
| Addition | `+` | `+?` | `+!` | `+%` | `+^` | `+` |
356+
| Subtraction | `-` | `-?` | `-!` | `-%` | `-^` | `-` |
357+
| Multiplication | `*` | `*?` | `*!` | `*%` | `*^` | `*` |
358+
| Division | `/` | `/?` | `/!` | n/a | n/a | n/a |
359+
| Modulo division | `%` | `%?` | `%!` | n/a | n/a | n/a |
360+
| Potentiation | `^` | `^?` | `^!` | `^%` | `^^` | `^` |
352361

353362
#### Bitwise operators
354363

content/stdlib.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,28 @@ template = "page.html"
3636

3737
- `read`
3838
- `write`
39+
40+
## Monads
41+
42+
```idris
43+
data Maybe: T => none | some(T)
44+
45+
data Result: T, E => err(E) | ok(T)
46+
47+
data Either: L, R => left(L) | right(R)
48+
49+
data Tern: N, P => neg(N)#0xff | zero#0 | pos(p)#1
50+
51+
data MayError: R, E => ok T, err Maybe E
52+
53+
fx map: T, U, F: T -> U => Maybe T, F -> Maybe U
54+
none, _ => none
55+
some(val), f => some(f val)
56+
57+
infx `=?=`: T eq => _ Maybe T, _ Maybe T -> Bool
58+
none, none => true
59+
some(_), none | none, some(_) => false
60+
some(v), some(w) => v =?= w
61+
62+
impl eq: T eq => Maybe T
63+
```

0 commit comments

Comments
 (0)