Skip to content Skip to sidebar Skip to footer

Equivalent Of Python Eval In Haskell

There is function in python called eval that takes string input and evaluates it. >>> x = 1 >>> print eval('x+1') 2 >>> print eval('12 + 32') 44 >>

Solution 1:

It is true that in Haskell, as in Java or C++ or similar languages, you can call out to the compiler, then dynamically load the code and execute it. However, this is generally heavy weight and almost never why people use eval() in other languages.

People tend to use eval() in a language because given that language's facilities, for certain classes of problem, it is easier to construct a string from the program input that resembles the language itself, rather than parse and evaluate the input directly.

For example, if you want to allow users to enter not just numbers in an input field, but simple arithmetic expressions, in Perl or Python it is going to be much easier to just call eval() on the input than writing a parser for the expression language you want to allow. Unfortunately, this kind of approach, almost always results in a poor user experience overall (compiler error messages weren't meant for non-programmers) and opens security holes. Solving these problems without using eval() generally involves a fair bit of code.

In Haskell, thanks to things like Parsec, it is actually very easy to write a parser and evaluator for these kinds of input problems, and considerably removes the yearning for eval.

Solution 2:

It doesn't have an inbuilt eval function. However are some packages on hackage that can do the same sort of thing. (docs). Thanks to @luqui there is also hint.

Solution 3:

There is no 'eval' built into the language, though Template Haskell allows compile time evaluation.

For runtime 'eval' -- i.e. runtime metaprogramming -- there are a number of packages on Hackage that essentially import GHC or GHCi, including the old hs-plugins package, and the hint package.

Solution 4:

Solution 5:

This question is asked 11 years ago, and now use the package hint we can define eval very easily, following is an example as self-contained script (you still need nix to run it)

#!/usr/bin/env nix-shell#! nix-shell -p "haskellPackages.ghcWithPackages (p: with p; [hint])"#! nix-shell -i "ghci -ignore-dot-ghci -fdefer-type-errors -XTypeApplications"

{-# LANGUAGE ScopedTypeVariables, TypeApplications, PartialTypeSignatures #-}

import Data.Typeable (Typeable)
import qualified Language.Haskell.Interpreter as Hint

-- DOC: https://www.stackage.org/lts-18.18/package/hint-0.9.0.4eval :: forall t. Typeable t => String -> IO t
eval s = do
    mr <- Hint.runInterpreter $ do
        Hint.setImports ["Prelude"]
        Hint.interpret s (Hint.as :: t)
    case mr of
        Left err -> error (show err)
        Right r -> pure r

-- * Interpret expressions into values:

e1 = eval @Int "1 + 1 :: Int"
e2 = eval @String "\"hello eval\""

-- * Send values from your compiled program to your interpreted program by interpreting a function:

e3 = do
    f <- eval @(Int -> [Int]) "\\x -> [1..x]"
    pure (f 5)

Post a Comment for "Equivalent Of Python Eval In Haskell"