6

I can use the and keyword to set up mutually recursive function definitions. I can also use and for mutually recursive types, but what if there is a mutually recursive relationship between a type and a function? Is my only option to make the function a member of the type or can I use something similar to and here too?

Edit: Adding a simplified pseudo-example that I hope illustrates what I'm trying to do

// A machine instruction type
type Instruction = Add | CallMethod int (* method ID *) | ...

// A class representing a method definition
type MethodDef (fileName : string) =
    member x.Params with get () = ...
    member x.Body with get() =
        let insts = readInstructions fileName
        Array.map toAbstractInst insts

// a more abstract view of the instructions
and AbstractInstruction = AbstAdd | AbstCallMethod MethodDef | ...

// a function that can transform an instruction into its abstract form
let toAbstractInst = function
    | Add -> AbstAdd
    | CallMethod methodId -> AbstCallMethod (somehowResolveId methodId)
    | ...

So you can see here that the recursive relationship is set up pretty indirectly: MethodDef <-> AbstractInst AND MethodDef -> toAbstractInst -> AbstractInstruction (where -> means "depends on")

Keith
  • 2,820
  • 5
  • 28
  • 39

1 Answers1

10

This question is difficult to answer without an example

  • If you have mutually recursive types that do not have members, then the types don't need to know about the functions (so you can first define types and then functions).

  • If you have mutually recursive types that have the functions as members, then the members can see each other (across types) and you should be fine

The only tricky case is when you have mutually recursive types, mutually recursive functions and you also want to exposes some functions as members. Then you can use type extensions:

// Declare mutually recursive types 'A' and 'B'
type A(parent:option<B>) =
  member x.Parent = parent

and B(parent:option<A>) =
  member x.Parent = parent

// Declare mutually recursive functions 'countA' and 'countB'
let rec countA (a:A) =
  match a.Parent with None -> 0 | Some b -> (countB b) + 1
and countB (b:B) =
  match b.Parent with None -> 0 | Some a -> (countA a) + 1

// Add the two functions as members of the types
type A with 
  member x.Count = countA x

type B with 
  member x.Count = countB x

In this case, you could just make countA and countB members of the two types, because it would be easier, but if you have more complex code that you want to write as functions, then this is an option.

If everything is written in a single module (in a single file), then the F# compiler compiles type extensions as standard instance members (so it looks just like normal type from the C# point of view). If you declare extensions in a separate module, then they will be compiled as F#-specific extension methods.

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • thanks Tomas, I've added an example to my question. I'm thinking now it may be best to just make toAbstractInst a member even though I don't think it is logically a member of MethodDef – Keith Aug 13 '11 at 17:24
  • 1
    @Keith - In this case, you could make `toAbstractInst` a private `let`-declared function of `MethodDef` (if it is not used anywhere else). Using the approach with type extensions would work too. I guess that it could also be a static member of `AbstractInstruction` (called e.g. `FromConcreteInstruction`) – Tomas Petricek Aug 14 '11 at 09:10