For Programmers: Free Programming Magazines  


Home > Archive > Prolog > June 2007 > Re: Possible to register a foreign function dynamically?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Possible to register a foreign function dynamically?
yuce

2007-06-01, 4:20 am

> >> > def atom_checksum(a0, arity, context):
>
>
>
> PL_get_atom_chars() provides a pointer to the actual string that is
> part of the Prolog atom. Don't change!


Ok, from what I understand, PL_get_atom_chars doesn't allocate
anything, it just returns the pointer to the actual string, so I don't
need to allocate any memory to keep it.

I am trying to make things more Python then C, so I am wrapping PL_*
functions in similar Python functions [for example,
`PL_register_foreign` becomes registerForeign]. Here's the result of
applying that to atom_checksum:

from pyswip.prolog import Prolog
from pyswip.easy import getAtomChars, unifyInteger, registerForeign

def atom_checksum(*a):
s = getAtomChars(a[0])
if s is not None:
sum = 0
for c in s:
sum += ord(c)&0xFF
return unifyInteger(a[1], sum&0xFF)
else:
return False
atom_checksum.arity = 2

p = Prolog()
registerForeign(atom_checksum)
print list(p.query("X='Python', atom_checksum(X, Y)"))

My next aim is covering the SWI-Prolog FFI functions responsible for
creating terms and predicates, so avoiding the need to consult a
Prolog program for complex code. I think this could be more flexible
[but maybe with a slightly less performance]

If you would like to comment on any of these, I'd appreciate that :)

Thanks again,

Yuce

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com