Apologies for another question, I think I shall re-phrase my original question here as this is probably a pretty simple thing to do:
If i had a language which i wanted to create, like so:
A B <something> C
this would mean only an input like: A B …. C would be valid.
In contrast, B A ….. would not be valid.
Is there any easy way to define a constructor such as ‘B’ must always occur after ‘A’? I have only ever seen the A | B type of constructor ‘A or B’
Like so?
This compiles fine with
ghc -c Temp.hs– you can’t usedatastatements inghci, that’s one of the limitations ofghci.So, here I’m defining three data types:
C, which has three constructors which take no arguments:C1,C2, andC3. Each of these is a value of typeCB, which has constructorsB1which wraps aIntvalue, soB1 3andB1 544are values of typeBB2which wraps aStringvalue, soB2 "hello"andB2 "world"are values of type BB3is a simple argument-less constructor, soB3is a value of type B.A awhich is a polymorphic data type – for every typea,A ais also a data type. It has one constructor,MkAwhich takesBaCand creates a value of type
A a. So, for instanceMkA B3 True C1is of typeA BoolMkA (B1 30) "Foo" C3is of typeA String