# How to make a SWI-Prolog compiler online?

**URL:** <https://swi-prolog.discourse.group/t/how-to-make-a-swi-prolog-compiler-online/4818>\
**Category:** Help!\
**Tags:** swish, how-to\
**Created:** [December 28, 2021, 1:59pm UTC](https://swi-prolog.discourse.group/t/how-to-make-a-swi-prolog-compiler-online/4818 "2021-12-28T13:59:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![joseph-vidal-rosset](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/joseph-vidal-rosset/32/2594_2.png) [@joseph-vidal-rosset](https://swi-prolog.discourse.group/u/joseph-vidal-rosset)\
**Post date:** [December 28, 2021, 1:59pm UTC](https://swi-prolog.discourse.group/t/how-to-make-a-swi-prolog-compiler-online/4818/1 "2021-12-28T13:59:31Z")

</div>

Hello everybody.  
It seems really very hard to install on a website a Prolog compiler where visitors can test some programs. I have downloaded pengines and tested it locally, but I met quickly sandbox limitations without understanding of getting rid off theses limitations.  
By contrast, I had no problem to publish fCube.pl here:  
[https://rextester.com/SEOO25214](https://rextester.com/SEOO25214)  
which works smoothly in this website, but not in SWISH.  
I would be happy to get for my personal website something that works like in this example. I guess that it is not so complicated, but I need help.  
Best wishes ,  
Jo.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [December 28, 2021, 3:27pm UTC](https://swi-prolog.discourse.group/t/how-to-make-a-swi-prolog-compiler-online/4818/3 "2021-12-28T15:27:53Z")

</div>

I uploaded the code and example to [SWISH -- SWI-Prolog for SHaring](https://swish.swi-prolog.org/p/AahUngtW.pl). It raised an error on tab/1 where the SWISH emulation didn’t accept an expression as the built-in one does. Fixed it first by using is/2 before calling tab/1. Now the example seems to work. Updated SWISH to make the behavior of tab/1 compatible after which the code runs unmodified.

That said, the system performs a lot of one-character write/1 calls that result in a web server interaction each and thus it writes as computers in very old movies did 🙂 Just needs green on black 🙂 You can fix that by using with\_output\_to(string(S), Goal) and then write(S) to send the output as one object. You could also use the HTML output facilities to both avoid most of this overhead and get pretty looking output.

Still, SWISH is not the same as an online Prolog in a terminal. It adds things, especially when it comes to presenting results and looses, mostly due to lack of persistent state and sandbox limitations. For fCube it looks like you can build a nice looking web application in SWISH (see [SWISH -- SWI-Prolog for SHaring](https://swish.swi-prolog.org/example/swish_tutorials.swinb))

There are enough systems around that package arbitrary systems as a remote service. Most will run the system in an OS sandbox I guess and connect the I/O.

---

<div class="post-metadata">

**Author:** ![joseph-vidal-rosset](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/joseph-vidal-rosset/32/2594_2.png) [@joseph-vidal-rosset](https://swi-prolog.discourse.group/u/joseph-vidal-rosset)\
**Post date:** [December 28, 2021, 4:50pm UTC](https://swi-prolog.discourse.group/t/how-to-make-a-swi-prolog-compiler-online/4818/4 "2021-12-28T16:50:26Z")

</div>

Thanks Jan, for having replied to Jan and also for having published fCube on SWISH .

Have a good evening !

Best wishes,

Jo.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [December 28, 2021, 6:46pm UTC](https://swi-prolog.discourse.group/t/how-to-make-a-swi-prolog-compiler-online/4818/5 "2021-12-28T18:46:30Z")

</div>

> [@joseph-vidal-rosset](#):
>
> sandbox limitations

If you don’t care about security and want to be able to execute arbitrary queries, there’s a server here (and a javascript client): [GitHub - kamahen/swipl-server-js-client: Sample SWI-Prolog server with JavaScript client](https://github.com/kamahen/swipl-server-js-client)

---

<div class="post-metadata">

**Author:** ![joseph-vidal-rosset](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/joseph-vidal-rosset/32/2594_2.png) [@joseph-vidal-rosset](https://swi-prolog.discourse.group/u/joseph-vidal-rosset)\
**Post date:** [December 28, 2021, 7:16pm UTC](https://swi-prolog.discourse.group/t/how-to-make-a-swi-prolog-compiler-online/4818/6 "2021-12-28T19:16:07Z")

</div>

Thanks Peter ! It is indeed very interesting for me and I will look at it very soon. I will inform you.

All the best,

Jo.

---

<div class="post-metadata">

**Author:** ![joseph-vidal-rosset](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/joseph-vidal-rosset/32/2594_2.png) [@joseph-vidal-rosset](https://swi-prolog.discourse.group/u/joseph-vidal-rosset)\
**Post date:** [December 29, 2021, 9:23am UTC](https://swi-prolog.discourse.group/t/how-to-make-a-swi-prolog-compiler-online/4818/7 "2021-12-29T09:23:22Z")

</div>

Hello Peter,  
I had tested your simpleserver, and indeed it works perfectly with the examples of command that you give. But this is almost the only thing that I am able to do with it. I stay unable to load and consult a file (to put it in static/ directory changes nothing), and I have written in simple\_client.html a text area with leanseq.pl like [in this webpage](https://www.vidal-rosset.net/sequent_calculus_prover_with_antisequents_for_classical_propositional_logic.html), but unfortunately, to the query `provable(((a & b) => a), Proof). ` I get :

```prolog
Success: error
Error: error(syntax_error(operator_expected),string("provable(((a & b) => a), Proof). ",12))

```

I am lost… ☹

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [December 29, 2021, 11:44pm UTC](https://swi-prolog.discourse.group/t/how-to-make-a-swi-prolog-compiler-online/4818/8 "2021-12-29T23:44:35Z")

</div>

The easiest way of running your code is to put it into a file (e.g. `leanseq.pl`) and then simply typing `[leanseq].` into the REPL prompt for the server, like this:

```prolog
$ swipl simple_server.pl --port=9999 --staticdir=static
% static dir: static
% Started server at http://localhost:9999/
% Starting REPL ...
?- [leanseq].

```

After I did this, I tried your query on the browser and got the following:

| **Query:** | `provable(((a & b) => a), Proof). ` |
| --- | --- |
| **(after)** | `provable((a&b=>a),rcond([]>[(a&b=>a)],land([(a&b)]>[a],ax([a,b]>[a],a))))` |
| **Success:** | _true_ |
| **Error:** | _``_ |
| **Vars:** | **`Proof`** `rcond([]>[(a&b=>a)],land([(a&b)]>[a],ax([a,b]>[a],a)))` |
| **Printed:** | |

If you want to be able to execute an arbitrary program (as opposed to a simple query), then you’ll need to enhance the javascript client to take a program and consult it. You’ll also need to figure out a way of keeping separate “engines” for the various users, etc., etc. and pretty soon you’ll have something like SWISH. 😉

(The `static` directory is for serving static HTML pages, images, and Javascript code; e.g., `simple_client.html` is here, so that the server can send it to the browser – on the server, this is done by the `http_handler(static(.),...)` directive, and an HTTP GET request results in the contents of `static/index.html` (this is done with a 301 redirect). You can see how this happens if you use `curl` on `localhost:9999` … the resulting page contains a `<script>` tag that loads `simple_client.js`, which is also in the `static` directory.)

---

<div class="post-metadata">

**Author:** ![joseph-vidal-rosset](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/joseph-vidal-rosset/32/2594_2.png) [@joseph-vidal-rosset](https://swi-prolog.discourse.group/u/joseph-vidal-rosset)\
**Post date:** [December 30, 2021, 1:21pm UTC](https://swi-prolog.discourse.group/t/how-to-make-a-swi-prolog-compiler-online/4818/9 "2021-12-30T13:21:30Z")

</div>

Many thanks Peter. I made the error to try to load leanseq.pl via the query area. It works smoothly, and the output appears as quick as in emacs, maybe more.  
I will try now to use your work online, in my personal blog.  
Thanks again and happy december 31 ! 😉

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [December 30, 2021, 5:27pm UTC](https://swi-prolog.discourse.group/t/how-to-make-a-swi-prolog-compiler-online/4818/10 "2021-12-30T17:27:09Z")

</div>

Glad to have been of help. If you notice anything that needs improving in my code, please send me a change (I’m not very good at git, so the best way would be a PR; if you send me a diff, I don’t know how to incorporate it while crediting you, except in the commit comment).

---

<div class="post-metadata">

**Author:** ![joseph-vidal-rosset](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/joseph-vidal-rosset/32/2594_2.png) [@joseph-vidal-rosset](https://swi-prolog.discourse.group/u/joseph-vidal-rosset)\
**Post date:** [December 30, 2021, 5:44pm UTC](https://swi-prolog.discourse.group/t/how-to-make-a-swi-prolog-compiler-online/4818/11 "2021-12-30T17:44:50Z")

</div>

My goal is now to use your tool in adapting it for [my blog](https://www.vidal-rosset.net) which runs on https.

I have to understand how to run SWI-Prolog daemon , etc. I do not doubt that for experts like Jan and you, it is a piece of cake, but I do not want to abuse of your kindness and of your time. I will try, and if I am lost again, if I am in a dead end, I will warn you.  
Best wishes ,  
Jo.

---

<div class="post-metadata">

**Author:** ![joseph-vidal-rosset](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/joseph-vidal-rosset/32/2594_2.png) [@joseph-vidal-rosset](https://swi-prolog.discourse.group/u/joseph-vidal-rosset)\
**Post date:** [January 2, 2022, 1:56pm UTC](https://swi-prolog.discourse.group/t/how-to-make-a-swi-prolog-compiler-online/4818/12 "2022-01-02T13:56:19Z")

</div>

[https://swi-prolog.discourse.group/t/how-to-use-of-upcase-atom-2-to-get-uppercase-letters-in-an-output/4693/63?u=joseph-vidal-rosset](https://swi-prolog.discourse.group/t/how-to-use-of-upcase-atom-2-to-get-uppercase-letters-in-an-output/4693/63)

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [January 2, 2022, 2:02pm UTC](https://swi-prolog.discourse.group/t/how-to-make-a-swi-prolog-compiler-online/4818/13 "2022-01-02T14:02:09Z")

</div>

Discourse has a feature called [Onebox](https://github.com/discourse/onebox/blob/b1c62d97ad3380289e455041da3b206fed1832bb/README.md) where a standalone URL with no preceding spaces on a line by itself will be converted into a Onebox, E.g.

> [@How to use of upcase\_atom/2 to get uppercase letters in an output](https://swi-prolog.discourse.group/t/how-to-use-of-upcase-atom-2-to-get-uppercase-letters-in-an-output/4693/63):
>
> I just tested the proof of Pelletier problem 17 with swipl simple\_server.pl and it works, Firefox provides the proof and one can read on the terminal: % 54,890,488 inferences, 4.423 CPU in 4.424 seconds (100% CPU, 12409457 Lips) Security worries apart, that would be a good solution for Prolog provers online.

🙂
