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 1054573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:25:40+00:00 2026-05-16T17:25:40+00:00

I am having some problems with trying to implement Automatic Differentiation in F#. I

  • 0

I am having some problems with trying to implement Automatic Differentiation in F#. I think the problem is down to the evaluation not being ‘lazy’.

Here is my code:

type Diff =
    {d : double; df : Diff}
    static member (+) (x : Diff, y : Diff) =
        {d = x.d + y.d; df = x.df + y.df}
    static member (-) (x : Diff, y : Diff) =
        {d = x.d - y.d; df = x.df - y.df}
    static member (*) (x : Diff, a : double) =
        {d = x.d * a; df = x.df * a}
    static member (*) (x : Diff, y : Diff) =
        {d = x.d * y.d; df = (x.df * y) + (y.df * x)}

let rec dZero = {d = 0.0; df = dZero}

let dConst x = {d = x; df = dZero}

let dId x = {d = x; df = dConst 1.0}

let test = dId 5.0

let add (x:Diff) = (x+x).d

If I try to use ‘add test’ I get a stack overflow error, which I think is down to the definition of (+) inside my type itself relying on ‘+’.

Is there any way I can fix this? Any help would be greatly appreciated.

Many thanks, Ash

  • 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-16T17:25:40+00:00Added an answer on May 16, 2026 at 5:25 pm

    As you thought, the problem is that the F# doesn’t use lazy evaluation and that the data structure you’re creating is “infinite” (because dZero recursively references itself). When calculating the +, the operator calls + on the df values and that in turn invokes + on the df.df values and so on…

    One way to correct this is to make the df member of the record explicitly lazy:

    type Diff = 
        {d : double; df : Lazy<Diff>} 
        static member (+) (x : Diff, y : Diff) = 
            {d = x.d + y.d; df = lazy (x.df.Value + y.df.Value) } 
        static member (-) (x : Diff, y : Diff) = 
            {d = x.d - y.d; df = lazy (x.df.Value - y.df.Value) } 
        static member (*) (x : Diff, a : double) = 
            {d = x.d * a; df = lazy (x.df.Value * a) } 
        static member (*) (x : Diff, y : Diff) = 
            {d = x.d * y.d; df = lazy ((x.df.Value * y) + (y.df.Value * x)) } 
    
    let rec dZero = {d = 0.0; df = lazy dZero} 
    let dConst x = {d = x; df = lazy dZero} 
    let dId x = {d = x; df = lazy dConst 1.0} 
    

    This will evaluate the df value only when it is actually used, so the + operation will calculate the value of d and only provide a lazy value for df (which can be evaluated if someone needs it).

    Another alternative would be to make the Diff type a discriminated union and represent zero as a special value (rather than as a recursive record), which would work unless you use recursive references for something else. The declaration would be roughly something like:

    type Diff = 
        | DiffValue of double * Diff
        | DiffZero 
        static member (+) // etc...
    

    This would make the implementation a bit longer, because you would need to check for the Zero case in all the primitive operations. In this case, you would only create finite data structures (and the operators would process them eagerly).

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

Sidebar

Related Questions

I've been having some problems trying to implement an overloaded << operator function that
I'm trying to implement a plugin system, but I'm having some problems. In the
I'm having some problems trying to figure out how to generate a Google Map
I am having some problems trying to add a ValueConverter in my xaml usercontrol:
I'm having some problems trying to get the code below to output the data
I'm having some problems trying to perform a query. I have two tables, one
I'm having some problems with the texture mapping in OpenGL in python. I'm trying
I am trying to move from MySQL to MySQLi but am having some problems.
I'm trying to build firefox but I'm having some problems. I currently have Visual
I'm trying to do some browser automation, but I'm having some problems. Basically, what

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.