Skip to content

Commit 66d8c24

Browse files
committed
update
1 parent da40a99 commit 66d8c24

File tree

9 files changed

+1457
-275
lines changed

9 files changed

+1457
-275
lines changed

docs/04-overloading.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,30 @@ This rule governs the application of overloaded functions. If a function `f` has
4242
#### Subtyping for Superposition Types
4343

4444
$$
45-
(A \sqcup B) <: (A \oplus B)
45+
(A \oplus B) \leq (A \sqcap B)
4646
$$
4747

48-
This rule states that a sum type $A \sqcup B$, which represents an exclusive choice between `A` and `B`, is a subtype of the corresponding superposition type $A \oplus B$. Superposition types provide more flexibility, allowing the function’s behavior to be dynamically resolved based on the context.
48+
The superposition type $A \oplus B$ is a subtype of the intersection type $A \sqcap B$.
49+
This rule comes from the property of an intersection type:
4950

51+
$$
52+
(A_1 \to B_1) \sqcap (A_2 \to B_2) \equiv (A_1 \sqcup A_2) \to (B_1 \sqcap B_2)
53+
$$
54+
55+
A superposition type $(A_1 \to B_1) \sqcap (A_2 \to B_2)$ yields $B_1$ when applied to $A_1$ and $B_2$ when applied to $A_2$. The input type satisfies $A_1 \geq A_1 \sqcup A_2$ and $A_2 \geq A_1 \sqcup A_2$, and the output type satisfies $B_1 \leq B_1 \sqcap B_2$ and $B_2 \leq B_1 \sqcap B_2$.
56+
And because the covariance of the input type and the contravariance of the output type:
57+
58+
$$
59+
\begin{align}
60+
\begin{array}{c}
61+
\Gamma \vdash A_1 \geq B_1 \quad A_2 \leq B_2
62+
\\\hline
63+
\Gamma \vdash A_1 \rightarrow A_2 \leq B_1 \rightarrow B_2
64+
\end{array}
65+
\end{align}
66+
$$
67+
68+
Thus, the superposition type is considered as a subtype of the intersection type.
5069

5170
## Function Application with Overloading
5271

@@ -182,8 +201,6 @@ eval concat "Hello! "
182201

183202
Here, `concat` can be partially applied to an integer or string, with its final behavior determined by the type of the subsequent argument. Specifically, applying an overloaded lambda to an argument invokes a selection process based on the argument’s type, identifying the corresponding function branch. This type-based selection mechanism is termed "**measurement**" of an overloaded lambda, analogous to quantum state measurement, where an indeterminate superposed state collapses into a definite outcome upon observation.
184203

185-
186-
187204
### Why Superposition Types?
188205

189206
Superposition types in **Saki** elegantly combine **function overloading** (ad-hoc polymorphism) with **currying**, overcoming challenges that typically arise when attempting to integrate these concepts in functional programming languages.

docs/05-subtyping.md

Lines changed: 52 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ $$
1010
\forall T, \ T \leq \top \quad \text{and} \quad \forall T, \ \bot \leq T
1111
$$
1212

13+
## The Subsumption Rule
14+
15+
In a type system with subtyping, a type \( A \) can be considered a **subtype** of another type \( B \) if \( A \) satisfies all the constraints and properties of \( B \), and possibly some additional ones. The subsumption rule formalizes this idea by allowing expressions of a more specific type to be treated as expressions of a more general type.
16+
17+
The subsumption rule can be expressed as:
18+
19+
\[
20+
\frac{A \leq B \quad \Gamma \vdash e : A}{\Gamma \vdash e : B}
21+
\]
22+
23+
This means that if we have an expression \( e \) of type \( A \), and we know that \( A \) is a subtype of \( B \) (denoted \( A \leq B \)), then we can treat \( e \) as an expression of type \( B \) without any further checks.
24+
1325
## Basic Subtype Relationships
1426

1527
### Reflexivity of Subtyping
@@ -239,138 +251,107 @@ For example:
239251

240252

241253

254+
## Subtyping for Recursive Type
242255

243-
244-
256+
$$
257+
\frac{
258+
\Gamma \vdash T : \mathcal{U} \quad \Gamma, x : T \vdash \sigma \leq T
259+
}{
260+
\Gamma \vdash \mu (x : T) . \sigma \leq T
261+
}
262+
$$
245263

246264

247265

248266
## Dependent Types
249267

250268
Dependent types extend the expressive power of type systems by allowing types to depend on values or other types. Algebraic subtyping supports dependent types, incorporating union and intersection constructs to define relationships between dependent types effectively.
251269

252-
### Intersection of Dependent \( \Pi \)-Types
270+
### Subtyping Relationships
253271

254-
#### Type Formation Rule for Intersection of Dependent \( \Pi \)-Types
272+
#### Dependent $\Pi$-Type
255273

256-
Dependent \( \Pi \)-types are function-like types where the codomain depends on the specific value of the domain. For the intersection of such types:
274+
The dependent $\Pi$-type rule for subtyping is given by:
257275

258276
$$
259277
\frac{
260-
\Gamma \vdash A_1 : \mathcal{U} \quad \Gamma \vdash A_2 : \mathcal{U} \quad \Gamma, x:A_1 \vdash B_1(x) : \mathcal{U} \quad \Gamma, x:A_2 \vdash B_2(x) : \mathcal{U}
278+
\Gamma \vdash A_2 \leq A_1 \quad \Gamma, x:A_1 \vdash B_1(x) \quad \Gamma, x:A_2 \vdash B_1(x) \leq B_2(x)
261279
}{
262-
\Gamma \vdash \Pi(x:A_1). B_1(x) \cap \Pi(x:A_2). B_2(x) : \mathcal{U}
280+
\Gamma \vdash \Pi(x:A_1). B_1(x) \leq \Pi(x:A_2). B_2(x)
263281
}
264282
$$
265283

266-
Refinement ensures that \( x \) must be in both \( A_1 \) and \( A_2 \), producing:
284+
#### Dependent $\Sigma$-Type
285+
286+
The dependent $\Sigma$-type rule for subtyping is given by:
267287

268288
$$
269289
\frac{
270-
\Gamma \vdash A = A_1 \cap A_2 : \mathcal{U} \quad \Gamma, x:A \vdash B(x) = B_1(x) \cap B_2(x) : \mathcal{U}
290+
\Gamma \vdash A_1 \leq A_2 \quad \Gamma, x:A_1 \vdash B_1(x) \leq B_2(x)
271291
}{
272-
\Gamma \vdash \Pi(x:A). B(x) : \mathcal{U}
292+
\Gamma \vdash \Sigma(x:A_1). B_1(x) \leq \Sigma(x:A_2). B_2(x)
273293
}
274294
$$
275295

276-
#### Subtyping Rule for Intersection of Dependent \( \Pi \)-Types
296+
### Intersection of Dependent \( \Pi \)-Types
297+
298+
#### Type Formation Rule for Intersection of Dependent \( \Pi \)-Types
277299

278-
Subtyping for dependent \( \Pi \)-types follows:
300+
Dependent \( \Pi \)-types are function-like types where the codomain depends on the specific value of the domain. For the intersection of such types:
279301

280302
$$
281303
\frac{
282-
\Gamma \vdash A_2 \leq A_1 \quad \Gamma \vdash B_1(x) \leq B_2(x) \quad \Gamma \vdash B_2(x) \leq B_1(x)
304+
\Gamma \vdash A_1 : \mathcal{U} \quad \Gamma \vdash A_2 : \mathcal{U} \quad \Gamma, x:A_1 \vdash B_1(x) : \mathcal{U} \quad \Gamma, x:A_2 \vdash B_2(x) : \mathcal{U}
283305
}{
284-
\Gamma \vdash \Pi(x : A_1). B_1(x) \cap \Pi(x : A_2). B_2(x) \leq \Pi(x : A_2). B_2(x)
306+
\Gamma \vdash \Pi(x:A_1). B_1(x) \cap \Pi(x:A_2). B_2(x) : \mathcal{U}
285307
}
286308
$$
287309

288-
### Union of Dependent \( \Pi \)-Types
289-
290-
#### Type Formation Rule for Union of Dependent \( \Pi \)-Types
291-
292-
For unions of dependent \( \Pi \)-types, a sum type representation is used:
293-
294-
$$
295-
\Pi(x : A_1). B_1(x) \cup \Pi(x : A_2). B_2(x) \equiv \text{inl}(\Pi(x : A_1). B_1(x)) \cup \text{inr}(\Pi(x : A_2). B_2(x))
296-
$$
297-
298-
Where \( \text{inl} \) and \( \text{inr} \) represent injections into the left and right components.
299-
300-
#### Typing Rule
301-
302-
A term \( f \) of either constituent type satisfies:
310+
Refinement ensures that \( x \) must be in both \( A_1 \) and \( A_2 \), producing:
303311

304312
$$
305313
\frac{
306-
\Gamma \vdash f : \Pi(x : A_1). B_1(x)
307-
}{
308-
\Gamma \vdash f : \Pi(x : A_1). B_1(x) \cup \Pi(x : A_2). B_2(x)
309-
}
310-
\quad \text{or} \quad
311-
\frac{
312-
\Gamma \vdash f : \Pi(x : A_2). B_2(x)
314+
\Gamma \vdash A = A_1 \cap A_2 : \mathcal{U} \quad \Gamma, x:A \vdash B(x) = B_1(x) \cap B_2(x) : \mathcal{U}
313315
}{
314-
\Gamma \vdash f : \Pi(x : A_1). B_1(x) \cup \Pi(x : A_2). B_2(x)
316+
\Gamma \vdash \Pi(x:A). B(x) : \mathcal{U}
315317
}
316318
$$
317319

318-
#### Subtyping Rule for Union of Dependent \( \Pi \)-Types
320+
#### Type Formation Rule for Intersection of Dependent \( \Sigma \)-Types
319321

320-
Subtyping ensures inclusion and equivalence between unions and their constituents:
322+
Dependent \( \Sigma \)-types represent dependent pairs, where the type of the second component depends on the value of the first. The intersection of such types is defined as:
321323

322324
$$
323325
\frac{
324-
\Gamma \vdash \Pi(x : A_1). B_1(x) \leq \Pi(x : A_1). B_1(x) \cup \Pi(x : A_2). B_2(x)
326+
\Gamma \vdash A_1 : \mathcal{U} \quad \Gamma \vdash A_2 : \mathcal{U} \quad \Gamma, x:A_1 \vdash B_1(x) : \mathcal{U} \quad \Gamma, x:A_2 \vdash B_2(x) : \mathcal{U}
325327
}{
326-
\Gamma \vdash \Pi(x : A_1). B_1(x) \cup \Pi(x : A_2). B_2(x) \leq \Pi(x : A_1). B_1(x)
328+
\Gamma \vdash \Sigma(x:A_1). B_1(x) \cap \Sigma(x:A_2). B_2(x) : \mathcal{U}
327329
}
328330
$$
329331

330-
A similar rule applies for the second component of the union.
331-
332-
### Intersection of Dependent \( \Sigma \)-Types
333-
334-
#### Type Formation Rule for Intersection of Dependent \( \Sigma \)-Types
335-
336-
For dependent \( \Sigma \)-types, intersections represent the set of pairs satisfying constraints for both components:
337-
338-
$$
339-
\Sigma(x : A_1). B_1(x) \cap \Sigma(x : A_2). B_2(x) \equiv \Sigma(x : A_1 \cap A_2). (B_1(x) \cap B_2(x))
340-
$$
332+
### Union of Dependent \( \Pi \)-Types
341333

342-
#### Subtyping Rule for Intersection of Dependent \( \Sigma \)-Types
334+
#### Type Formation Rule for Union of Dependent \( \Pi \)-Types
343335

344-
The subtyping rule for intersected \( \Sigma \)-types is:
336+
The union of dependent \( \Pi \)-types combines the domains and codomains of the respective types:
345337

346338
$$
347339
\frac{
348-
\Gamma \vdash A_2 \leq A_1 \quad \Gamma \vdash B_1(x) \leq B_2(x) \quad \Gamma \vdash B_2(x) \leq B_1(x)
340+
\Gamma \vdash A_1 : \mathcal{U} \quad \Gamma \vdash A_2 : \mathcal{U} \quad \Gamma, x:A_1 \vdash B_1(x) : \mathcal{U} \quad \Gamma, x:A_2 \vdash B_2(x) : \mathcal{U}
349341
}{
350-
\Gamma \vdash \Sigma(x : A_1). B_1(x) \cap \Sigma(x : A_2). B_2(x) \leq \Sigma(x : A_2). B_2(x)
342+
\Gamma \vdash \Pi(x:A_1). B_1(x) \cup \Pi(x:A_2). B_2(x) : \mathcal{U}
351343
}
352344
$$
353345

354-
### Union of Dependent \( \Sigma \)-Types
355-
356346
#### Type Formation Rule for Union of Dependent \( \Sigma \)-Types
357347

358-
Unions of dependent \( \Sigma \)-types follow a sum-type structure:
359-
360-
$$
361-
\Sigma(x : A_1). B_1(x) \cup \Sigma(x : A_2). B_2(x) \equiv \text{inl}(\Sigma(x : A_1). B_1(x)) \cup \text{inr}(\Sigma(x : A_2). B_2(x))
362-
$$
363-
364-
Here, \( \text{inl} \) and \( \text{inr} \) designate the left and right components.
365-
366-
#### Typing Rule
367-
368-
Subtyping for unions of \( \Sigma \)-types maintains compatibility:
348+
The union of dependent \( \Sigma \)-types combines the types of the first and second components:
369349

370350
$$
371351
\frac{
372-
\Gamma \vdash (a, b) : \Sigma(x : A_1). B_1(x)
352+
\Gamma \vdash A_1 : \mathcal{U} \quad \Gamma \vdash A_2 : \mathcal{U} \quad \Gamma, x:A_1 \vdash B_1(x) : \mathcal{U} \quad \Gamma, x:A_2 \vdash B_2(x) : \mathcal{U}
373353
}{
374-
\Gamma \vdash (a, b) : \Sigma(x : A_1). B_1(x) \cup \Sigma(x : A_2). B_2(x)
354+
\Gamma \vdash \Sigma(x:A_1). B_1(x) \cup \Sigma(x:A_2). B_2(x) : \mathcal{U}
375355
}
376356
$$
357+

docs/examples/hello.saki

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
eval "Hello, world!"

0 commit comments

Comments
 (0)