Reasoning with Collections

This example demonstrates some facilities for defining concepts via enumerated collections and how to reason with them.

    Welcome to PowerLoom 3.0.1.beta

Copyright (C) USC Information Sciences Institute, 1997-2003.
PowerLoom is a registered trademark of the University of Southern California.
PowerLoom comes with ABSOLUTELY NO WARRANTY!
Type `(copyright)' for detailed copyright information.
Type `(help)' for a list of available commands.
Type `(demo)' for a list of example applications.
Type `bye', `exit', `halt', `quit', or `stop', to exit.


|= (demo 3)

Now reading from `PL:sources;logic;demos;collections.ste'.
Type `?' at the pause prompt for a list of available commands.

;;; -*- Mode: Lisp; Package: STELLA; Syntax: COMMON-LISP; Base: 10 -*-

;;; Version: collections.ste,v 1.11 2003/03/31 23:18:19 hans Exp

;;; Reasoning with collections
;;; ==========================

;;; This file demonstrates some facilities for defining concepts via
;;; enumerated collections and how to reason with them.

;;; The best way to view this file is by calling `(demo)' and
;;; selecting it from the menu of example demos.  This demo assumes
;;; familiarity with some basic PowerLoom concepts which are described
;;; in the introductory demo (#1 on the demo menu) and other demos
;;; preceding this one.


;; Standard demo preamble:

|= (in-package "STELLA")
------ pause ------c


|= (defmodule "PL-USER/COLLECTIONS")

|MDL|/PL-KERNEL-KB/PL-USER/COLLECTIONS

|= (in-module "COLLECTIONS")


|= (clear-module "COLLECTIONS")


|= (reset-features)

|l|(:EMIT-THINKING-DOTS :JUST-IN-TIME-INFERENCE)

|= (in-dialect KIF)

:KIF

;; Let's start by defining a concept `Color'.  The idiom below
;; defines the concept in terms of an enumerated set using the
;; built-in `setof' function.  `setof' takes an arbitrary number
;; of arguments and generates a term representing the enumerated
;; set.  

|= (defconcept Color (?c)
  :<=> (member-of ?c (setof red orange yellow green blue purple)))

|c|COLOR

;; Again, the :<=> syntax is nothing special and just a syntactic shortcut
;; for specifying an equivalence between a concept (or relation) and
;; a defining logical expression.  Any concept or relation definition
;; of the form
;;
;;    (def... <name> (<var>+) :<=> <sentence ... <var> ...>)
;;
;; is simply interpreted as the following two-step definition:
;;
;;    (def... <name> (<var>+))
;;    (forall (<var>+)
;;       (<=> (<name> <var>+)
;;            <sentence ... <var> ...>))
;;
;; The definition above is therefore equivalent to the following:

|= (defconcept Color)

Redefining the `concept' named `COLOR'
|c|COLOR

|= (assert
  (forall (?c)
    (<=> (Color ?c)
         (member-of ?c (setof red orange yellow green blue purple)))))

(|P|(FORALL (?c)
   (<= (EXISTS (?v07)
          (AND (= (SETOF RED ORANGE YELLOW GREEN BLUE PURPLE) ?v07)
               (MEMBER-OF ?c ?v07)))
       (COLOR ?c))) |P|(FORALL (?c)
   (<= (COLOR ?c)
       (EXISTS (?v08)
          (AND (= (SETOF RED ORANGE YELLOW GREEN BLUE PURPLE) ?v08)
               (MEMBER-OF ?c ?v08))))))

;; Next we define two subconcepts (or subsets) of `Color':

|= (defconcept Primary-Color (?c Color)
  :<=> (member-of ?c (setof red yellow blue)))

|c|PRIMARY-COLOR

|= (defconcept Alliance-Color (?c Color)
  :<=> (member-of ?c (setof red blue)))

|c|ALLIANCE-COLOR

;; Above we explicitly specified `Color' as a superconcept of the two
;; subsets (via the variable type).  However, even without that
;; PowerLoom can determine the subset relationship by using a
;; subsumption test (at the expense of some extra CPU cycles).  For
;; example:

|= (defconcept Traffic-Light-Color (?c)
  :<=> (member-of ?c (setof red orange green)))

|c|TRAFFIC-LIGHT-COLOR

|= (ask (subset-of Traffic-Light-Color Color))

TRUE

;; Now let's run some queries:

|= (ask (Color green))

TRUE

;; Retrieving various sets of colors:

|= (retrieve all (Color ?x))

There are 6 solutions:
  #1: ?X=GREEN
  #2: ?X=ORANGE
  #3: ?X=RED
  #4: ?X=YELLOW
  #5: ?X=BLUE
  #6: ?X=PURPLE

|= (retrieve all (and (Color ?x) (= ?x green)))

Processing check-types agenda...
WARNING: Type check violation on argument `@GREEN' in proposition
   (= ?x//GREEN GREEN).
   Argument must have type `COLOR'.
   Warning occurred while parsing the proposition: 
   (KAPPA (?X) (AND (COLOR ?X) (= ?X @GREEN)))
There is 1 solution:
  #1: ?X=GREEN

|= (retrieve all (Primary-Color ?x))

There are 3 solutions:
  #1: ?X=RED
  #2: ?X=YELLOW
  #3: ?X=BLUE

;; Since `Primary-Color' was defined via an explicit enumeration of
;; its members, PowerLoom can determine that it is a closed
;; collection, i.e., for any color it can determine whether it is a
;; member of the set (or concept) or not.  This is different from the
;; usual open-world assumption where non-membership is often simply
;; unknown.  For example:

|= (ask (not (Primary-Color orange)))

TRUE

|= (ask (not (member-of orange primary-color)))

TRUE

;; Retrieve the relative complement of `Primary-Color'.  Here it is
;; important to specify `Color' as a restriction, since PowerLoom will
;; not attempt to generate the infinite set of things that are not
;; `Primary-Color's:

|= (retrieve all (and (Color ?x)
                   (not (Primary-Color ?x))))

There are 3 solutions:
  #1: ?X=GREEN
  #2: ?X=ORANGE
  #3: ?X=PURPLE

;; Simple membership tests:

|= (ask (member-of blue (setof blue yellow)))

TRUE

|= (ask (member-of green (setof blue yellow)))

FALSE

|= (ask (not (member-of blue (setof blue yellow))))

FALSE

|= (ask (not (member-of green (setof blue yellow))))

TRUE

;; Simple subset tests:

|= (ask (subset-of (setof blue yellow) (setof blue yellow purple)))

TRUE

|= (ask (subset-of Alliance-Color Primary-Color))

TRUE

;; More subset-of tests that mainly demonstrate various syntactic
;; variations for specifying the set of primary colors:

|= (ask (subset-of (setof red blue) Primary-Color))

TRUE

|= (ask (subset-of (setof red blue) (setofall ?x (Primary-Color ?x))))

TRUE

|= (ask (subset-of (setof red blue) (setofall (?x Primary-Color))))

TRUE

|= (ask (subset-of (setof red blue) (setofall (?x Primary-Color) TRUE)))

TRUE

;; Subset-of tests involving complements:

|= (ask (subset-of (setofall (?x Color) (not (Primary-Color ?x))) Color))

TRUE

|= (ask (subset-of (setofall (?x Color) (not (Primary-Color ?x)))
                (setofall (?x Color) (not (Alliance-Color ?x)))))

TRUE

;; Non-subset-of tests:

|= (ask (not (subset-of Alliance-Color Primary-Color)))

UNKNOWN

|= (ask (not (subset-of Primary-Color Alliance-Color)))

TRUE

|= (ask (not (subset-of (setofall (?x Color) (not (Primary-Color ?x)))
                     (setofall (?x Color) (not (Alliance-Color ?x))))))

UNKNOWN

|= (ask (not (subset-of (setofall (?x Color) (not (Alliance-Color ?x)))
                     (setofall (?x Color) (not (Primary-Color ?x))))))

TRUE

|= 

Finished demo `PL:sources;logic;demos;collections.ste'.

|= 

Information Sciences Institute PowerLoom Home Page
PowerLoom is a registered trademark of the University of Southern California.
Last modified: May 27, 2006