# From: "Dat Nguyen" <thucdat@hotmail.com>
# Subject: Logic Programming in C-Kermit
# Date: Sat, 08 May 2004 15:36:29 -0400
#
# Patterns and Inference Engines.
#
define symmetric {
    if > \find({\%2},\m(\%1.contain)) 0 {
        echo Relation \%1 \%2 already defined
        return 1
    }
    local \&z[]
    void \fsplit(\%2,&z,{,})
    if > \farraylook(who,&z) -1 {
        echo Cannot relate with who
        return 0
    }
    _define \%1 1
    _asg \%1.contain \m(\%1.contain)|\%2|
    return 1
}

define prove {
    if not define \m(\%1) {
        echo Relation \%1 does not exist
        return 0
    }
    local \%n \&w[] \&z[]
    asg \%n \fsplit(\m(\%1.contain),&w,|)
    void \fsplit(\%2,&z,{,})
    if > \farraylook(\%2,&w) -1 {
        echo Yes
        return 1
    } else if > \farraylook({\&z[2],\&z[1]},&w) -1 {
        echo Yes
        return 1
    } else if > \find(\&z[2],who) 0 {
        local \%f \%i
        asg \%f 0
        for \%i 1 \%n 1 {
            if > \find(\&z[1],\&w[\%i]) 0 {
                echo Who = \freplace(\freplace(\&w[\%i],\&z[1]),{,})
                asg \%f 1
            }
        }
        return \%f
    } else {
        echo No
        return 0
    }
}

###############################################################################

# In a typical soccer tournament, the following matches are usual:
symmetric playsoccer Brazil,Italy
symmetric playsoccer Germany,Italy
symmetric playsoccer England,Brazil
symmetric playsoccer England,Germany
symmetric playsoccer Italy,Germany
symmetric playsoccer Holland,Germany
symmetric playsoccer France,Italy
symmetric playsoccer England,France

# In a chess tournament, the following encounters are possible:
symmetric playchess Karpov,Kasparov
symmetric playchess Leko,Kasparov
symmetric playchess Leko,Kramnik
symmetric playchess Karpov,Kramnik
symmetric playchess Annand,Kramnik
symmetric playchess Leko,Karpov
symmetric playchess Ponomariov,Karpov
symmetric playchess Polga,Karpov
symmetric playchess Leko,Polga

# In a party, the following pairs would happen:
symmetric danceWith Jane,Robert
symmetric danceWith Jennifer,Mark
symmetric danceWith Mary,John
symmetric danceWith Mary,Robert
symmetric danceWith Suzie,George
symmetric danceWith Kevin,Carol
symmetric danceWith Mark,Carol
