In FSYACC it is common to have terminals that result in tuples. However, for convenience I want to use a record type instead. For example, if I have the following in my Abstract Syntax Tree (AbstractSyntaxTree.fsl):
namespace FS
module AbstractSyntaxTree =
type B = { x : int; y : int }
type Either =
| Record of B
| Tuple of int * string
type A =
| Int of int
| String of string
| IntTuple of Either
I'm not clear on the correct syntax in FSYACC (parser.fsy), because if I use:
%start a
%token <string> STRING
%token <System.Int32> INT
%token ATOMTOKEN TUPLETOKEN EOF
%type < A > a
%%
a:
| atomS { $1 }
| atomI { $1 }
| either { $1 }
atomI:
| ATOMTOKEN INT { Int($2) }
atomS:
| ATOMTOKEN STRING { String($2) }
either:
| TUPLETOKEN INT INT { Record {x=$2;y=$3} } // !!!
| TUPLETOKEN TUPLETOKEN INT STRING { Tuple( $3, $4) } // !!!
I would expect the type B and the Tuple to be inferred. However, FSYACC gives the error for both of the lines marked with "!!!":
This expression was expected to have type A but here has type Either
What is the correct syntax to for the "either" production on the last two lines?
A
, so all you need to doIntTuple(Record{ x = $2; y = $3 }
andIntTuple(Tuple( $3, $4))
– Daniel Fabian