Introducing Simple Web For SWI-Prolog

18th November 2018

For some strange reason developers seem surprised when I tell them I love using Prolog for web development. I understand it's not a popular choice, but it is a suitable one. In this post I explain why I think Prolog is great for web development, and introduce Simple Web, which I developed to lower the barrier to entry.


Why Prolog For Web Development?!

SO I’VE done most of my previous web development work in Python, using Flask, Klein, Django and Wagtail. I’ve also done some PHP, but those were dark days, and I’ve flirted with Haskell. I may have glanced over the odd Ruby On Rails project with the indignation that comes from being a Python fan boy in my younger days. In Python, the web development frameworks are wonderfully mature, they clearly layout a best practise, and in true “batteries included” style, you can pip install just about anything you need. This is awesome.

The “batteries included” nature of web development in Python means you spend most of your time just doing declarations. You declare your models, declare your routes, responses, templates, forms, etc, etc. You don’t have to know how it works, all you have to do is say what you want, and is if by magic it all comes together. Being a multi-paradigm programmer, this got me thinking, “If I’m just declaring stuff, why not use a declarative language?”.

This brought me to SWI-Prolog. Despite Prolog being an older language, unfortunately SWI-Prolog is not as mature as Python for web development, I can’t pack install something equivalent to Flask-RESTful, or Flask-Security. But it does have quite a web development story of it’s own, and it’s perfectly possible to create great websites using it. Cliopatria and Pengines are unparalleled by anything in Python. If you’d like to do web development in Prolog that’s not Simple, I’d highly recommend this tutorial from Anne.

pack_install(simple_web).

Lowering The Barrier To Entry

STARTING OUT with web development in SWI-Prolog can be a little confusing, not because the logic or programming is difficult, but because the imports are all over the place. It’s hard to remember which library module you need to import for which predicate. Plus some predicates start http_, and others skip this. This got me thinking, “Wouldn’t it be wonderful if I could just make a single import and get everything I need? Wouldn’t it also be wonderful if all reply predicates started reply_?”

Thus Simple Web was born. There’s not much cleverness to it, it’s heavily reliant on SWI-Prolog’s existing web packs and libraries, but I do believe it is much easier to learn and work with. For the most part complexity has been reduced by removing options you’re unlikely to use, and by making some decisions for you. It expects static to be in one directory and templates in another. You’re expected to use dictionaries for working with JSON, and Simple Template is favoured over other methods for generating HTML in Prolog. Sure you can customise it and extend it too, there’s no reason you can’t use whatever you want with it, but not everything is built in to it.

With Simple Web you can make one import and have all the predicates you need to make the average website.

:- use_module(library(sw/simple_web))

You can reply with a file, json, use a template, a string of html, termarized html, redirect and 404. Making an API is wonderfully simple too, so is using it as a server for a single page application.

What Does It Look Like?

BEST PLACE to see is a repository of examples I made to show it off. There you’ll see how to use templates, and static, but also examples with Vue.js and Bootstrap. I think you’ll find it very easy to use, and surprisingly similar to frameworks in other languages, which have ended up being basically declarative.

:- use_module(library(sw/simple_web)).

sw:route('/', _Request) :-
    blog_titles(Titles),
    reply_template(bootstrap_base,
                   data{ content: blog,
                         titles: Titles
                       }
                  ).

The Future?

PROLOG IS a pretty niche language, but it is very much suited to getting websites developed quickly. There’s areas that need attention, work is ongoing on [Simple Templates], and other extensions are emerging like Simple Bootstrap. More extension packs could easily be created to add functionality in a simple manner for the user. Currently Simple Web is using SWI-Prolog’s http_dispatch as its server, however this could use a little attention too as it is a little dated. There’s definitely progress to be made and code to write.

One great advantage of SWI-Prolog is if we have a time consuming task, it’s relatively easy to code that in C and expose it to SWI-Prolog as a predicate. This means, potentially, we could take advantage of the best minds in the business to get optimum performance, whilst letting the rest of us just write Prolog. I’d like to see this exploited. Plus, SQL injection isn’t exactly going to work on a Prolog database!

The disadvantage of Prolog is that it is still considered niche, with not so many developers working with it. At the end of the day, the success of Simple Web will depend on how much attention it garners, and how many developers wish to contribute. I’ve started the ball rolling, but I have neither the time or expertise to manage every task from server, to interface, to templates, to security, etc. Web development has become primarily declarative in nature, so why not use a declarative language?

Post Tags


All Tags



LIFE IS better when we share.


Comments

JOIN THE conversation: awesome comments.