Passing function pointer to crunch via chicken

Diogo via Chicken-users <[email protected]> Fri, 12 Dec 2025 08:34:22 +0100
Newsgroups gmane.lisp.scheme.chicken
Message-ID <n6ebncfleioopd5owiwcp5boq7oqqijmj3bucn56pywvls7lzy@xjrdpgov2x7y>
Dear crunchers,

I'm stuck with the following situation: I have a crunched procedure `dispatch`
that takes one argument. This argument is the crunched procedure to be executed.
A CHICKEN program selects which is the argument and passes to dispatch.

Here is the minimal example that shows the issue:

(import (scheme base)
        (scheme write)
        (crunch))

;; one of the implementation procedures that should be executed
(crunch
 	(import (crunch declarations))
 	(: (impl byte) byte)
 	(define (impl b)
            (display "b = ")
            (display b)
            b))

;; the dispatch procedure which takes the compute type
(crunch
 	(import (crunch declarations))
 	(define-type compute (byte -> byte))
 	(: (dispatch compute byte) void)
 	(define (dispatch fptr b)
            (fptr b)))

;; Here is call to dispatch passing impl as argument.
(display "call (dispatch impl value))\n")
(let ((ret (dispatch impl #xAA)))
    (display "ret =")
    (display ret)
    (newline))
EOF

The problem seems to be related to the compute type because I get this error:

    % csc fpointer.scm
    Error: during expansion of (crunch ...) - argument type for foreign wrapper
    is invalid
    dispatch
    (procedure (integer) integer)

	Call history:
        ...

If I replace the compute type with (pointer (byte -> byte)), it also does not
work. Here is the crunch block:

(crunch
 	(import (crunch declarations)
                (crunch memory))
 	(define-type compute (pointer (byte -> byte)))
 	(: (dispatch compute byte) byte)
 	(define (dispatch fptr b)
            ((pointer-ref fptr) b)))

And the error message:

    % csc fpointer.scm
    Error: illegal foreign type `#f'


Specially the last case seems to be a misgeneration of the CHICKEN wrapper, but
I am not sure if this use case I am trying to build is supposed to be forbidden
or not.

Do you have any insights for me? Any way how to circumvent that?

Thanks.

Cheers,
-Diogo