Merge branch 'main' into fix-#247

This commit is contained in:
Shunsuke Shibayama 2022-11-27 20:31:50 +09:00 committed by GitHub
commit a373185dec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 62 additions and 25 deletions

View file

@ -1,4 +1,8 @@
class Array(list): class Array(list):
def dedup(self):
return Array(list(set(self)))
def dedup_by(self, f):
return Array(list(set(map(f, self))))
def push(self, value): def push(self, value):
self.append(value) self.append(value)
return self return self

View file

@ -8,3 +8,32 @@ class Str(str):
return Str(s) return Str(s)
else: else:
return Error("Str can't be other than str") return Error("Str can't be other than str")
def get(self, i: int):
if len(self) > i:
return Str(self[i])
else:
return None
class StrMut(Str):
def try_new(s: str):
if isinstance(s, str):
return StrMut(s)
else:
return Error("Str! can't be other than str")
def clear(self):
self = ""
def pop(self):
if len(self) > 0:
last = self[-1]
self = self[:-1]
return last
else:
return Error("Can't pop from empty `Str!`")
def push(self, c: str):
self += c
def remove(self, idx: int):
char = self[idx]
self = self[:idx] + self[idx+1:]
return char
def insert(self, idx: int, c: str):
self = self[:idx] + c + self[idx:]

View file

@ -47,7 +47,7 @@ assert points.iter().map(x -> x.norm()).collect(Array) == [5, 25].
## Trait inclusion ## Trait inclusion
The expansion operator `...` allows you to define a trait that contains a certain trait as a supertype. This is called the __subsumption__ of a trait. `Subsume` allows you to define a trait that contains a certain trait as a supertype. This is called the __subsumption__ of a trait.
In the example below, `BinAddSub` subsumes `BinAdd` and `BinSub`. In the example below, `BinAddSub` subsumes `BinAdd` and `BinSub`.
This corresponds to Inheritance in a class, but unlike Inheritance, multiple base types can be combined using `and`. Traits that are partially excluded by `not` are also allowed. This corresponds to Inheritance in a class, but unlike Inheritance, multiple base types can be combined using `and`. Traits that are partially excluded by `not` are also allowed.

View file

@ -9,7 +9,7 @@ However, external libraries may not support multiple languages.
```python ```python
record: {.name = Str; .age = Nat; .height = CentiMeter} record: {.name = Str; .age = Nat; .height = CentiMeter}
{height; rest; ...} = record {height; ...rest} = record
mut_record = {.height = !height; ...rest} mut_record = {.height = !height; ...rest}
``` ```
@ -71,7 +71,7 @@ method 1:
```python ```python
arr = [...] arr = [...]
for! arr.iter().enumerate(start: 1), i => for! arr.iter().enumerate(start := 1), i =>
... ...
``` ```
@ -118,16 +118,18 @@ C.
... ...
``` ```
## Want the argument names to be identified on the type system ## When implementing a trait's methods, warnings are given for variables that were not used
You can receive arguments by record. You can use `discard`.
```python ```python
Point = {x = Int; y = Int} T = Trait {.f = (Self, x: Int, s: Str) -> Int}
norm: Point -> Int C = Class T
norm({x: Int; y: Int}): Int = x**2 + y**2 C|<: T|.
assert norm({x = 1; y = 2}) == norm({y = 2; x = 1}) f self, x, s =
discard s
...
``` ```
## Want to stop warnings ## Want to stop warnings

View file

@ -34,7 +34,7 @@ f: (x: Int, y: Int) -> Int
f: (Int, Int) -> Int f: (Int, Int) -> Int
``` ```
引数名を明示して宣言した場合、定義時に名前が違うと型エラーとなります。引数名の任意性を与えたい場合は2番目の方法で宣言すると良いでしょう。その場合、型検査で見られるのはメソッド名とその型のみです。 引数名を明示して宣言した場合、定義時に名前が違うと型エラーとなります。引数名の任意性を与えたい場合は2番目の方法で宣言すると良いでしょう。その場合、型検査で見られるのはメソッド名とその型のみです。キーワード指定による呼び出しはできなくなります。
```python ```python
T = Trait { T = Trait {

View file

@ -83,7 +83,7 @@ f u := 6, v := 5, w := 4, x := 1, y := 2, z := 3
ある引数が大抵の場合決まりきっており省略できるようにしたい場合、デフォルト引数を使うと良いでしょう。 ある引数が大抵の場合決まりきっており省略できるようにしたい場合、デフォルト引数を使うと良いでしょう。
デフォルト引数は`:=`(or-assign operator)で指定します。`base`が指定されなかったら`math.E``base`に代入します。 デフォルト引数は`:=`(default-assign operator)で指定します。`base`が指定されなかったら`math.E``base`に代入します。
```python ```python
math_log x: Ratio, base := math.E = ... math_log x: Ratio, base := math.E = ...

View file

@ -49,7 +49,7 @@ assert points.iter().map(x -> x.norm()).collect(Array) == [5, 25]
## トレイトの包摂 ## トレイトの包摂
展開演算子`...`によって、あるトレイトを上位型として含むトレイトを定義できます。これをトレイトの __包摂(Subsumption)__ と呼びます。 関数`Subsume`によって、あるトレイトを上位型として含むトレイトを定義できます。これをトレイトの __包摂(Subsumption)__ と呼びます。
下の例でいうと、`BinAddSub``BinAdd``BinSub`を包摂しています。 下の例でいうと、`BinAddSub``BinAdd``BinSub`を包摂しています。
これはクラスにおける継承(Inheritance)に対応しますが、継承と違い複数の基底型を`and`で合成して指定できます。`not`によって一部を除外したトレイトでもOKです。 これはクラスにおける継承(Inheritance)に対応しますが、継承と違い複数の基底型を`and`で合成して指定できます。`not`によって一部を除外したトレイトでもOKです。
@ -147,15 +147,15 @@ SafeDiv R, O = Subsume Div, {
```python ```python
Add R = Trait { Add R = Trait {
.Output = Type .Output = Type
.`_+_` = Self.(R) -> .Output .`_+_` = (Self, R) -> .Output
} }
Sub R = Trait { Sub R = Trait {
.Output = Type .Output = Type
.`_-_` = Self.(R) -> .Output .`_-_` = (Self, R) -> .Output
} }
Mul R = Trait { Mul R = Trait {
.Output = Type .Output = Type
.`*` = Self.(R) -> .Output .`*` = (Self, R) -> .Output
} }
``` ```

View file

@ -11,7 +11,7 @@
```python ```python
record: {.name = Str; .age = Nat; .height = CentiMeter} record: {.name = Str; .age = Nat; .height = CentiMeter}
{height; rest; ...} = record {height; ...rest} = record
mut_record = {.height = !height; ...rest} mut_record = {.height = !height; ...rest}
``` ```
@ -73,7 +73,7 @@ method 1:
```python ```python
arr = [...] arr = [...]
for! arr.iter().enumerate(start: 1), i => for! arr.iter().enumerate(start := 1), i =>
... ...
``` ```
@ -120,16 +120,18 @@ C.
... ...
``` ```
## 引数名を型システム上で識別させたい ## トレイトのメソッドを実装する際に、使わなかった変数の警告が出る
引数をレコードで受け取ると良いでしょう。 `discard`を使うとよいでしょう。
```python ```python
Point = {x = Int; y = Int} T = Trait {.f = (Self, x: Int, s: Str) -> Int}
norm: Point -> Int C = Class T
norm({x: Int; y: Int}): Int = x**2 + y**2 C|<: T|.
assert norm({x = 1; y = 2}) == norm({y = 2; x = 1}) f self, x, s =
discard s
...
``` ```
## 警告を出さないようにしたい ## 警告を出さないようにしたい

View file

@ -49,7 +49,7 @@ assert points.iter().map(x -> x.norm()).collect(Array) == [5, 25].
## Trait包含 ## Trait包含
扩展运算符 `...` 允许您将包含某个Trait的Trait定义为超类型。这称为Trait的 __subsumption__ `Subsume` 允许您将包含某个Trait的Trait定义为超类型。这称为Trait的 __subsumption__
在下面的示例中,`BinAddSub` 包含 `BinAdd``BinSub` 在下面的示例中,`BinAddSub` 包含 `BinAdd``BinSub`
这对应于类中的继承,但与继承不同的是,可以使用"和"组合多个基类型。也允许被 `not` 部分排除的Trait 这对应于类中的继承,但与继承不同的是,可以使用"和"组合多个基类型。也允许被 `not` 部分排除的Trait

View file

@ -49,7 +49,7 @@ assert points.iter().map(x -> x.norm()).collect(Array) == [5, 25].
## Trait包含 ## Trait包含
擴展運算符 `...` 允許您將包含某個Trait的Trait定義為超類型。這稱為Trait的 __subsumption__ `Subsume` 允許您將包含某個Trait的Trait定義為超類型。這稱為Trait的 __subsumption__
在下面的示例中,`BinAddSub` 包含 `BinAdd``BinSub` 在下面的示例中,`BinAddSub` 包含 `BinAdd``BinSub`
這對應于類中的繼承,但與繼承不同的是,可以使用"和"組合多個基類型。也允許被 `not` 部分排除的Trait 這對應于類中的繼承,但與繼承不同的是,可以使用"和"組合多個基類型。也允許被 `not` 部分排除的Trait