381 lines
13 KiB
Plaintext
381 lines
13 KiB
Plaintext
type Item { name: Text, count: Int }
|
|
type Pair { Both(first: Text, second: Text) }
|
|
type Choice { Small(x: Int), Large(x: Int) }
|
|
type Problem { error Missing(message: Text) }
|
|
type Reply { Ready(value: Int), error Failed(problem: Problem) }
|
|
|
|
flow flowText(value: Text?) (out: Text)
|
|
| step | kind | do |
|
|
| - | check | value != null else Invalid |
|
|
| out | calc | join(value, "!") |
|
|
|
|
flow flowChecked(value: Int) (out: Int)
|
|
| step | kind | do |
|
|
| - | calc | maybe(value) |
|
|
| out | calc | value + 1 |
|
|
|
|
flow flowNested(value: Text?) (out: Text)
|
|
| step | kind | do |
|
|
| out | flow | flowText(value) |
|
|
|
|
flow flowMeasured(value: Text) (out: Int)
|
|
| step | kind | do |
|
|
| out | calc | length(value) |
|
|
|
|
size(value: Text) (n: Int) { alias = value
|
|
n = length(alias) }
|
|
identity(value: Text) (out: Text) { out = value }
|
|
twice(value: Text) (first: Text, second: Text) { first = identity(value)
|
|
second = value }
|
|
increment(item: Item) (out: Item) { out = item
|
|
out.count = out.count + 1 }
|
|
unchanged(item: Item) (next: Item, old: Item) { next = increment(item)
|
|
old = item }
|
|
name(item: Item) (text: Text) { text = item.name }
|
|
nameSize(item: Item) (n: Int) { n = length(item.name) }
|
|
count(item: Item) (n: Int) { n = item.count }
|
|
requireText(value: Text?) (out: Text) { check value != null else Invalid
|
|
out = value }
|
|
nullEquality() (same: Bool, different: Bool) { same = null == null
|
|
different = null != null }
|
|
nullableSize(value: Text?) (n: Int) { check value != null else Invalid
|
|
n = length(value) }
|
|
make() (value: Text?) { value = "你好" }
|
|
freshSize() (n: Int) { value = make()
|
|
check value != null else Invalid
|
|
n = length(value) }
|
|
add(x: Int, y: Int) (out: Int) { out = x + y }
|
|
subtract(x: Int, y: Int) (out: Int) { out = x - y }
|
|
multiply(x: Int, y: Int) (out: Int) { out = x * y }
|
|
quotient(x: Int, y: Int) (out: Int) { out = x // y }
|
|
remainder(x: Int, y: Int) (out: Int) { out = x % y }
|
|
negate(x: Int) (out: Int) { out = -x }
|
|
divide(x: Decimal, y: Decimal) (out: Decimal) { out = x / y }
|
|
fraction() (out: Decimal) { out = 1 / 3 }
|
|
join(a: Text, b: Text) (out: Text) { out = a + b }
|
|
addItem(values: List<Text>, item: Text) (out: List<Text>) { out = append(values, item) }
|
|
getItem(values: List<Text>, index: Int) (out: Text?) { out = get(values, index) }
|
|
lookup(values: Map<Text, Int>, key: Text) (out: Int?) { out = get(values, key) }
|
|
map() (out: Map<Text, Int>) { out = { "first" = 1, "second" = 2 } }
|
|
maybe(value: Int) (next: Int) { check value >= 1 else Problem.Missing("small")
|
|
next = value }
|
|
capture(value: Int) (out: Reply) { out = result(maybe(value)) }
|
|
raise() (value: Int) { fail(Problem.Missing("missing")) }
|
|
parameterFailure() (out: Reply) { out = result(maybe(raise())) }
|
|
rule choose(flag: Bool, value: Text) (out: Text)
|
|
| flag | value | out |
|
|
| true | - | value |
|
|
| false | - | value |
|
|
rule split(pair: Pair) (first: Text, second: Text)
|
|
| pair | first | second |
|
|
| Both(a, b) | a | b |
|
|
rule keep(pair: Pair) (first: Text, whole: Pair)
|
|
| pair | first | whole |
|
|
| Both(a, b) | a | pair |
|
|
rule mixed(pair: Pair) (first: Text, n: Int)
|
|
| pair | first | n |
|
|
| Both(a, b) | a | length(b) |
|
|
rule payload(value: Choice) (n: Int)
|
|
| value | n |
|
|
| Small(number), Large(number) | number |
|
|
rule absolute(value: Int) (out: Int >= 0)
|
|
| value | out |
|
|
| < 0 | -value |
|
|
| other | value |
|
|
rule nullable(value: Text?) (out: Text)
|
|
| value | out |
|
|
| null | "absent" |
|
|
| other | value |
|
|
rule nullablePayload(value: Pair?) (out: Text)
|
|
| value | out |
|
|
| null | "absent" |
|
|
| Both(a, b) | a |
|
|
rule read(value: Reply?) (out: Int)
|
|
| value | out |
|
|
| null | 0 |
|
|
| Ready(number) | number |
|
|
| Failed(problem) | fail(value) |
|
|
|
|
requireOnly(value: Text?) () { check value != null else Invalid }
|
|
getNullable(values: List<Text?>, index: Int) (out: Text?) { out = get(values, index) }
|
|
getNullableInt(values: List<Int?>, index: Int) (out: Int?) { out = get(values, index) }
|
|
lookupNullable(values: Map<Text, Text?>, key: Text) (out: Text?) { out = get(values, key) }
|
|
temporaryGet() (out: Int) { out = nullableSize(get(["temporary"], 0)) }
|
|
borrowNullable(values: List<Text?>) (out: Int) { out = nullableSize(get(values, 0)) }
|
|
rule borrowPayload(value: Pair?) (out: Int)
|
|
| value | out |
|
|
| null | 0 |
|
|
| Both(a, b) | length(a) |
|
|
|
|
dynamicMap(first: Text, second: Text) (out: Map<Text, Int>) { out = { first = 1, second = 2 } }
|
|
|
|
aliasAfterMove(value: Text) (out: Text, n: Int) { alias = value
|
|
out = value
|
|
n = length(alias) }
|
|
rule payloadAfterMove(pair: Pair) (whole: Pair, n: Int)
|
|
| pair | whole | n |
|
|
| Both(a, b) | pair | length(a) |
|
|
fieldAliasAfterMove(value: Item) (out: Item, n: Int) { first = value.name
|
|
alias = first
|
|
out = value
|
|
n = length(alias) }
|
|
nonnullAliasAfterMove(value: Text?) (out: Text?, n: Int) { alias = value
|
|
check alias != null else Invalid
|
|
out = value
|
|
n = length(alias) }
|
|
aliasBeforeMove(value: Text) (out: Text, n: Int) { alias = value
|
|
n = length(alias)
|
|
out = value }
|
|
|
|
containsText(values: List<Text>, target: Text) (found: Bool) {
|
|
found = contains(values, target)
|
|
}
|
|
|
|
containsNullable(values: List<Text?>, target: Text?) (found: Bool) {
|
|
found = contains(values, target)
|
|
}
|
|
|
|
containsRecord(values: List<Item>, target: Item) (found: Bool) {
|
|
found = contains(values, target)
|
|
}
|
|
|
|
containsNested(values: List<List<Int>>, target: List<Int>) (found: Bool) {
|
|
found = contains(values, target)
|
|
}
|
|
|
|
lookupSize(values: List<Text>, index: Int) (n: Int) { value = get(values, index)
|
|
check value != null else NotFound
|
|
n = length(value) }
|
|
lookupBeforeMove(values: List<Text>) (out: List<Text>, n: Int) { value = get(values, 0)
|
|
check value != null else NotFound
|
|
n = length(value)
|
|
out = append(values, "tail") }
|
|
lookupAfterMove(values: List<Text>) (out: List<Text>, n: Int) { value = get(values, 0)
|
|
check value != null else NotFound
|
|
out = append(values, "tail")
|
|
n = length(value) }
|
|
lookupMapSize(values: Map<Text, Text?>, key: Text) (n: Int) { value = get(values, key)
|
|
check value != null else NotFound
|
|
n = length(value) }
|
|
|
|
putMap(values: Map<Text, Text>, key: Text, value: Text) (out: Map<Text, Text>) {
|
|
out = put(values, key, value)
|
|
}
|
|
removeMap(values: Map<Text, Text>, key: Text) (out: Map<Text, Text>) {
|
|
out = remove(values, key)
|
|
}
|
|
keepMap(values: Map<Text, Text>) (out: Map<Text, Text>, original: Map<Text, Text>) {
|
|
out = put(values, "first", "changed")
|
|
original = values
|
|
}
|
|
removeIntKey(values: Map<Int, Text>, key: Int) (out: Map<Int, Text>) {
|
|
out = remove(values, key)
|
|
}
|
|
|
|
mapEntries(values: Map<Text, Text>) (out: List<MapEntry<Text, Text>>) {
|
|
out = entries(values)
|
|
}
|
|
entryValueSize(entry: MapEntry<Text, Text>) (n: Int) {
|
|
n = length(entry.value)
|
|
}
|
|
renamedEntry(entry: MapEntry<Text, Text>) (out: MapEntry<Text, Text>) {
|
|
out = entry
|
|
out.key = "renamed"
|
|
}
|
|
entryIntValue(entry: MapEntry<Int, Int>) (out: Int) {
|
|
out = entry.value
|
|
}
|
|
|
|
handlerIdentity(value: Text) (out: Text) { out = value }
|
|
handlerSuffix(value: Text) (out: Text) { out = value + "!" }
|
|
handlerApply(value: Text, transform: handler(value: Text) (out: Text)) (out: Text) {
|
|
out = transform(value)
|
|
}
|
|
handlerForward(value: Text, transform: handler(value: Text) (out: Text)) (out: Text) {
|
|
out = handlerApply(value, transform)
|
|
}
|
|
handlerNative(value: Text) (first: Text, second: Text) {
|
|
first = handlerForward(value, handlerIdentity)
|
|
second = handlerApply(value, handlerSuffix)
|
|
}
|
|
type HandlerResult { Done(value: Int), error Rejected(message: Text) }
|
|
handlerPositive(value: Int) (out: Int) {
|
|
check value > 0 else HandlerResult.Rejected("positive required")
|
|
out = value
|
|
}
|
|
handlerCapture(value: Int, operation: handler(value: Int) (out: Int)) (out: HandlerResult) {
|
|
out = result(operation(value))
|
|
}
|
|
handlerCaptured(value: Int) (out: HandlerResult) {
|
|
out = handlerCapture(value, handlerPositive)
|
|
}
|
|
rule handlerRule(value: Text, transform: handler(value: Text) (out: Text)) (out: Text)
|
|
| value | transform | out |
|
|
| - | - | transform(value) |
|
|
handlerRuled(value: Text) (out: Text) { out = handlerRule(value, handlerSuffix) }
|
|
handlerTextLength(value: Text) (out: Int) { out = length(value) }
|
|
handlerMeasure(value: Text, operation: handler(value: Text) (out: Int)) (out: Int) { out = operation(value) }
|
|
handlerMeasured(value: Text) (out: Int) { out = handlerMeasure(value, handlerTextLength) }
|
|
handlerPair(value: Int) (first: Int, second: Int) { first = value
|
|
second = value + 1 }
|
|
handlerPairApply(value: Int, operation: handler(value: Int) (first: Int, second: Int)) (out: Int) {
|
|
pair = operation(value)
|
|
out = pair.first + pair.second
|
|
}
|
|
handlerPaired(value: Int) (out: Int) { out = handlerPairApply(value, handlerPair) }
|
|
handlerPositiveRange(value: Int) (out: Int >= 1) { check value >= 1 else Invalid
|
|
out = value }
|
|
handlerGuarded(value: Int, operation: handler(value: Int) (out: Int)) (out: Int) {
|
|
check operation(value) >= 1 else Conflict
|
|
out = 1
|
|
}
|
|
handlerGuardedNative(value: Int) (out: Int) { out = handlerGuarded(value, handlerPositiveRange) }
|
|
const heading = "中文😀"
|
|
|
|
constantSize() (out: Int) {
|
|
out = length(heading)
|
|
}
|
|
|
|
loop loopMeasured(values: List<Text>) (total: Int)
|
|
| each value in values | result |
|
|
| total | sum length(value) |
|
|
|
|
loop rangeMeasured(start: Int, end: Int) (total: Int)
|
|
| each value in range(start, end) | result |
|
|
| total | sum value |
|
|
|
|
loop filterMeasured(values: List<Text>) (total: Int)
|
|
| each value in values | length(value) | result |
|
|
| total | > 1 | sum length(value) |
|
|
|
|
type Message { Named(label: Text), Empty }
|
|
|
|
loop payloadMeasured(values: List<Message>) (total: Int)
|
|
| each value in values | value | result |
|
|
| total | Named(label) | sum length(label) |
|
|
|
|
loop takeNone(values: List<Text>) (out: List<Text>)
|
|
| each value in values | take | result |
|
|
| out | 0 | list value |
|
|
|
|
emptyKeep(values: List<Text>) (out: List<Text>, size: Int) {
|
|
out = takeNone(values)
|
|
size = length(values)
|
|
}
|
|
|
|
loop sortedMeasured(values: List<Text>) (out: List<Text>)
|
|
| each value in values | sort | result |
|
|
| out | [length(value).asc] | list value |
|
|
|
|
foldSize(value: Text, previous: Int) (out: Int) { out = previous + length(value) }
|
|
|
|
loop foldMeasured(values: List<Text>) (out: Int)
|
|
| each value in values | result |
|
|
| out | fold(acc = 0, foldSize(value, acc)) |
|
|
|
|
keepState(previous: Text) (out: Text) { out = previous }
|
|
|
|
loop foldMove(values: List<Int>, seed: Text) (out: Text)
|
|
| each value in values | result |
|
|
| out | fold(acc = seed, keepState(acc)) |
|
|
|
|
rule measuredNext(state: Int) (out: Walk<Int, Int>)
|
|
| state | out |
|
|
| > 0 | Next(state, state - 1) |
|
|
| <= 0 | Done |
|
|
|
|
loop walkMeasured(seed: Int) (out: Int)
|
|
| each value in walk(seed, measuredNext) | result |
|
|
| out | sum value |
|
|
|
|
type FieldPair { left: Text, right: Text, count: Int }
|
|
|
|
swapFields(value: FieldPair) (out: FieldPair) {
|
|
out = FieldPair { left = value.right, right = value.left, count = value.count }
|
|
}
|
|
|
|
copyField(value: FieldPair) (out: FieldPair) {
|
|
out = FieldPair { left = value.left, right = value.left, count = value.count }
|
|
}
|
|
|
|
keepFields(value: FieldPair) (out: FieldPair, original: FieldPair) {
|
|
out = swapFields(value)
|
|
original = value
|
|
}
|
|
|
|
|
|
putAtLength(values: Map<Int, Int>) (out: Map<Int, Int>) {
|
|
out = put(values, length(values), 17)
|
|
}
|
|
|
|
appendOwnLength(values: List<Int>) (out: List<Int>) {
|
|
out = append(values, length(values))
|
|
}
|
|
|
|
type CollectionRecord { values: List<Int>, lookup: Map<Int, Int> }
|
|
|
|
appendFieldLength(state: CollectionRecord) (out: List<Int>, lookup: Map<Int, Int>) {
|
|
out = append(state.values, length(state.values))
|
|
lookup = state.lookup
|
|
}
|
|
|
|
putFieldLength(state: CollectionRecord) (out: Map<Int, Int>, values: List<Int>) {
|
|
out = put(state.lookup, length(state.lookup), length(state.values))
|
|
values = state.values
|
|
}
|
|
|
|
appendFieldKeepOriginal(state: CollectionRecord) (out: List<Int>, original: CollectionRecord) {
|
|
out = append(state.values, length(state.values))
|
|
original = state
|
|
}
|
|
|
|
checkedCollection(value: Int) (out: CollectionRecord) {
|
|
check value >= 0 else Forbidden
|
|
out = CollectionRecord { values = [], lookup = {} }
|
|
}
|
|
|
|
appendEvaluatedField() (out: List<Int>) {
|
|
out = append(checkedCollection(-1).values, maybe(-1))
|
|
}
|
|
|
|
loop sumMap(values: Map<Int, Int>) (out: Int)
|
|
| each entry in entries(values) | result |
|
|
| out | sum entry.value |
|
|
|
|
loop firstMapKey(values: Map<Int, Int>) (out: Int?)
|
|
| each entry in entries(values) | result |
|
|
| out | first entry.key |
|
|
|
|
sumTemporaryMap() (out: Int) { out = sumMap({ 3 = 11, 1 = 17 }) }
|
|
|
|
loop copyMapEntries(values: Map<Int, Int>) (out: List<MapEntry<Int, Int>>)
|
|
| each entry in entries(values) | result |
|
|
| out | list entry |
|
|
|
|
type FoldRecord { values: List<Int>, left: Int, right: Int }
|
|
|
|
appendRecordFields(state: FoldRecord, item: Int) (out: FoldRecord) {
|
|
size = length(state.values)
|
|
out = FoldRecord { values = append(state.values, item + size), left = state.right, right = state.left }
|
|
}
|
|
|
|
loop recordFields(values: List<Int>, initial: FoldRecord) (out: FoldRecord)
|
|
| each item in values | result |
|
|
| out | fold(state = initial, appendRecordFields(state, item)) |
|
|
|
|
appendKeepOldSize(item: Int, state: FoldRecord) (out: FoldRecord) {
|
|
out = FoldRecord { values = append(state.values, item), left = length(state.values), right = state.right }
|
|
}
|
|
|
|
loop recordOldSize(values: List<Int>) (out: FoldRecord)
|
|
| each item in values | result |
|
|
| out | fold(state = FoldRecord { values = [], left = 0, right = 0 }, appendKeepOldSize(item, state)) |
|
|
|
|
recordFault(item: Int, state: FoldRecord) (out: FoldRecord) {
|
|
out = FoldRecord { left = state.left + 1, values = append(state.values, item + 1), right = state.right }
|
|
}
|
|
|
|
loop foldedFault(values: List<Int>, initial: FoldRecord) (out: FoldRecord)
|
|
| each item in values | result |
|
|
| out | fold(state = initial, recordFault(item, state)) |
|