Search code examples
f#monosmlf#-interactive

F# - Mono crashes/ returns inconsistent


I am trying to learn F# and I have some background in Standard ML. I am working through the text "Functional Approach to Programming" converting its code into F#. The code sample presented below is from the book mentioned above.

in SML

datatype 'a genTree = GenNode of 'a * ('a genTree list);;

val tree: string genTree =
  GenNode("a", [GenNode("b", []),
                GenNode("c", [GenNode("d", []),
                              GenNode("e", [])]),
                GenNode("f", [])]);;

fun map f [] = []
  | map f (l::ls) = (f l) :: (map f ls);;

fun foldl f e [] = e
  | foldl f e (l::ls) = foldl f (f(e, l)) ls;;

fun sigma xs = foldl (op +) 0 xs;;

fun genSize (GenNode(_, ls)) =
    1 + sigma (map genSize ls);;

SML> genSize tree (* returns 6: int *)

Attempt One - in F# Interactive Shell F# 3.1 (this one crashes the shell)

type genTree<'a> = GenNode of 'a * genTree<'a> list;;

let tree: string genTree =
  GenNode("a", [GenNode("b", []);
                GenNode("c", [GenNode("d", []);
                              GenNode("e", [])]);
                GenNode("f", [])]);;

let rec map f ls =
  match ls with
  | [] -> []
  | (l::ls) -> (f l) :: (map f ls);;

let rec foldl f e ls =
  match ls with
  | [] -> e
  | (l::ls) -> foldl f (f e l) ls;;

let sigma ls = foldl ( + ) 0 ls;;

let rec genSize (GenNode(lbl, ls)) =
  1 + sigma(map genSize ls);;

Attempt Two - in F# Interactive Shell F# 3.1 (give a base case for genSize, hoping that'll do the trick!)

Here is the new definition for genSize, the rest of the code from attempt #1 carries unchanged.

let rec genSize (t: genTree<'a>) =
  match t with
  | GenNode(_, []) -> 1
  | GenNode(_, ls) -> 1 + sigma (map genSize ls);;

FSI> genSize tree = 5

Attempt Three - in MonoDevelop 5.9.4

Copy the code from Attempt 2 into MonoDevelop 5.9.4 and run it there and it correctly returns 6.

Question:

Could someone please explain to me what is causing the discrepancy between attempt 1 - 3?

Many thanks and kind regards.

PS: As I am currently learning F# and Functional Programming I tend to implement most of library functionality as a part of the learning process.

Stack track from Attempt 1

  • Assertion at sgen-alloc.c:460, condition `*p == NULL' not met

Stacktrace:

at <0xffffffff> at (wrapper managed-to-native) object.__icall_wrapper_mono_object_new_specific (intptr) <0xffffffff> at FSI_0002.DEL.sigma (Microsoft.FSharp.Collections.FSharpList1<int>) <0x00013> at FSI_0002.DEL.genSize<a> (FSI_0002.DEL/genTree1) <0x0005f> at FSI_0002.DEL/genSize@51.Invoke (FSI_0002.DEL/genTree1<a>) <0x00023> at FSI_0002.DEL.map<FSI_0002.DEL/genTree1, int> (Microsoft.FSharp.Core.FSharpFunc2<FSI_0002.DEL/genTree1, int>,Microsoft.FSharp.Collections.FSharpList1<FSI_0002.DEL/genTree1>) <0x00032> at FSI_0002.DEL.genSize (FSI_0002.DEL/genTree1<a>) <0x00056> at <StartupCode$FSI_0004>.$FSI_0004.main@ () <0x0001b> at (wrapper runtime-invoke) object.runtime_invoke_void (object,intptr,intptr,intptr) <0xffffffff> at <unknown> <0xffffffff> at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) <0xffffffff> at System.Reflection.MonoMethod.Invoke (object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo) <0x000c0> at System.MonoType.InvokeMember (string,System.Reflection.BindingFlags,System.Reflection.Binder,object,object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,string[]) <0x0039a> at System.Reflection.Emit.TypeBuilder.InvokeMember (string,System.Reflection.BindingFlags,System.Reflection.Binder,object,object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,string[]) <0x00066> at System.Type.InvokeMember (string,System.Reflection.BindingFlags,System.Reflection.Binder,object,object[],System.Globalization.CultureInfo) <0x0005a> at Microsoft.FSharp.Compiler.AbstractIL.ILRuntimeWriter/execEntryPtFun@1986-1.Invoke (Microsoft.FSharp.Core.Unit) <0x00083> at Microsoft.FSharp.Compiler.Interactive.Shell/clo@883-37.Invoke (Microsoft.FSharp.Core.FSharpFunc2,Microsoft.FSharp.Collections.FSharpList`1<T>) <0x00038> at Microsoft.FSharp.Collections.ListModule.Iterate<T> (Microsoft.FSharp.Core.FSharpFunc`2,Microsoft.FSharp.Collections.FSharpList`1<T>) <0x00027> at Microsoft.FSharp.Compiler.Interactive.Shell.arg10@882 (Microsoft.FSharp.Compiler.Interactive.Shell/FsiDynamicCompiler,Microsoft.FSharp.Collections.FSharpList`1>>,Microsoft.FSharp.Core.Unit) <0x0005b> at Microsoft.FSharp.Compiler.Interactive.Shell/FsiDynamicCompiler.ProcessInputs (Microsoft.FSharp.Compiler.Interactive.Shell/FsiDynamicCompilerState,Microsoft.FSharp.Collections.FSharpList1<Microsoft.FSharp.Compiler.Ast/ParsedInput>,bool,bool,bool,Microsoft.FSharp.Collections.FSharpList1) <0x006e3> at Microsoft.FSharp.Compiler.Interactive.Shell/FsiDynamicCompiler.EvalParsedDefinitions (Microsoft.FSharp.Compiler.Interactive.Shell/FsiDynamicCompilerState,bool,bool,Microsoft.FSharp.Collections.FSharpList1<Microsoft.FSharp.Compiler.Ast/SynModuleDecl>) <0x001db> at Microsoft.FSharp.Compiler.Interactive.Shell/FsiDynamicCompiler.EvalParsedExpression (Microsoft.FSharp.Compiler.Interactive.Shell/FsiDynamicCompilerState,Microsoft.FSharp.Compiler.Ast/SynExpr) <0x0005f> at Microsoft.FSharp.Compiler.Interactive.Shell/clo@1590-45.Invoke (Microsoft.FSharp.Compiler.Interactive.Shell/FsiDynamicCompilerState) <0x012ef> at Microsoft.FSharp.Compiler.Interactive.Shell/FsiInteractionProcessor.InteractiveCatch<a> (Microsoft.FSharp.Core.FSharpFunc22<a, Microsoft.FSharp.Compiler.Interactive.Shell/FsiInteractionStepStatus>>,a) <0x00039> at Microsoft.FSharp.Compiler.Interactive.Shell/FsiInteractionProcessor.ExecInteraction (bool,Microsoft.FSharp.Compiler.Build/TcConfig,Microsoft.FSharp.Compiler.Interactive.Shell/FsiDynamicCompilerState,Microsoft.FSharp.Compiler.Ast/ParsedFsiInteraction) <0x00067> at Microsoft.FSharp.Compiler.Interactive.Shell/FsiInteractionProcessor.ExecInteractions (bool,Microsoft.FSharp.Compiler.Build/TcConfig,Microsoft.FSharp.Compiler.Interactive.Shell/FsiDynamicCompilerState,Microsoft.FSharp.Core.FSharpOption1) <0x005fb> at Microsoft.FSharp.Compiler.Interactive.Shell/FsiInteractionProcessor.MainThreadProcessParsedInteraction (bool,Microsoft.FSharp.Core.FSharpOption1<Microsoft.FSharp.Compiler.Ast/ParsedFsiInteraction>,Microsoft.FSharp.Compiler.Interactive.Shell/FsiDynamicCompilerState) <0x0013b> at Microsoft.FSharp.Compiler.Interactive.Shell/res@1782.Invoke (Microsoft.FSharp.Compiler.Interactive.Shell/FsiDynamicCompilerState) <0x00033> at Microsoft.FSharp.Compiler.Interactive.Shell/runCodeOnMainThread@2021-2.Invoke (Microsoft.FSharp.Core.Unit) <0x0003b> at <StartupCode$FSharp-Compiler-Interactive-Settings>.$Fsiaux/Microsoft-FSharp-Compiler-Interactive-IEventLoop-Invoke@47.Invoke (Microsoft.FSharp.Core.Unit) <0x0001a> at <StartupCode$FSharp-Compiler-Interactive-Settings>.$Fsiaux/run@38-4.Invoke (Microsoft.FSharp.Core.FSharpFunc2) <0x00039> at Microsoft.FSharp.Primitives.Basics.List.iter (Microsoft.FSharp.Core.FSharpFunc2<T, Microsoft.FSharp.Core.Unit>,Microsoft.FSharp.Collections.FSharpList1) <0x00038> at Microsoft.FSharp.Collections.ListModule.Iterate (Microsoft.FSharp.Core.FSharpFunc2<T, Microsoft.FSharp.Core.Unit>,Microsoft.FSharp.Collections.FSharpList1) <0x00027> at .$Fsiaux.run@35 (Microsoft.FSharp.Compiler.Interactive.SimpleEventLoop,Microsoft.FSharp.Core.Unit) <0x000d3> at Microsoft.FSharp.Compiler.Interactive.SimpleEventLoop.Microsoft-FSharp-Compiler-Interactive-IEventLoop-Run () <0x0001f> at Microsoft.FSharp.Compiler.Interactive.Shell.runLoop@2066 (Microsoft.FSharp.Compiler.Interactive.Shell/FsiConsoleOutput,Microsoft.FSharp.Core.Unit) <0x00205> at Microsoft.FSharp.Compiler.Interactive.Shell.DriveFsiEventLoop (Microsoft.FSharp.Compiler.Interactive.Shell/FsiConsoleOutput) <0x0001b> at Microsoft.FSharp.Compiler.Interactive.Shell/FsiEvaluationSession.Run () <0x00bfb> at Microsoft.FSharp.Compiler.Interactive.Shell.evaluateSession@2382 (string[],Microsoft.FSharp.Core.Unit) <0x0009b> at Microsoft.FSharp.Compiler.Interactive.Shell.MainMain (string[]) <0x00123> at Microsoft.FSharp.Compiler.Interactive.Main.FsiMain (string[]) <0x00013> at (wrapper runtime-invoke) .runtime_invoke_int_object (object,intptr,intptr,intptr) <0xffffffff>

Native stacktrace:

mono() [0x8102a23]
[0xb775240c]
[0xb7752424]
/lib/i386-linux-gnu/libc.so.6(gsignal+0x47) [0xb7545607]
/lib/i386-linux-gnu/libc.so.6(abort+0x143) [0xb7548a33]
mono() [0x827b381]
mono() [0x827b508]
mono() [0x827b5a3]
mono() [0x8233338]
mono() [0x8233a4c]
mono() [0x81f20a8]
mono(mono_object_new_alloc_specific+0x40) [0x81f6570]
mono(mono_object_new_specific+0x88) [0x81f6628]
[0xb725cc71]
[0xb25a974c]
[0xb25a9640]
[0xb25a9724]
[0xb25a96c3]
[0xb25a9637]
[0xb25a94ac]
[0xb725b5f7]
mono() [0x80723d1]

Debug info from gdb:

Could not attach to process. If your uid matches the uid of the target process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try again as the root user. For more details, see /etc/sysctl.d/10-ptrace.conf ptrace: Operation not permitted. No threads.

================================================================= Got a SIGABRT while executing native code. This usually indicates a fatal error in the mono runtime or one of the native libraries

used by your application.

Aborted


Solution

  • Looks like a Mono bug. I tested in both VS2013 and VS2015 and your code works just fine. If you can grab the stack trace I would file a bug with the Mono guys.

    I get the same stack trace as you. For the record by Mono version is

    Mono JIT compiler version 4.0.2 ((detached/c99aa0c Thu Jun 11 18:53:01 EDT 2015)

    If you Google "Assertion at sgen-alloc.c:460, condition `*p == NULL' not met" you will see a few references to this error with Mono.

    EDIT: Tested on Mono 4.0.3 and it has the same issue. Whats interesting is if you make genTree non generic like

    type genTree = GenNode of string * genTree list
    

    it works perfectly.