Skip to content

Commit

Permalink
[BoS] Fix description of subtyping (Wasm-DSL#54)
Browse files Browse the repository at this point in the history
Stack types are contravariant in all their parameters, including the return
stack reference.
  • Loading branch information
tlively authored May 17, 2024
1 parent f749c3f commit b7c4bcf
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions proposals/bag-o-stacks/Explainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,36 +114,24 @@ In order for a switch between coroutines to be valid, the type of the target sta

Given this, we can statically verify that WebAssembly programs that switch between coroutines are guaranteed to observe type safety during the switch.

#### Subtype relations between `stack` types
#### Subtyping

Like function types, two stack types
Like function types, stack types are contravariant in their parameters.

```wasm
(stack (param P1*) (ref $c1))
```

and

```wasm
(stack (param P2*) (ref $c2))
```pseudo
C |- stack t_1* rt_1 <: stack t_2* rt_2
-- C |- t_2* rt_2 <: t_1* rt_1
```

are in a subtype relationship if the `P1*` vector is a vector subtype of `P2*` and the type referenced in `$c2` is a subtype of $c1. I.e., covariant in the result types but contravariant in the parameter types. This is directly analogous to function subtyping.
`stack t_1 rt_1` is a subtype of `stack t_2* rt_2` iff:
- `t_2* rt_2` is a subtype of `t_1* rt_1`.

The top type for stack references is simply:
The top type for stack references is `stack` and the bottom type is `nostack`. Like other bottom types, the nostack type is uninhabited.

```wasm
stack
```

and the bottom type is `nostack` which results in a new extension to the heap type hierarchy:

```wasm
heaptype ::= ... | stack | nostack
```pseudo
absheaptype ::= ... | stack | nostack
```

Like other bottom types, the nostack type is uninhabited.

### Life-cycle of a coroutine

A coroutine is started using the `stack.new_switch` instruction. This performs the equivalent of a function call – on a new stack resource. In addition to the arguments normally expected in a function call, an additional argument is provided that is a stack reference to the caller code -- the caller is suspended as a result of the `stack.new_switch` instruction.
Expand Down

0 comments on commit b7c4bcf

Please sign in to comment.