Thread: Factorial in C++ and Prolog

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    26

    Factorial in C++ and Prolog

    I would like to work out a number's factorial. My factorial rule is in a Prolog file and I am connecting it to a C++ code. Can someone tell me what is wrong with my C++ interface please?

    Code:
        % factorial.pl
    
        factorial( 1, 1 ):-
            !.
        factorial( X, Fac ):-
            X > 1,
            Y is X - 1,
            factorial( Y, New_Fac ),
            Fac is X * New_Fac.
    
    
        // factorial.cpp
    
        # headerfiles
    
        term_t t1;
        term_t t2;
        term_t goal_term;
        functor_t goal_functor;
    
        int main( int argc, char** argv )
        {
            argc = 4;
    
            argv[0] = "libpl.dll";
            argv[1] = "-G32m";
            argv[2] = "-L32m";
            argv[3] = "-T32m";
    
            PL_initialise(argc, argv);
    
            if ( !PL_initialise(argc, argv) )
                PL_halt(1);
    
            PlCall( "consult(swi('plwin.rc'))" );
            PlCall( "consult('factorial.pl')" );
    
            cout << "Enter your factorial number: ";
            long n;
            cin >> n;
    
            PL_put_integer( t1, n );
            t1 = PL_new_term_ref();
            t2 = PL_new_term_ref();
            goal_term = PL_new_term_ref();
            goal_functor = PL_new_functor( PL_new_atom("factorial"), 2 );
            PL_put_atom( t1, t2 );
            PL_cons_functor( goal_term, goal_functor, t1, t2 );
    
            PL_halt( PL_toplevel() ? 0 : 1 );
        }
    Edited by moderator: Added code tags
    Last edited by VirtualAce; 04-05-2010 at 04:20 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You're going to have to give more information. Then someone who is familiar with your setup may be able to advise. The method of interfacing between C (or C++) and prolog is highly dependent on both the compiler and on the prolog interpreter.

    Overwriting argc and argv (as you have done, before the prolog-related stuff) is often an effective way to crash a program. Better to set up your own local storage for argv, and pass that to PL_initialise().

    It would probably be easier to write your own factorial function in C++, but I assume your purpose here is learning to interface with your prolog rather than avoiding coding in C++ (as interfacing to other languages is not trivial).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Tags for this Thread