clips

Use a dynamic slot name for modify function


In CLIPS, Is it on purpose or an oversight that the modify function cannot be given a dynamic slot name.

; This compiles and works.
(deffunction set_tpl_value(?addr ?key ?val)
    (modify ?addr (the-slot ?val)))

; This gives a syntax error, at position: (?key
(deffunction set_tpl_value(?addr ?key ?val)
    (modify ?addr (?key ?val)))

Solution

  • CLIPS started as a prototype developed by a coop student over a couple of months in 1985. I took over after that and worked on it for the 11 years that I was at NASA and then continued supporting the public domain version after I left.

    Most of the initial work focused on making CLIPS usable and maintainable. The source code consisted of several files to make editing easier, but these had to be concatenated together as a single file for compilation. There were places in the code that had dozens of levels of nested if/while/for statements. So one of the first things I did was to split and organize the source code into files that could be compiled individually and refactor the functions so I could make sense of what they were was doing. The code was initially developed on an HP-9000, so I worked on making it portable by getting it to compile on a VAX 11/780 and IBM PC. There was no Read-Eval-Print-Loop in the original prototype, so one was added so that developers could interact with and debug their CLIPS code.

    When CLIPS first ran on an IBM PC, it literally executed rules from a small program of a few dozen rules at the rate of one per minute. The code had numerous memory leaks and unnecessary computations. The implementation of the rete algorithm was inefficient. During pattern matching, variables encountered in patterns were added to a linked list of variable values which would be compared to other linked lists to determine whether variable bindings were consistent between patterns--two patterns each having 5 variables would cause 25 comparisons even if there were no variables in common between the two lists. It wasn't until CLIPS 4.2 that there was a reasonably efficient implementation of the rete algorithm in CLIPS.

    Deftemplates weren't added until CLIPS 4.3 came out in 1989. They were functionally equivalent to the literalize statement from OPS5. The underlying facts were still ordered facts, so deftemplates had the same restriction as the OPS5 literalize: you were limited to one multifield slot in the deftemplate because the multifield slot position was always placed at the end of the ordered fact so that the multifield value was always the last slot position in the ordered fact plus any excess values.

    It's been 35 years, but probably at the time I didn't consider making the slot name dynamic because I hadn't written any programs that needed that flexibility so it just didn't occur to me. The really big benefit of the deftemplate was being able to specify just the slots of interest, and deffunctions weren't added until version 5.0 in 1991 so there wasn't a mechanism for generalizing code that could be shared among rules. Once COOL was added in version 5.0 by my colleague Brian Dantes and then fully integrated with rules in version 6.0 in 1993, there wasn't a lot of need to add some features to deftemplates since COOL was a more considered implementation of an object-oriented programming language.