haskell own datatypes - using signs or numbers in the datatype definition - haskell

I have a question according to the definition of datatypes:
Is it possible to use signs or numbers in the definition?
For instance if I want to create a datatype for the lower and equal sign the following code works of course
data Signs = Lo | Eq
The constructor Lo stands for "<" and the constructor Eq is "=".
But I can't use the "real" signs. For instance the following codes won't work
data Signs = Lo "<" | Eq "="
type Signs = "<" | "="
type MyInt = '1' | '2'
data MyInt = One '1' | Two '2'
So I would like to know if there is a possibility to use "real" signs and numbers within the definition. And if there is one it would be nice if you could tell me how it works. ;)

Operator identifiers prefixed with : can be used in data constructors.
data Signs = (:<) | (:=)
if they are nullary then AFAIK you have to keep the parens:
[(:<), (:=)]
You can use numbers but the first character must be an uppercase letter.
data MyInt = N1 | N2
See http://www.haskell.org/onlinereport/lexemes.html

Related

How do fixpoint, variable, let and tag schema constructors work in Winery?

I had previously asked where the Winery types are indexed. I noticed that in the serialization for the schema for Bool, which is [4,6], the 4 is the version number, and 6 is the index of SBool in SchemaP. I verified the hypothesis using other "primitive" types like Integer (serialization: 16), Double (18), Text (20). Also, [Bool] will be SVector SBool, serialized to [4,2,6], which makes sense: the 2 is for SVector, the 6 is for SBool.
But SchemaP also contains constructors that I don't intuitively see how are used: SFix, SVar, STag and SLet. What are they, and which type would I need to construct the schema for, to see them used? Why is SLet at the end, but SFix at the beginning?
SFix looks like a µ quantifier for a recursive type. The type µx. T is the type T where x refers to the whole type µx. T. For example, a list data List a = Nil | Cons a (List a) can be represented as L(a) = µr. 1 + a × r, where the recursive occurrence of the type is replaced with the variable r. You could probably see this with a recursive user-defined type like data BinTree a = Leaf | Branch a (BinTree a) (BinTree a).
This encoding doesn’t explicitly include a variable name, because the next constructor SVar specifies that “SVar n refers to the nth innermost fixpoint”, where n is an Int in the synonym type Schema = SchemaP Int. This is a De Bruijn index. If you had some nested recursive types like µx. µy. … = SFix (SFix …), then the inner variable would be referenced as SVar 0 and the outer one as SVar 1 within the body …. This “relative” notation means you can freely reorganise terms without worrying about having to rename variables for capture-avoiding substitution.
SLet is a let binding, and since it’s specified as SLet !(SchemaP a) !(SchemaP a), I presume that SLet e1 e2 is akin to let x = e1 in e2, where the variable is again implicit. So I suspect this may be a deficiency of the docs, and SVar can also refer to Let-bound variables. I don’t know how they use this constructor, but it could be used to make sharing explicit in the schema.
Finally, STag appears to be a way to attach extra “tag” metadata within the schema, in some way that’s specific to the library.
The ordering of these constructors might be maintained for compatibility with earlier versions, so adding new constructors at the end would avoid disturbing the encoding, and I figure the STag and SLet constructors at the end were simply added later.

Make a type be either one type or another

I'm a beginner in Haskell playing around with parsing and building an AST. I wonder how one would go about defining types like the following:
A Value can either be an Identifier or a Literal. Right now, I simply have a type Value with two constructors (taking the name of the identifier and the value of the string literal respectively):
data Value = Id String
| Lit String
However, then I wanted to create a type representing an assignment in an AST, so I need something like
data Assignment = Asgn Value Value
But clearly, I always want the first part of an Assignment to always be an Identifier! So I guess I should make Identifier and Literal separate types to better distinguish things:
data Identifier = Id String
data Literal = Lit String
But how do I define Value now? I thaught of something like this:
-- this doesn't actually work...
data Value = (Id String) -- How to make Value be either an Identifier
| (Lit String) -- or a Literal?
I know I can simply do
data Value = ValueId Identifier
| ValueLit Literal
but this struck me as sort of unelegant and got me wondering if there was a better solution?
I first tried to restructure my types so that I would be able to do it with GADTs, but in the end the simpler solution was to go leftroundabout's suggestion. I guess it's not that "unelegant" anyways.

Express a rule with ANTLR4

I must define a rule which expresses the following statement: {x in y | x > 0}.
For the first part of that comprehension "x in y", i have the subrule:
FIRSTPART: Name "in" Name
, whereas Name can be everything.
My problem is that I do not want a greedy behaviour. So, it should parse until the "|" sign and then stop. Since I am new in ANTLR4, I do not know how to achieve that.
best regards,
Normally, the lexer/parser rules should represent the allowable syntax of the source input stream.
The evaluation (and consequences) of how the source matches any rule or subrule is a matter of semantics -- whether the input matches a particular subrule and whether that should control how the rule is finally evaluated.
Normally, semantics are implemented as part of the tree-walker analysis. You can use alternate subrule lables (#inExpr, etc) to create easily distinguishable tree nodes for analysis purposes:
comprehension : LBrace expression property? RBrace ;
expression : ....
| Name In Name #inExpr
| Name BinOp Name #binExpr
| ....
;
property : Provided expression ;
BinOp : GT | LT | GTE | .... ;
Provided : '|' ;
In : 'in' ;

What is the Set defined by this Regular Expression?

I am trying to go through regular expression and language questions however, this one seems to have gotten me stuck.
Can somebody help?
I am trying to write out the set that is defined by this regular expression:
To understand this regular expression, lets consider its three parts separately:
( a | Ɛ ) abb (a | b)
\---1---- --2--- ---3---
this regular expression is defined in three groups/parts using parenthesis
Part-1: Ɛ is a null symbol in regular expression, if it appears with some other symbol (or a group of symbols) with union operator | that means that symbol(or group) is option e.g. can be appear or not appear in some strings of language ( Ɛ symbols in FA as edge label defines 'null-transition' — which allows a transformation to a new state without consuming any input symbols).
In your regular expression, first 'a' is written with Ɛ — ( a | Ɛ ) so it is option - it can appear in some string or absent in other. Hence strings generated with using this regular expression either starts with two 'a' or one 'a'.
Part-2: Sub-string 'aab' always appears in all possible string using this regular expression.
so strings can be in two possible forms:
aabb(a|b)
abb(a|b)
Part-3: (a | b) string either ends with symbol 'a' or symbol 'b'.
if both above forms ends with 'a'
aabba
abba
if both above forms ends with 'b'
aabbb
abbb
Of-course it is a finite language and its DFA does not contain any loop. Its DFA for this language { aabba, abba, aabbb, abbb } would be as following:

How can I create a singelton value in a module?

I have a type as follows:
data Stitch mark = OverStitch mark (Stitch mark) | TokenStitch | TerminalStitch
There can only be one single value of TerminalStitch. So I wish I could define this value at the top level of my module something like that:
terminalStitch :: Stitch
terminalStitch = TerminalStitch -- <--- value = constructor()
But it doesn't seem to work. What should I do instead?
Well the concrete problem here is a typo
terminalSticth = TerminalStitch
-- ^ swapped the letters
Also in your type signature, you need to provide stitch a type
terminalStitch :: Stitch a
What are you trying to achieve here? In Haskell you can't compare things "by identity" only by value. So using terminalStitch is completely identical to just using TerminalStitch.

Resources