O-Prolog



O-Prolog(OPL) is an interpreter and compiler to be compatible with Edinburgh Prolog and ISO-Prolog. OPL was written by me(Kenichi Sasagawa) for appendix of Kindle book. [Prolog syorikei wo tukurou]
I published execution code of OPL on my WEB page. For non-commercial use, it is free of charge. I will not be responsible for the results of your execute. Please take self responsibility
In 2019, February, O-Prolog became OSS.
O-Prolog Github
O-Prolog(Windows)

O-Prolog(Linux Ubuntu)

O-Prolog(for raspberry3 Linux Raspbian)



When Linux version, Please unpack tar file.

When Windows version, Please unzip file.

tar xvf oprolog.tar
or
tar xvf rasopl.tar

To invoke opl, enter command from terminal

opl (Windows)
./opl (Linux)

-c option is for start up file.

./opl -c init,pl

-r option is for Not editable REPL mode.
Default, REPL is editable.

startup.pl

Even if you do not specify an option, if there is a file startup.pl in the current directory, OPL loads it and start it.

Goal

I hope that Prolog will become more popular. I hope many people enjoy Prolog. OPL aims at easy handling.

Intaraction

| (vertical bar) is prompt.

following code is assertion.

human(john).
error(X) :- human(X).

To question, require ?- operator.

?- error(john).

Compiler

read compiler from library

| ?- use_module(library(compiler)).
yes

?- compile_file(filename.pl).

The Compiler generates filename.o object file.

?- ['filename.o'].

Char set

Linux default is unicode.

Windows default is SJIS.

to change. set_prolog_flag(char_set,unicode).



Builtin editor

OPL(Linux version) has builtin editor.

Invoke editor

?- edit(file-name). example edit('queens.pl')

?- edit([]). edit recent file again.

Command

CTR+O save file.
CTRL+X quit editor.
CTRL+K delete selection.
CTRL+U paste from clip board.

Editing

↑↓←→ move cursol
ESC A mark current row position. After this operation, cursol up or down reverse selected rows. ESC A again, unmark. Similer to nano editor
Enter key insert tab for Lisp automatic
Insert key switch insert/overwrite
Tab key insert tab for Lisp
BackSpace key delete backword char
Delete key delete forwaord char
Home key display top page
ESC |(SSH)
End key display end page
ESC /(SSH)
PageUp key PageUp
Ctrl+Y(SSH)
PageDown PageDown
Ctrl+V(SSH)

ESC TAB completion.

Setting

set_editor(indent,auto) set auto indent mode.
set_editor(indent,manual) set manual indet mode. To indent tab key.
set_editor(tab,2) set tab 2.
set_editor(tab,4) set tab 4.
set_editor(tab,8) set tab 8.

syntax highlighting.
n = 0-7
0=Black, 1=Red, 2 =Green, 4=Blue, 5=Maggenta, 6=Syan, 7=White.
set_editor(operator_color,n)
set_editor(builtin_color,n)
set_editor(extended_color,n)
set_editor(quote_color,n)
set_editor(comment_color,n)


WiringPi

Version for Raspberry has wiringPi.

In order to use wiringPi, you need to invoke OPL with super user.

sudo ./opl

?- ['wiringpi.o'].

OPL <==================================> C 
wiringpi_spi_setup(ch,speed) <===>  wiringPiSPISetup (SPI_CH, SPI_SPEED) wiringpi_setup_gpio(X) <===> X=wiringPiSetupGpio() pin-mode(N,output) <====> pinMode(N, OUTPUT) or input -> INPUT or pwm_output ->PWM-OUTPUT digital_write(n,v) <===> digitalWrite(n, v); digital_write_byte(value) <===> digitalWriteByte(value) digital_read(pin,X) <===> X=digitalRead(pin) delay(howlong) <===> void delay(unsigned int howLong) pull_up_dn_control(pin, pud) <===> pullUpDnControl(pin,pud) pwm_set_mode(pwm_mode_ms) <===> pwmSetMode(PWM_MODE_MS) or pwm_mode_bal -> PWM_MODE_BAL pwm_set_clock(n) <===> pwmSetClock(n); pwm_set_range(n) <===> pwmSetRange(n); pwm_write(pin,value) <===> pwmWrite(pin , value)

LED on/off

setup :-
    not(flag),wiringpi_setup_gpio(X),assert(flag),
    pin_mode(5,output).

test(0).
test(N) :-
    digital_write(5,1),
    delay(1000),
    digital_write(5,0),
    delay(1000),
    N1 is N - 1,
    test(N1).


%execute

?- setup.
?- test(10).


control servo moter

%hardware Micro servo Digital 9g SG90

setup :-
    not(flag),wiringpi_setup_gpio(X),assert(flag),
    pin_mode(18,pwm_output),
    pwm_set_mode(pwm_mode_ms),
    pwm_set_clock(400),
    pwm_set_range(1024).


test1(N) :-
    pwm_write(18,N).

%execute

?- setup.
?- test1(24).
?- test1(115).

TCP/IP



server_create/2
  server_create(N,S)
N = atom of port number
S = socket descriptor

example
server_create('5000',S).

server_create/3
server_create(IP,N,S)
IP = atom of IP address
N = atom of port number
S = socket discriptor

example
server_create('127.0.0.1','5000',S).

server_accept/3

server_accept(S,IP,C)
S = socket discriptor
C = socket discriptor for client


clinet_connect/3
client_connect(IP,N,S)
IP = atom of ip address
N = atom of port number
S = discriptor

socket_send/2
socket_send(S,X)
S = socket discriptor
X = atom as data


example
socket_send(S,hello).

socket_recieve/2
socket_send(S,Y)
S = socket discriptor
Y = atom as recieved data

socket_close/1
socket_close(S)
S = socket discriptor


example code

%test for TCP/IP

server :-
    server_create('127.0.0.1','5000',S),
    server_accept(S,Y,C),
    repeat,
    socket_recieve(C,D),
    write(D),
    socket_send(C,D),
    D == end,
    socket_close(S).


client :-
    client_connect('127.0.0.1','5000',S),
    repeat,
    read(X),
    socket_send(S,X),
    socket_recieve(S,Y),
    write(Y),
    Y == end,
    socket_close(S).

document

Sorry japanese only. O-Prolog document


| return |