Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Ambiguous type variable (Daniel Fischer)
2. Re: Haskell-style types in C or C++ (Christopher Howard)
3. Re: Ambiguous type variable (Ovidiu Deac)
4. Re: Ambiguous type variable (Daniel Fischer)
5. Re: Haskell-style types in C or C++ (Thomas Davie)
6. Re: Haskell-style types in C or C++ (John Harrison)
7. Re: Ambiguous type variable (Ovidiu Deac)
----------------------------------------------------------------------
Message: 1
Date: Sat, 30 Jul 2011 22:37:39 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Ambiguous type variable
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="utf-8"
On Saturday 30 July 2011, 22:19:28, Ovidiu Deac wrote:
> I'm playing with Haskell so I wrote a stack module (see the code
> below). I have a problem with the pop function which returns a tuple
> (Nothing, EmptyStack) if called with an EmptyStack.
>
> I kind of understand that the compiler cannot cannot figure out what
> type to use for a. But how could I tell the compiler that if the list
> is empty I don't care about that type?
You can't really, the compiler needs a specific type to know which Eq
instance to use.
If you had a Num constraint, the defaulting rules (language report,
http://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-790004.3.4
) would let the compiler pick a type (normally Integer, unless you have a
default declaration that says otherwise).
I'm not sure what GHC's ExtendedDefaultRules extension does, but there's a
good chance that a
{-# LANGUAGE ExtendedDefaultRules #-}
pragma at the top will make it compile (and let GHC pick () for the type).
Another option is that you choose a type and write your condition
(pop (EmptyStack :: Stack [Char]) == (Nothing, EmptyStack))
(which is portable, hence preferable).
>
> Thanks,
> Ovidiu
>
> /////////////////
> This is the hspec
> ...
> it "pop empty stack gives Nothing"
> ( (pop EmptyStack) ? (Nothing, EmptyStack))
> ...
>
> This is the code:
> module Stack where
> import Prelude
>
> data Stack a =
> EmptyStack |
> StackEntry a (Stack a)
> deriving(Show, Eq)
> ...
> pop :: Stack a ? (Maybe a, Stack a)
> pop EmptyStack = (Nothing, EmptyStack)
> pop (StackEntry a s) = ((Just a), s)
>
> ...and this is the error I get:
> test/TestStack.hs:20:28:
> Ambiguous type variable `a0' in the constraint:
> (Eq a0) arising from a use of `=='
> Probable fix: add a type signature that fixes these type variable(s)
> In the second argument of `it', namely
> `((pop EmptyStack) == (Nothing, EmptyStack))'
> In the expression:
> it
> "pop empty stack gives Nothing"
> ((pop EmptyStack) == (Nothing, EmptyStack))
> In the second argument of `describe', namely
> `[it "empty stack is empty" (isEmpty EmptyStack),
> it
> "non-empty stack is not empty"
> (not (isEmpty (push 10 EmptyStack))),
> it
> "push then pop retrieves the same value"
> ((pop $ push 10 EmptyStack) == (Just 10, EmptyStack)),
> it
> "push push then pop retrieves the last value"
> ((pop $ push 2 (push 1 EmptyStack))
> ==
> (Just 2, (push 1 EmptyStack))),
> ....]'
> make: *** [test] Error 1
>
------------------------------
Message: 2
Date: Sat, 30 Jul 2011 12:55:34 -0800
From: Christopher Howard <[email protected]>
Subject: Re: [Haskell-beginners] Haskell-style types in C or C++
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
On 07/30/2011 11:58 AM, Alexander Batischev wrote:
> Hi!
>
> On Sat, Jul 30, 2011 at 10:45:06AM -0800, Christopher Howard wrote:
>> One of the things that I love about Haskell is the syntax for
>> creating user defined types. E.g.:
>>
>> Data QueryResult = NoResult | DatabaseError String | Results [String]
>>
>> I can prototype an entire program around these self-made types and
>> it is a lot of fun. Out of intellect curiosity, though: what would
>> be the equivalent construction in C or C++? (Say, for the type
>> defined above).
>
> Recently, same question occurred to me too, and the only solution I
> could think of is defining base class (analog of data type name,
> QueryResult in your case) and creating subclasses for each of data type'
> constructors (NoResult, DatabaseError, Results). And you should resort
> to templates if you want something like [a] as a parameter.
>
> Google provides few nice links [1] [2] which may be helpful.
>
> Just for your information, types like in the example above are called
> Algebraic Data Types (ADT).
>
> 1. http://cpp-next.com/archive/2010/09/algebraic-data-types-in-c/
> 2. http://stackoverflow.com/questions/16770/haskells-algebraic-data-types
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
Thank you, this puts me on the right track. The above links were rather
difficult to follow, but the following article has been pretty good so far:
http://cpp-next.com/archive/2010/07/algebraic-data-types/
--
frigidcode.com
theologia.indicium.us
------------------------------
Message: 3
Date: Sat, 30 Jul 2011 23:57:04 +0300
From: Ovidiu Deac <[email protected]>
Subject: Re: [Haskell-beginners] Ambiguous type variable
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<CAKVsE7t+xJvMELSN_=6YW0a+Zmjz5=qwrmov42yrq3jdt77...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
I tried {-# LANGUAGE ExtendedDefaultRules #-} and it didn't work. I
tried to put it both in Stack.hs and in TestStack.hs
Then I tried to be specific about the type in the test.
Both this code:
(pop EmptyStack :: Stack[Char]) ? (Nothing, EmptyStack)
and this:
(pop EmptyStack :: Stack[Char]) ? (Nothing :: Maybe[Char],
EmptyStack :: Stack[Char])
...give me:
test/TestStack.hs:20:12:
Couldn't match expected type `Stack [Char]'
with actual type `(Maybe a0, Stack a0)'
In the return type of a call of `pop'
In the first argument of `(==)', namely
`(pop EmptyStack :: Stack [Char])'
In the second argument of `it', namely
`((pop EmptyStack :: Stack [Char]) == (Nothing, EmptyStack))'
On Sat, Jul 30, 2011 at 11:37 PM, Daniel Fischer
<[email protected]> wrote:
> On Saturday 30 July 2011, 22:19:28, Ovidiu Deac wrote:
>> I'm playing with Haskell so I wrote a stack module (see the code
>> below). I have a problem with the pop function which returns a tuple
>> (Nothing, EmptyStack) if called with an EmptyStack.
>>
>> I kind of understand that the compiler cannot cannot figure out what
>> type to use for a. But how could I tell the compiler that if the list
>> is empty I don't care about that type?
>
> You can't really, the compiler needs a specific type to know which Eq
> instance to use.
>
> If you had a Num constraint, the defaulting rules (language report,
> http://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-790004.3.4
> ) would let the compiler pick a type (normally Integer, unless you have a
> default declaration that says otherwise).
> I'm not sure what GHC's ExtendedDefaultRules extension does, but there's a
> good chance that a
>
> {-# LANGUAGE ExtendedDefaultRules #-}
>
> pragma at the top will make it compile (and let GHC pick () for the type).
>
> Another option is that you choose a type and write your condition
>
> ? (pop (EmptyStack :: Stack [Char]) == (Nothing, EmptyStack))
>
> (which is portable, hence preferable).
------------------------------
Message: 4
Date: Sat, 30 Jul 2011 23:23:49 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Ambiguous type variable
To: Ovidiu Deac <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="utf-8"
On Saturday 30 July 2011, 22:57:04, Ovidiu Deac wrote:
> I tried {-# LANGUAGE ExtendedDefaultRules #-} and it didn't work. I
> tried to put it both in Stack.hs and in TestStack.hs
Aha. I don't have the impression ExtendedDefaultRules will become a much-
used extension.
>
> Then I tried to be specific about the type in the test.
>
> Both this code:
> (pop EmptyStack :: Stack[Char]) ? (Nothing, EmptyStack)
This needs parentheses since the '::' has very low fixity.
Without parentheses it's parsed
((pop EmptyStack) :: Stack [Char]) == ...
so the type sugnature applies to the result of pop.
I wrote
( pop (EmptyStack :: Stack [Char]) == (...,...) )
(extra spaces added to unclutter the outermost parentheses) so that the
type signature applies only to EmptyStack.
Getting the parentheses for expression type signatures right is notoriously
tricky (unless you use parens in every case of the slightest doubt) at
first, I should better have given the signature to Nothing,
( pop EmptyStack == (Nothing :: Maybe [Char], EmptyStack) )
that would have been harder to misread.
> and this:
> (pop EmptyStack :: Stack[Char]) ? (Nothing :: Maybe[Char],
> EmptyStack :: Stack[Char])
Note that specifying the type of one subexpression is enough, pop's type
then determines the others.
------------------------------
Message: 5
Date: Sat, 30 Jul 2011 23:10:19 +0100
From: Thomas Davie <[email protected]>
Subject: Re: [Haskell-beginners] Haskell-style types in C or C++
To: Christopher Howard <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252
typedef enum
{
QueryResultTypeNoResult,
QueryResultTypeDatabaseError,
QueryResultTypeResults
} QueryResultType;
typedef union
{
char *error;
char **results;
} QueryResultData;
typedef struct
{
QueryResultType type;
QueryResultData data;
} QueryResult
Or something to that effect?
Bob
On 30 Jul 2011, at 19:45, Christopher Howard wrote:
> One of the things that I love about Haskell is the syntax for creating user
> defined types. E.g.:
>
> Data QueryResult = NoResult | DatabaseError String | Results [String]
>
> I can prototype an entire program around these self-made types and it is a
> lot of fun. Out of intellect curiosity, though: what would be the equivalent
> construction in C or C++? (Say, for the type defined above).
>
> --
> frigidcode.com
> theologia.indicium.us
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 6
Date: Sat, 30 Jul 2011 17:13:32 -0500
From: John Harrison <[email protected]>
Subject: Re: [Haskell-beginners] Haskell-style types in C or C++
To: Christopher Howard <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID:
<CALQGZC9dTMJLb9k=_mqcxp59vby39aef6epjsq3cjsrevdn...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
For:
Data QueryResult = NoResult | DatabaseError String | Results [String]
A close approximation I can think of is:
enum {NoResult, DatabaseError, Results};
struct QueryResult {
int type;
union {
char* error_msg;
struct {
char** msgs;
int len;
} results;
};
};
https://gist.github.com/1116058 is a simple use case of the code I
suggested. I am not sure there is only 1 solution to this though.
On Sat, Jul 30, 2011 at 1:45 PM, Christopher Howard <
[email protected]> wrote:
> One of the things that I love about Haskell is the syntax for creating user
> defined types. E.g.:
>
> Data QueryResult = NoResult | DatabaseError String | Results [String]
>
> I can prototype an entire program around these self-made types and it is a
> lot of fun. Out of intellect curiosity, though: what would be the equivalent
> construction in C or C++? (Say, for the type defined above).
>
> --
> frigidcode.com
> theologia.indicium.us
>
> ______________________________**_________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/**mailman/listinfo/beginners<http://www.haskell.org/mailman/listinfo/beginners>
>
--
John Harrison
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110730/d8eff885/attachment-0001.htm>
------------------------------
Message: 7
Date: Sun, 31 Jul 2011 02:12:06 +0300
From: Ovidiu Deac <[email protected]>
Subject: Re: [Haskell-beginners] Ambiguous type variable
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<cakvse7twaguwugrt0mxo3ju0aoczqrahga3xjepeepr2v4k...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
I missed those paranthesis in your initial reply.
Indeed it worked once I put them around EmptyStack :: Stack[Char]
Thanks!
On Sun, Jul 31, 2011 at 12:23 AM, Daniel Fischer
<[email protected]> wrote:
> On Saturday 30 July 2011, 22:57:04, Ovidiu Deac wrote:
>> I tried ?{-# LANGUAGE ExtendedDefaultRules #-} and it didn't work. I
>> tried to put it both in Stack.hs and in TestStack.hs
>
> Aha. I don't have the impression ExtendedDefaultRules will become a much-
> used extension.
>
>>
>> Then I tried to be specific about the type in the test.
>>
>> Both this code:
>> ? ? ? ? ?(pop EmptyStack :: Stack[Char]) ? (Nothing, EmptyStack)
>
> This needs parentheses since the '::' has very low fixity.
> Without parentheses it's parsed
>
> ? ?((pop EmptyStack) :: Stack [Char]) == ...
>
> so the type sugnature applies to the result of pop.
>
> I wrote
>
> ? ( pop (EmptyStack :: Stack [Char]) == (...,...) )
>
> (extra spaces added to unclutter the outermost parentheses) so that the
> type signature applies only to EmptyStack.
>
> Getting the parentheses for expression type signatures right is notoriously
> tricky (unless you use parens in every case of the slightest doubt) at
> first, I should better have given the signature to Nothing,
>
> ? ( pop EmptyStack == (Nothing :: Maybe [Char], EmptyStack) )
>
> that would have been harder to misread.
>
>> and this:
>> ? ? ? ? ?(pop EmptyStack :: Stack[Char]) ? (Nothing :: Maybe[Char],
>> EmptyStack :: Stack[Char])
>
> Note that specifying the type of one subexpression is enough, pop's type
> then determines the others.
>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 37, Issue 68
*****************************************