Polymorphic Lists in ML

I have this piece of code in ML:

local

fun unfolder( [] , n ) = []
    | unfolder( l::ls, n ) = (n, l) :: unfolder( ls, n )

in

fun flat list = unfolder(list, 1)  

end;

this gives me an error:

unexpected exception (bug?) in SML/NJ: EA [EA]
  raised at: ../../MLRISC/x86/mltree/x86.sml:417.32-417.34
             ../compiler/Basics/stats/stats.sml:198.40
             ../compiler/Basics/stats/stats.sml:198.40
             ../compiler/Basics/stats/stats.sml:198.40
             ../compiler/TopLevel/interact/evalloop.sml:44.55

but when I change its (n, l) to (n, l:int), it works, and when (n, l:'a), can someone explain why the polymorphic type does not work, thanks in advance

+3
source share
1 answer

This is an internal error in SML / NJ. The program works flawlessly if compiled with MLton, adding:

val _ =
  let val l = flat [1,2,3]
      fun printer (a,b) = Int.toString(a) ^ ", " ^ Int.toString(b)
  in
    print (printer (hd l) ^ "\n")
  end

Report this. Your example seems to be digestible in size - and probably related to the representation of polymorphism.

+2
source

Source: https://habr.com/ru/post/1780875/


All Articles