mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-29 04:24:43 +00:00
59 lines
1.6 KiB
Markdown
59 lines
1.6 KiB
Markdown
# 結構
|
|
|
|
[](https://gezf7g7pd5.execute-api.ap-northeast-1.amazonaws.com/default/source_up_to_date?owner=erg-lang&repos=erg&ref=main&path=doc/EN/API/modules/external/alstruct.md&commit_hash=14657486719a134f494e107774ac8f9d5a63f083)
|
|
|
|
模塊為它們提供代表代數結構和補丁的Trait
|
|
|
|
* 成員
|
|
|
|
## BinOp(二進制運算)
|
|
|
|
```python
|
|
BinOp Op: Kind 2 = Subsume Op(Self, Self.ReturnTypeOf Op), Additional: {
|
|
.ReturnTypeof = TraitType -> Type
|
|
}
|
|
|
|
Nat <: BinOp Add
|
|
assert Nat. ReturnTypeof(Add) == Nat
|
|
assert Nat. ReturnTypeof(Sub) == Int
|
|
assert Nat. ReturnTypeof(Mul) == Nat
|
|
assert Nat.ReturnTypeof(Div) == Positive Ratio
|
|
```
|
|
|
|
## SemiGroup(半群)
|
|
|
|
```python
|
|
SemiGroup Op: Kind 2 = Op(Self, Self)
|
|
|
|
IntIsSemiGroupAdd = Patch Int, Impl=SemiGroupAdd
|
|
|
|
Int <: SemiGroup Add
|
|
```
|
|
|
|
## Functor(函子)
|
|
|
|
```python
|
|
# * Identity law: x.map(id) == x
|
|
# * Composition law: x.map(f).map(g) == x.map(f.then g)
|
|
Functor = Trait {
|
|
.map|T, U: Type| = (Self(T), T -> U) -> Self U
|
|
}
|
|
```
|
|
|
|
## Applicative(應用)
|
|
|
|
```python
|
|
# * Identity law: x.app(X.pure(id)) == x
|
|
Applicative = Subsume Functor, Additional: {
|
|
.pure|T: Type| = T -> Self T
|
|
.app|T, U: Type| = (Self(T), Self(T -> U)) -> Self U
|
|
}
|
|
```
|
|
|
|
## Monad(單子)
|
|
|
|
```python
|
|
Monad = Subsume Applicative, Additional: {
|
|
.bind|T, U: Type| = (Self(T), T -> Self U) -> Self U
|
|
}
|
|
```
|