Reputation: 6442
I'm making a mini Pascal parser, this Pascal syntax supports declaring infinite types:
program ::= program identifier ; block .
block ::= variable-declaration-part procedure-declaration-part statement-part
procedure-declaration-part ::= { procedure-declaration ; }
procedure-declaration ::= procedure identifier ; block
I try to create AST domain types for this syntax:
type Program = Identifier * Block
and Block = VariableDeclaration list option * ProcedureDeclaration list option * Statement list
and ProcedureDeclaration = Identifier * Block
But the compiler would throw an error, because I use type aliases, and they are erased at compile-time:
This type definition involves an immediate cyclic reference through an abbreviation
Some said I could fix this problem with wrapped discriminated union types, what should I do?
Upvotes: 2
Views: 129
Reputation: 118865
and ProcedureDeclaration = ProcedureDeclaration of Identifier * Block
Now rather than just a tuple, it's a single-case DU type, which you can create with e.g.
ProcedureDeclaration(someID, someBlock)
and decompose with
match somePD with
| ProcedureDeclaration(i,b) -> ...
Upvotes: 4