
As a .NET (C#/F#) programmer learning Haskell, I would love to know the best online sources about run-time compilation etc. like Reflection.Emit in .NET. I am making heavy use of this .NET API to compile customized (regular-expressions-) FSAs at run-time and want to learn how I might achieve the same in Haskell. Book or online article references specific to this issue will be highly appreciated ^_^ Best Regards, Cetin Sert INF 521, 4-6-2 69120 Heidelberg Germany http://www.corsis.de

Hello Cetin, Friday, January 18, 2008, 12:52:27 AM, you wrote:
As a .NET (C#/F#) programmer learning Haskell, I would love to know the best online sources about run-time compilation etc. like
hs-plugins (unix-only afair), and GHC-as-a-library
Reflection.Emit in .NET. I am making heavy use of this .NET API to compile customized (regular-expressions-) FSAs at run-time and want to learn how I might achieve the same in Haskell. Book or online article references specific to this issue will be highly appreciated ^_^
it was Runtime_Compilation page on old Haskell wiki, written by Andrew Bromage and me. in short, some form of run-time compilation works without actual compiling code. in particular, RegEx package compiles regexps on the fly. my own program includes very simple variant of run-time compiled regexps: -- Compiled regexp Example data RegExpr = RE_End -- "" | RE_Anything -- "*" | RE_AnyStr RegExpr -- '*':"bc*" | RE_FromEnd RegExpr -- '*':"bc" | RE_AnyChar RegExpr -- '?':"bc" | RE_Char Char RegExpr -- 'a':"bc" -- |Compile string representation of regexpr into RegExpr compile_RE s = case s of "" -> RE_End "*" -> RE_Anything '*':cs | cs `contains` '*' -> RE_AnyStr (compile_RE cs) | otherwise -> RE_FromEnd (compile_RE$ reverse s) '?':cs -> RE_AnyChar (compile_RE cs) c :cs -> RE_Char c (compile_RE cs) -- |Check match of string s with regexpr re match_RE re s = case re of RE_End -> null s RE_Anything -> True RE_AnyStr r -> any (match_RE r) (tails s) RE_FromEnd r -> match_RE r (reverse s) RE_AnyChar r -> case s of "" -> False _:xs -> match_RE r xs RE_Char c r -> case s of "" -> False x:xs -> x==c && match_RE r xs -- |Check match of string s with regexpr re, represented as string match re {-s-} = match_RE (compile_RE re) {-s-} actually, you will not find anything unusual here. the code for matching with RegExpr is just constructed at runtime (as tree of closures) which makes matching quite efficient -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Bulat Ziganshin wrote:
it was Runtime_Compilation page on old Haskell wiki, written by Andrew Bromage and me. in short, some form of run-time compilation works without actual compiling code.
-- |Check match of string s with regexpr re match_RE re s = case re of RE_End -> null s RE_Anything -> True RE_AnyStr r -> any (match_RE r) (tails s) RE_FromEnd r -> match_RE r (reverse s) RE_AnyChar r -> case s of "" -> False _:xs -> match_RE r xs RE_Char c r -> case s of "" -> False x:xs -> x==c && match_RE r xs
-- |Check match of string s with regexpr re, represented as string match re {-s-} = match_RE (compile_RE re) {-s-}
actually, you will not find anything unusual here. the code for matching with RegExpr is just constructed at runtime (as tree of closures) which makes matching quite efficient
I doubt that this works as intendend, I think you have to explicitly fist the lambdas into the case expression: match_RE r = case r of RE_End -> null RE_Anything -> const True RE_AnyStr r -> let re = match_RE r in \s -> any re (tails s) RE_FromEnd r -> let re = match_RE r in re . reverse RE_AnyChar r -> let re = match_RE r in \s -> case s of "" -> False _:xs -> re xs RE_Char c r -> let re = match_RE r in \s -> case s of "" -> False x:xs -> x==c && re r xs This way, every match_RE r will evaluated to the function corresponding to r and the result will be shared. In your code, match_RE r s will be "run-time compile" the regex matcher for each string s again and again. Regards, apfelmus

Hello apfelmus, Friday, January 18, 2008, 11:52:17 AM, you wrote:
I doubt that this works as intendend, I think you have to explicitly fist the lambdas into the case expression:
thank you. it was written in this style initially. then i rewrote it believing in GHC power but don't really checked results -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Cetin Sert schrieb:
As a .NET (C#/F#) programmer learning Haskell, I would love to know the best online sources about run-time compilation etc. like Reflection.Emit in .NET. I am making heavy use of this .NET API to compile customized (regular-expressions-) FSAs at run-time and want to learn how I might achieve the same in Haskell. Book or online article references specific to this issue will be highly appreciated ^_^
I am not familiar with C# but I think what you looking for is the GHC api which allows you to compile haskell expressions at runtime. Look for metaplug on hackage (http://hackage.haskell.org/cgi-bin/hackage-scripts/package/metaplug-0.1.1). In its source you ll find an example on how to use the api. You may of course use metaplug itself. I must warn you though that the GHC api is not the best documented and maintained. Currently (ghc 6.8.1) f.i. it seems that reloading modules is broken. The most official docu is here: http://www.haskell.org/haskellwiki/GHC/As_a_library. regards, Martin

That would be possible, but wouldn't Template Haskell also be an option? Peter Martin Lütke wrote:
Cetin Sert schrieb:
As a .NET (C#/F#) programmer learning Haskell, I would love to know the best online sources about run-time compilation etc. like Reflection.Emit in .NET. I am making heavy use of this .NET API to compile customized (regular-expressions-) FSAs at run-time and want to learn how I might achieve the same in Haskell. Book or online article references specific to this issue will be highly appreciated ^_^
I am not familiar with C# but I think what you looking for is the GHC api which allows you to compile haskell expressions at runtime. Look for metaplug on hackage (http://hackage.haskell.org/cgi-bin/hackage-scripts/package/metaplug-0.1.1). In its source you ll find an example on how to use the api. You may of course use metaplug itself. I must warn you though that the GHC api is not the best documented and maintained. Currently (ghc 6.8.1) f.i. it seems that reloading modules is broken. The most official docu is here: http://www.haskell.org/haskellwiki/GHC/As_a_library.
regards, Martin ------------------------------------------------------------------------
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hello Peter, Friday, January 18, 2008, 9:21:20 AM, you wrote:
That would be possible, but wouldn't Template Haskell also be an option?
TH is just macroprocessor. it does nothing at runtime -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
participants (5)
-
apfelmus
-
Bulat Ziganshin
-
Cetin Sert
-
Martin Lütke
-
Peter Verswyvelen