Scripting language: is Haskell a good choice?
Hi, I would like to create a scripting language, similar to Ruby, Perl and Python. Pugs, written in Haskell, is a Perl6 implementation. Is Haskell a good choice for me? I have no experience with Haskell (yet), but I like the concept of functional programming. Because Haskell will probably be too slow for the final implementation, I will have to rewrite it in C or maybe D. Haskell can be very useful as a test/prototype implementation, where speed is not very important. But will I be able to create a clean, and easy to understand implementation in Haskell? The scripting language will be object oriented, and imperative. Is that a problem because Haskell is functional, or is there be an obvious and nice way to implement an imperative scripting language? The language is very dynamic, and the source-tree needs to be in memory because it is modifiable at run-time. Would it be good to do this in Haskell, and port it to C if I like the implementation, or start in C? Keep the parser/lexer for the source code in Haskell, but port only the interpreter to C? What would be a good place to start? I am reading Yet Another Haskell tutorial, and I've read the first 6 of two dozen lessons in Haskell. What to do next, practice/read more/start with the implementation of the scripting language? Thanks in advance, Jules
On Tue, 24 Jan 2006, Jules Jacobs wrote:
Is that a problem because Haskell is functional, or is there be an obvious and nice way to implement an imperative scripting language?
There're several. Perhaps the most obvious if your scripting language will do IO anyway is just to use IORefs, or you could use the ST monad or a map from references to values (good for debugging purposes because you can keep an entire trace in memory - because individual maps're immutable, they can share data so this means less memory consumed than you might think). -- flippa@flippac.org Performance anxiety leads to premature optimisation
Thanks, I will check these out. On 1/24/06, Philippa Cowderoy <flippa@flippac.org> wrote:
On Tue, 24 Jan 2006, Jules Jacobs wrote:
Is that a problem because Haskell is functional, or is there be an obvious and nice way to implement an imperative scripting language?
There're several. Perhaps the most obvious if your scripting language will do IO anyway is just to use IORefs, or you could use the ST monad or a map from references to values (good for debugging purposes because you can keep an entire trace in memory - because individual maps're immutable, they can share data so this means less memory consumed than you might think).
-- flippa@flippac.org
Performance anxiety leads to premature optimisation
-- Groeten, Jules
Hello Jules, Wednesday, January 25, 2006, 12:29:48 AM, you wrote: JJ> I would like to create a scripting language, similar to Ruby, Perl and JJ> Python. Pugs, written in Haskell, is a Perl6 implementation. Is Haskell a JJ> good choice for me? yes, if you ready to learn many new things. Haskell is very different from non-FP languages JJ> I have no experience with Haskell (yet), but I like the JJ> concept of functional programming. Because Haskell will probably be too slow JJ> for the final implementation, I will have to rewrite it in C or maybe D. i'm not sure that you will not change your plans. may be sometime you will prefer to rewrite existing Haskell implemetations just to make your interpreter faster ;) JJ> Haskell can be very useful as a test/prototype implementation, where speed JJ> is not very important. But will I be able to create a clean, and easy to JJ> understand implementation in Haskell? if speed is not main goal - definitely yes JJ> The scripting language will be object JJ> oriented, and imperative. Is that a problem because Haskell is functional, JJ> or is there be an obvious and nice way to implement an imperative scripting JJ> language? it's no problem at all JJ> The language is very dynamic, and the source-tree needs to be in memory JJ> because it is modifiable at run-time. JJ> Would it be good to do this in Haskell, and port it to C if I like the JJ> implementation, or start in C? Keep the parser/lexer for the source code in JJ> Haskell, but port only the interpreter to C? yes, you can use C and Haskell together. but C code can't walk Haskell data structures, so such co-working will need some manual work JJ> What would be a good place to start? I am reading Yet Another Haskell JJ> tutorial, and I've read the first 6 of two dozen lessons in Haskell. What to JJ> do next, practice/read more/start with the implementation of the scripting JJ> language? read about parsec library and start :) this lib already contains several examples of implementing small FP and imperative languages, each implemetation is just 5-10 kb in size -- Best regards, Bulat mailto:bulatz@HotPOP.com
You have some way to go. It may be helpful to take a look at our course on compiler construction, in which we explain how to use the various tools we have built to build compilers and interpreters. I suggest you download the code and the tools and the lecture notes, and start to try to make some of the exercises. Our students somehow manage ;-} http://www.cs.uu.nl/wiki/Ipt/WebHome Doaitse Swierstra On 2006 jan 25, at 17:23, Bulat Ziganshin wrote:
Hello Jules,
Wednesday, January 25, 2006, 12:29:48 AM, you wrote:
JJ> I would like to create a scripting language, similar to Ruby, Perl and JJ> Python. Pugs, written in Haskell, is a Perl6 implementation. Is Haskell a JJ> good choice for me?
yes, if you ready to learn many new things. Haskell is very different from non-FP languages
JJ> I have no experience with Haskell (yet), but I like the JJ> concept of functional programming. Because Haskell will probably be too slow JJ> for the final implementation, I will have to rewrite it in C or maybe D.
i'm not sure that you will not change your plans. may be sometime you will prefer to rewrite existing Haskell implemetations just to make your interpreter faster ;)
JJ> Haskell can be very useful as a test/prototype implementation, where speed JJ> is not very important. But will I be able to create a clean, and easy to JJ> understand implementation in Haskell?
if speed is not main goal - definitely yes
JJ> The scripting language will be object JJ> oriented, and imperative. Is that a problem because Haskell is functional, JJ> or is there be an obvious and nice way to implement an imperative scripting JJ> language?
it's no problem at all
JJ> The language is very dynamic, and the source-tree needs to be in memory JJ> because it is modifiable at run-time.
JJ> Would it be good to do this in Haskell, and port it to C if I like the JJ> implementation, or start in C? Keep the parser/lexer for the source code in JJ> Haskell, but port only the interpreter to C?
yes, you can use C and Haskell together. but C code can't walk Haskell data structures, so such co-working will need some manual work
JJ> What would be a good place to start? I am reading Yet Another Haskell JJ> tutorial, and I've read the first 6 of two dozen lessons in Haskell. What to JJ> do next, practice/read more/start with the implementation of the scripting JJ> language?
read about parsec library and start :) this lib already contains several examples of implementing small FP and imperative languages, each implemetation is just 5-10 kb in size
-- Best regards, Bulat mailto:bulatz@HotPOP.com
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
I used Haskell to implement a kind of scripting language, and it seemed most usable to me. I particular, I liked the fact that it was possible to write a Parsec parser that "compiles" script language expressions directly into executable Haskell functions, avoiding the need for a separate interpretation layer, and because of this I think performance is probably quite respectable. Higher order functions are, I believe, a real boon for this kind of work. My work in this area is a bit stale, but can be found at: http://www.ninebynine.org/RDFNotes/Swish/Intro.html The particular module that "compiles" script language to functions is here: http://www.ninebynine.org/Software/Swish-0.2.1/HaskellRDF/SwishScript.hs The parser assembles a list of function that uses the "SwishState helper functions" as primitives, and operate in the SwishStateIO monad - an I/O monad that also incorprates some specific state that is used by my interpreter. Upper-level functions are 'parseScriptFromString' and 'script'. #g -- Jules Jacobs wrote:
Hi,
I would like to create a scripting language, similar to Ruby, Perl and Python. Pugs, written in Haskell, is a Perl6 implementation. Is Haskell a good choice for me? I have no experience with Haskell (yet), but I like the concept of functional programming. Because Haskell will probably be too slow for the final implementation, I will have to rewrite it in C or maybe D. Haskell can be very useful as a test/prototype implementation, where speed is not very important. But will I be able to create a clean, and easy to understand implementation in Haskell? The scripting language will be object oriented, and imperative. Is that a problem because Haskell is functional, or is there be an obvious and nice way to implement an imperative scripting language?
The language is very dynamic, and the source-tree needs to be in memory because it is modifiable at run-time.
Would it be good to do this in Haskell, and port it to C if I like the implementation, or start in C? Keep the parser/lexer for the source code in Haskell, but port only the interpreter to C?
What would be a good place to start? I am reading Yet Another Haskell tutorial, and I've read the first 6 of two dozen lessons in Haskell. What to do next, practice/read more/start with the implementation of the scripting language?
Thanks in advance,
Jules
------------------------------------------------------------------------
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
-- Graham Klyne For email: http://www.ninebynine.org/#Contact
Jules, If you're looking for a text on implementing interpreters using functional programming languages, Essentials of Programming Languages by Friedman et al. is a good start. It uses Scheme instead of Haskell, but it might be helpful when you're reading Haskell code, like samples that come with the Parsec library, or Graham's Swish project. Also, Autrijus Tang, the author of Pugs mentions a couple of textbooks that he found invaluable while working on Pugs, although I think they're at a more advanced level, so you might not want to start off with them: http://svn.perl.org/perl6/pugs/trunk/docs/01Overview.html Davor On 1/26/06, Graham Klyne <GK@ninebynine.org> wrote:
I used Haskell to implement a kind of scripting language, and it seemed most usable to me.
I particular, I liked the fact that it was possible to write a Parsec parser that "compiles" script language expressions directly into executable Haskell functions, avoiding the need for a separate interpretation layer, and because of this I think performance is probably quite respectable. Higher order functions are, I believe, a real boon for this kind of work.
My work in this area is a bit stale, but can be found at: http://www.ninebynine.org/RDFNotes/Swish/Intro.html The particular module that "compiles" script language to functions is here: http://www.ninebynine.org/Software/Swish-0.2.1/HaskellRDF/SwishScript.hs
The parser assembles a list of function that uses the "SwishState helper functions" as primitives, and operate in the SwishStateIO monad - an I/O monad that also incorprates some specific state that is used by my interpreter. Upper-level functions are 'parseScriptFromString' and 'script'.
#g --
Jules Jacobs wrote:
Hi,
I would like to create a scripting language, similar to Ruby, Perl and Python. Pugs, written in Haskell, is a Perl6 implementation. Is Haskell a good choice for me? I have no experience with Haskell (yet), but I like the concept of functional programming. Because Haskell will probably be too slow for the final implementation, I will have to rewrite it in C or maybe D. Haskell can be very useful as a test/prototype implementation, where speed is not very important. But will I be able to create a clean, and easy to understand implementation in Haskell? The scripting language will be object oriented, and imperative. Is that a problem because Haskell is functional, or is there be an obvious and nice way to implement an imperative scripting language?
The language is very dynamic, and the source-tree needs to be in memory because it is modifiable at run-time.
Would it be good to do this in Haskell, and port it to C if I like the implementation, or start in C? Keep the parser/lexer for the source code in Haskell, but port only the interpreter to C?
What would be a good place to start? I am reading Yet Another Haskell tutorial, and I've read the first 6 of two dozen lessons in Haskell. What to do next, practice/read more/start with the implementation of the scripting language?
Thanks in advance,
Jules
------------------------------------------------------------------------
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
-- Graham Klyne For email: http://www.ninebynine.org/#Contact
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
Thanks, everyone, for the kind responses. I will read Yet Another Haskell Tutorial first, and then I am going to study All About Monads, and maybe a textbook (or more basic Haskell) after that. Thanks again, the Haskell community is very helpful! Jules
On 24/01/06, Jules Jacobs <julesjacobs@gmail.com> wrote:
Hi,
I would like to create a scripting language, similar to Ruby, Perl and Python. Pugs, written in Haskell, is a Perl6 implementation. Is Haskell a good choice for me? I have no experience with Haskell (yet), but I like the concept of functional programming. Because Haskell will probably be too slow for the final implementation, I will have to rewrite it in C or maybe D.
You might be surprised here. What are you scripting? It should be possible to get more than acceptable performance for most tasks. Hey, GHC is only second to GCC in the computer language shootout right now. :)
Haskell can be very useful as a test/prototype implementation, where speed is not very important. But will I be able to create a clean, and easy to understand implementation in Haskell? The scripting language will be object oriented, and imperative. Is that a problem because Haskell is functional, or is there be an obvious and nice way to implement an imperative scripting language?
Well, it shouldn't matter too much. You'll probably represent your abstract syntax directly with some data type, and implement the interpreter as a map from that type to IO (or a monad over IO with some extra features, like a symbol table).
The language is very dynamic, and the source-tree needs to be in memory because it is modifiable at run-time.
It shouldn't be too hard to allow for that sort of thing to happen, but I wouldn't be able to suggest an implementation without knowing more about it.
Would it be good to do this in Haskell, and port it to C if I like the implementation, or start in C? Keep the parser/lexer for the source code in Haskell, but port only the interpreter to C?
Porting things from Haskell to C would be a pain in general. You can usually expect a factor of 10 to 15 blowup in the amount of code you have to write, and you'd generally need a fairly major redesign of how the thing works internally. Haskell supports various mechanisms like laziness and parametric polymorphism that don't have easy translations to C. One thing you might try is to write a bytecode compiler for the language in Haskell, and write a bytecode interpreter in hand-tuned C/Assembler or something.
What would be a good place to start? I am reading Yet Another Haskell tutorial, and I've read the first 6 of two dozen lessons in Haskell. What to do next, practice/read more/start with the implementation of the scripting language?
It's probably *possible* to start, but be aware that you may want to rewrite large chunks of code later. It's probably better to practise on smaller projects and read more. Then again, if you start with a tiny subset of the language you have in mind, writing an interpreter for that would probably be a good way to learn, so you might do both. Also, if you haven't checked it out yet, we have a great IRC channel, #haskell on freenode, where you can ask questions and get help. Early on in my progress, I learned a lot just by lurking there, and stealing people's homework problems to do myself :) Most everyone there is pretty friendly, and eager to help. - Cale
participants (7)
-
Bulat Ziganshin -
Cale Gibbard -
Davor Cubranic -
Doaitse Swierstra -
Graham Klyne -
Jules Jacobs -
Philippa Cowderoy