hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

F# eval

Last post 10-02-2009, 13:14 by Ox69. 10 replies.
Sort Posts: Previous Next
  •  04-27-2007, 11:15 2948

    F# eval

    Is there a way to execute/evaluate an expression that's being stored as data?  To be a little more specific, I mean could I, for example, read a set of rules from XML, do some fairly trivial manipulation, like substituting a value for a variable name in the equation, then evaluate that equation?

    If I had a rule that said something like "1 - (variable * 2)" in a file somewhere, I could easily pull that in, and substitute, say, 4 for 'variable'.  That leaves a string, or list, or something containing "1 - (4 * 2)".  Is it possible for me to evaluate that and come up with 7?

    DC
  •  04-27-2007, 13:16 2953 in reply to 2948

    Re: F# eval

    I know that there isn't an eval as there is in Lisp-world.  At least not yet.

    I think it would be cool to have a compiler API that could turn text into a tree, and trees into bytecode using System.Reflection.Emit or whatever similar API the F# compiler uses.

    Rob

  •  04-27-2007, 14:22 2955 in reply to 2948

    Re: F# eval

    You might want to try looking at this blog post:

    http://fsharpnews.blogspot.com/2007/02/symbolic-manipulation.html

    If you read in your expressions into these kind of symbolic datastructures, they are pretty easy to evaluate.

    Jurgen

  •  04-27-2007, 14:44 2957 in reply to 2955

    Re: F# eval

    Also see the article on the front page

    http://cs.hubfs.net/blogs/f_team/archive/2007/04/07/2662.aspx

     

  •  04-30-2007, 9:44 2998 in reply to 2955

    Re: F# eval

    I like the way that works, but it still runs me up against the problem of decomposing a string into those types.  It's trivial for me to break the string into a list, but it seems like I'd still need to be able to generate an expression, or a syntax tree, or something like that.

    DC
  •  05-03-2007, 6:59 3041 in reply to 2948

    Re: F# eval

    You could evaluate an expression fairly trivially using a .Net compiler for example VB from F#:

    open System.CodeDom.Compiler
    open System.Reflection;
    open Microsoft.VisualBasic;

    let CompileExpression (expression:string) =
        let source =
            "Imports System.Math\r\n" ^
            "Public Module Module1\r\n" ^
            "\tPublic Function Eval(ByVal x As Double)\r\n" ^
            "\tReturn " ^ expression ^ "\r\n" ^
            "\tEnd Function\r\n" ^
            "End Module"
        let provider = new VBCodeProvider();
        let parameters = new CompilerParameters()
        parameters.GenerateInMemory <- true
        provider.CompileAssemblyFromSource(parameters, [|source|])
       
    let CreateMethod (expression:string) =
        let results = CompileExpression expression
        if not results.Errors.HasErrors then
            let ass = results.CompiledAssembly
            let t = ass.GetType("Module1")
            t.GetMethod("Eval")
        else
            null
       
    let InvokeMethod (mi:MethodInfo) (value:float) =
        mi.Invoke(null, [|box value|]) :?> float

    You can see the technique used for a dynamic graph plot here: http://www.trelford.com/eval.aspx

    Hope this helps,

    Phil

  •  05-03-2007, 15:53 3047 in reply to 3041

    Re: F# eval

    A long time back (.NET 1.0 prelaunch) I experienced incredulity when I realised that the new languages had no version of eval.

    I found a solution back then.  The little publicised JScript.NET does work.  I wrote a tiny JScript assembly that did the job.  I passed in some objects and code (string).  The code in this case sat in a database and gave me ways to construct objects on the fly without being limited by compilation.

    Some issues are:

    1)  Code is in JScript.

    2)  JScript seems to lack a passionate evangelist like Don.  I'm not sure what the future is, in fact I haven't used it for some time.

    3)  I got a slew of suggestions which involved a lot more work than the solution I picked.  (Peter Torr and Eric Lippert spring to mind as potential sources of futher information, as does LSharp.  I suspect the status of the latter is research finished, no or slow progress from here on.)

    (I haven't yet seriously looked into doing that job with F#, but would be genuinely surprised, and disappointed, if a reformulation didn't offer a way.)

  •  05-03-2007, 16:28 3048 in reply to 3047

    Re: F# eval

    Since all I really need is a way to do basic math and variable substitution, I wound up adapting one of the F# samples to lex/parse a very simple arithmetic language.  It's pretty much BASIC without a "for" construct, and there are a couple of holes in it, but I got it to work without too much difficulty.  I did it using fslex and fsyacc, but I suspect I could have done something with one of the compilers, as was suggested earlier.

    DC
  •  05-04-2007, 20:34 3057 in reply to 2953

    Re: F# eval

    RobNik:
    I know that there isn't an eval as there is in Lisp-world.

    Judging by the discussion of lexers and parsers, I think doomchild is after something like BASIC's EVAL rather than Lisp's EVAL. The former lexes, parses and evaluates a string containing BASIC code whereas the latter evaluates an s-expr.

    Cheers,
    Jon.

     

  •  05-05-2007, 10:29 3063 in reply to 3057

    Re: F# eval

    jdh30:

    RobNik:
    I know that there isn't an eval as there is in Lisp-world.

    Judging by the discussion of lexers and parsers, I think doomchild is after something like BASIC's EVAL rather than Lisp's EVAL. The former lexes, parses and evaluates a string containing BASIC code whereas the latter evaluates an s-expr.

    Cheers,
    Jon.

     

    Verily, he speaks the truth.  What would be perfect would be the ability to eval a string containing F# code.  For the time being, I've done my best to put together a little math language that'll do what I'm trying to do, but I'm pretty sure there's a cleaner way to go about things.

    DC

  •  10-02-2009, 13:14 11932 in reply to 2948

    Re: F# eval

    There are already very good answers/solutions, but... anyway I wanted to show here my implementation of eval() which is more "clean", because it not uses CodeDom or lexers/parsers. Every needed bit of evaluation - is in code itself :-) Take a look :

    http://coding-experiments.blogspot.com/2009/10/evaluating-math-expressions-in-f.html
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems