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. GHCI in my app... (Sean Charles)
2. Re: GHCI in my app... (Felipe Almeida Lessa)
3. Re: GHCI in my app... (Brandon Allbery)
4. Re: GHCI in my app... (Sean Charles)
5. User-defined polymorphic data type: heterogeneous list?
(Christopher Howard)
6. Re: User-defined polymorphic data type: heterogeneous list?
(Mats Rauhala)
7. Re: User-defined polymorphic data type: heterogeneous list?
(David McBride)
8. Re: User-defined polymorphic data type: heterogeneous list?
(Arlen Cuss)
----------------------------------------------------------------------
Message: 1
Date: Sun, 10 Jul 2011 17:43:28 +0100
From: Sean Charles <[email protected]>
Subject: [Haskell-beginners] GHCI in my app...
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Hi,
I've now got a working OpenGL test-bed and I have also written myself a
"GLI" as I call it, openGl command Line Interface... a forkIO thread
that listens on port 3000 and once my program is up, I telnet in and
issue commands to it so I can fiddle with OpenGL whilst it is running. I
can change the camera position and angle and issue a few simple commands
to affect the rendered environment so I can better understand how OpenGL
works. If it works well and the code isn't to amateurish I will github
it I guess!
However, it seems to me that I am re-inventing the wheel to some degree:
I am opening a socket, getting input, parsing it and executing
commands.... what I would really like to be able to do is to *use* the
already pretty slick ghci interface instead...
...so, is there a way to create an instance of it and attach it to the
socket?
f I run my app from within ghci it still works but the event loop steals
any further input.
So, is it possible. I *did* manage this with SLIME and Emacs some years
ago albeit the slime funtionality was a little broken but I could
actually recompile code and have it affect the still running application.
I've also been using Parsec latelt and the "wai-handler-devel" also
entered my mind as an option but I only want a simple CLI after all!
:)
Thanks for your upcoming suggestions.....
Sean
------------------------------
Message: 2
Date: Sun, 10 Jul 2011 14:57:48 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] GHCI in my app...
To: Sean Charles <[email protected]>
Cc: [email protected]
Message-ID:
<CANd=ogek-nmkvkribvhjohk0y9k+tqzeh+ijg-xhy84x8k+...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
Hey!
I don't know how to answer your question. I just wanted to say that
some kind of integration of GHCi with the internals of a running
program may be useful as a 'manhole', e.g. on web apps. Something
like what Twisted have [1].
Cheers!
[1] http://www.lothar.com/tech/twisted/manhole.xhtml
--
Felipe.
------------------------------
Message: 3
Date: Sun, 10 Jul 2011 15:48:13 -0400
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] GHCI in my app...
To: Sean Charles <[email protected]>
Cc: [email protected]
Message-ID:
<cakfcl4v1xvzfmdonu8o3b3fq_kg8is1g11km8drbjsdptp4...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
On Sun, Jul 10, 2011 at 12:43, Sean Charles <[email protected]> > am
opening a socket, getting input, parsing it and executing commands....
> what I would really like to be able to do is to *use* the already pretty
> slick ghci interface instead...
> ...so, is there a way to create an instance of it and attach it to the
> socket?
Sort of. See http://www.haskell.org/haskellwiki/GHC/As_a_library; in
particular,
http://www.haskell.org/ghc/docs/latest/html/libraries/ghc/InteractiveEval.html
is probably of interest. I don't know how much of GHCi you'd have to
reimplement, though ? the library is there, and does everything GHCi
does, but I don't think it includes the REPL per se. Looks to me like
you'd need to implement your own parser for ":"-commands and "import".
--
brandon s allbery ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [email protected]
wandering unix systems administrator (available) ? ? (412) 475-9364 vm/sms
------------------------------
Message: 4
Date: Sun, 10 Jul 2011 21:01:35 +0100
From: Sean Charles <[email protected]>
Subject: Re: [Haskell-beginners] GHCI in my app...
To: Felipe Almeida Lessa <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
On 10/07/11 18:57, Felipe Almeida Lessa wrote:
> Hey!
>
> I don't know how to answer your question. I just wanted to say that
> some kind of integration of GHCi with the internals of a running
> program may be useful as a 'manhole', e.g. on web apps. Something
> like what Twisted have [1].
That'smy point! I have *already* done that, I just "telnet localhost
3000" and I get a prompt up I can enter commands that I have implemented.
What I am after is if I can embed the GHCI code and just have it work...
until then I am continuing down my chosen path with it to just get it
working
> Cheers!
>
> [1] http://www.lothar.com/tech/twisted/manhole.xhtml
>
------------------------------
Message: 5
Date: Sun, 10 Jul 2011 21:00:51 -0800
From: Christopher Howard <[email protected]>
Subject: [Haskell-beginners] User-defined polymorphic data type:
heterogeneous list?
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
I'm trying to understand parametric polymorphism and polymorphic data
types. I especially want to go beyond simply using the defined
polymorphic data types (lists and so forth) and see how I can make my
own useful ones.
As my first stab at it, it seemed like I should be able to create my own
heterogeneous "list" data type -- i.e., a "list" data type that can
contain elements of different types. (like, [3,'a',True], for example)
But I'm a little stuck. My first try was like so:
data HeteroList a b = Null | Element a (HeteroList a b) deriving (Show)
...but this of course did not work, because all elements end up having
to be the same type.
Then I tried
data HeteroList a b = Null | Element a (HeteroList b a) deriving (Show)
...but this doesn't work because every other other element has to be the
same type:
Element 'a' (Element 1 (Element 'a' (Element 2 Null)))
...I could go on and embarrass myself some more, but since I'm likely
widely off-base I'll just ask if somebody can point me in the right
direction.
--
frigidcode.com
theologia.indicium.us
------------------------------
Message: 6
Date: Mon, 11 Jul 2011 08:11:26 +0300
From: Mats Rauhala <[email protected]>
Subject: Re: [Haskell-beginners] User-defined polymorphic data type:
heterogeneous list?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
On 21:00 Sun 10 Jul , Christopher Howard wrote:
> I'm trying to understand parametric polymorphism and polymorphic
> data types. I especially want to go beyond simply using the defined
> polymorphic data types (lists and so forth) and see how I can make
> my own useful ones.
>
> As my first stab at it, it seemed like I should be able to create my
> own heterogeneous "list" data type -- i.e., a "list" data type that
> can contain elements of different types. (like, [3,'a',True], for
> example)
>
> But I'm a little stuck. My first try was like so:
>
> data HeteroList a b = Null | Element a (HeteroList a b) deriving (Show)
>
> ...but this of course did not work, because all elements end up
> having to be the same type.
>
> Then I tried
>
> data HeteroList a b = Null | Element a (HeteroList b a) deriving (Show)
>
> ...but this doesn't work because every other other element has to be
> the same type:
>
> Element 'a' (Element 1 (Element 'a' (Element 2 Null)))
>
> ...I could go on and embarrass myself some more, but since I'm
> likely widely off-base I'll just ask if somebody can point me in the
> right direction.
I'm not an expert on the subject, but existential quantification allows
it.
{-# LANGUAGE ExistentialQuantification #-}
data HeteroElement = forall a. Element a
list = [Element 1, Element 'a', Element True]
http://en.wikibooks.org/wiki/Haskell/Existentially_quantified_types
http://haskell.org/haskellwiki/Heterogenous_collections
--
Mats Rauhala
MasseR
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110711/94495b3f/attachment-0001.pgp>
------------------------------
Message: 7
Date: Mon, 11 Jul 2011 01:22:13 -0400
From: David McBride <[email protected]>
Subject: Re: [Haskell-beginners] User-defined polymorphic data type:
heterogeneous list?
To: Christopher Howard <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID:
<can+tr41exvdf9eczaespufvuuuodrodrfum8x-a_bate9oo...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
There are two main ways to get lists with different types.
Method one is the most common.
data MyTypes = MyInt Int | MyString String
--Then in your code you deal with it like so:
blah = map myTypeFunc [MyInt 1, MyString "asdf"]
where
myTypeFunc :: MyTypes -> IO ()
myTypeFunc (MyInt i) = putStrLn $ show i
myTypeFunc (MyString s) = putStrLn s
You can even use parametric types to make it more general
data MyTypes a b c = Type1 a | Type2 b | Type3 c
But you are still going to have to deal with each type explicitly
every function that handles them.
The other way is to use type classes:
class MyClass where
somefunction :: Bool -> a
instance MyClass Int where
somefunction True = 1
somefunction False = 0
instance MyClass String where
somefunction True = "true"
somefunction False = "false"
Then you make functions that ultimately make use of only the class's
functions. Since it is the only property on every element of the list
that is guaranteed to be there, it is all you can use.
myList :: MyClass a => [a]
myList = [somefunction True, somefunction False]
There are cases where one is the better option and cases where the
other is best.
Adapting this to your custom lists, you'd make a datatype like:
data HeteroList = Null | Element MyType HeteroList
On Mon, Jul 11, 2011 at 1:00 AM, Christopher Howard
<[email protected]> wrote:
> I'm trying to understand parametric polymorphism and polymorphic data types.
> I especially want to go beyond simply using the defined polymorphic data
> types (lists and so forth) and see how I can make my own useful ones.
>
> As my first stab at it, it seemed like I should be able to create my own
> heterogeneous "list" data type -- i.e., a "list" data type that can contain
> elements of different types. (like, [3,'a',True], for example)
>
> But I'm a little stuck. My first try was like so:
>
> data HeteroList a b = Null | Element a (HeteroList a b) deriving (Show)
>
> ...but this of course did not work, because all elements end up having to be
> the same type.
>
> Then I tried
>
> data HeteroList a b = Null | Element a (HeteroList b a) deriving (Show)
>
> ...but this doesn't work because every other other element has to be the
> same type:
>
> Element 'a' (Element 1 (Element 'a' (Element 2 Null)))
>
> ...I could go on and embarrass myself some more, but since I'm likely widely
> off-base I'll just ask if somebody can point me in the right direction.
>
> --
> frigidcode.com
> theologia.indicium.us
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 8
Date: Mon, 11 Jul 2011 16:21:44 +1000
From: Arlen Cuss <[email protected]>
Subject: Re: [Haskell-beginners] User-defined polymorphic data type:
heterogeneous list?
To: Christopher Howard <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Christopher,
> As my first stab at it, it seemed like I should be able to create my own
> heterogeneous "list" data type -- i.e., a "list" data type that can
> contain elements of different types. (like, [3,'a',True], for example)
One problem I can see would be dealing with the contents of such a list.
Imagine you have a list with many different types in it. Would every
type appear in the type of the list? If so, that would have to be a
consideration before you even get to the "=" sign in your data type
definition. If not, whence do they come?
> But I'm a little stuck. My first try was like so:
> data HeteroList a b = Null | Element a (HeteroList a b) deriving (Show)
> Then I tried
> data HeteroList a b = Null | Element a (HeteroList b a) deriving (Show)
We can see where that there are never any "new" element types
introduced, so you're necessarily limited to two, and as for where they
can appear, it's made clear in your own definitions:
> data HeteroList a b = Null | Element a (HeteroList a b) deriving (Show)
Here 'a' is plugged back into HeteroList's 'a', and thus is always the
actual value of Element.
> data HeteroList a b = Null | Element a (HeteroList b a) deriving (Show)
And here, 'a' and 'b' simply switch places.
As Mats pointed out, an existential quantification will let you define
your polymorphic element (and to quote Mats):
> {-# LANGUAGE ExistentialQuantification #-}
>
> data HeteroElement = forall a. Element a
>
> list = [Element 1, Element 'a', Element True]
The question is, what can you do with this list? You can't "show" it,
because there's no requirement on HeteroElement's "a" type of it having
a Show instance (adding 'deriving Show' to the data statement will cause
an error, as it cannot be done for all 'a'!). You can't find out their
types. Indeed, you can't do anything at all with an Element, simply
because there's no restriction placed on their value. They could contain
anything at all.
I hope this helps. There's more to the story, though, and that's where
someone else will hopefully come in: what use would such a type be?
Cheers,
A
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 37, Issue 17
*****************************************