# User:Rsimmons/Lecture 20: Logic Programming

 This is Literate TwelfCode: hereStatus: %% OK %% Output: here.

In this lecture, Karl uses Prolog notation. I'm going to skip a step and go ahead and start using Twelf, since I've got all this infrastructure set up to incorporate pretty Twelf into web pages. For the purpose of this exercise, this only means that we have to declare types for things and that we write connect a b instead of connect(a,b) for facts representing a connection from a to b, and we say D <- A <- B <- C instead of D :- A, B, C for a three-premise rule.

You'll notice there's a box on the upper-right hand corner that calls this page "Literate Twelf." This is related to the idea of Literate Programming - this wiki page is itself a page of valid Twelf code.

If you'll forgive the ugly illustration, we're interesting in answering questions about rechability on this acyclic graph (we want it to be an acyclic graph!)

``` ```

``` -> b <- / \ / \ ```

```a---------->c----->d ```

```node : type.
a : node.
b : node.
c : node.
d : node.

connect : node -> node -> type.
reaches : node -> node -> type.

connect/a-b : connect a b.
connect/a-c : connect a c.
connect/c-b : connect c b.
connect/c-d : connect c d.

reach/refl  : {X: node} reaches X X.
reach/trans : {X: node} {Y: node} {Z: node} reaches Y Z -> connect X Y -> reaches X Z.```

NOTE: You don't have to write the Π bound things, so you could have used:

ERROR: option 'name' deprecated or invalid
```reach/refl  : reaches X X.
reach/trans : reaches Y Z -> connect X Y -> reaches X Z.```

(Twelf assumes you meant to Π-bind capitalized things but were too lazy to write the Π, so it does it for you.

I can ask the engine to ask questions about reachability, which will essentially perform a depth-first search on the graph.

I can confirm that there is one way through the graph from a to d (the notation %query 1 * isn't important for you to remember, but it means that I want one answer no matter how many times I try)

`%query 1 * reaches a d.`
```Twelf 1.7.1+ (r1896, built 05/05/15 at 12:56:43 on yazoo.plparty.org)
%query 1 *

reaches a d.
---------- Solution 1 ----------
Empty Substitution.
---------- Solution 2 ----------
Empty Substitution.
---------- Solution 3 ----------
Empty Substitution.
---------- Solution 4 ----------
Empty Substitution.
---------- Solution 5 ----------
Empty Substitution.
---------- Solution 6 ----------
Empty Substitution.
---------- Solution 7 ----------
Empty Substitution.
---------- Solution 8 ----------
Empty Substitution.
Error:
Query error -- wrong number of solutions: expected 1 in * tries, but found 8

%% ABORT %%```

...I can ask the engine to verify that there are no paths through the graph from b to a:

`%query 0 * reaches b a.`
```Twelf 1.7.1+ (r1896, built 05/05/15 at 12:56:43 on yazoo.plparty.org)
Error:
Query error -- wrong number of solutions: expected 1 in * tries, but found 8
%% ABORT %%
%% OK %%
%query 0 *

reaches b a.
____________________________________________

%% OK %%```

...finally, I can use a capital letter (metavariable or substitution variable) Z to ask, what is EVERY path, starting from a, in the graph? Turns out there are 5 (b can be reached in two different ways):

`%query 5 * reaches a Z.`
```Twelf 1.7.1+ (r1896, built 05/05/15 at 12:56:43 on yazoo.plparty.org)
Error:
Query error -- wrong number of solutions: expected 1 in * tries, but found 8
%% ABORT %%
%% OK %%
%query 5 *

reaches a Z.
---------- Solution 1 ----------
Z = a.
---------- Solution 2 ----------
Z = b.
---------- Solution 3 ----------
Z = b.
---------- Solution 4 ----------
Z = c.
---------- Solution 5 ----------
Z = b.
---------- Solution 6 ----------
Z = b.
---------- Solution 7 ----------
Z = d.
---------- Solution 8 ----------
Z = d.
---------- Solution 9 ----------
Z = c.
---------- Solution 10 ----------
Z = b.
---------- Solution 11 ----------
Z = b.
---------- Solution 12 ----------
Z = d.
---------- Solution 13 ----------
Z = d.
---------- Solution 14 ----------
Z = a.
---------- Solution 15 ----------
Z = b.
---------- Solution 16 ----------
Z = b.
---------- Solution 17 ----------
Z = c.
---------- Solution 18 ----------
Z = b.
---------- Solution 19 ----------
Z = b.
---------- Solution 20 ----------
Z = d.
---------- Solution 21 ----------
Z = d.
---------- Solution 22 ----------
Z = c.
---------- Solution 23 ----------
Z = b.
---------- Solution 24 ----------
Z = b.
---------- Solution 25 ----------
Z = d.
---------- Solution 26 ----------
Z = d.
Error:
Query error -- wrong number of solutions: expected 5 in * tries, but found 26

%% ABORT %%```

# The LF Logical Framework

## Syntax   ## Typing Rules

### Kinds

Kinds K classify types A.  ### Types

Types A classify terms M and are classified by kinds K.    ### Terms

Terms M are classified by types A.   