views:

59

answers:

1

I'm working on an experimental programming language that has global polymorphic type inference.

I recently got the algorithm working sufficiently well to correctly type the bits of sample code I'm throwing at it. I'm now looking for something more complex that will exercise the edge cases.

Can anyone point me at a source of really gnarly and horrible code fragments that I can use for this? I'm sure the functional programming world has plenty. I'm particularly looking for examples that do evil things with function recursion, as I need to check to make sure that function expansion terminates correctly, but anything's good --- I need to build a test suite. Any suggestions?

My language is largely imperative, but any ML-style code ought to be easy to convert.

+2  A: 

My general strategy is actually to approach it from the opposite direction -- ensure that it rejects incorrect things!

That said, here are some standard "confirmation" tests I usually use:

The eager fix point combinator (unashamedly stolen from here):

datatype 'a t = T of 'a t -> 'a

val y = fn f => (fn (T x) => (f (fn a => x (T x) a)))
               (T (fn (T x) => (f (fn a => x (T x) a))))

Obvious mutual recursion:

fun f x = g (f x)
and g x = f (g x)

Check out those deeply nested let expressions too:

val a = let
   val b = let 
      val c = let
         val d = let
            val e = let
               val f = let
                  val g = let
                     val h = fn x => x + 1
                  in h end
               in g end
            in f end
         in e end
      in d end
   in c end
in b end

Deeply nested higher order functions!

fun f g h i j k l m n = 
   fn x => fn y => fn z => x o g o h o i o j o k o l o m o n o x o y o z

I don't know if you have to have the value restriction in order to incorporate mutable references. If so, see what happens:

fun map' f [] = []
  | map' f (h::t) = f h :: map' f t

fun rev' [] = []
  | rev' (h::t) = rev' t @ [h]

val x = map' rev'

You might need to implement map and rev in the standard way :)

Then with actual references lying around (stolen from here):

val stack =
let val stk = ref [] in
  {push = fn x => stk := x :: !stk,
   pop  = fn () => stk := tl (!stk),
   top  = fn () => hd (!stk)}
end

Hope these help in some way. Make sure to try to build a set of regression tests you can re-run in some automatic fashion to ensure that all of your type inference behaves correctly through all changes you make :)

Gian
Fantastic --- thank-you! (Although I may need to hand back my 'tumbleweed' badge.) My language is procedural rather than functional, and uses a kind of mutated CPA rather than Hindley-Milner, but I can adapt most of these. If I can implement your map' and rev' using in-language car, cdr and cons functions and have it work I'll be happy. Alas, I don't have higher-order functions, but I'll try and think of a way to make them work.
David Given
well, in terms of car and cdr, map is simply `fun map' f l = if l = [] then [] else cons(f (car l), map' f (cdr l))`. rev' is `fun rev' l = if l = [] then [] else append(rev' (cdr l), cons(car l, nil))`, I guess. Mostly I would just focus on generating examples that the type checker should reject!
Gian