Escaping type variables in some recursive functions
- Dominant language
- OCaml
- Stars
- 45
- Forks
- 28
- PR merge metrics
- No merged PRs in 30d
Description
Some functions cannot be annotated properly.
Consider the this seemingly useless list copy function:
```
let rec copy xs =
match xs with
| [] => []
| x :: xs => x :: copy xs
end
```
This variant works fine.
However, annotated variant is rejected due to "escaping type var":
```
let rec copy {X} (xs : List X) =
match xs with
| [] => []
| x :: xs => x :: copy xs
end
```
```
error: This expression has type List a, but an expression was expected of type List X
-> Lazy.fram
81 | match xs with
82 | | [] => []
83 |> | x :: xs => x :: copy xs
| ^^^^^^^
note: Type variable X escapes its scope
-> Lazy.fram
81 | match xs with
82 | | [] => []
83 |> | x :: xs => x :: copy xs
```
One way to get around this hurdle is to explicitly annotate returning value:
```
let rec copy {X} (xs : List X) =
(match xs with
| [] => []
| x :: xs => x :: copy xs
end : List X)
```
This issue is especially annoying when effect are involved. Speeding up type checker requires us to explicitly annotate effects, yet sometimes it is not possible without annotating return value.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.