Hi Chris,

The mistake is a mismatch between your interface and your implementation. The interface defines a function of two arguments (which can be partially bound) whereas the implementation only assumes one argument which is a tuple.

Here is the fix

1
2
3
4
type 'a IField = interface
   inherit INumeric<'a>
   abstract Divide : 'a * 'a -> 'a
end

Best wishes,
Ralf Herbrich

By on 6/7/2007 11:01 AM ()

Ralf,

Thanks for the quick response. There was a bug in the code I posted that you pointed out. After fixing it, it unfortunately that does not answer the question that I was really trying to ask. I am posting an extended version of the code; hopefully the problem is a little clearer.

Essentially I want to extend the INumeric<'a> operation to rational, real or complex numbers, and include the division operation.

Sample 1:

1
2
3
4
5
6
7
8
9
type 'a IField = interface
   abstract Divide : 'a * 'a -> 'a
end

let floatFieldOps = 
{
   new IField<float> with
   member x.Divide(x,y) = if y = 0.0 then float.NaN else x / y
   };;

Here it complains that NaN is not defined.

Sample 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
type 'a IField = interface
   inherit INumeric<'a>
   abstract Divide : 'a * 'a -> 'a
end

let floatFieldOps = 
{
   new IField<float> with
   member x.Divide(x,y) = if y = 0.0 then float.NaN else x / y
   interface INumeric<float>
      with
      member x.Zero = 0.0;
      member x.One = 1.0;
      member x.Add(x,y) = x + y
      member x.Subtract(x,y) = x - y
      member x.Parse(x) = float.Parse(x)
      member x.Sign(x) = System.Math.Abs(x)
      member x.Subtract(x,y) = x - y
      member x.LessThan(x,y) = x < y
end
};;

There are a couple of problems: At the interface line, it is complaining about a syntax error. Second, rather than list all the functions again e.g. Add, Subtract, Zero etc, is there any way to just default to the GlobalAssociations.GetNumericAssociation<float>() implementation for the rest of the functions?

Thanks for any help in this matter.

Regards

Chris

By on 6/7/2007 11:43 AM ()

Note there are extensions of INumeric in the "Microsoft.FSharp.Math" namespace, e.g. [link:research.microsoft.com], and some instances here [link:research.microsoft.com].

But it is a helpful exercise to do this yourself.

For the first problem you want System.Double.NaN. In expressions "float" is a conversion function, e.g.

1
2
float 3
float (3u + 4u)

In the second case the syntax error was an indentation thing (white-space indentation...). So you might want:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
    open System
    open Math.Types
    type IField<'a> = interface
       inherit INumeric<'a>
       abstract Divide : 'a * 'a -> 'a
    end

    let floatFieldOps = 
      { new IField<float> with
          member ops.Divide(x,y) = if y = 0.0 then Double.NaN else x / y
        interface INumeric<float> with
          member ops.Zero = 0.0;
          member ops.One = 1.0;
          member ops.Add(x,y) = x + y
          member ops.Abs(x) = abs x
          member ops.Parse(a,b,c) = Double.Parse(a,b,c)
          member ops.ToString(a,b,c) = a.ToString(b,c)
          member ops.Sign(x) = (compare x 0.0)
          member ops.Subtract(x,y) = x - y
          member ops.Negate(x) = -x
          member ops.Multiply(x,y) = x * y }

By on 6/7/2007 5:53 PM ()

Don,

Thanks for your help.

Regards

Chris

By on 6/8/2007 5:27 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper