Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does printing 5000 numbers in F# Interactive cause a StackOverflowException?

Tested on F# 3.1 on windows 7

fsi.PrintLength <- 5000;;

[1..5000];;

Process is terminated due to StackOverflowException. Session termination detected. Press Enter to restart.

on Mono (F# 4.0), there doesn't seem to be such a limitation.

like image 639
Jon Schoning Avatar asked Dec 11 '14 21:12

Jon Schoning


1 Answers

I think this is a bug in the formatting module that takes care of pretty printing to F# Interactive.

There are some non-tail recursive functions that uses PrintLength e.g. boundedUnfoldL in this line. Implementation of boundedUnfoldL is indeed not tail-recursive:

let boundedUnfoldL
                (itemL     : 'a -> layout)
                (project   : 'z -> ('a * 'z) option)
                (stopShort : 'z -> bool)
                (z : 'z)
                maxLength =
      let rec consume n z =
        if stopShort z then [wordL "..."] else
        match project z with
          | None       -> []  // exhaused input 
          | Some (x,z) -> if n<=0 then [wordL "..."]               // hit print_length limit 
                                  else itemL x :: consume (n-1) z  // cons recursive... 
      consume maxLength z  

I don't know why it doesn't blow up on Mono. It would be surprising if F# Interactive on Mono can handle length > 5000 successfully.

You can report this as a bug to https://visualfsharp.codeplex.com/workitem/list/basic.

like image 55
pad Avatar answered Nov 15 '22 08:11

pad