Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6252665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:50:44+00:00 2026-05-24T13:50:44+00:00

I can use the and keyword to set up mutually recursive function definitions. I

  • 0

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”)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:50:45+00:00Added an answer on May 24, 2026 at 1:50 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C#, you can use the implicit keyword to define an implicit user-defined type
I know you can use C++ keyword 'explicit' for constructors of classes to prevent
What does the SQL Action keyword do? Can I use this keyword in a
When you have an object instance in C#, you can use the this keyword
As we use default keyword as a access specifier, and it can be used
I dont want to use the loop related keyword, how can I implement loop
In VB.NET, you can surround a variable name with brackets and use keywords as
You can use more than one css class in an HTML tag in current
You can use a standard dot notation or a method call in Objective-C to
I can use properties of an Excel Worksheet to tell if the worksheet is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.