dwayne/elm-trs2/1.0.0

Logic

A high-level API that brings together the Logic.* submodules into a cohesive module.

Value

type alias Value a =

A rich default set of values that can be associated with (logic) variables.

type Constant a
= Int Int
| Float Float
| Bool Bool
| Char Char
| String String
| Custom a

Work with integers, floats, booleans, characters, and strings out of the box. There's also the possibility to bring in your own custom types.

int : Int -> Value a

Create an integer value.

float : Float -> Value a

Create a floating-point value.

bool : Bool -> Value a

Create a boolean value.

char : Char -> Value a

Create a character value.

string : String -> Value a

Create a string value.

custom : a -> Value a

Create a value out of your custom type.

toConstant : Value a -> Maybe (Constant a)

Convert a value to a constant, if possible.

toConstant (int 5) == Just (Int 5)


toConstant null == Nothing
null : Value a

The empty list value.

cons : Value a -> Value a -> Value a

Create a dotted pair of any two values.

list : List (Value a) -> Value a

Create a proper list of zero or more values.

list [] == null


list [ int 1 ] == cons (int 1) null


list [ int 1, bool True ] == cons (int 1) (cons (bool True) null)


list [ int 1, bool True, char 'a' ] == cons (int 1) (cons (bool True) (cons (char 'a') null))
dottedList : Value a -> List (Value a) -> Value a -> Value a

Create an improper list of two or more values.

dottedList (int 1) [] (bool True) == cons (int 1) (bool True)


dottedList (int 1) [ bool True ] (char 'a') == cons (int 1) (cons (bool True) (char 'a'))
car : Value a -> Maybe (Value a)

Get the first value from a dotted pair, if possible.

car (cons x y) == Just x


car null == Nothing


car (string "(cons x y)") == Nothing
cdr : Value a -> Maybe (Value a)

Get the second value from a dotted pair, if possible.

cdr (cons x y) == Just y


cdr null == Nothing


cdr (string "(cons x y)") == Nothing
uncons : Value a -> Maybe ( Value a, Value a )

Get the first and second value from a dotted pair, if possible.

uncons (cons x y) == Just ( x, y )


uncons null == Nothing


uncons (string "(cons x y)") == Nothing

Goal

type alias Goal a =

Imbue the underlying Goal type with a richer set of constants.

See Logic.Goal for more details.

succeed : Goal a

A goal that succeeds.

fail : Goal a

A goal that fails.

always : Goal a

A goal that succeeds an unbounded number of times.

never : Goal a

A goal that neither succeeds nor fails.

Relational operators

equals : Value a -> Value a -> Goal a

Try to unify two values. It succeeds if unification is possible, and fails otherwise.

disj2 : Goal a -> Goal a -> Goal a

The disjunction of two goals. It succeeds if and only if either of the goals succeed.

disj : List (Goal a) -> Goal a

The disjunction of zero or more goals. It succeeds if and only if there is at least one goal that succeeds.

conj2 : Goal a -> Goal a -> Goal a

The conjunction of two goals. It succeeds if and only if both goals succeed.

conj : List (Goal a) -> Goal a

The conjunction of zero or more goals. It succeeds if and only if all the goals succeed.

conde : List (List (Goal a)) -> Goal a

The disjunction of conjunctions.

See Logic.Goal.conde for more details.

Define

lazy : (() -> Goal a) -> Goal a

Useful for defining recursive goals.

Lexically-scoped logic variables

fresh : (Value a -> Goal a) -> Goal a

Create a lexically-scoped logic variable.

fresh
    (\fruit ->
        equals (Const "plum") fruit
    )
fresh2 : (Value a -> Value a -> Goal a) -> Goal a

Create two lexicaly-scoped logic variables.

fresh3 : (Value a -> Value a -> Value a -> Goal a) -> Goal a

Create three lexicaly-scoped logic variables.

fresh4 : (Value a -> Value a -> Value a -> Value a -> Goal a) -> Goal a

Create four lexicaly-scoped logic variables.

fresh5 : (Value a -> Value a -> Value a -> Value a -> Value a -> Goal a) -> Goal a

Create five lexicaly-scoped logic variables.

(Value a -> Value a -> Value a -> Value a -> Value a -> Value a -> Goal a)
-> Goal a

Create six lexicaly-scoped logic variables.

( Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Goal a
)
-> Goal a

Create seven lexicaly-scoped logic variables.

( Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Goal a
)
-> Goal a

Create eight lexicaly-scoped logic variables.

Non-relational operators

See Logic.Goal for more details on these operators.

once : Goal a -> Goal a
conda : List (List (Goal a)) -> Goal a
condu : List (List (Goal a)) -> Goal a

Run

See Logic.Goal for more details on these runners.

run : (Value a -> Goal a) -> List (Value a)
run2 : (Value a -> Value a -> Goal a) -> List (Value a)
run3 : (Value a -> Value a -> Value a -> Goal a) -> List (Value a)
run4 : (Value a -> Value a -> Value a -> Value a -> Goal a) -> List (Value a)
(Value a -> Value a -> Value a -> Value a -> Value a -> Goal a)
-> List (Value a)
(Value a -> Value a -> Value a -> Value a -> Value a -> Value a -> Goal a)
-> List (Value a)
( Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Goal a
)
-> List (Value a)
( Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Goal a
)
-> List (Value a)
runAtMost : Int -> (Value a -> Goal a) -> List (Value a)
run2AtMost : Int -> (Value a -> Value a -> Goal a) -> List (Value a)
run3AtMost : Int -> (Value a -> Value a -> Value a -> Goal a) -> List (Value a)
Int
-> (Value a -> Value a -> Value a -> Value a -> Goal a)
-> List (Value a)
Int
-> (Value a -> Value a -> Value a -> Value a -> Value a -> Goal a)
-> List (Value a)
Int
-> (Value a -> Value a -> Value a -> Value a -> Value a -> Value a -> Goal a)
-> List (Value a)
Int
-> ( Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Goal a
)
-> List (Value a)
Int
-> ( Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Value a
-> Goal a
)
-> List (Value a)

Debug

toString : List (Value String) -> String

Convert to a representation that's useful for debugging and testing.

toString [ int 1, float 3.14, bool True, bool False, char 'c', string "apple", custom "banana" ] == "(1 3.14 #t #f c apple banana)"


toString [ null, cons (int 1) (int 2), list [ int 1 ], list [ int 1, bool True ] ] == "(() (1 . 2) (1) (1 #t))"


toString [ dottedList (int 1) [ bool True ] (char 'a') ] == "((1 #t . a))"

N.B. For convenience, it assumes your custom type is String. If your custom type is something else you can use customToString instead.

type Presenter a
= Default (a -> String)
| Alternative (Constant a -> String)

Used by customToString to determine how to represent your custom type.

With the Default constructor you get to use the default representation for the constants while providing a function to represent your custom type.

The default representation for the constants is as follows:

toString [ int 1 ] == "(1)"


toString [ float 3.14 ] == "(3.14)"


toString [ bool True ] == "(#t)"


toString [ bool False ] == "(#f)"


toString [ char 'a' ] == "(a)"


toString [ string "xyz" ] == "(xyz)"

If you want to change the default representation for the constants you can do that by using the Alternative constructor.

customToString : Presenter a -> List (Value a) -> String

Convert to a representation that's useful for debugging and testing.

Additionally, you get to determine how to represent your custom type.