Code Monkey home page Code Monkey logo

jolog's Introduction

Synopsis

:- use_module(library(jolog)).
% Prolog runs this predicate automatically
main :-
    writeln('Starting Jolog'),
    % start Jolog, block until it's done
    start_jolog(user).

% Jolog runs this pattern when it starts
main &-
    writeln('In Jolog main'),
    then,
    ( send(hello)  % in one parallel process
    & sleep(1),    % in another parallel process
      send(world)
    ).

% Once there are messages on both the 'hello' and 'world' channels
world, hello &-
    writeln('Hello, World!').

which generates the following output with a small delay between the second and third lines.

Starting Jolog
In Jolog main
Hello, World!

Description

WARNING: As suggested by the version number, this is early alpha software. APIs and behavior are likely to change.

Jolog is an implementation of join patterns for Prolog that's inspired by JoCaml. Join patterns provide a clean, powerful way of thinking about concurrent and parallel programming.

Jolog clauses are defined using the &-/2 operator. Use it just like the :-/2 operator. The clause's head contains a join pattern. The body contains processes and optional guards.

Join Patterns

A join pattern is typically a conjunction (using ,/2) of smaller patterns. Each small pattern is a term. The term's name indicates a channel. The term's arguments indicate the content of a message on that channel. As you'd expect, patterns are matched via standard Prolog unification.

So if our Jolog system has the following outstanding messages:

  • temperature(75)
  • humidity(50)
  • air(overcast, windy)

then all the following join patterns would match

  • temperature(75)
  • temperature(T) - binds T to 75
  • air(overcast,windy), humidity(H) - binds H to 50

Guards

If a Jolog clause body contains the goal then/0, all goals occuring before then/0 are considered guards. The guards typically examine those bindings created by the head's join patterns. All guard goals must succeed before the matched messages are consumed.

For example, imagine we're programming a vending machine. The predicate price(+Item, -Cost) tells us how much it costs to purchase a particular item. The channel balance/1 receives messages indicating how much money has been deposited into the vending machine. The channel selected/1 receives a message when a customer presses an item selection button on the vending machine. We only want to dispense an item if the balance exceeds the selected item's price.

balance(Bal), selected(Item) &-
    price(Item, Cost),
    Bal >= Cost,
    then,
    ...

Before the elided code (...) starts executing, the balance/1 and selected/1 messages are consumed and no longer available to other Jolog clauses.

If there is no then/0 goal, it's the same as if the guard had been true.

Processes

Processes are concurrent computations. They're executed after a join pattern matches and all associated guards succeed. They're inexpensive to create, so don't worry about making thousands of them if you need to.

A process can be any code that'd you pass to call/1. The simplest process is just a goal. For example, in the world, hello pattern in the Synopsis above, the process is writeln('Hello, World!'). That goal is executed concurrently and the enclosing Jolog clause doesn't wait for it to complete.

It's often convenient to create multiple processes from a single Jolog clause. That's done with the &/2 operator. Use this operator just as you would use ;/2. For example the main Jolog clause in the Synopsis creates two parallel processes.

Additional examples are available.

Messages

Messages are consumed via join patterns (described above). They're created by calling send/1. You can send any term as a message, but you'll get a warning if no Jolog clauses are listening for that message.

Changes in this Version

See History.md file.

Installation

Using SWI-Prolog 6.3 or later:

?- pack_install(jolog).

This module uses semantic versioning.

Source code available and pull requests accepted at http://github.com/mndrix/jolog

jolog's People

Contributors

mndrix avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

samer-- erlanger

jolog's Issues

lazily create worker threads

Jolog currently spawns CpuCount*2 threads on startup. For many programs, we don't need that many threads so starting them is wasteful. Instead, start with one worker thread. Each time the manager thread prepares to block (because it's outpaced the workers), start a new thread. At most CpuCount*2 workers should ever run.

There are many more optimizations we could make to tune worker count based on CPU usage and blocking IO usage, but this small adjustment is easy and should help quite a bit.

Synchronous channels

JoCaml also provides synchronous channels, which would be useful in Jolog as well. I guess the ideal interface would be to communicate the results back via variables - so I naively tried to sendsomething like get_result(X) then assign value(s) to X in the body of jolog clause. However as messages are sent using queues and those copy terms, the reference to original variable is lost. I guess something like https://github.com/mndrix/spawn could be used?

@mndrix Do you have any thoughts about this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.