Skip to content

Commit 8b4f0fa

Browse files
Add more details page for dependent function types
1 parent 2fc1d6b commit 8b4f0fa

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
layout: doc-page
3+
title: "Dependent Function Types - More Details"
4+
---
5+
6+
Initial implementation in (#3464)[https://github.com/lampepfl/dotty/pull/3464].
7+
8+
## Syntax
9+
10+
FunArgTypes ::= InfixType
11+
| ‘(’ [ FunArgType {‘,’ FunArgType } ] ‘)’
12+
| '(' TypedFunParam {',' TypedFunParam } ')'
13+
TypedFunParam ::= id ':' Type
14+
15+
Dependent function types associate to the right, e.g.
16+
`(s: S) ⇒ (t: T) ⇒ U` is the same as `(s: S) ⇒ ((t: T) ⇒ U)`.
17+
18+
## Implementation
19+
20+
Dependent function types are shorthands for class types that define `apply`
21+
methods with a dependent result type.Dependent function types desugar to
22+
refinement types of `scala.FunctionN`. A dependent functon type
23+
`(x1: K1, ..., xN: KN) => R` of arity `N` translates to
24+
25+
FunctionN[K1, ..., Kn, R'] {
26+
def apply(x1: K1, ..., xN: KN): R
27+
}
28+
29+
where the result type parameter `R'` is the least upper approximation of the
30+
precise result type `R` without any referance to value parameters `x1, ..., xN`.
31+
32+
The syntax and sementics of anonymous dependent functions is identical to the
33+
one of regular functions. Eta expansion is naturaly generalized to produce
34+
dependent function types for methods with dependent result types.
35+
36+
Dependent functions can be implicit, and generalize to arity `N > 22` in the
37+
same way that other functions do, see [the corresponding
38+
documentation](https://dotty.epfl.ch/docs/reference/dropped/limit22.html).
39+
40+
## Examples
41+
42+
- (depfuntype.scala)[https://github.com/lampepfl/dotty/blob/0.10.x/tests/pos/depfuntype.scala]
43+
44+
- (eff-dependent.scala)[https://github.com/lampepfl/dotty/blob/0.10.x/tests/run/eff-dependent.scala]
45+
46+
### Type Checking
47+
48+
After desugaring no additional typing rules are required for dependent function types.

docs/docs/reference/dependent-function-types.md

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ on the function's parameter values. Example:
1010

1111
def extractKey(e: Entry): e.Key = e.key // a dependent method
1212
val extractor: (e: Entry) => e.Key = extractKey // a dependent function value
13-
// ║ ⇓ ⇓ ⇓ ⇓ ⇓ ⇓ ⇓ ║
14-
// ║ Dependent ║
15-
// ║ Function Type ║
13+
// ║ ⇓ ⇓ ⇓ ⇓ ⇓ ⇓ ⇓ ║
14+
// ║ Dependent ║
15+
// ║ Function Type ║
1616
// ╚═══════════════════╝
1717
Scala already has _dependent methods_, i.e. methods where the result
1818
type refers to some of the parameters of the method. Method
@@ -40,14 +40,4 @@ refinement. In fact, the dependent function type above is just syntactic sugar f
4040
def apply(e: Entry): e.Key
4141
}
4242

43-
In general, a dependent functon type `(x1: K1, ..., xN: KN) => R` of arity `N`
44-
translates to
45-
46-
FunctionN[K1, ..., Kn, R'] {
47-
def apply(x1: K1, ..., xN: KN): R
48-
}
49-
50-
where the result type parameter `R'` is an upper approximation of the
51-
true result type `R` that does not mention any of the parameters `x1, ..., xN`.
52-
53-
43+
[More details](./dependent-function-types-spec.html)

0 commit comments

Comments
 (0)