[Haskell-cafe] Language.Haskell.Parser question

2009-03-26 Thread Vasyl Pasternak
Hi,

I want to parse haskell file to find all calls to function 'foo' and
gathers a create a list of all
argumets, which passed to it. E.g. from the following code:

f1 = foo 5
f2 = foo 8
f3 = foo 9

 I want to extract a list [5, 8, 9] (suppouse function takes only one argument)

The most obvious way is to use Language.Haskell for this task. The
parser works pretty good,
but its output data type is terrible. As I understand, I need to
extract all objects that looks like
HsApp (HsVar (UnQual (HsIdent foo))) 

The question is, is there a method to do it quickly or I have to
process each object of different type
separately ?

Maybe it is a work for a template Haskell ?

Thanks.

-- 
Best regards,
Vasyl Pasternak
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Language.Haskell.Parser question

2009-03-26 Thread Neil Mitchell
Hi

 f1 = foo 5
 f2 = foo 8
 f3 = foo 9

  I want to extract a list [5, 8, 9] (suppouse function takes only one 
 argument)

Firstly, use haskell-src-exts and Language.Haskell.Exts - its a much
better library, deals with many extensions, and gives you everything
Language.Haskell did.

 parser works pretty good,
 but its output data type is terrible. As I understand, I need to
 extract all objects that looks like
 HsApp (HsVar (UnQual (HsIdent foo))) 

It's trivial, if you use a generics solution, say  Uniplate:
http://community.haskell.org/~ndm/uniplate

[x | App foo x - universeBi src, prettyPrint foo == foo ]

I often use prettyPrint to follow down Ident/Qual/UnQual paths without
having to know as much of the data type. Using universeBi makes it
easy to find everything that occurs everywhere.

If you attempt this without using a generics library, I'd say you are
destined to fail.

Thanks

Neil
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Language.Haskell.Parser question

2009-03-26 Thread minh thu
2009/3/26 Vasyl Pasternak vasyl.paster...@gmail.com:
 Hi,

 I want to parse haskell file to find all calls to function 'foo' and
 gathers a create a list of all
 argumets, which passed to it. E.g. from the following code:

 f1 = foo 5
 f2 = foo 8
 f3 = foo 9

  I want to extract a list [5, 8, 9] (suppouse function takes only one 
 argument)

 The most obvious way is to use Language.Haskell for this task. The
 parser works pretty good,
 but its output data type is terrible. As I understand, I need to
 extract all objects that looks like
 HsApp (HsVar (UnQual (HsIdent foo))) 

 The question is, is there a method to do it quickly or I have to
 process each object of different type
 separately ?

Have a look at this:
http://neilmitchell.blogspot.com/2009/03/concise-generic-queries.html

Cheers,
Thu
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Language.Haskell.Parser question

2009-03-26 Thread John Van Enk
Actually, looking at the docs for UniplateStr[1], isn't there an error in
the following example statement in the Queries section?

vals x = [Val i | i - universe x]

Shouldn't that be:

vals x = [i | Val i - universe x]

?

/jve

1.
http://hackage.haskell.org/packages/archive/uniplate/1.2.0.3/doc/html/Data-Generics-UniplateStr.html


On Thu, Mar 26, 2009 at 1:47 PM, minh thu not...@gmail.com wrote:

 2009/3/26 Vasyl Pasternak vasyl.paster...@gmail.com:
  Hi,
 
  I want to parse haskell file to find all calls to function 'foo' and
  gathers a create a list of all
  argumets, which passed to it. E.g. from the following code:
 
  f1 = foo 5
  f2 = foo 8
  f3 = foo 9
 
   I want to extract a list [5, 8, 9] (suppouse function takes only one
 argument)
 
  The most obvious way is to use Language.Haskell for this task. The
  parser works pretty good,
  but its output data type is terrible. As I understand, I need to
  extract all objects that looks like
  HsApp (HsVar (UnQual (HsIdent foo))) 
 
  The question is, is there a method to do it quickly or I have to
  process each object of different type
  separately ?

 Have a look at this:
 http://neilmitchell.blogspot.com/2009/03/concise-generic-queries.html

 Cheers,
 Thu
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Language.Haskell.Parser question

2009-03-26 Thread John Van Enk
Excellent!
The black magic had me scratching my head until I realized it was broken
magic. :)

/jve


On Thu, Mar 26, 2009 at 4:23 PM, Neil Mitchell ndmitch...@gmail.com wrote:

 Hi John,

  Actually, looking at the docs for UniplateStr[1], isn't there an error in
  the following example statement in the Queries section?
 
  vals x = [Val i | i - universe x]
 
  Shouldn't that be:
  vals x = [i | Val i - universe x]

 Yep, you are indeed right. I've fixed the examples in the darcs
 version, and next time there is a release these changes will be
 incorporated.

 Many thanks

 Neil

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Language.Haskell.Parser question

2009-03-26 Thread Neil Mitchell
Hi John,

 Actually, looking at the docs for UniplateStr[1], isn't there an error in
 the following example statement in the Queries section?

 vals x = [Val i | i - universe x]

 Shouldn't that be:
 vals x = [i | Val i - universe x]

Yep, you are indeed right. I've fixed the examples in the darcs
version, and next time there is a release these changes will be
incorporated.

Many thanks

Neil
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Language.Haskell.Parser question

2009-03-26 Thread Neil Mitchell
 Excellent!
 The black magic had me scratching my head until I realized it was broken
 magic. :)

I should probably rerelease Uniplate so the documentation gets fixed,
but its not actually broken - although there is no way it does what
I intended!

vals x = [Val i | i - universe x]

Given the type rules:

Val :: Int - Expr
universe :: a - [a]

And the knowledge that:

universe (i :: Int) = [i]

We can conclude that i :: Int and x :: Int. Therefore the function is
equivalent to:

vals :: Int - [Expr]
vals i = [Val i]

Certainly not what I was going for, but it does work! You could even
argue it would be sensible to name such a function vals...

Thanks

Neil


 On Thu, Mar 26, 2009 at 4:23 PM, Neil Mitchell ndmitch...@gmail.com wrote:

 Hi John,

  Actually, looking at the docs for UniplateStr[1], isn't there an error
  in
  the following example statement in the Queries section?
 
  vals x = [Val i | i - universe x]
 
  Shouldn't that be:
  vals x = [i | Val i - universe x]

 Yep, you are indeed right. I've fixed the examples in the darcs
 version, and next time there is a release these changes will be
 incorporated.

 Many thanks

 Neil


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Language.Haskell.Parser question

2009-03-26 Thread Vasyl Pasternak
Wow, uniplate is the library from my dreams :)

I knew there should be easy, elegant and simple solution (I hate brute
force, that's why I start using Haskell).

Many thanks.

2009/3/26 Neil Mitchell ndmitch...@gmail.com:
 Hi

 f1 = foo 5
 f2 = foo 8
 f3 = foo 9

  I want to extract a list [5, 8, 9] (suppouse function takes only one 
 argument)

 Firstly, use haskell-src-exts and Language.Haskell.Exts - its a much
 better library, deals with many extensions, and gives you everything
 Language.Haskell did.

 parser works pretty good,
 but its output data type is terrible. As I understand, I need to
 extract all objects that looks like
 HsApp (HsVar (UnQual (HsIdent foo))) 

 It's trivial, if you use a generics solution, say  Uniplate:
 http://community.haskell.org/~ndm/uniplate

 [x | App foo x - universeBi src, prettyPrint foo == foo ]

 I often use prettyPrint to follow down Ident/Qual/UnQual paths without
 having to know as much of the data type. Using universeBi makes it
 easy to find everything that occurs everywhere.

 If you attempt this without using a generics library, I'd say you are
 destined to fail.

 Thanks

 Neil




-- 
Best regards,
Vasyl Pasternak
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe