Add required array element constraints
This commit is contained in:
@@ -17,7 +17,7 @@ let / function env
|
||||
```
|
||||
|
||||
Module top-level bindings shadow prelude bindings.
|
||||
Primitive type names such as `String`, `Int`, `Float`, `Bool`, and `Array` are handled before environment lookup, so they are reserved and cannot be shadowed by host bindings.
|
||||
Primitive type names such as `String`, `Int`, `Float`, and `Bool` are handled before environment lookup, so they are reserved and cannot be shadowed by host bindings.
|
||||
|
||||
## Global bindings
|
||||
|
||||
@@ -59,12 +59,16 @@ HostValue =
|
||||
Float
|
||||
Bool
|
||||
Array(Vec<HostValue>)
|
||||
ArrayConstraint { item, constraints, default }
|
||||
Object(Vec<HostField>)
|
||||
Abstract { constraints, default }
|
||||
```
|
||||
|
||||
When a host value is bound, the engine internalizes it into `RuntimeValue` and allocates value thunks for object fields, array items, and defaults.
|
||||
|
||||
`HostValue::array_of(item)` builds an array constraint with a required element schema.
|
||||
There is no host API for an unconstrained abstract array.
|
||||
|
||||
## Abstract host objects
|
||||
|
||||
A host-provided schema object is represented as a concrete object structure whose fields may contain abstract values.
|
||||
|
||||
@@ -123,12 +123,16 @@ constraint は concrete value とは別の型として扱う。
|
||||
```text
|
||||
Constraint =
|
||||
Type(PrimitiveType)
|
||||
ArrayItems(ThunkId)
|
||||
Compare(Op, Literal)
|
||||
Regex(Pattern)
|
||||
BuiltinPredicate(Symbol)
|
||||
ObjectConstraint(...)
|
||||
```
|
||||
|
||||
`ArrayItems` は配列そのものの型と、すべての要素へ合成する schema thunk を表す。
|
||||
配列用の primitive type は持たず、抽象配列には必ず要素制約が必要である。
|
||||
|
||||
初期実装では、object の形は主に `Concrete(Object)` の field に `Abstract` を置くことで表現する。
|
||||
object 全体にかかる constraint は必要になった時点で追加する。
|
||||
|
||||
|
||||
@@ -124,6 +124,19 @@ String & /^a$/ & /^b$/
|
||||
Host = IPv4Address;
|
||||
```
|
||||
|
||||
## 配列制約
|
||||
|
||||
配列制約には要素制約が必須であり、`[...T]` と書く。
|
||||
|
||||
```dcdl
|
||||
Names = [...String];
|
||||
PositiveInts = [...(Int & > 0)];
|
||||
```
|
||||
|
||||
複数の配列制約を `&` で合成した場合、各 concrete 要素へすべての要素制約を合成する。
|
||||
要素が object schema の場合は、その schema に含まれる default も各要素へ適用される。
|
||||
要素制約のない `Array` primitive type は存在しない。
|
||||
|
||||
## default
|
||||
|
||||
`default` は制約ではない。
|
||||
|
||||
@@ -31,6 +31,23 @@ disabled_config = NewConfig & {
|
||||
enabled_config = NewConfig;
|
||||
```
|
||||
|
||||
## 配列スキーマ
|
||||
|
||||
```dcdl
|
||||
Services = [...{
|
||||
name = String;
|
||||
port = Int default 8080;
|
||||
}];
|
||||
|
||||
Services & [
|
||||
{ name = "api"; },
|
||||
{ name = "worker"; port = 9000; },
|
||||
]
|
||||
```
|
||||
|
||||
抽象配列には要素制約が必須である。
|
||||
この例では、1 番目の要素の `port` は `8080` に materialize される。
|
||||
|
||||
## 関数と制約
|
||||
|
||||
```dcdl
|
||||
|
||||
@@ -7,6 +7,46 @@ array expression は、順序付きの値の列を表す。
|
||||
["a", "b", "c"]
|
||||
```
|
||||
|
||||
配列リテラルは concrete value であり、要素型を揃える必要はない。
|
||||
|
||||
```dcdl
|
||||
["api", 8080, true]
|
||||
```
|
||||
|
||||
## Array constraint
|
||||
|
||||
配列制約は `[` の直後に ellipsis を置き、その後へ要素制約を記述する。
|
||||
|
||||
```dcdl
|
||||
Names = [...String];
|
||||
Ports = [...(Int & >= 1 & <= 65535)];
|
||||
```
|
||||
|
||||
`[...T]` は、すべての要素が `T` を満たす長さ 0 以上の配列を表す。
|
||||
空配列は任意の配列制約を満たす。
|
||||
|
||||
```dcdl
|
||||
[...String] & []
|
||||
[...String] & ["api", "worker"]
|
||||
```
|
||||
|
||||
要素制約は object schema にもできる。
|
||||
要素 schema の default は、配列へ制約を合成するときに各要素へ適用される。
|
||||
|
||||
```dcdl
|
||||
Services = [...{
|
||||
name = String;
|
||||
enabled = Bool default true;
|
||||
}];
|
||||
|
||||
Services & [{ name = "api"; }]
|
||||
```
|
||||
|
||||
要素制約のない抽象配列型は提供しない。
|
||||
旧来の `Array` primitive type は使用できず、`[...T]` の `T` は必須である。
|
||||
`[String]` は配列制約ではなく、未解決の `String` 制約を 1 要素に持つ concrete array になる。
|
||||
長さ制約、位置別の tuple 制約、unique 制約は現在サポートしない。
|
||||
|
||||
## Array concat
|
||||
|
||||
`++` は concrete array 同士を連結する。
|
||||
|
||||
@@ -11,6 +11,7 @@ Expr
|
||||
├─ path reference
|
||||
├─ object
|
||||
├─ array
|
||||
├─ array constraint
|
||||
├─ function
|
||||
├─ function call
|
||||
├─ let
|
||||
|
||||
@@ -57,6 +57,7 @@ primary_expression = literal
|
||||
| identifier
|
||||
| comparison_constraint
|
||||
| object
|
||||
| array_constraint
|
||||
| array
|
||||
| let_expression
|
||||
| function_expression
|
||||
@@ -73,6 +74,7 @@ field_definition = field_path , "=" , expression ;
|
||||
field_path = identifier , { "." , identifier } ;
|
||||
|
||||
array = "[" , [ expression , { "," , expression } , [ "," ] ] , "]" ;
|
||||
array_constraint = "[" , "..." , expression , [ "," ] , "]" ;
|
||||
|
||||
let_expression = "let" , { field_definition , ";" } , "in" , expression ;
|
||||
function_expression = "(" , [ parameter_list ] , ")" , "=>" , expression ;
|
||||
|
||||
@@ -121,6 +121,7 @@ false
|
||||
default fallback 指定
|
||||
=> 関数
|
||||
. フィールド参照 / ドットパス定義
|
||||
... 配列の要素制約
|
||||
```
|
||||
|
||||
演算子の優先順位は [合成演算子](./operators.md) で定義する。
|
||||
|
||||
@@ -9,10 +9,11 @@ name = String;
|
||||
retry = Int default 3;
|
||||
ratio = Float;
|
||||
enable = Bool default true;
|
||||
tags = Array;
|
||||
tags = [...String];
|
||||
```
|
||||
|
||||
現在の primitive type は `String`、`Int`、`Float`、`Bool`、`Array` である。
|
||||
現在の primitive type は `String`、`Int`、`Float`、`Bool` である。
|
||||
配列は primitive type ではなく、必須の要素制約を持つ `[...T]` で表現する。
|
||||
各型の個別仕様へのリンクは [Manual Index](../../index.md) に集約する。
|
||||
|
||||
primitive type と制約合成の詳細は [制約と default](../constraints-and-defaults.md) も参照する。
|
||||
|
||||
Reference in New Issue
Block a user