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

Add support for optional arguments in define_scheme_primitive #105

Closed
ngeiswei opened this issue Jun 22, 2015 · 4 comments
Closed

Add support for optional arguments in define_scheme_primitive #105

ngeiswei opened this issue Jun 22, 2015 · 4 comments

Comments

@ngeiswei
Copy link
Member

I tried overloading a scheme definition with multiple define_scheme_primitive on the same scheme function name, see ngeiswei@ee6995c

When calling cog-fc with a single argument it works, but when calling with 2 it tells me that the number of arguments is wrong.

I'd like to upgrade define_scheme_primitive to somehow support optional arguments, but before I start doing that I'll like to know if that's a good idea, maybe there's already an available workaround I am not aware of. Thanks.

@williampma
Copy link
Member

How about creating an external scheme function (in .scm file) that takes optional arguments, and call the appropriate primitive as needed?

@linas
Copy link
Member

linas commented Jun 30, 2015

William's solution is the easiest, by far. That is, write a very small scheme wrapper (only 3-6 lines of scheme code) that takes an optional number of arguments, have it supply defaults for the missing values, and then always call C++ with a fixed number of arguments.

Example in next post ...

We need a version of cog-bind which takes an optional integer N for the number of solutions N to return. Currently, we have cog-bind, which returns all solutions (and may take too long, e.g. for the genetic data) The 'cog-single` accepts N but is hardwired for N=1.

@linas
Copy link
Member

linas commented Jun 30, 2015

So for example, use C++ to define cog-fc-full, and the in the (opencog query) module, add something like this:

(define* (cog-fc arg-a #:optional opt-arg-b)  
   (if (not opt-arg-b)  ;;; it arg-b is #f i.e. if arg-b is missing ...
        (cog-fc-full arg-a  42)   ; default arg-b is 42
        (cog-fc-full arg-a opt-arg-b)  ;; user supplied arg-b
))

this way, cog-fc can be called with one or two args.

See https://www.gnu.org/software/guile/manual/html_node/lambda_002a-and-define_002a.html for details

@ngeiswei
Copy link
Member Author

ngeiswei commented Jul 5, 2015

I found the best solution is to define dedicated functions for each rule-based system. For instance in the scm config file of PLN the following 2 functions are defined

(pln-fc source)
(pln-bc target)

@ngeiswei ngeiswei closed this as completed Jul 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants