Properties of Relations

Below is the code we used in class. In the cslab, it can also be found in /cslab/class/cs243/relatprop.ml.

datatype Tribe = Asher | Naphtali | Zebulun | Issachar | Dan 
               | Gad | Manasseh | Reuben | Ephraim | Benjamin 
               | Judah | Simeon;


 datatype WaterBody = Mediterranean | Dead | Jordan 
                    | Chinnereth | Arnon | Jabbok;

fun isElementOf(x, []) = false 
   |  isElementOf(x, y::rest) = if x = y then true else isElementOf(x, rest);

fun isRelatedTo(a, b, relat) = isElementOf((a,b), relat);

fun isTransitive(relation) =
let 
  fun testOnePair((a,b), []) = true
     | testOnePair((a,b), (c, d)::rest) = 
       if b = c
       then isElementOf((a, d), relation) andalso testOnePair((a, b), rest)
       else testOnePair((a,b), rest);
  fun test([]) = true
     | test((a,b) :: rest) =  testOnePair((a,b), relation) andalso test(rest);
in
     test(relation)
end;




val waterWestOf = 
 [(Mediterranean, Chinnereth), (Mediterranean, Jordan), (Mediterranean, Dead),
  (Mediterranean, Jabbok), (Mediterranean, Arnon), (Chinnereth, Jabbok), 
  (Chinnereth, Arnon), (Jordan, Jabbok), (Jordan, Arnon), (Dead, Jabbok), 
  (Dead, Arnon)];

fun counterTransitive(relation) =
let 
  fun testOnePair((a,b), []) = []
     | testOnePair((a,b), (c, d)::rest) = 
       if b = c
       then 
         (if isElementOf((a, d), relation) 
         then   testOnePair((a,b), rest) 
         else (a,d):: testOnePair((a,b), rest))
       else testOnePair((a,b), rest);
  fun test([]) = []
     | test((a,b) :: rest) =  testOnePair((a,b), relation) @ test(rest);
in
     test(relation)
end;


val waterImmedWestOf = 
  [(Mediterranean, Chinnereth), (Mediterranean, Jordan), (Mediterranean, Dead),
   (Chinnereth, Jabbok), (Chinnereth, Arnon), (Jordan, Jabbok),
   (Jordan, Arnon), (Dead, Jabbok), (Dead, Arnon)];

val waterVerticalAlign =
 [(Chinnereth, Jordan), (Chinnereth, Dead), (Jordan, Chinnereth), (Jordan, Dead),
  (Dead, Chinnereth), (Dead, Jordan), (Jabbok, Arnon), (Arnon, Jabbok),
(Mediterranean, Mediterranean)];

val wvaCorrected = 
  makeNoRepeats(waterVerticalAlign @ counterTransitive(waterVerticalAlign));

fun makeNoRepeats([]) =[]
| makeNoRepeats(a :: rest) = if isElementOf(a,rest) then makeNoRepeats(rest)
      else a::makeNoRepeats(rest);

Thomas VanDrunen
Last modified: Fri Nov 2 10:55:21 CDT 2007