Function composition in run-time?

Hi, What is the Haskell way to compose functions in run-time? Depending on configuration parameters I need to be able to compose function in several ways without recompilation. When program starts it reads configuration parameters from a text file. For example, I have three functions, f1, f2, f3, each doing some string processing. I need to support two configurations of string processors : if param1 then sp = f1 . f2 . f3 else sp = f1 . f3 I'd like to avoid 'if' somehow and instead use some declarative way to specify code to run in external configuration file. In other words I need some easy tools to create mini DSLs without all the efforts usually involved with implementing full-blown DSL. Thanks, Dmitri

On Wed, Aug 24, 2011 at 04:35:42PM +0400, dokondr wrote:
Hi, What is the Haskell way to compose functions in run-time? Depending on configuration parameters I need to be able to compose function in several ways without recompilation. When program starts it reads configuration parameters from a text file. For example, I have three functions, f1, f2, f3, each doing some string processing. I need to support two configurations of string processors :
if param1 then sp = f1 . f2 . f3 else sp = f1 . f3
I'd like to avoid 'if' somehow and instead use some declarative way to specify code to run in external configuration file. In other words I need some easy tools to create mini DSLs without all the efforts usually involved with implementing full-blown DSL.
A simple alternative to if would be: options = [ ("foo", f1 . f2 . f3) , ("bar", f1 . f3 )] and then "lookup param options". I don't know if this is what you're looking for, though. regards, iustin

On Wed, Aug 24, 2011 at 4:44 PM, Iustin Pop
On Wed, Aug 24, 2011 at 04:35:42PM +0400, dokondr wrote:
Hi, What is the Haskell way to compose functions in run-time? Depending on configuration parameters I need to be able to compose function in several ways without recompilation.
A simple alternative to if would be:
options = [ ("foo", f1 . f2 . f3) , ("bar", f1 . f3 )]
and then "lookup param options". I don't know if this is what you're looking for, though.
Thanks! Yes, this is what I need - simple and easy. Yet, how function application will work in this case ? I mean after lookup returns me a composition ... need to check what type will it be. -- All the best, dokondr

On Wed, Aug 24, 2011 at 04:57:19PM +0400, dokondr wrote:
On Wed, Aug 24, 2011 at 4:44 PM, Iustin Pop
wrote: On Wed, Aug 24, 2011 at 04:35:42PM +0400, dokondr wrote:
Hi, What is the Haskell way to compose functions in run-time? Depending on configuration parameters I need to be able to compose function in several ways without recompilation.
A simple alternative to if would be:
options = [ ("foo", f1 . f2 . f3) , ("bar", f1 . f3 )]
and then "lookup param options". I don't know if this is what you're looking for, though.
Thanks! Yes, this is what I need - simple and easy. Yet, how function application will work in this case ? I mean after lookup returns me a composition ... need to check what type will it be.
Well, as with your 'if', all compositions must have the same type, since lists are homogeneous. regards, iustin

If your functions have the same type, then you can easily collect them
in a data structure, say list, and fold that.
For example:
function :: String -> (String -> String)
function "f1" = f1
function "f2" = f2
function "f3" = f3
runAUserSpecifiedComposition :: String -> F
runAUserSpecifiedComposition = foldl (.) id . map function . words
runAUserSpecifiedComposition "f1 f2 f3" should be equal to (f1 . f2 . f3) now.
On 24 August 2011 13:35, dokondr
Hi, What is the Haskell way to compose functions in run-time? Depending on configuration parameters I need to be able to compose function in several ways without recompilation. When program starts it reads configuration parameters from a text file. For example, I have three functions, f1, f2, f3, each doing some string processing. I need to support two configurations of string processors :
if param1 then sp = f1 . f2 . f3 else sp = f1 . f3
I'd like to avoid 'if' somehow and instead use some declarative way to specify code to run in external configuration file. In other words I need some easy tools to create mini DSLs without all the efforts usually involved with implementing full-blown DSL.
Thanks, Dmitri
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Wed, Aug 24, 2011 at 4:52 PM, Arseniy Alekseyev < arseniy.alekseyev@gmail.com> wrote:
If your functions have the same type, then you can easily collect them in a data structure, say list, and fold that.
For example:
function :: String -> (String -> String) function "f1" = f1 function "f2" = f2 function "f3" = f3
runAUserSpecifiedComposition :: String -> F runAUserSpecifiedComposition = foldl (.) id . map function . words
runAUserSpecifiedComposition "f1 f2 f3" should be equal to (f1 . f2 . f3) now.
This is a nice one, looks already like tiny DSL ) I think I've got the main idea - enumerate in my program all function compositions in some data structure for Haskell to compile, and the associate these with parameter values in external file. Thanks everybody! dokondr

dokondr
This is a nice one, looks already like tiny DSL )
I think I've got the main idea - enumerate in my program all function compositions in some data structure for Haskell to compile, and the associate these with parameter values in external file.
In Haskell you get a not-even-too-tiny DSL for free for composing functions in arbitrary, dynamic ways. Hint: (a ->) is a monad and (->) is an ArrowChoice and an ArrowApply. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

Hi,
if param1 then sp = f1 . f2 . f3 else sp = f1 . f3
If you have many situations like these, i.e. where one or several
components are conditional, you can define a function
when True f = f
when False _ = id
And now you can define sp like this:
sp = f1 . when param1 f2 . f3
Regards,
Jonas
On 24 August 2011 14:35, dokondr
Hi, What is the Haskell way to compose functions in run-time? Depending on configuration parameters I need to be able to compose function in several ways without recompilation. When program starts it reads configuration parameters from a text file. For example, I have three functions, f1, f2, f3, each doing some string processing. I need to support two configurations of string processors :
if param1 then sp = f1 . f2 . f3 else sp = f1 . f3
I'd like to avoid 'if' somehow and instead use some declarative way to specify code to run in external configuration file. In other words I need some easy tools to create mini DSLs without all the efforts usually involved with implementing full-blown DSL.
Thanks, Dmitri
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (5)
-
Arseniy Alekseyev
-
dokondr
-
Ertugrul Soeylemez
-
Iustin Pop
-
Jonas Almström Duregård