dvberkel / microkanren / MicroKanren.Kernel

μKanren provides an implementation of the

minimalist language in the miniKanren family of relational (logic) programming languages.

Types


type alias Goal a =
State a -> Stream a

a μKanren program proceeds through the application of a goal to a state.

Goals are often understood by analogy to predicates. Whereas the application of a predicate to an element of its domain can be either true or false, a goal pursued in a given state can either succeed or fail . When it succeeds it returns a non-empty stream, otherwise it fails and returns an empty stream.


type alias State a =
{ substitution : Substitution a
, fresh : Var 
}

A state is a pair of a substitution (represented as an association list) and a non-negative integer representing a fresh-variable counter.


type Stream a
    = Empty
    | Immature (() -> Stream a)
    | Mature (State a) (Stream a)

A sequences of states is a stream.

A goal's success may result in a sequence of (enlarged) states, which we term a stream. The result of a μKanren program is a stream of satisfying states. The stream may be finite or infinite, as there may be finite or infinitely many satisfying states


type Term a
    = Variable Var
    | Value a
    | Pair (( Term a, Term a ))

Terms of the language consist of variables, objects deemed identical under eqv? , and pairs of the foregoing.


type alias Var =
Basics.Int

Variable indices are identified by integers.


type alias Substitution a =
Dict Var (Term a)

A substitution binds variables to terms.

Goal Constuctors

callFresh : (Term a -> Goal a) -> Goal a

The call/fresh goal constructor takes a unary function f whose body is a goal, and itself returns a goal.

This returned goal, when provided a state s/c , binds the formal parameter of f to a new logic variable (built with the variable constructor operator var ), and passes a state, with the substitution it originally received and a newly incremented fresh-variable counter, c , to the goal that is the body of f.

conjoin : Goal a -> Goal a -> Goal a

The conj goal constructor similarly takes two goals as arguments and returns a goal that succeeds if both goals succeed for that state.

disjoin : Goal a -> Goal a -> Goal a

The disj goal constructor takes two goals as arguments and returns a goal that succeeds if either of the two subgoals succeed.

identical : Term a -> Term a -> Goal a

≡ takes two terms as arguments and returns a goal that succeeds if those two terms unify in the received state.

Constructors

emptyState : State a

The empty state is a common starting point for many μKanren programs.

While in principle the user of the system may begin with any state, in practice the user almost always begins with empty-state . empty-state is a user-level alias for a state virtually devoid of information: the substitution is empty, and the first variable will be indexed at 0.

unit : State a -> Stream a

unit lifts the state into a stream whose only element is that state.

Accessors

walk : Term a -> Substitution a -> Term a

The walk operator searches for a variable's value in the substitution.