Answer Set Programming with clasp and glingo

Answer Set Programming with clasp and glingo 
by Forrest Sheng Bao @ Dept. of Computer Science, Texas Tech University

This document is licensed under Creative Commons BY-NC-SA United States License 3.0 http://creativecommons.org/licenses/by-nc-sa/3.0/us/


$version 0.2 $  $Last update: Nov. 10, 2011 $
The $2.56: If you find an error in the doc or you contribute a useful comment, I will buy you an ice cream - limited in US continental states.

Introduction:Answer Set Programming (ASP) is a ``new'' programming paradigm under the banner of logic programming. Here, by ASP, we also mean a programming language. ASP extends the classical logic programming language Prolog to support a cool feature called non-monotonicity (we will see later). In this tutorial, we will see how to solve an ASP program using clasp and glingo. glingo parses and grounds your ASP program into an intermediate code on which clasp solve to find the answer set(s). We call glingo the grounder and clasp the solver

Step 1: Installing clasp and gringo

Step 2: Configurations
  • I suggest to change file names of binaries into ``easy names,'' such as claps from clasp-1.1.0.
  • On Linux/Mac/Solaris, edit PATH environmental variable. I simply put the line below to the end of my ~/.bashrc
    export PATH=$PATH:the_PATH_TO_My_BINARIES.
    Replace the_PATH_TO_My_BINARIES by actual path, such as /home/forrest/bin 
  • After that, run
    source ~/.bashrc
    or, simply log out your session and then log in.
  • I don't provide support for Windows platform. 
  • Now you should be able to execute commands clasp and gringo from your command line interface. 

Step 3 (optional): Preparing your ASP program
  • Now we write a very simple ASP program. Please save the following lines as program.lp
        fly(X) :- bird(X), not -fly(X).  % X can fly if X is a bird and we do not know that X cannot fly. 
        bird(twittie).                   % twittie is a bird.
        -fly(X) :- penguin(X).           % X cannot fly if X is a penguin.
  • Please note that ASP follows the syntax of Prolog that variables begin with upper case letters and constants begin with lower case letters. 

Step 4: Solving your ASP program
  • You can now solve your ASP program as shown on clasp download webpage http://www.cs.uni-potsdam.de/clasp/?page=downloads
  • This is what happened on my Linux prompt. 
        forrest@myMac:/forrest/$ gringo program.lp | clasp
        clasp version 1.3.6
        Reading from stdin
        Solving...
        Answer: 1
        bird(twittie) fly(twittie) 
        SATISFIABLE

        Models      : 1     
        Time        : 0.000s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
    I highlighted the answer set part. So Twittie is a bird and thus it can fly. 

  • (optional) Experience the non-monotonic reasoning in ASP: Now add the line below to the end of the program.
          penguin(twittie).   % Twittie is a penguin. 
    Re-solve and check the output. You will see -fly(twittie) in the Answer segment. Now twittie CANNOT fly any more! Very reasonable, because we know it's a penguin now. 
    Here you can see that more knowledge (i.e., knowing Twittie is a penguin.) does not increase the conclusion you can infer - you do not have fly(twittie) any more. This is what I mean by ``non-monotonicity'' earlier. Indeed, this allows you to revise your belief easily whereas this cannot be done easily in classical logic, which is monotonic. 
  • (optional) Macro tricks. There many tricks to play in your ASP program. Some tricks are used in my sample program in the appendix. 
    • hide: For example, your answer set may contain many literals, and you do not wanna see all of them. Then you can use the #hide to hide all of them. The pad sign in not required.
    • show: But you wanna see something. For example, you wanna see all literals built from the predicate occurs, then you can put this line in your program, #show occurs(A,B). The pad sign in not required.
    • A full list of tricks is in Section 4.1.2 of Clingo guide.pdf

Comments and questions are welcomed. I know many people do not use logic programming often. So, if you feel confused, please free feel to let me know and i will add more explanations. 

2 comments:

fleiv said...

Hi hello how did you compile clasp and gringo for mac? if so what do I need?

thanks

Ivan said...

At the beginning of the document you have Intorduction, instead of Introduction. You own me an ice cream, unfortunately I'm in Australia.