Search code examples
multithreadingfunctional-programmingprolognon-deterministic

Non-deterministic programming languages


I know in Prolog you can do something like

someFunction(List) :- 
    someOtherFunction(X, List)
    doSomethingWith(X)
    % and so on

This will not iterate over every element in List; instead, it will branch off into different "machines" (by using multiple threads, backtracking on a single thread, creating parallel universes or what have you), with a separate execution for every possible value of X that causes someOtherFunction(X, List) to return true!
(I have no idea how it does this, but that's not important to the question)

My question is: What other non-deterministic programming languages are out there? It seems like non-determinism is the simplest and most logical way to implement multi-threading in a language with immutable variables, but I've never seen this done before - Why isn't this technique more popular?


Solution

  • Prolog is actually deterministic—the order of evaluation is prescribed, and order matters.

    Why isn't nondeterminism more popular?

    Nondeterminism is unpopular because it makes it harder to reason about the outcomes of your programs, and truly nondeterministic executions (as opposed to semantics) are hard to implement.

    The only nondeterministic languages I'm aware of are

    • Dijkstra's calculus of guarded commands, which he wanted never to be implemented

    • Concurrent ML, in which communications may be synchronized nondeterministically

    • Gerard Holzmann's Promela language, which is the language of the model checker SPIN

    SPIN does actually use the nondeterminism and explores the entire state space when it can.

    And of course any multithreaded language behaves nondeterministically if the threads are not synchronized, but that's exactly the sort of thing that's difficult to reason about—and why it's so hard to implement efficient, correct lock-free data structures.

    Incidentally, if you are looking to achieve parallelism, you can achieve the same thing by a simple map function in a pure functional language like Haskell. There's a reason Google MapReduce is based on functional languages.