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

PutLink specification of executable parts #577

Closed
ngeiswei opened this issue Jan 7, 2016 · 7 comments
Closed

PutLink specification of executable parts #577

ngeiswei opened this issue Jan 7, 2016 · 7 comments

Comments

@ngeiswei
Copy link
Member

ngeiswei commented Jan 7, 2016

I just want to be sure I understand PutLink specification correctly. On the wiki http://wiki.opencog.org/wikihome/index.php/PutLink it says

"The PutLink consists of a graph, with a set of variables in it. It is followed by a list of values. When the PutLink is executed, with the cog-execute! function, the variables are replaced by the values, to create a new graph. That is, cog-execute! function performs beta reduction on the PutLink. "

It's not clear from that comment whether it is supposed to perform just a single step of beta-reduction or more.

I noticed that it executes as well the substituted parts that are executable, see the following

(define pat-body (List (Variable "$X") (Variable "$Y")))
(define plus (Plus (Number "2") (Number "3")))
(define times (Times (Number "4") (Number "5")))
(define pat-put (Put pat-body (List plus times)))
(cog-execute! pat-put)

The result is

(ListLink
   (NumberNode "5.000000")
   (NumberNode "20.000000")
)

that is plus and times are executed. I'd to know if that is considered a bug or a feature. If that's a feature I'll update the wiki accordingly (cause it's nowhere mentioned).

There's perhaps a disadvantage with that, as far as I'm currently concerned anyway, I cannot use a BindLink to query and build patterns involving executable parts. For instance the following

(define pat-var (VariableList (TypedVariable (Variable "$X") (Type "PlusLink")) (TypedVariable (Variable "$Y") (Type "TimesLink"))))
(define pat-bind (Bind pat-var pat-body pat-body))
(cog-bind pat-bind)

is gonna return

(SetLink
   (ListLink
      (NumberNode "5.000000")
      (NumberNode "20.000000")
   )
)

there seems to be no way of creating my pattern of Plus and Times. I tried using Quote/Unquote, but no way. The only way I found is to manually reconstruct them with Atom constructors after querying them with GetLink only. See the following

(define pat-get (Get pat-var pat-body))
(cog-execute! pat-get)
(SetLink
   (ListLink
      (PlusLink
         (NumberNode "2.000000")
         (NumberNode "3.000000")
      )
      (TimesLink
         (NumberNode "4.000000")
         (NumberNode "5.000000")
      )
   )
)

Here of course the example is trivial, but my use case involves more complex things (embedded ExecutionOutputLink in patterns, I just tried to build a really simple example here).

@linas
Copy link
Member

linas commented Jan 8, 2016

Well, there are several issues. One is that, right now, cog-execute! does eager execution, and it should be doing lazy execution. I started working on that just now, in pull req #581 but have not finished. -- its actually a fair amount of work and hassle to finish, and low priority for me just right now, so is unlikely to get finished any time soon. Here, "eager execution" means that all of the arguments to a function are executed before the function is called. viz, the arguments to Put are executed before the Put is performed. So, with lazy execution, the composition (put) could be done without executing the arguments.

As to what should happen, that is unclear. In the common case, it is desirable to execute everything, all the way down -- all the nesting and wrapping and etc. I suppose that, in some cases, maybe one wants to do only one layer, and no more. I don't know who would want to do that or why.

I'm not sure what the gig is with cog-bind, and I'm somewhat surprised. As currently designed, cog-bind does NOT execute the pattern, before performing a search. However, perhaps it does execute after getting the search results -- i.e. it executes the search results. Yes, this seems like a bug. I know where it is happening, too, its in Instantiate.cc, which, in the good-old days, only substituted values for variables, but now has morphed into executing things as well.

Again, its not entirely clear what should happen. If one is nesting these arbitrarily deep, one will typically want to execute everything (lazily) all the way down, but if one is searching for things that are executable, then maybe not. I currently do not have any clear answers for "what should happen", since the most-common use case seems to contradict the more narrow ones.

What does haskell do?

@linas
Copy link
Member

linas commented Jan 8, 2016

lazy execution issue tracker here: #583

@ngeiswei
Copy link
Member Author

ngeiswei commented Jan 8, 2016

Indeed the problem with BindLink is that it executes the searched results, just like PutLink would if you hand it the searched results. I also don't know if that has to be feature or a bug.

Haskell is lazy by default but uses https://prime.haskell.org/wiki/BangPatterns when you want a variable to be eagerly evaluated. Basically just put ! in front of it.

-- lazy
let y = map foo x
-- eager
let !y = map foo x

I guess for now we could have execute functions, cog-execute! that is lazy, and cog-eager-execute! that is eager (or maybe remove the ! at the end of cog-execute! for the lazy version, but I don't know if that's good scheme practice).

@linas
Copy link
Member

linas commented Jan 8, 2016

well, the problem is with nesting: you can have gets inside puts inside gets inside puts, ad infinituum, with other functions (plus, times. etc.) intervening. Which suggests that we should invent BangLink and LazyLink and use those to wrap things appropriately. Ugh.

I would rather not invent yet more link types unless they are really really needed. I feel like I've opened some pandora's box with all these link types, and now the lid won't close.

@linas
Copy link
Member

linas commented Jan 8, 2016

The very very last example in https://github.com/linas/atomspace/blob/master/examples/guile/map.scm fails, due to eager evaluation. Hmm.. Although I guess I could work around that in other ways...

@linas
Copy link
Member

linas commented Mar 27, 2016

There is a new atom, called DontExecLink http://wiki.opencog.org/w/DontExecLink It was added in pull req #714 to fix issue #704 Maybe this will serve your purposes?

@ngeiswei
Copy link
Member Author

Yes, DontExecLink served my purpose, I'm closing this.

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

2 participants