-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[parser] allow to type binders in lambda abstractions + tests
- no shift-head reduce error when adding types to abstractions - conflict_COLON.md contains a description about the resolution of the error - add some tests (one positive and one negative)
- Loading branch information
Showing
7 changed files
with
148 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Explanation of the shift-reduce error with typed binders | ||
(At the end of the file there is a copy paste of the original conflict message) | ||
|
||
## Informal description of the problem | ||
|
||
The extension of the grammar such that it is possible to give types to binders | ||
in lambda abstractions causes a shift/reduce conflict since the sentence: | ||
|
||
`(x : ty \ t as y) ...` | ||
|
||
is ambiguous. | ||
|
||
It can be **shifted** by: | ||
|
||
``` | ||
closed_term | ||
LPAREN term AS term RPAREN | ||
binder_term | ||
constant . COLON type_term BIND term | ||
``` | ||
|
||
and reduced by: | ||
|
||
``` | ||
head_term nonempty_list(closed_term) option(binder_body_no_ty) | ||
LPAREN term COLON type_term RPAREN // lookahead token appears | ||
closed_term // lookahead token is inherited | ||
head_term // lookahead token is inherited | ||
constant . | ||
``` | ||
|
||
The ambiguity may occur if we had the derivation (meaningless in our grammar): | ||
|
||
`type_term: type_term BIND term AS term { ... }` | ||
|
||
Allowing the sentence: `(x : ty \ t as y) ...` to be derived in two distinct | ||
ways. | ||
|
||
## Solution of the problem: | ||
|
||
The derivation we want is the shifted one, with the derivation tree: | ||
|
||
``` | ||
( . as y ) # from the closed term AS rule | ||
x : ty \ y # from the binder with type rule | ||
``` | ||
|
||
To solve the issue, we want the `constant` production of the `head_term` rule to | ||
have lower priority than the `COLON` token. To do so we add to the grammar: | ||
|
||
``` | ||
%nonassoc colon_minus1 | ||
%nonassoc COLON | ||
``` | ||
|
||
where `colon_minus1` has lower priority than the token `COLON`. Finally we | ||
attach the tag `colon_minus1` to the `constant` production of the `head_term` | ||
rule. | ||
|
||
This way, the reduction `head_term nonempty_list(closed...` is neglected in | ||
favour of the the wanted derivation. | ||
|
||
Note that the production rule: | ||
|
||
``` | ||
head_term: | ||
LPAREN; t = constant; COLON; ty = type_term RPAREN | ||
{ mkCast (loc $loc) (mkConst (loc $loc(t)) t) ty }` | ||
``` | ||
|
||
has to be added, otherwise `(x : int)` could not be parsed, this is because of | ||
the impossibility of parsing the `CONSTANT` before a `COLON` in the rule | ||
`head_term`. | ||
|
||
|
||
## BELOW THE TRACE OF THE SHIFT/REDUCE CONFLICT | ||
|
||
``` | ||
** Conflict (shift/reduce) in state 137. | ||
** Token involved: COLON | ||
** This state is reached from goal after reading: | ||
LPAREN constant | ||
** The derivations that appear below have the following common factor: | ||
** (The question mark symbol (?) represents the spot where the derivations begin to differ.) | ||
goal | ||
term EOF | ||
(?) | ||
** In state 137, looking ahead at COLON, shifting is permitted | ||
** because of the following sub-derivation: | ||
closed_term | ||
LPAREN term AS term RPAREN | ||
binder_term | ||
constant . COLON type_term BIND term | ||
** In state 137, looking ahead at COLON, reducing production | ||
** head_term -> constant | ||
** is permitted because of the following sub-derivation: | ||
open_term | ||
head_term nonempty_list(closed_term) option(binder_body_no_ty) | ||
LPAREN term COLON type_term RPAREN // lookahead token appears | ||
closed_term // lookahead token is inherited | ||
head_term // lookahead token is inherited | ||
constant . | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
% Test for the correct parsing of binders with types | ||
|
||
main :- | ||
X = (x : bool\ x), | ||
Y = X tt, | ||
print Y. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
% Test for the correct parsing of binders with types | ||
% Similar to test lambda4 but with type_checking error | ||
% since the lambda term is applied to a term with | ||
% an unexpected type | ||
|
||
main :- | ||
X = (x : bool\ x), | ||
Y = X 1. % Here type error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters