2017-02-20 14:37:26 +00:00
|
|
|
% % % -*-Mode : Prolog; -*-
|
2012-10-08 23:58:22 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2012-10-17 10:56:44 +01:00
|
|
|
% Author: Vitor Santos Costa
|
|
|
|
% E-mail: vsc@dcc.fc.up.pt
|
|
|
|
% Copyright (C): Universidade do Porto
|
2012-10-08 23:58:22 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
%
|
2012-10-17 10:56:44 +01:00
|
|
|
% This file is part of the YAP Python Interface
|
2012-10-08 23:58:22 +01:00
|
|
|
% distributed according to Perl Artistic License
|
|
|
|
% check LICENSE file for distribution license
|
|
|
|
%
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
%%%
|
|
|
|
|
2015-08-07 22:57:53 +01:00
|
|
|
:- module(python,
|
|
|
|
[
|
|
|
|
init_python/0,
|
|
|
|
end_python/0,
|
|
|
|
python_command/1,
|
2015-09-21 23:05:36 +01:00
|
|
|
python_run_file/1,
|
|
|
|
python_run_command/1,
|
|
|
|
python_run_script/2,
|
2015-08-07 22:57:53 +01:00
|
|
|
python_assign/3,
|
|
|
|
python_import/1,
|
2015-09-21 23:05:36 +01:00
|
|
|
array_to_python_list/4,
|
|
|
|
array_to_python_tuple/4,
|
|
|
|
array_to_python_view/5,
|
2015-08-07 22:57:53 +01:00
|
|
|
python/2,
|
2016-10-20 04:38:17 +01:00
|
|
|
acquire_GIL/0,
|
|
|
|
release_GIL/0,
|
2017-02-20 14:37:26 +00:00
|
|
|
python_threaded/0,
|
|
|
|
prolog_list_to_python_list/3,
|
2018-03-02 21:18:24 +00:00
|
|
|
op(100,fy,$),
|
2015-08-07 22:57:53 +01:00
|
|
|
op(950,fy,:=),
|
|
|
|
op(950,yfx,:=),
|
|
|
|
op(950,fx,<-),
|
|
|
|
op(950,yfx,<-),
|
|
|
|
op(50, yf, []),
|
|
|
|
op(50, yf, '()'),
|
|
|
|
op(100, xfy, '.'),
|
|
|
|
op(100, fy, '.')
|
|
|
|
]).
|
|
|
|
|
2015-01-04 23:58:23 +00:00
|
|
|
|
|
|
|
/** <module> python
|
|
|
|
|
|
|
|
A C-based Prolog interface to python.
|
|
|
|
|
|
|
|
@author Vitor Santos Costa
|
|
|
|
@version 0:0:5, 2012/10/8
|
|
|
|
@license Perl Artistic License
|
|
|
|
|
2015-08-07 22:57:53 +01:00
|
|
|
This is an interface to allow calling Python from Prolog. Please look
|
|
|
|
at the SWIG package if you want to embedd Prolog with Python.
|
|
|
|
|
|
|
|
The interface should be activated by consulting the python lybrary. It
|
|
|
|
immediately boots a Python image.
|
|
|
|
|
|
|
|
To best define the interface, one has to address two opposite goals:
|
|
|
|
- make it as similar to python as possible
|
|
|
|
- make all embedded language interfaces (python, R, Java) as
|
|
|
|
similar as possible.
|
|
|
|
|
2015-08-18 21:03:21 +01:00
|
|
|
YAP supports the following translation between Prolog and Python:
|
|
|
|
|
|
|
|
| *Prolog* | *Pyhon* | *Prolog Examples* |
|
|
|
|
|:-------------:|:-------------:|---------------------------------------:|
|
|
|
|
| Numbers | Numbers | 2.3
|
|
|
|
| | | 1545
|
|
|
|
| | |
|
|
|
|
| Atom | Symbols | var
|
|
|
|
| $Atom | | $var [ = var]
|
|
|
|
| `string` | 'string' | \`hello\`
|
|
|
|
| "string" | ' | "hello"
|
|
|
|
| | |
|
|
|
|
| Atom(...) | Symb(...) | f( a, b, named=v)
|
|
|
|
| E.F(...) | E.F (...) | mod.f( a) [ = [mod\|f(a)] ]
|
|
|
|
| Atom() | | f() [ = '()'(f) ]
|
|
|
|
| Lists | Lists | [1,2,3]
|
|
|
|
| t(....) | Tuples | t(1,2,3) to (1,2,3)
|
|
|
|
| (..., ...) | | (1,2,3)[ = (1,(2,3))]
|
|
|
|
| {.=., .=.} | Dict | {\`one\`: 1, \`two\`: 2, \`three\`: 3}
|
2015-08-07 22:57:53 +01:00
|
|
|
|
2015-01-04 23:58:23 +00:00
|
|
|
*/
|
|
|
|
|
2012-10-17 10:56:44 +01:00
|
|
|
|
2012-10-25 00:33:02 +01:00
|
|
|
|
|
|
|
/************************************************************************************************************
|
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
|
2012-10-25 00:33:02 +01:00
|
|
|
Python interface
|
|
|
|
|
|
|
|
Data types are
|
|
|
|
|
2015-08-07 22:57:53 +01:00
|
|
|
Python Prolog
|
|
|
|
string atoms
|
|
|
|
numbers numbers
|
|
|
|
lists lists
|
|
|
|
tuples t(...)
|
|
|
|
generic objs __pointer__(Address)
|
2012-10-25 00:33:02 +01:00
|
|
|
|
2012-10-26 00:24:07 +01:00
|
|
|
$var refers to the attribute __main__.var
|
2012-10-25 00:33:02 +01:00
|
|
|
|
|
|
|
*************************************************************************************************************/
|
|
|
|
|
2017-02-20 14:37:26 +00:00
|
|
|
|
2012-10-17 10:56:44 +01:00
|
|
|
:- use_module(library(lists)).
|
|
|
|
:- use_module(library(apply_macros)).
|
|
|
|
:- use_module(library(charsio)).
|
2012-12-01 14:28:25 +00:00
|
|
|
:- dynamic python_mref_cache/2, python_obj_cache/2.
|
2012-10-25 00:33:02 +01:00
|
|
|
|
2018-03-02 21:18:24 +00:00
|
|
|
:- multifile user:(:=)/2,
|
|
|
|
user:(:=)/1,
|
|
|
|
user:(<-)/1,
|
|
|
|
user:(<-)/2, user:'()'/1, user:'{}'/1, user:dot_qualified_goal/2, user:import_arg/1.
|
|
|
|
|
|
|
|
|
|
|
|
import( F ) :- catch( python:python_import(F), _, fail ).
|
|
|
|
|
|
|
|
user:dot_qualified_goal(Fs) :- catch( python:python_proc(Fs), _, fail ).
|
|
|
|
|
|
|
|
user:F() :-
|
|
|
|
catch( python:python_proc(F() ), _, fail ).
|
|
|
|
|
|
|
|
|
|
|
|
user(P1,P2) :- !,
|
|
|
|
:= P1,
|
|
|
|
:= P2.
|
|
|
|
|
|
|
|
:= F :- catch( python:python_proc(F), _, fail ).
|
|
|
|
|
2016-08-22 23:02:40 +01:00
|
|
|
:= (P1,P2) :- !,
|
|
|
|
:= P1,
|
|
|
|
:= P2.
|
2017-02-20 14:37:26 +00:00
|
|
|
|
2018-03-02 21:18:24 +00:00
|
|
|
user:(:= ) :- catch( python:python_proc(F), _, fail ).
|
2017-02-20 14:37:26 +00:00
|
|
|
|
2018-03-02 21:18:24 +00:00
|
|
|
user:( V := F ) :-
|
|
|
|
python:python_assign(F, V).
|
2015-08-07 22:57:53 +01:00
|
|
|
|
2018-03-02 21:18:24 +00:00
|
|
|
user:(<- F) :-
|
|
|
|
catch( python:python_proc(F), _, fail ).
|
2015-08-07 22:57:53 +01:00
|
|
|
|
2018-03-02 21:18:24 +00:00
|
|
|
user:(V <- F) :-
|
|
|
|
V := F.
|
|
|
|
|
|
|
|
python:python_import(Module) :-
|
|
|
|
python:python_import(Module, _).
|
2017-02-20 14:37:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
python(Exp, Out) :-
|
|
|
|
Out := Exp.
|
2012-10-17 10:56:44 +01:00
|
|
|
|
2012-10-08 23:58:22 +01:00
|
|
|
python_command(Cmd) :-
|
2018-03-02 21:18:24 +00:00
|
|
|
python:python_run_command(Cmd).
|
2015-08-18 21:03:21 +01:00
|
|
|
|
2012-10-08 23:58:22 +01:00
|
|
|
start_python :-
|
2018-03-02 21:18:24 +00:00
|
|
|
python:python_import('inspect', _),
|
2012-12-02 13:19:10 +00:00
|
|
|
at_halt(end_python).
|
2012-10-25 00:33:02 +01:00
|
|
|
|
|
|
|
add_cwd_to_python :-
|
2012-10-23 10:18:24 +01:00
|
|
|
unix(getcwd(Dir)),
|
|
|
|
atom_concat(['sys.path.append(\"',Dir,'\")'], Command),
|
2018-03-02 21:18:24 +00:00
|
|
|
python:python_command(Command),
|
|
|
|
python:python_command("sys.argv = [\"yap\"]").
|
2015-08-07 22:57:53 +01:00
|
|
|
% done
|
2012-10-08 23:58:22 +01:00
|
|
|
|
2017-06-16 11:53:46 +01:00
|
|
|
:- initialization( load_foreign_files(['YAPPython'], [], init_python_dll), now ).
|