What is InteLib

InteLib is a library of C++ classes which lets you do Lisp programming within your C++ program even without any additional preprocessing, without all those calling conventions etc. You can write a C++ code (that is, a code which is accepted by your C++ compiler) thinking in a “Lisp mode” and the code you write will look much like Lisp code altough it will be pure C++.

To give you the essential feeling, the following example is provided.

(defun isomorphic (tree1 tree2)
   (cond ((atom tree1) (atom tree2))
         ((atom tree2) NIL)
         (t (and (isomorphic (car tree1) 
                             (car tree2))
                 (isomorphic (cdr tree1) 
                             (cdr tree2))
))))

Just a Lisp function, isn't it? Now look at the following code:

(L|DEFUN, ISOMORPHIC, (L|TREE1, TREE2),
  (L|COND, 
    (L|(L|ATOM, TREE1), (L|ATOM, TREE2)),
    (L|(L|ATOM, TREE2), NIL),
    (L|T, (L|AND,
      (L|ISOMORPHIC, (L|CAR, TREE1), 
                     (L|CAR, TREE2)),
      (L|ISOMORPHIC, (L|CDR, TREE1), 
                     (L|CDR, TREE2))
))))

Obviously the code is just the same, the syntax changed a bit, but it's still the same. Well, do I surprise you if I say it is C++ code? If you don't believe, look at the following:

 //       File isomorph.cpp
 #include <intelib/lisp/lisp.hpp>
 #include <intelib/lisp/lsymbol.hpp>
 #include <intelib/lfun_std.hpp>
 #include <intelib/lfun_sel.hpp>
 
 
 LSymbol ISOMORPHIC("ISOMORPHIC");
 
 static LFunctionalSymbol<LFunctionDefun> DEFUN("DEFUN");
 static LFunctionalSymbol<LFunctionCond> COND("COND");
 static LFunctionalSymbol<LFunctionAtom> ATOM("ATOM");
 static LFunctionalSymbol<LFunctionAnd> AND("AND");
 static LFunctionalSymbol<LFunctionCar> CAR("CAR");
 static LFunctionalSymbol<LFunctionCdr> CDR("CDR");
 
 static LListConstructor L;
 
 void LispInit_isomorphic() {
   static LSymbol TREE1("TREE1");
   static LSymbol TREE2("TREE2");
   ////////////////////////////////////////////////
   //
   (L|DEFUN, ISOMORPHIC, (L|TREE1, TREE2),
     (L|COND, 
       (L|(L|ATOM, TREE1), (L|ATOM, TREE2)),
       (L|(L|ATOM, TREE2), NIL),
       (L|T, (L|AND,
         (L|ISOMORPHIC, (L|CAR, TREE1), 
                        (L|CAR, TREE2)),
         (L|ISOMORPHIC, (L|CDR, TREE1), 
                        (L|CDR, TREE2))
   )))).Evaluate();
   //
   ////////////////////////////////////////////////
 }

Well, this code is a complete C++ module and it does compile pretty well. No joke, it's real.

By the way, don't try to find any use I made out of the macroprocessor. No macros have ever been used by InteLib (except those for conditional compile directives). Instead, just recall that comma is an operator in C++ and can be overloaded for user-invented data types.

If you now wonder what can be actually done with this module, consider the following program:

 // file main.cpp
 #include <stdio.h>
 #include <stdlib.h>
 #include <intelib/sexpress/sexpress.hpp>
 #include <intelib/sexpress/sstring.hpp>
 #include <intelib/lisp/lsymbol.hpp>
 
 extern LSymbol ISOMORPHIC;
 void LispInit_isomorphic();
 
 static LListConstructor L;
 
 void call_isomorph(const SReference &l1, const SReference &l2)
 {
     SReference res = (L|ISOMORPHIC, ~l1, ~l2).Evaluate();
     printf("%s ~~ %s : %s\n",
            l1->TextRepresentation().c_str(),
            l2->TextRepresentation().c_str(),
            res->TextRepresentation().c_str());
 }
 
 int main(int argc, char *argv[])
 {
     LispInit_isomorphic();
     SReference ls1 = (L|(L|1, 2), 3, 4);         // ((1 2) 3 4)
     SReference ls2 = (L|(L|"a", "b"), "c", "d"); // (("a" "b") "c" "d")
     SReference ls3 = (L|(L|1, 2), (L|3, 4));     // ((1 2) (3 4))
     call_isomorph(ls1, ls2);
     call_isomorph(ls1, ls3);
     return 0;
 }

If you have InteLib installed in your system, just issue the command

    g++ -Wall -g isomorph.cpp main.cpp -lintelib 

and the produced executable will run and print the following:

 ((1 2) 3 4) ~~ (("a" "b") "c" "d") : T
 ((1 2) 3 4) ~~ ((1 2) (3 4)) : NIL

So, the first two of our lists were isomorphic, and the last wasn't isomorphic to them (which is obvious), and the program shown us exactly this.

See the samples/ directory within the InteLib tarball for more useful examples.

 
projects/intelib/intro.txt · Last modified: 2010/10/12 17:54 by croco
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki