Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend.C.FromCore.genFunDefSig: not a function #186

Open
anfelor opened this issue Aug 1, 2021 · 5 comments · May be fixed by #637
Open

Backend.C.FromCore.genFunDefSig: not a function #186

anfelor opened this issue Aug 1, 2021 · 5 comments · May be fixed by #637

Comments

@anfelor
Copy link
Collaborator

anfelor commented Aug 1, 2021

Given the following part of a Koka translation of Haskells Data.List.sort:

module haskell-sort

alias elem = int

fun sequences(xs : list<elem>) {
    match(xs) {
        Cons(a, Cons(b, xs)) {
            if(a > b) then descending(b, [a], xs)
            else ascending(b, (fn(x) { Cons(a, x) }), xs)
        }
        _ -> [xs]
    }
}

fun descending(a : elem, as_, bs) {
    match(bs) {
        Cons(b, bs) | a > b -> descending(b, Cons(a, as_), bs)
        _ -> Cons(Cons(a, as_), sequences(bs))
    }
}

fun ascending(a : elem, as_, bs) {
    match(bs) {
        Cons(b, bs) | !(a > b) -> ascending(b, fn(ys) { as_(Cons(a, ys)) }, bs)
        _ -> Cons(as_([a]), sequences(bs))
    }
}

Koka fails with

failure during code generation:
  Backend.C.FromCore.genFunDefSig: not a function: (haskell-sort/ascending,std/core/types/.dup(bs0);
  fn<(div :: E)>(a: haskell-sort/elem, as_: ((list :: V -> V)<haskell-sort/elem>) -> (div :: E) (list :: V -> V)<haskell-sort/elem>, bs: (list :: V -> V)<int>){
    std/core/types/.drop(bs0);
    match (bs) {
      (std/core/Cons(((.skip std/core/types/.Box((b: int) : int) : .Box ) as .box-x255: .Box) : int, (bs0: (list :: V -> V)<int>) : (list :: V -> V)<int>) : (list :: V -> V)<int> )
         | std/core/types/(!)((std/core/(>.1)((std/core/types/.dup(a)), (std/core/types/.dup(b))))) -> (match ((std/core/types/.is-unique(bs))) {
          (std/core/types/True() : bool )
             -> std/core/types/.free(bs);
          _
             -> private val _ : int;
                private val _ : (list :: V -> V)<int>;
            std/core/types/.dec-ref(bs);
        });
        haskell-sort/ascending(b, (fn<(div :: E)>(ys: (list :: V -> V)<haskell-sort/elem>){
            (as_((std/core/Cons((std/core/types/.box(a)), ys))));
          }), bs0);
      _
         -> private val _ : ();
            private val .b.264.260 : (list :: V -> V)<haskell-sort/elem>;
            private val .b.265.261 : (list :: V -> V)<(list :: V -> V)<haskell-sort/elem>>;
        std/core/Cons((std/core/types/.box(.b.264.260)), .b.265.261);
    };
  })

Koka v2.1.9

@anfelor
Copy link
Collaborator Author

anfelor commented Sep 29, 2021

This seems to have something to do with the fact that .dup(bs0) is lifted outside of the function (see debug log above). The issue went away when I renamed the new name bs to bs2 by hand (so that there was no shadowing anymore). Could well be the Parc code...

@anfelor
Copy link
Collaborator Author

anfelor commented May 14, 2024

I can no longer reproduce this in 3.1.1 and similar issues seem to have been fixed, so I'll close this.

@anfelor anfelor closed this as completed May 14, 2024
@anfelor
Copy link
Collaborator Author

anfelor commented Aug 23, 2024

Reopening, since this happened again on current dev (e2bfb9c, v3.1.2). MWE:

alias elem = int

type heap
  Empty
  Heap( x : elem, h1 : heap, h2 : delayed<pure,heap> )

type neheap
  NEHeap( x : elem, h1 : heap, h2 : delayed<pure,heap> )

fun merge( a : heap, b : heap ) : div heap
  match (a, b)
    (Empty, b) -> b
    (a, Empty) -> a
    (Heap( x, a1, a2 ), Heap( y, b1, b2 )) ->
      if x <= y then
        link(NEHeap( x, a1, a2 ), b)
      else
        link(NEHeap( y, b1, b2 ), a)

fun link( xbm : neheap, a : heap ) : div heap
  match xbm
    NEHeap( x, Empty, m ) -> Heap( x, a, m )
    NEHeap( x, b, m ) -> Heap( x, Empty, delay(fn() -> merge(merge(a, b), m.force())) )

results in the error:

heap/lazypairing-thunk(1, 1): internal error: Backend.C.FromCore.genFunDefSig: not a function: (heap/lazypairing-thunk/merge,val  : heap/lazypairing-thunk/heap;
val  : heap/lazypairing-thunk/heap;
fn<(div :: X)>(a@0@0: heap/lazypairing-thunk/heap, b@0@0: heap/lazypairing-thunk/heap){
  match (a@0@0, b@0@0) {
    _, (heap/lazypairing-thunk/Empty() : heap/lazypairing-thunk/heap )
       -> val  : ();
          val  : ();
      b@0@0;
    (heap/lazypairing-thunk/Empty() : heap/lazypairing-thunk/heap ), (@skip heap/lazypairing-thunk/Heap((@pat@2@0: heap/lazypairing-thunk/elem) : heap/lazypairing-thunk/elem, (@pat@3@0: heap/lazypairing-thunk/heap) : heap/lazypairing-thunk/heap, (@pat@4@0: (delayed/delayed :: (E, V) -> V)<(pure :: E),heap/lazypairing-thunk/heap>) : (delayed/delayed :: (E, V) -> V)<(pure :: E),heap/lazypairing-thunk/heap>) : heap/lazypairing-thunk/heap )
       -> val  : ();
          val  : ();
      a@0@0;
    (@skip heap/lazypairing-thunk/Heap((y: heap/lazypairing-thunk/elem) : heap/lazypairing-thunk/elem, (b1: heap/lazypairing-thunk/heap) : heap/lazypairing-thunk/heap, (b2: (delayed/delayed :: (E, V) -> V)<(pure :: E),heap/lazypairing-thunk/heap>) : (delayed/delayed :: (E, V) -> V)<(pure :: E),heap/lazypairing-thunk/heap>) : heap/lazypairing-thunk/heap ), (@skip heap/lazypairing-thunk/Heap((x@1@0: heap/lazypairing-thunk/elem) : heap/lazypairing-thunk/elem, (a1: heap/lazypairing-thunk/heap) : heap/lazypairing-thunk/heap, (a2: (delayed/delayed :: (E, V) -> V)<(pure :: E),heap/lazypairing-thunk/heap>) : (delayed/delayed :: (E, V) -> V)<(pure :: E),heap/lazypairing-thunk/heap>) : heap/lazypairing-thunk/heap )
       -> val  : ();
          val @ru-x110 : @reuse;
          val  : ();
          val @match-x103 : bool;
      (match (@match-x103) {
        (std/core/types/True() : bool )
           -> val  : ();
              val  : ();
              val  : ();
              val  : ();
          heap/lazypairing-thunk/link((match ((std/core/types/@reuse-is-valid(@ru-x110))) {
              (std/core/types/True() : bool )
                 -> std/core/types/@con-tag-fields-assign(@ru-x110, (1));
              _
                 -> heap/lazypairing-thunk/NEHeap(x@1@0, a1, a2);
            }), b@0@0@0@0@0@0@0@0);
        (@skip std/core/types/False() : bool )
           -> val  : ();
              val  : ();
              val  : ();
              val  : ();
          heap/lazypairing-thunk/link((std/core/types/@alloc-at(@ru-x110, (heap/lazypairing-thunk/NEHeap(y, b1, b2)))), a@0@0@0@0@0@0@0@0);
      });
  };
})
CallStack (from HasCallStack):
  error, called at src/Backend/C/FromCore.hs:326:42 in koka-3.1.2-7SWN83ElxwDEblvTDAD6Nq:Backend.C.FromCore

It is possible to work around this bug by avoiding using a,b again after they have been matched on, as in:

    (Heap( x, a1, a2 ), Heap( y, b1, b2 )) ->
      if x <= y then
        link(NEHeap( x, a1, a2 ), Heap( y, b1, b2 ))
      else
        link(NEHeap( y, b1, b2 ), Heap( x, a1, a2 ))

@anfelor anfelor reopened this Aug 23, 2024
@TimWhiting
Copy link
Collaborator

Yeah, I ran into this yesterday as well. I think it has something to do with the fact that in your match you are shadowing the function parameter a and b by a and b. It's interesting that it doesn't cause issues if you don't use the variables, but I think the root cause is the shadowing.

@TimWhiting TimWhiting added this to the Small Release milestone Dec 21, 2024
@TimWhiting
Copy link
Collaborator

I run into this a lot. This is due to shadowing issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants