hubFS: THE place for F#

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

Generic numeric function question

Last post 02-06-2010, 18:32 by gary. 5 replies.
Sort Posts: Previous Next
  •  02-04-2010, 2:41 13018

    • mau is not online. Last active: 03-21-2010, 11:28 mau
    • Top 50 Contributor
    • Joined on 11-24-2008
    • Melbourne, Australia
    • Posts 40

    Generic numeric function question

    I want to write something like this:

    // 'a is numeric
    let foo (value : int) (coeff : 'a) =
        coeff * convert_to_'a (value >>> 8)


    I want to be able to pass either an int or a float as coeff, and have the correct type as result. Right now I pass an extra argument which is the convert_to_'a function (int() or float()). I was wondering if there's any way to do it without the extra argument and without ifs inside foo's body.

    I'm a conciseness maniac :)
  •  02-04-2010, 18:00 13019 in reply to 13018

    Re: Generic numeric function question

    Seems to be another case where type class would help. That is if 'a implement fromintegral it would work.

    the equivalent in F# I believe would be something like the following :



    let inline foo (value:int) coeff = coeff * (^a : (static member fromIntegral : int -> ^a) value)

    Unfortunately, 'a in this case must be some defined type rather than standard type like float or int as I failed to find a way to extend the builtin type like int/float. Even c# cannot do it for value type I would assume. I also failed to find the C# extension method equivalent in F# to extend builtin type like list or array.

  •  02-05-2010, 4:12 13026 in reply to 13019

    Re: Generic numeric function question

    Hi,
    I posted a more detailed answer about static constraints at StackOverflow here:
    http://stackoverflow.com/questions/2192852/how-are-abs-sign-etc-implemented-in-f/2193118#2193118

    Unfortunately, I don't think this will help you - as Gary wrote, his example won't work with standard F# data types and there is no pre-defined function in LanguagePrimitives that would allow you to do this.

    In some cases, you could also use NumericAssociations (in Microsoft.FSharp.Math in PowerPack.dll), which is a lookup table that you can use to get basic operations for working with numeric types with runtime type info as the key. For more info, look for example here: http://cs.hubfs.net/forums/5510/ShowThread.aspx But again, it doesn't look like it has operations powerful enough to express what you want.


    Tomas Petricek (Blog), C# MVP
    My book: Real-world Functional Programming in .NET
  •  02-06-2010, 12:41 13037 in reply to 13026

    Re: Generic numeric function question

    Found a blog post by Mattew Podwysocki about extension methods.

    http://codebetter.com/blogs/matthew.podwysocki/archive/2008/09/10/object-oriented-f-extension-everything.aspx

    Though I don't understand why the following failed:



    module MyExtensons

    type System.Double with
      static member fromIntegral x = float(x)
      
    let inline fromIntegral< ^a when ^a: (static member fromIntegral : int -> ^a) > x =
      (^a : (static member fromIntegral : int -> ^a) x)
     
    let x = fromIntegral<System.Double> 1 

    yet just saying 'System.Double.fromIntegral 1' does work.

     

  •  02-06-2010, 12:56 13038 in reply to 13037

    Re: Generic numeric function question

    For now, inline static member constraints can't "see" extension methods.  This could be useful, and is a suggestion we'll consider in a future release.
  •  02-06-2010, 18:32 13041 in reply to 13018

    Re: Generic numeric function question

    Capable of doing that would be great(hope to see it in some future release) and answer the question in the comment of Matthew's blog(what good is static member extension):

    Is Enumerable.CharRange('a', 'z') really better than EnumerableExtensions.CharRange('a', 'z')?

    The answer is of course, I can write these 'type class' style generic function.

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems