opencog / atomspace

The OpenCog (hyper-)graph database and graph rewriting system
https://wiki.opencog.org/w/AtomSpace
Other
801 stars 225 forks source link

Add support for optional arguments in define_scheme_primitive #105

Closed ngeiswei closed 9 years ago

ngeiswei commented 9 years ago

I tried overloading a scheme definition with multiple define_scheme_primitive on the same scheme function name, see https://github.com/ngeiswei/atomspace/commit/ee6995c772a6a0c606edf9f8451d25c2d4f2dfc6

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 commented 9 years ago

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

linas commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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)