[Haskell-cafe] Re: Reflection.Emit in Haskell

apfelmus apfelmus at quantentunnel.de
Fri Jan 18 03:52:17 EST 2008


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



More information about the Haskell-Cafe mailing list