User:Rsimmons/Lecture 20: Logic Programming

From The Twelf Project

Jump to: navigation, search
This is Literate Twelf
Code: here
Status: %% 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:

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.5R3, Aug 30, 2005 (%trustme)

%query 1 *

reaches a d. ---------- Solution 1 ---------- Empty Substitution. ____________________________________________

%% OK %%

...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.5R3, Aug 30, 2005 (%trustme)

%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.5R3, Aug 30, 2005 (%trustme)

%query 5 *

reaches a Z. ---------- Solution 1 ---------- Z = a. ---------- Solution 2 ---------- Z = b. ---------- Solution 3 ---------- Z = c. ---------- Solution 4 ---------- Z = b. ---------- Solution 5 ---------- Z = d. ____________________________________________

%% OK %%

The LF Logical Framework

Syntax


K ::= \texttt{type} \mid A \rightarrow K


A ::= \Pi x{:}A.A \mid \lambda x{:}A.A \mid A~M \mid \texttt{a}


M ::= x \mid \lambda x{:}A.M \mid M~M \mid \texttt{c}

Typing Rules

Kinds

Kinds K classify types A.



{}
\over
{\texttt{type} : \texttt{kind}}



{A : \texttt{type} \qquad
K : \texttt{kind}}
\over
{A \rightarrow K : \texttt{kind}}

Types

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



{A : \texttt{type}\qquad
x : A \vdash B : \texttt{type}}
\over
{\Pi x{:}A.B : \texttt{type}}



{A : \texttt{type} \qquad
x : A \vdash B : K}
\over
{\lambda x{:}A.B : A \rightarrow K}



{A : A' \rightarrow K \qquad
M : A'}
\over
{A~M : K}



{\texttt{a} : K \in \Sigma}
\over
{\texttt{a} : K}

Terms

Terms M are classified by types A.


{A : \texttt{type} \qquad
x : A \vdash M : A'}
\over
{\lambda x{:}A . M : \Pi x {:} A . A'}



{M_1 : \Pi x{:}A . A' \qquad
M_2 : A}
\over
{M_1~M_2 : A'[M_2/x]}



{\texttt{c} : A \in \Sigma}
\over
{\texttt{c} : A}

Personal tools