Download CLIPS User's Guide
Transcript
— 153 — compare two strings using the ">" function. CLIPS> (clear) CLIPS> (> "duck2" "duck1") [ARGACCES5] Function > expected argument #1 to be of type integer or float CLIPS> It's not possible to do this comparison with the ">" function because it expects NUMBER types as arguments. However, it's easy to define a (defgeneric) which will overload the ">" to accept STRING types as well as NUMBER types. For example, if the arguments of ">" are of type STRING, the defgeneric will do a string comparison, letter by letter starting from the left until the ASCII codes differ. In contrast, if the arguments of ">" are of type NUMBER, the system compares the sign and magnitude of the numbers. The user-defined ">" for STRING types is an explicit method, while a system-defined or user-defined external function such as ">" for NUMBER type is an implicit method. The technique of overloading a function name so that the method which implements it is not known until run-time is another example of dynamic binding. Any object reference of name or address may be bound at run-time in CLIPS to functions through dynamic binding also. Some languages such as Ada have a more restrictive type of overloading in which the function name must be known at compile time rather than at run-time. The run-time dynamic binding is the least restrictive since methods can be created during execution by the (build) statement. However, you should be careful in using (build) since dynamically creating constructs is often hard to debug. Also, the resulting code may be difficult to verify and validate since you'll have to stop execution to examine the code. Dynamic binding is a characteristics of a true object-oriented programming language. Following is an example of a defgeneric, ">", for STRING types and its method. CLIPS> (defgeneric >) ; Header declaraction. Actually unnecessary CLIPS> (defmethod > ((?a STRING) (?b STRING)) (> (str-compare ?a ?b) 0)) CLIPS> (> "duck2" "duck1") ; The overload ">" works correctly TRUE ; in all three cases. CLIPS> (> "duck1" "duck1") FALSE CLIPS> (> "duck1" "duck2")