# Taida Lang -- 構文リファレンス

> **[PHILOSOPHY.md](/_corpus/PHILOSOPHY.md) -- I.** 深く考えずに適当にぶちこんでけ

Taida Lang は AI 協業時代のプログラミング言語です。演算子は 10 種のみ。null は存在しません。全ての型にデフォルト値が保証され、暗黙の型変換は一切行われません。

---

## 演算子 (Taida 固有の 10 演算子)

Taida の言語設計において特別な意味を持つ演算子は 10 種だけです。

| # | 演算子 | 名前 | 用途 |
|---|--------|------|------|
| 1 | `=` | 定義 | 型、関数、エラー天井の定義 |
| 2 | `=>` | 右ぶち (前方代入) | パイプラインと代入 (左から右) |
| 3 | `<=` | 左ぶち (後方代入) | 代入と関数適用 (右から左) |
| 4 | `]=>` | 前方アンモールド | Mold から値を取り出す (左から右) |
| 5 | `<=[` | 後方アンモールド | Mold から値を取り出す (右から左) |
| 6 | `\|==` | エラー天井 | エラーの捕捉と処理 |
| 7 | `\|` | 条件ガード | 分岐の区切り |
| 8 | `\|>` | 条件抽出 | 分岐値の取り出し |
| 9 | `>>>` | インポート | モジュールの読み込み |
| 10 | `<<<` | エクスポート | シンボルの公開 |

### その他の演算子 (10 種には含まれない)

一般的なプログラミング言語に共通する演算子です:

| カテゴリ | 演算子 |
|----------|--------|
| 算術 | `+` `-` `*` (`/` と `%` はなし -- `Div` と `Mod` モールドを使用) |
| 比較 | `==` `!=` `<` `>` `>=` (`<=` はなし -- 左ぶち演算子として使用) |
| 論理 | `&&` `\|\|` `!` |

### 単一方向制約

一文内で `=>` と `<=` を混在させることはできません。同様に `]=>` と `<=[` も混在不可です。

```taida
// OK: single direction
data => filter(_) => map(_) => result
result <= map(_) <= filter(_) <= data

// NG: mixed directions (compile error)
data => filter(_) <= result
```

---

## 基本型

全ての型にデフォルト値が保証されています。null や undefined は存在しません。

| 型 | デフォルト値 | 例 |
|----|-------------|-----|
| `Int` | `0` | `x <= 42` |
| `Float` | `0.0` | `pi <= 3.14` |
| `Str` | `""` | `name <= "Rei"` |
| `Bool` | `false` | `active <= true` |

除算と剰余は `Lax[T]` を返し、ゼロ除算エラーを排除します:

```taida
Div[10, 3]() ]=> result   // 3
Div[10, 0]() ]=> result   // 0 (default value, no error)
Mod[10, 3]() ]=> remainder // 1
```

---

## BuchiPack `@(...)` とリスト `@[...]`

> **[PHILOSOPHY.md](/_corpus/PHILOSOPHY.md) -- II.** だいじなものはふくろにしまっておきましょう

### BuchiPack (名前付きフィールドの袋)

```taida
// Value creation
pilot <= @(name <= "Misato", age <= 29, role <= "Captain")

// Type definition
Pilot = @(name: Str, age: Int, role: Str)

// Typed instance
asuka <= Pilot(name <= "Asuka", age <= 14, role <= "Pilot")

// Field access
stdout(pilot.name)  // "Misato"
```

### リスト

```taida
numbers <= @[1, 2, 3, 4, 5]
names <= @["Rei", "Asuka", "Shinji"]
empty <= @[]

// List operations use molds
Map[numbers, _ n = n * 2]() ]=> doubled       // @[2, 4, 6, 8, 10]
Filter[numbers, _ n = n > 3]() ]=> filtered    // @[4, 5]
Sum[numbers]() ]=> total                        // 15
Sort[names]() ]=> sorted                        // @["Asuka", "Rei", "Shinji"]
```

---

## Mold[T] -- モールディング型

> **[PHILOSOPHY.md](/_corpus/PHILOSOPHY.md) -- III.** カタめたいなら、鋳型を作りましょう

モールドはパラメータ化された型です。値をモールドに流し込み (モールディング)、`]=>` または `<=[` で取り出します (アンモールディング)。

```taida
// Built-in molds
Div[10, 3]() ]=> result       // Arithmetic mold
Str[42]() ]=> text             // Type conversion mold
Upper["hello"]() ]=> upper     // String mold
JSON[raw, Schema]() ]=> data   // JSON mold (returns Lax[T])

// Custom mold definition
Mold[T] => Container[T] = @(count: Int)

// Usage
Container[42](count <= 1) ]=> value  // 42
```

### 主な組み込みモールド

| カテゴリ | モールド |
|----------|---------|
| 高階関数 | `Map`, `Filter`, `Fold`, `Foldr`, `Reduce`, `Take`, `TakeWhile`, `Drop`, `DropWhile` |
| 文字列 | `Upper`, `Lower`, `Trim`, `Split`, `Replace`, `Slice`, `CharAt`, `Repeat`, `Reverse`, `Pad` |
| 数値 | `ToFixed`, `Abs`, `Floor`, `Ceil`, `Round`, `Truncate`, `Clamp` |
| リスト | `Concat`, `Append`, `Prepend`, `Join`, `Sum`, `Sort`, `Unique`, `Flatten`, `Find`, `FindIndex`, `Count`, `Zip`, `Enumerate` |
| 型変換 | `Str[x]()`, `Int[x]()`, `Float[x]()`, `Bool[x]()` |
| 算術 | `Div[x, y]()`, `Mod[x, y]()` |
| 非同期 | `Async[T]`, `All`, `Race`, `Timeout` |

---

## Lax[T] -- 安全な不確実性

`Lax[T]` は値が存在するかもしれないし、しないかもしれない値をラップします。Optional/Option とは異なり、Lax は常にフォールバック値 (型のデフォルト値) を持ちます。

```taida
// Div returns Lax
lax <= Div[10, 0]()
lax.hasValue()   // false
lax ]=> value    // 0 (Int default)

// List access returns Lax
numbers <= @[10, 20, 30]
numbers.get(0) ]=> first   // 10
numbers.get(99) ]=> oob    // 0 (Int default, no error)

// JSON parsing returns Lax
JSON[rawJson, UserSchema]() ]=> user  // Lax[User] -> User (with defaults for missing fields)
```

---

## 関数

```taida
// Function with arguments
add x: Int y: Int =
  x + y
=> :Int

// No-argument function
getVersion =
  "1.0.0"
=> :Str

// Single-line export
<<< double x: Int = x * 2 => :Int

// Pipeline with partial application
data => filter(_ x = x > 0) => map(_ x = x * 2) => result

// Calling functions
result <= add(3, 5)        // 8
8 => add(_, 2) => ten      // 10
```

### デフォルト引数

```taida
greet name: Str greeting: Str <= "Hello" =
  greeting + ", " + name + "!"
=> :Str

greet("Rei")              // "Hello, Rei!"
greet("Rei", "Hi")        // "Hi, Rei!"
```

---

## パターンマッチング (条件ガード)

```taida
// Multi-branch
classify x: Int =
  | x > 100 |> "huge"
  | x > 10  |> "large"
  | x > 0   |> "small"
  | _       |> "non-positive"
=> :Str

// Inline
status <= | connected |> "online" | _ |> "offline"
```

---

## エラー処理

```taida
// Define error type (inherits from Error)
Error => ValidationError = @(field: Str)

// Error ceiling (catch)
|== error: ValidationError =
  stderr("Validation failed: " + error.field)
  "default"
=> :Str

// Throw
| !isValid(input) |> ValidationError(
  type <= "ValidationError",
  message <= "Invalid input",
  field <= "name"
).throw()

// Gorilla literal -- immediate program termination
| config.dbUrl == "" |> ><
```

---

## モジュールシステム

```taida
// Import local file
>>> ./utils.td => @(helper, format)

// Import with alias
>>> ./math.td => @(add, subtract => sub)

// Import external package with version
>>> author/package@1.0.0 => @(funcA, funcB)

// Export multiple symbols
<<< @(add, subtract, Point)

// Export single symbol
<<< myFunction

// One-liner export
<<< double x: Int = x * 2 => :Int
```

### プリリュード (インポート不要)

以下は `>>>` なしで全てのファイルから利用できます:

- `stdout(value)`, `stderr(value)`, `stdin()`
- `jsonEncode(value)`, `jsonPretty(value)`
- `debug(value)`, `typeof(x)`, `range(start, end)`
- `assert(cond, msg)`, `hashMap(entries)`, `setOf(items)`

---

## 完全な例

```taida
>>> ./types.td => @(Staff)

// Create a staff record
misato <= Staff(
  id <= 1,
  name <= "Misato Katsuragi",
  rank <= "Captain",
  active <= true
)

// Pipeline: process staff name
misato.name
  => Split[_, " "]()
  ]=> parts
  => map(_ p = Upper[p]())
  => Join[_, " "]()
  ]=> displayName

stdout(displayName)  // "MISATO KATSURAGI"

// Safe division
Div[100, misato.id]() ]=> ratio
stdout(Str[ratio]() ]=> _)  // "100"

// Conditional
status <= | misato.active |> "Active" | _ |> "Inactive"
stdout(status)  // "Active"
```

---

## 関連ドキュメント

- [言語ガイド全章](/_corpus/guide/) -- 概要からイントロスペクションまで 13 章
- [演算子リファレンス](/_corpus/reference/operators.md)
- [モールド型リファレンス](/_corpus/reference/mold_types.md)
- [標準メソッドリファレンス](/_corpus/reference/standard_methods.md)
- [哲学](/_corpus/PHILOSOPHY.md)
