Manlio Perillo | 14 Jan 15:56
Favicon

some ideas for Haskell', from Python

Hi.

There are two features found in Python language, that I would like to 
see in Haskell.

1) In a Python string it is available the \U{name} escape, where name is
    a character name in the Unicode database.

    As an example:
        foo = u"abc\N{VULGAR FRACTION ONE HALF}"

2) In Python it is possible to import modules inside a function.

    In Haskell something like:

    joinPath' root name =
        joinPath [root, name]
        importing System.FilePath (joinPath)

Thanks  Manlio Perillo
Neil Mitchell | 14 Jan 16:12

Re: some ideas for Haskell', from Python

Hi

> 1) In a Python string it is available the \U{name} escape, where name is
>   a character name in the Unicode database.
>
>   As an example:
>       foo = u"abc\N{VULGAR FRACTION ONE HALF}"

Hmm, looks nice, and sensible. But as soon as you've got \N{....} syntax I want:

"foo\E{show i}bar"

i.e. embed expressions in strings. I think this would be fantastic.

> 2) In Python it is possible to import modules inside a function.
>
>   In Haskell something like:
>
>   joinPath' root name =
>       joinPath [root, name]
>       importing System.FilePath (joinPath)

Looks a bit ugly, but kind of useful. I'd make the syntax:

joinPath' root name = joinPath [root,name]
  where import System.FilePath(joinPath)

It does mean you need to read an entire file to see what functions it
imports, but perhaps that is the way it should be. I could also
imagine a syntax:
(Continue reading)

minh thu | 14 Jan 16:16

Re: some ideas for Haskell', from Python

2009/1/14 Neil Mitchell <ndmitchell <at> gmail.com>:
> Hi
>
>> 1) In a Python string it is available the \U{name} escape, where name is
>>   a character name in the Unicode database.
>>
>>   As an example:
>>       foo = u"abc\N{VULGAR FRACTION ONE HALF}"
>
> Hmm, looks nice, and sensible. But as soon as you've got \N{....} syntax I want:
>
> "foo\E{show i}bar"
>
> i.e. embed expressions in strings. I think this would be fantastic.

Hi,

why not simpy "foo\E{i}bar" ?

>> 2) In Python it is possible to import modules inside a function.
>>
>>   In Haskell something like:
>>
>>   joinPath' root name =
>>       joinPath [root, name]
>>       importing System.FilePath (joinPath)
>
> Looks a bit ugly, but kind of useful. I'd make the syntax:
>
> joinPath' root name = joinPath [root,name]
(Continue reading)

Neil Mitchell | 14 Jan 16:26

Re: some ideas for Haskell', from Python

>>>   As an example:
>>>       foo = u"abc\N{VULGAR FRACTION ONE HALF}"
>>
>> Hmm, looks nice, and sensible. But as soon as you've got \N{....} syntax I want:
>>
>> "foo\E{show i}bar"
>>
>> i.e. embed expressions in strings. I think this would be fantastic.
>
> why not simpy "foo\E{i}bar" ?

What if i is a string? You'd get: foo"i"bar

Having different behaviour for string vs everything else would be equally bad.

>> joinPath' root name = import.System.FilePath.joinPath [root,name]
>>
>> i.e. doing an import and use at the same time.
>
> and why not simply System.FilePath.joinPath (without the import.) ?

That is the same as saying everything is always in scope but fully
qualified. I'd rather have to explicitly say which modules were being
used - I'm not sure my enhanced import idea is a good idea at all.

Thanks

Neil
Favicon

Re: some ideas for Haskell', from Python

On 2009 Jan 14, at 10:26, Neil Mitchell wrote:
>>>>  As an example:
>>>>      foo = u"abc\N{VULGAR FRACTION ONE HALF}"
>>>
>>> Hmm, looks nice, and sensible. But as soon as you've got \N{....}  
>>> syntax I want:
>>>
>>> "foo\E{show i}bar"
>>>
>>> i.e. embed expressions in strings. I think this would be fantastic.
>>
>> why not simpy "foo\E{i}bar" ?
>
> What if i is a string? You'd get: foo"i"bar
>
> Having different behaviour for string vs everything else would be  
> equally bad.

...except that show already *has* different behavior for String vs.  
everything else.

--

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery <at> kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery <at> ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH
Jonathan Cast | 15 Jan 01:05
Favicon

Re: some ideas for Haskell', from Python

On Wed, 2009-01-14 at 18:59 -0500, Brandon S. Allbery KF8NH wrote:
> On 2009 Jan 14, at 10:26, Neil Mitchell wrote:
> >>>>  As an example:
> >>>>      foo = u"abc\N{VULGAR FRACTION ONE HALF}"
> >>>
> >>> Hmm, looks nice, and sensible. But as soon as you've got \N{....}  
> >>> syntax I want:
> >>>
> >>> "foo\E{show i}bar"
> >>>
> >>> i.e. embed expressions in strings. I think this would be fantastic.
> >>
> >> why not simpy "foo\E{i}bar" ?
> >
> > What if i is a string? You'd get: foo"i"bar
> >
> > Having different behaviour for string vs everything else would be  
> > equally bad.
> 
> ...except that show already *has* different behavior for String vs.  
> everything else.

Technically, showList has different behavior for Char vs. everything
else.

And not necessarily for *everything* else.

And that's very well *not* what he meant.

jcc
(Continue reading)

Dougal Stanton | 14 Jan 16:30

Re: some ideas for Haskell', from Python

On Wed, Jan 14, 2009 at 3:12 PM, Neil Mitchell <ndmitchell <at> gmail.com> wrote:

>> 2) In Python it is possible to import modules inside a function.
>>
>>   In Haskell something like:
>>
>>   joinPath' root name =
>>       joinPath [root, name]
>>       importing System.FilePath (joinPath)
>
> Looks a bit ugly, but kind of useful. I'd make the syntax:
>
> joinPath' root name = joinPath [root,name]
>  where import System.FilePath(joinPath)
>
> It does mean you need to read an entire file to see what functions it
> imports, but perhaps that is the way it should be. I could also
> imagine a syntax:
>
> joinPath' root name = import.System.FilePath.joinPath [root,name]
>
> i.e. doing an import and use at the same time.
>

This can be done with a fully-qualified name (or two). Not quite as
succinct, but I assume the scope of these imports is only local
anyway.

> joinPath root path = jp [root,filename path]
>  where jp = System.FilePath.joinPath
(Continue reading)

Manlio Perillo | 14 Jan 16:39
Favicon

Re: some ideas for Haskell', from Python

Neil Mitchell ha scritto:
> Hi
> 
>> 1) In a Python string it is available the \U{name} escape, where name is
>>   a character name in the Unicode database.
>>
>>   As an example:
>>       foo = u"abc\N{VULGAR FRACTION ONE HALF}"
> 
> Hmm, looks nice, and sensible. But as soon as you've got \N{....} syntax I want:
> 
> "foo\E{show i}bar"
> 

How this should/can work?
There is Text.Printf for this.

> i.e. embed expressions in strings. I think this would be fantastic.
> 
>> 2) In Python it is possible to import modules inside a function.
>>
>>   In Haskell something like:
>>
>>   joinPath' root name =
>>       joinPath [root, name]
>>       importing System.FilePath (joinPath)
> 
> Looks a bit ugly, but kind of useful. I'd make the syntax:
> 
> joinPath' root name = joinPath [root,name]
(Continue reading)

Favicon

Re: some ideas for Haskell', from Python


On 2009 Jan 14, at 10:39, Manlio Perillo wrote:
> Neil Mitchell ha scritto:
>> Hi
>>> 1) In a Python string it is available the \U{name} escape, where  
>>> name is
>>>  a character name in the Unicode database.
>>>
>>>  As an example:
>>>      foo = u"abc\N{VULGAR FRACTION ONE HALF}"
>> Hmm, looks nice, and sensible. But as soon as you've got \N{....}  
>> syntax I want:
>> "foo\E{show i}bar"
>
> How this should/can work?
> There is Text.Printf for this.

I vaguely recall seeing some TH go by that did this (albeit with $()  
TH foo).

> Sometime they are necessary, to avoid circular import problems (but  
> this not a problem with Haskell).

...in theory. In practice GHC needs help with circular imports, and  
some cycles might be impossible to resolve.

--

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery <at> kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery <at> ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH
(Continue reading)

Manlio Perillo | 15 Jan 11:55
Favicon

Re: some ideas for Haskell', from Python

Brandon S. Allbery KF8NH ha scritto:
 >
 > [... about Python import local to functions ...]
> 
>> Sometime they are necessary, to avoid circular import problems (but 
>> this not a problem with Haskell).
> 
> ...in theory. In practice GHC needs help with circular imports, and some 
> cycles might be impossible to resolve.
> 

This is interesting.
Where can I find some examples?

Is this explained in the Real World Haskell book?

Thanks  Manlio Perillo
Favicon

Re: some ideas for Haskell', from Python

On 2009 Jan 15, at 5:55, Manlio Perillo wrote:
> Brandon S. Allbery KF8NH ha scritto:
> >
> > [... about Python import local to functions ...]
>>> Sometime they are necessary, to avoid circular import problems  
>>> (but this not a problem with Haskell).
>> ...in theory. In practice GHC needs help with circular imports, and  
>> some cycles might be impossible to resolve.
>
> This is interesting.
> Where can I find some examples?

http://www.haskell.org/ghc/docs/latest/html/users_guide/separate-compilation.html#mutual-recursion
The "impossible to resolve" is my interpretation of a discussion on  
the lists several months ago.

I don't see any mention in RWH offhand.

--

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery <at> kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery <at> ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH
ChrisK | 19 Jan 20:00

Re: some ideas for Haskell', from Python

Manlio Perillo wrote:
> Brandon S. Allbery KF8NH ha scritto:
>  >
>> ...in theory. In practice GHC needs help with circular imports, and 
>> some cycles might be impossible to resolve.
>>
> 
> This is interesting.
> Where can I find some examples?
> 
> Is this explained in the Real World Haskell book?
> 

I have no idea about RWH, but there are certainly mutual import cycles that 
cannot be resolved by using hs-boot files with GHC.

Consider three modules A and B and C, which are A-B-C permutations of

> module A(A,AKBC, AKCB)
> import B(B,BKAC)
> import C(C,CKAB)
>
> data A
> AKBC :: Either B C
> AKCB :: Either C B
 >
 > instance Show (A,BKAC,CKAB) where ...

There is no way to break the ?K?? import cycle with just hs-boot files.  I had 
to solve this by generating "helper" modules.
(Continue reading)

Lennart Augustsson | 14 Jan 17:41
Favicon

Re: some ideas for Haskell', from Python

When Haskell was designed there was a bried discussion (if my memory
serves me) to have import be a decl, so it could occur anywhere a
normal declaration can occur.
I kinda like the idea, but some people didn't and it never happened.

  -- Lennart

On Wed, Jan 14, 2009 at 3:12 PM, Neil Mitchell <ndmitchell <at> gmail.com> wrote:
> Hi
>
>> 1) In a Python string it is available the \U{name} escape, where name is
>>   a character name in the Unicode database.
>>
>>   As an example:
>>       foo = u"abc\N{VULGAR FRACTION ONE HALF}"
>
> Hmm, looks nice, and sensible. But as soon as you've got \N{....} syntax I want:
>
> "foo\E{show i}bar"
>
> i.e. embed expressions in strings. I think this would be fantastic.
>
>> 2) In Python it is possible to import modules inside a function.
>>
>>   In Haskell something like:
>>
>>   joinPath' root name =
>>       joinPath [root, name]
>>       importing System.FilePath (joinPath)
>
(Continue reading)

Ketil Malde | 14 Jan 17:43

Re: some ideas for Haskell', from Python

"Neil Mitchell" <ndmitchell <at> gmail.com> writes:

>> 1) In a Python string it is available the \U{name} escape, where name is
>>   a character name in the Unicode database.

>>   As an example:
>>       foo = u"abc\N{VULGAR FRACTION ONE HALF}"

Why not:

    import Unicode.Entities as U

    foo = "abc"++U.vulgar_fraction_one_half

> Hmm, looks nice, and sensible. But as soon as you've got \N{....} syntax I want:
>
> "foo\E{show i}bar"

  "foo"++show i++"bar"

Change the language - save two characters.

>> 2) In Python it is possible to import modules inside a function.
>>
>>   In Haskell something like:
>>
>>   joinPath' root name =
>>       joinPath [root, name]
>>       importing System.FilePath (joinPath)

(Continue reading)

Job Vranish | 14 Jan 18:37

Re: some ideas for Haskell', from Python

What I would really like to see is locally scoped imports but with parameterized modules. (so modules could take types and values as parameters)
The places where I most want a feature like this is when I have a group of helper functions that need a value that is outside the modules scope, but that in general doesn't change. Usually we just curry the functions, but that generates a lot of wasted code.
A good example might be the token parsers in parsec. Rather than have something like this:

whiteSpace= P.whiteSpace lexer
lexeme = P.lexeme lexer
symbol = P.symbol lexer
natural = P.natural lexer
...
We could do something like:

import ParsecToken lexer

Having an import/module feature like this would replace almost all cases where someone might wish for a macro system for Haskell.

>> Hmm, looks nice, and sensible. But as soon as you've got \N{....} syntax I want:
>>
>> "foo\E{show i}bar"
>
> "foo"++show i++"bar"
>
>Change the language - save two characters.

For simple cases, doing "foo" ++ show i ++ "bar" isn't bad at all, but when you have five or six or ten (show i)s that you want to mix in, it can get pretty hard to read.


On Wed, Jan 14, 2009 at 11:43 AM, Ketil Malde <ketil <at> malde.org> wrote:
"Neil Mitchell" <ndmitchell <at> gmail.com> writes:

>> 1) In a Python string it is available the \U{name} escape, where name is
>>   a character name in the Unicode database.

>>   As an example:
>>       foo = u"abc\N{VULGAR FRACTION ONE HALF}"

Why not:

   import Unicode.Entities as U

   foo = "abc"++U.vulgar_fraction_one_half

> Hmm, looks nice, and sensible. But as soon as you've got \N{....} syntax I want:
>
> "foo\E{show i}bar"

 "foo"++show i++"bar"

Change the language - save two characters.

>> 2) In Python it is possible to import modules inside a function.
>>
>>   In Haskell something like:
>>
>>   joinPath' root name =
>>       joinPath [root, name]
>>       importing System.FilePath (joinPath)

> It does mean you need to read an entire file to see

Well, then you might as well allow multiple modules per file as per
the recent discussion.  And multi-module files will possibly let you
achieve the desired encapsulation without actually changing the
language.

-k
--
If I haven't seen further, it is by standing in the footprints of giants
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Tim Wawrzynczak | 14 Jan 18:48

Re: some ideas for Haskell', from Python



Having an import/module feature like this would replace almost all cases where someone might wish for a macro system for Haskell.


Don't say that until you've tried Lisp macros... read some of Paul Graham's essays or try some Common Lisp for yourself... macros can be an incredibly powerful tool, but "macros" from C, etc. aren't really macros, they're more like find-and-replace expressions :)
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Job Vranish | 14 Jan 19:23

Re: some ideas for Haskell', from Python

On Wed, Jan 14, 2009 at 12:48 PM, Tim Wawrzynczak
<inforichland <at> gmail.com> wrote:
>
>
>> Having an import/module feature like this would replace almost all cases where someone might wish for a
macro system for Haskell.
>
> Don't say that until you've tried Lisp macros... read some of Paul Graham's essays or try some Common Lisp
for yourself... macros can be an incredibly powerful tool, but "macros" from C, etc. aren't really
macros, they're more like find-and-replace expressions :)

You're probably right.
I've played around with LISP macros a little, but it seems that most
of the cases where you would use a macro in LISP you don't need one in
haskell due to lazy evaluation.  Although I haven't played around with
them enough to say much one way or another.

Do you know of a particular example where a macro would be a big help
in haskell?
Tim Wawrzynczak | 14 Jan 19:39

Re: some ideas for Haskell', from Python

You're probably right.
I've played around with LISP macros a little, but it seems that most
of the cases where you would use a macro in LISP you don't need one in
haskell due to lazy evaluation.  Although I haven't played around with
them enough to say much one way or another.

Do you know of a particular example where a macro would be a big help
in haskell?

Well, like many good programming tools, Lisp macros are another abstraction, but instead of dealing with data, they deal with code.  They are a syntactic abstraction.  They're often described as "programs that write programs."  We all know how much Haskell likes abstractions ;)
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Miguel Mitrofanov | 14 Jan 19:46
Favicon

Re: some ideas for Haskell', from Python

> Well, like many good programming tools, Lisp macros are another  
> abstraction, but instead of dealing with data, they deal with code.

I didn't know Lisp puts such an emphasis on the difference between  
code and data.
Jonathan Cast | 14 Jan 19:48
Favicon

Re: some ideas for Haskell', from Python

On Wed, 2009-01-14 at 12:39 -0600, Tim Wawrzynczak wrote:
>         You're probably right.
>         I've played around with LISP macros a little, but it seems
>         that most
>         of the cases where you would use a macro in LISP you don't
>         need one in
>         haskell due to lazy evaluation.  Although I haven't played
>         around with
>         them enough to say much one way or another.
>         
>         Do you know of a particular example where a macro would be a
>         big help
>         in haskell?
> 
> Well, like many good programming tools, Lisp macros are another
> abstraction, but instead of dealing with data, they deal with code.

Haskell already has a couple of abstraction tools for dealing with code.
One is called `first-class functions'; another is called `lazy
evaluation'.

> They are a syntactic abstraction.

What is this good for?  I suspect most Lisp macros are parametric in
form, rather than really syntactic; I know that every example of a Lisp
macro I've seen is parametric in form.  Parametric macros --- macros
that don't deconstruct their arguments --- don't usually need to be
macros at all in modern functional languages.  Do you have an example of
a macro that can't be replaced by higher-order functions and laziness?

> They're often described as "programs that write programs."

So are code generators.  The most common example of a code generator is
probably YACC --- but Parsec replaces it, with better readability even,
with first-class parsers (built atop first-class functions).

jcc
Favicon

Re: some ideas for Haskell', from Python

Jonathan Cast wrote:
> Haskell already has a couple of abstraction tools for dealing with code.
> One is called `first-class functions'; another is called `lazy
> evaluation'.

And for all the rest there is TH?

M.
Tim Wawrzynczak | 14 Jan 19:58

Re: some ideas for Haskell', from Python

On Wed, Jan 14, 2009 at 12:56 PM, Martijn van Steenbergen <martijn <at> van.steenbergen.nl> wrote:

Jonathan Cast wrote:
Haskell already has a couple of abstraction tools for dealing with code.
One is called `first-class functions'; another is called `lazy
evaluation'.

And for all the rest there is TH?

M.


Woah fellas, I wasn't trying to start a flame war, I was merely commenting that those who have not used Lisp don't really understand the power that macros can have in a language (such as Lisp) that supports them, and where code and data can be used interchangeably.  And I've never used TH so I can't comment on that.  Don't worry, I'm on your side :)
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Favicon

Re: some ideas for Haskell', from Python

Tim Wawrzynczak wrote:
> Woah fellas, I wasn't trying to start a flame war, I was merely 
> commenting that those who have not used Lisp don't really understand the 
> power that macros can have in a language (such as Lisp) that supports 
> them, and where code and data can be used interchangeably.  And I've 
> never used TH so I can't comment on that.  Don't worry, I'm on your side :)

Oh, I didn't mean that in a bad way. :-)

I was just thinking that if something turns out hard to express in pure 
functions, you can always resort to Template Haskell.

Martijn.
Max Rabkin | 14 Jan 20:06

Re: some ideas for Haskell', from Python

On Wed, Jan 14, 2009 at 10:48 AM, Jonathan Cast
<jonathanccast <at> fastmail.fm> wrote:
> Do you have an example of
> a macro that can't be replaced by higher-order functions and laziness?

I believe I do: one macro I found useful when writing a web app in
Lisp was something I called hash-bind, which binds variables to the
values in a hashtable, with the variable names as keys. For example:

(hash-bind (a b) hashtable body)
==
(let
    ((a (lookup hashtable "a"))
     (b (lookup hashtable "b"))
    body)

I found this very useful in places where I was given URL request
parameters in a hashtable and wanted to extract some variables from
it. I don't believe it can be replaced by a higher order function
(though I may be wrong).

--Max
Alex Queiroz | 14 Jan 20:11

Re: some ideas for Haskell', from Python

Hallo,

On Wed, Jan 14, 2009 at 5:06 PM, Max Rabkin <max.rabkin <at> gmail.com> wrote:
> On Wed, Jan 14, 2009 at 10:48 AM, Jonathan Cast
> <jonathanccast <at> fastmail.fm> wrote:
>> Do you have an example of
>> a macro that can't be replaced by higher-order functions and laziness?
>
> I believe I do: one macro I found useful when writing a web app in
> Lisp was something I called hash-bind, which binds variables to the
> values in a hashtable, with the variable names as keys. For example:
>

     I have one for binding GET/POST variables to regular variables
transparently and with error checking, just inside the body of the
macro.

--

-- 
-alex
http://www.ventonegro.org/
Max Rabkin | 14 Jan 20:16

Re: some ideas for Haskell', from Python

On Wed, Jan 14, 2009 at 11:11 AM, Alex Queiroz <asandroq <at> gmail.com> wrote:
>     I have one for binding GET/POST variables to regular variables
> transparently and with error checking, just inside the body of the
> macro.

Noooo! You reinvented PHP. What happens if a request variable shadows
the name of another variable used in the body?

> --
> -alex
> http://www.ventonegro.org/
>
Alex Queiroz | 14 Jan 20:21

Re: some ideas for Haskell', from Python

Hallo,

On Wed, Jan 14, 2009 at 5:16 PM, Max Rabkin <max.rabkin <at> gmail.com> wrote:
> On Wed, Jan 14, 2009 at 11:11 AM, Alex Queiroz <asandroq <at> gmail.com> wrote:
>>     I have one for binding GET/POST variables to regular variables
>> transparently and with error checking, just inside the body of the
>> macro.
>
> Noooo! You reinvented PHP. What happens if a request variable shadows
> the name of another variable used in the body?
>

     I list the variables I want to bind in the form. I do not ask to
automatically bind all of them. :)

(query-let (id name job salary)
   (if (> salary 1000) ...))

--

-- 
-alex
http://www.ventonegro.org/
Jonathan Cast | 14 Jan 20:14
Favicon

Re: some ideas for Haskell', from Python

On Wed, 2009-01-14 at 11:06 -0800, Max Rabkin wrote:
> On Wed, Jan 14, 2009 at 10:48 AM, Jonathan Cast
> <jonathanccast <at> fastmail.fm> wrote:
> > Do you have an example of
> > a macro that can't be replaced by higher-order functions and laziness?
> 
> I believe I do: one macro I found useful when writing a web app in
> Lisp was something I called hash-bind, which binds variables to the
> values in a hashtable, with the variable names as keys. For example:
> 
> (hash-bind (a b) hashtable body)
> ==
> (let
>     ((a (lookup hashtable "a"))
>      (b (lookup hashtable "b"))
>     body)
> 
> I found this very useful in places where I was given URL request
> parameters in a hashtable and wanted to extract some variables from
> it. I don't believe it can be replaced by a higher order function
> (though I may be wrong).

Thanks!  When you *know* there's a good reason people say something, and
can't find a good example of *why*, it's a tremendous relief when when
you find one.  Sort of restores your faith in humanity :)

jcc
Tim Wawrzynczak | 14 Jan 20:30

Re: some ideas for Haskell', from Python



On Wed, Jan 14, 2009 at 1:14 PM, Jonathan Cast <jonathanccast <at> fastmail.fm> wrote:
On Wed, 2009-01-14 at 11:06 -0800, Max Rabkin wrote:
> On Wed, Jan 14, 2009 at 10:48 AM, Jonathan Cast
> <jonathanccast <at> fastmail.fm> wrote:
> > Do you have an example of
> > a macro that can't be replaced by higher-order functions and laziness?
>
> I believe I do: one macro I found useful when writing a web app in
> Lisp was something I called hash-bind, which binds variables to the
> values in a hashtable, with the variable names as keys. For example:
>
> (hash-bind (a b) hashtable body)
> ==
> (let
>     ((a (lookup hashtable "a"))
>      (b (lookup hashtable "b"))
>     body)
>
> I found this very useful in places where I was given URL request
> parameters in a hashtable and wanted to extract some variables from
> it. I don't believe it can be replaced by a higher order function
> (though I may be wrong).

Thanks!  When you *know* there's a good reason people say something, and
can't find a good example of *why*, it's a tremendous relief when when
you find one.  Sort of restores your faith in humanity :)

jcc



I thought of another good case (Shamelessly stolen from Paul Graham's 'On Lisp').  When defining a function to average the results of the list, you could define avg like this:

(defun avg (&rest args)
  (/ (apply #'+ args) (length args)))

Or as a macro like this:

(defmacro avg (&rest args)
  `(/ (+ , <at> args) ,(length args)))

The reason the macro is better is that the length of the list is known at compile time, so you don't need to traverse the list to calculate the length of the list.

Food for thought, anyway.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Max Rabkin | 14 Jan 20:34

Re: some ideas for Haskell', from Python

2009/1/14 Tim Wawrzynczak <inforichland <at> gmail.com>:
> The reason the macro is better is that the length of the list is known at
> compile time, so you don't need to traverse the list to calculate the length
> of the list.

Or you could use a real compiler (perhaps even a glorious one) that
does constant folding when the list length is constant, but have your
function still work when it isn't constant.

--Max
Dougal Stanton | 14 Jan 23:47

Re: some ideas for Haskell', from Python

> (defun avg (&rest args)
>   (/ (apply #'+ args) (length args)))
>
> Or as a macro like this:
>
> (defmacro avg (&rest args)
>   `(/ (+ ,@args) ,(length args)))
>
> The reason the macro is better is that the length of the list is known at
> compile time, so you don't need to traverse the list to calculate the length
> of the list.
>

I'm trying to work out how awesome the (+) operator is that you can
add the values together without needing to traverse the list ;-)

D
Alex Queiroz | 15 Jan 00:10

Re: some ideas for Haskell', from Python

Hallo,

On Wed, Jan 14, 2009 at 8:47 PM, Dougal Stanton <ithika <at> gmail.com> wrote:
>> (defun avg (&rest args)
>>   (/ (apply #'+ args) (length args)))
>>
>> Or as a macro like this:
>>
>> (defmacro avg (&rest args)
>>   `(/ (+ ,@args) ,(length args)))
>>
>> The reason the macro is better is that the length of the list is known at
>> compile time, so you don't need to traverse the list to calculate the length
>> of the list.
>>
>
>
> I'm trying to work out how awesome the (+) operator is that you can
> add the values together without needing to traverse the list ;-)
>

     Don't need to traverse the list at run time, not compile
(actually macro-expansion) time.

Cheers,
--

-- 
-alex
http://www.ventonegro.org/
Lennart Augustsson | 15 Jan 00:47
Favicon

Re: some ideas for Haskell', from Python

With macros you can define new variable binding constructs.
That's something I occasionally miss in Haskell.

  -- Lennart

On Wed, Jan 14, 2009 at 6:48 PM, Jonathan Cast
<jonathanccast <at> fastmail.fm> wrote:
> On Wed, 2009-01-14 at 12:39 -0600, Tim Wawrzynczak wrote:
>>         You're probably right.
>>         I've played around with LISP macros a little, but it seems
>>         that most
>>         of the cases where you would use a macro in LISP you don't
>>         need one in
>>         haskell due to lazy evaluation.  Although I haven't played
>>         around with
>>         them enough to say much one way or another.
>>
>>         Do you know of a particular example where a macro would be a
>>         big help
>>         in haskell?
>>
>> Well, like many good programming tools, Lisp macros are another
>> abstraction, but instead of dealing with data, they deal with code.
>
> Haskell already has a couple of abstraction tools for dealing with code.
> One is called `first-class functions'; another is called `lazy
> evaluation'.
>
>> They are a syntactic abstraction.
>
> What is this good for?  I suspect most Lisp macros are parametric in
> form, rather than really syntactic; I know that every example of a Lisp
> macro I've seen is parametric in form.  Parametric macros --- macros
> that don't deconstruct their arguments --- don't usually need to be
> macros at all in modern functional languages.  Do you have an example of
> a macro that can't be replaced by higher-order functions and laziness?
>
>> They're often described as "programs that write programs."
>
> So are code generators.  The most common example of a code generator is
> probably YACC --- but Parsec replaces it, with better readability even,
> with first-class parsers (built atop first-class functions).
>
> jcc
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
David Leimbach | 14 Jan 19:13

Re: some ideas for Haskell', from Python


joinPath' root name = import.System.FilePath.joinPath [root,name]

How is this different from 

joinPath' root name = System.FilePath.joinPath [root,name]
 

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
David Leimbach | 14 Jan 19:15

Re: some ideas for Haskell', from Python



On Wed, Jan 14, 2009 at 10:13 AM, David Leimbach <leimy2k <at> gmail.com> wrote:

joinPath' root name = import.System.FilePath.joinPath [root,name]

How is this different from 

joinPath' root name = System.FilePath.joinPath [root,name]
 

I'm sorry I didn't mean "different", I meant "better than"?  I don't get the advantage.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Marc Weber | 15 Jan 12:35

Re: some ideas for Haskell', from Python

>  1) In a Python string it is available the \U{name} escape, where name is
>     a character name in the Unicode database.
> 
>     As an example:
>         foo = u"abc\N{VULGAR FRACTION ONE HALF}"
> 
I think you can use quasi quotation of ghc to achieve this ?
Your code would look like this then:
  let foo = [$mystr|abc\N{VULGAR FRACTION ONE HALF}]

Sincerly
Marc Weber
Immanuel Litzroth | 15 Jan 12:52

Re: some ideas for Haskell', from Python


2) In Python it is possible to import modules inside a function.

  In Haskell something like:

  joinPath' root name =
      joinPath [root, name]
      importing System.FilePath (joinPath)


In Python importing a module has totally different semantics from importing in Haskell.
I runs the initialization code for the module & makes the names in that module
available to you code. In Haskell modules are just namespace control, and you can always
refer to names imported through import X through the syntax X.name.
This means that the local import in Python solves two problems
1) making a name available locally.
2) running initialization code only when a specific function is called.
Neither of those makes any sense for Haskell as far as I can tell.
Immanuel
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Artyom Shalkhakov | 16 Jan 05:22

Re: some ideas for Haskell', from Python

Hi Immanuel,

2009/1/15 Immanuel Litzroth <immanuel203 <at> gmail.com>:
> In Python importing a module has totally different semantics from importing
> in Haskell.
> I runs the initialization code for the module & makes the names in that
> module
> available to you code. In Haskell modules are just namespace control, and
> you can always
> refer to names imported through import X through the syntax X.name.
> This means that the local import in Python solves two problems
> 1) making a name available locally.
> 2) running initialization code only when a specific function is called.
> Neither of those makes any sense for Haskell as far as I can tell.

Well, how about extensible programs? XMonad uses run-time module
loading to remain configurable. I think this approach is much better than
the ill-defined, partially-specified configuration files used by e.g. Apache,
wpa_supplicant, and other widely used programs.

Cheers,
Artyom Shalkhakov.
Bas van Dijk | 20 Jan 13:54
Gravatar

Re: some ideas for Haskell', from Python

On Wed, Jan 14, 2009 at 3:59 PM, Manlio Perillo
<manlio_perillo <at> libero.it> wrote:
> 2) In Python it is possible to import modules inside a function.
>
>   In Haskell something like:
>
>   joinPath' root name =
>       joinPath [root, name]
>       importing System.FilePath (joinPath)

I just like to point out the dependently typed, Haskell-like,
programming language Agda[1] which has a very nice module system with
the following features:

 * Modules can contain other modules

 * Modules can be locally opened. For example:
   mapMaybe f m = let open Maybe in maybe nothing (just . f) m

 * Renaming of important names: For example:
   open Maybe renaming (Maybe to option; nothing to none; just to some)

 * Parameterized modules: For example:

   module Sort (A : Set) (_<_ : A -> A -> Bool) where
     insert : A -> List A -> List A
     insert y [] = y :: []
     insert y (x :: xs) with x < y
     ... | true  = x :: insert y xs
     ... | false = y :: x :: xs

See section 2.7 of the following Agda tutorial (an open minded Haskell
hacker should be able to read that section on its own):

http://www.cs.chalmers.se/~ulfn/darcs/AFP08/LectureNotes/AgdaIntro.pdf

Hopefully Haskell can borrow some of these ideas sometime.

regards,

Bas

[1] http://wiki.portal.chalmers.se/agda/
Lennart Augustsson | 20 Jan 16:04
Favicon

Re: some ideas for Haskell', from Python

Yes, the Agda modules remind me of Cayenne. :)

On Tue, Jan 20, 2009 at 12:54 PM, Bas van Dijk <v.dijk.bas <at> gmail.com> wrote:
> On Wed, Jan 14, 2009 at 3:59 PM, Manlio Perillo
> <manlio_perillo <at> libero.it> wrote:
>> 2) In Python it is possible to import modules inside a function.
>>
>>   In Haskell something like:
>>
>>   joinPath' root name =
>>       joinPath [root, name]
>>       importing System.FilePath (joinPath)
>
> I just like to point out the dependently typed, Haskell-like,
> programming language Agda[1] which has a very nice module system with
> the following features:
>
>  * Modules can contain other modules
>
>  * Modules can be locally opened. For example:
>   mapMaybe f m = let open Maybe in maybe nothing (just . f) m
>
>  * Renaming of important names: For example:
>   open Maybe renaming (Maybe to option; nothing to none; just to some)
>
>  * Parameterized modules: For example:
>
>   module Sort (A : Set) (_<_ : A -> A -> Bool) where
>     insert : A -> List A -> List A
>     insert y [] = y :: []
>     insert y (x :: xs) with x < y
>     ... | true  = x :: insert y xs
>     ... | false = y :: x :: xs
>
> See section 2.7 of the following Agda tutorial (an open minded Haskell
> hacker should be able to read that section on its own):
>
> http://www.cs.chalmers.se/~ulfn/darcs/AFP08/LectureNotes/AgdaIntro.pdf
>
> Hopefully Haskell can borrow some of these ideas sometime.
>
> regards,
>
> Bas
>
> [1] http://wiki.portal.chalmers.se/agda/
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
George Pollard | 21 Jan 03:44
Favicon
Gravatar

Re: some ideas for Haskell', from Python

On Wed, 2009-01-14 at 15:59 +0100, Manlio Perillo wrote:
> 1) In a Python string it is available the \U{name} escape, where name is
>     a character name in the Unicode database.
> 
>     As an example:
>         foo = u"abc\N{VULGAR FRACTION ONE HALF}"

This is possible via QuasiQuotation, you can write a parser that will
let you do it like this:

	foo = [$s|abc\N{VULGAR FRACTION ONE HALF}|]

I started to write one but got stuck :P By working from the wiki page
[1] I ended up with some code that will let you do:

	let e = 3 in [$s|h\V{e}llo\U{32}world|] == "h3llo world"

I got stuck on a few things:
- how best to allow arbitrary expressions (requires additional parsing
to allow braces inside strings and so on, e.g. [$s|hello \E{"}"}
world|])
- can't figure out how to write the quoting function for the patterns...
this would be awesome if it worked:

	everythingAfterFirstK [$s|\V{before}k\V{after}|] = after

- there's no library for looking up characters by name. 'unicode-names'
has getCharacterName but not the inverse.

Code follows:

StringSplicer.hs
> {-# LANGUAGE DeriveDataTypeable #-}
> 
> module StringSplicer 
> where
> 
> import Data.Generics
> import Text.ParserCombinators.Parsec
> import Control.Monad
> 
> data Exp = StringLit String
>           | Unicode Int
>           | Variable String
>           | Backslash
>           deriving (Show, Typeable, Data)
> 
> interp = do
> 	char '\\'
> 	c <- choice [char 'U', char 'V', char '\\']
> 	case c of
> 		'U' -> do
> 			char '{'
> 			n <- many1 digit
> 			char '}'
> 			return $ Unicode (read n)
> 		'V' -> do
> 			char '{'
> 			s <- manyTill anyChar (try $ char '}')
> 			return $ Variable s
> 		'\\' -> return Backslash
> 
> str = do
> 	s <- many1 $ noneOf ['\\']
> 	return $ StringLit s
> 
> expr = many $ interp <|> str
> 
> parseString :: Monad m => (String, Int, Int) -> String -> m [Exp]
> parseString (file, line, col) s =
> 	case runParser p () "" s of
> 		Left err -> fail $ show err
> 		Right e -> return e
> 	where
> 		p = do
> 			pos <- getPosition
> 			setPosition $
> 				(flip setSourceName) file $
> 				(flip setSourceLine) line $
> 				(flip setSourceColumn) col $
> 				pos
> 			e <- expr
> 			eof
> 			return e

StringSplicer.Quote.hs
> module StringSplicer.Quote
> where
> 
> import Data.Generics
> import qualified Language.Haskell.TH as TH
> import Language.Haskell.TH.Quote
> import Data.Char (chr)
> import StringSplicer
> 
> quoteExprExp :: String -> TH.ExpQ
> quoteExprPat :: String -> TH.PatQ
> 
> s :: QuasiQuoter
> s = QuasiQuoter quoteExprExp quoteExprPat
> 
> parseIt x = do
> 	loc <- TH.location
> 	let pos =
> 		(TH.loc_filename loc,
> 		fst (TH.loc_start loc),
> 		snd (TH.loc_start loc))
> 	parseString pos x
> 
> quoteExprExp x = do
> 	expr <- parseIt x
> 	it <- dataToExpQ (const Nothing `extQ` antiExprExp) expr
> 	return $ TH.AppE (TH.VarE (TH.mkName "concat")) it
> 
> quoteExprPat x = do
> 	expr <- parseIt x
> 	it <- dataToPatQ (const Nothing `extQ` antiExprPat) expr
> 	error "help!"
> 
> antiExprExp :: Exp -> Maybe (TH.Q TH.Exp)
> antiExprExp (StringLit s) = Just $ TH.litE (TH.stringL s)
> antiExprExp (Backslash) = Just $ TH.litE (TH.stringL "\\")
> antiExprExp (Unicode n) = Just $ TH.litE (TH.stringL [chr n])
> antiExprExp (Variable v) = Just $ TH.appE
> 	(TH.varE (TH.mkName "show"))
> 	(TH.varE (TH.mkName v))
> 
> antiExprPat :: Exp -> Maybe (TH.Q TH.Pat)
> antiExprPat (Unicode n) = Just $ TH.litP (TH.stringL [chr n])
> antiExprPat (Backslash) = Just $ TH.litP (TH.stringL "\\")
> antiExprPat (StringLit s) = Just $ TH.litP (TH.stringL s)
> antiExprPat (Variable v) = Just $ TH.varP (TH.mkName v)

[1]: http://haskell.org/haskellwiki/Quasiquotation

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane