Skip to content

Commit

Permalink
* README.md: space we need space
Browse files Browse the repository at this point in the history
  • Loading branch information
swannodette committed Apr 29, 2011
1 parent a93cdc9 commit aefa4c0
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions README.md
Expand Up @@ -11,19 +11,19 @@ ____
Ok, we’re ready to being. Type lein repl or cake repl, this will drop you into the Clojure prompt. First lets double check that everything went ok. Enter the following at the Clojure REPL:

```clj
user=>(require 'clojure.core.logic.minikanren)
user=> (require 'clojure.core.logic.minikanren)
```

The REPL should print nil and it should return control to you. If it doesn’t file an issue for this tutorial and I’ll look into it. If all goes well run the following:

```clj
user=>(load "logic_tutorial/tut1")
user=> (load "logic_tutorial/tut1")
```

You’ll see some harmless warning, then run the following:

```clj
user=>(in-ns 'logic-tutorial.tut1)
user=> (in-ns 'logic-tutorial.tut1)
```

Your prompt will change and you’re now working in a place that has the magic of logic programming available to you. It will show logic-tutorial.tut1, we're going show tut1 to keep things a bit more readable.
Expand All @@ -34,93 +34,93 @@ Baby Steps
Unlike most programming systems, with relational programming we can actually ask the computer questions. But before we ask the computer questions, we need define some facts! The first thing we want the computer to know about is that there are men:

```clj
tut1=>(defrel man x)
tut1=> (defrel man x)
#'tut1/man
```

And then we want to define some men:

```clj
tut1=>(fact man 'Bob)
tut1=> (fact man 'Bob)
nil
tut1=>(fact man 'John)
tut1=> (fact man 'John)
nil
```

No we can ask who are men. First we have to formulate a question and then tell the computer we want the computer to find answers to our question:

```clj
tut1=> (run 1 [q] (man q))
tut1=> (run 1 [q] (man q))
(John)
```

We’re asking the computer to give us at least one answer to the question - “Who is a man?”. We can ask for more than one answer:

```clj
tut1=>(run 2 [q] (man q))
tut1=> (run 2 [q] (man q))
(John Bob)
```

Now that is pretty cool. What happens if we ask for more answers?

```clj
tut1=>(run 3 [q] (man q))
tut1=> (run 3 [q] (man q))
(John Bob)
```

The same result. That’s because we’ve only told the computer that two men exist in the world. It can’t give results for things it doesn’t know about. Let’s define another kind of relationship and a fact:

```clj
tut1=>(defrel fun x)
tut1=> (defrel fun x)
#'tut1/fun
tut1=>(fact fun 'Bob)
tut1=> (fact fun 'Bob)
nil
```

Let’s ask a new kind of question:

```clj
tut1=>(run* [q] (man q) (fun q))
tut1=> (run* [q] (man q) (fun q))
(Bob)
```

There’s a couple of new things going on here. We’re asking who is both a man and who is fun. Now this getting interesting. Enter in the following:

```clj
tut1=>(defrel woman x)
tut1=> (defrel woman x)
#'tut1/woman
tut1=>(fact woman 'Lucy)
tut1=> (fact woman 'Lucy)
nil
tut1=>(fact woman 'Mary)
tut1=> (fact woman 'Mary)
nil
tut1=>(defrel likes x y)
tut1=> (defrel likes x y)
#'tut1/likes
```

Relations don’t have to be a about a single entity. We can define relationship between things!

```clj
tut1=>(fact likes 'Bob 'Mary)
tut1=> (fact likes 'Bob 'Mary)
nil
tut1=>(fact likes 'John 'Lucy)
tut1=> (fact likes 'John 'Lucy)
nil
tut1=>(run* [q] (likes 'Bob q))
tut1=> (run* [q] (likes 'Bob q))
(Mary)
```

We can now ask who likes who! Let's try this:

```clj
tut1=>(run* [q] (likes 'Mary q))
tut1=> (run* [q] (likes 'Mary q))
()
```

Hmm that doesn’t work. This is because we never actually said who Mary liked, only that Bob liked Mary:

```clj
tut1=>(fact likes 'Mary 'Bob)
tut1=> (fact likes 'Mary 'Bob)
nil
tut1=>(run* [q] (exist [x y] (== q [x y]) (likes x y) ))
tut1=> (run* [q] (exist [x y] (== q [x y]) (likes x y) ))
([Bob Mary] [John Lucy])
```

Expand All @@ -129,7 +129,7 @@ Wow that’s a lot of new information. The exist expression isn’t something we
I’ve done a lot of lying in the last paragraph. Run the following:

```clj
tut1=>(run* [q] (exist [x y] (likes x y) (== q [x y])))
tut1=> (run* [q] (exist [x y] (likes x y) (== q [x y])))
([Bob Mary] [John Lucy])
```

Expand All @@ -141,25 +141,25 @@ Genealogy
We’ve actually defined some interesting relations in this namespace that we’ll use before we take a closer look at them:

```clj
tut1=>(fact parent 'John 'Bobby)
tut1=> (fact parent 'John 'Bobby)
nil
tut1=>(fact male 'Bobby)
tut1=> (fact male 'Bobby)
nil
```

We can now run this query:

```clj
tut1=>(run* [q] (son q 'John))
tut1=> (run* [q] (son q 'John))
(Bobby)
```

Let’s add another fact:

```clj
tut1=>(fact parent 'George 'John)
tut1=> (fact parent 'George 'John)
nil
tut1=>(run* [q] (grandparent q 'Bobby))
tut1=> (run* [q] (grandparent q 'Bobby))
(George)
```

Expand Down

0 comments on commit aefa4c0

Please sign in to comment.