| 1 | (******************************************************************************* |
|---|
| 2 | Copyright 2009 Sun Microsystems, Inc., |
|---|
| 3 | 4150 Network Circle, Santa Clara, California 95054, U.S.A. |
|---|
| 4 | All rights reserved. |
|---|
| 5 | |
|---|
| 6 | U.S. Government Rights - Commercial software. |
|---|
| 7 | Government users are subject to the Sun Microsystems, Inc. standard |
|---|
| 8 | license agreement and applicable provisions of the FAR and its supplements. |
|---|
| 9 | |
|---|
| 10 | Use is subject to license terms. |
|---|
| 11 | |
|---|
| 12 | This distribution may include materials developed by third parties. |
|---|
| 13 | |
|---|
| 14 | Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered |
|---|
| 15 | trademarks of Sun Microsystems, Inc. in the U.S. and other countries. |
|---|
| 16 | ******************************************************************************) |
|---|
| 17 | |
|---|
| 18 | component FunctionalMethodLookup |
|---|
| 19 | export Executable |
|---|
| 20 | |
|---|
| 21 | trait Comp |
|---|
| 22 | extends { StdPartialOrder[\Comp\] } |
|---|
| 23 | comprises { Unord, TotalComp } |
|---|
| 24 | opr EQL(self, other:Comp): Boolean = do |
|---|
| 25 | println("Comp " self " EQL " other) |
|---|
| 26 | false |
|---|
| 27 | end |
|---|
| 28 | opr LEXICO(self, other:Comp): Comp = Unord |
|---|
| 29 | opr INVERSE(self): Comp |
|---|
| 30 | end |
|---|
| 31 | |
|---|
| 32 | (** Unord is the outcome of a CMP b when a and b are partially |
|---|
| 33 | ordered and no ordering relationship exists between them. **) |
|---|
| 34 | object Unord extends Comp |
|---|
| 35 | getter asString(): String = "Unord" |
|---|
| 36 | opr EQL(self, other:Unord): Boolean = do |
|---|
| 37 | println("Unord EQL Unord") |
|---|
| 38 | true |
|---|
| 39 | end |
|---|
| 40 | opr LSTH(self, other:Comp): Boolean = do println("Unord LSTH"); false end |
|---|
| 41 | opr INVERSE(self): Comp = Unord |
|---|
| 42 | end |
|---|
| 43 | |
|---|
| 44 | trait TotalComp |
|---|
| 45 | extends { Comp, StdTotalOrder[\TotalComp\] } |
|---|
| 46 | comprises { LessTh, EqTo, GreaterTh } |
|---|
| 47 | (* We're both a partial order (including Unord) and a total |
|---|
| 48 | order (TotalComp alone). Avoid ambiguity between the |
|---|
| 49 | default definitions of CMP and GREQ. *) |
|---|
| 50 | opr EQL(self, other:Comp): Boolean = do |
|---|
| 51 | println("TotalComp " self " EQL " other) |
|---|
| 52 | false |
|---|
| 53 | end |
|---|
| 54 | opr CMP(self, other:Unord): Comp = Unord |
|---|
| 55 | opr GREQ(self, other:Unord): Boolean = false |
|---|
| 56 | opr GREQ(self, other:Comp): Boolean = NOT (other LSTH self) |
|---|
| 57 | opr LSTH(self, other:Unord): Boolean = false |
|---|
| 58 | opr LEXICO(self, other:TotalComp): TotalComp = self |
|---|
| 59 | opr LEXICO(self, other:()->TotalComp): TotalComp = self |
|---|
| 60 | opr INVERSE(self): TotalComp |
|---|
| 61 | end |
|---|
| 62 | |
|---|
| 63 | object LessTh extends TotalComp |
|---|
| 64 | getter asString(): String = "LessTh" |
|---|
| 65 | opr EQL(self, other:LessTh): Boolean = do |
|---|
| 66 | println("LessTh EQL LessTh") |
|---|
| 67 | true |
|---|
| 68 | end |
|---|
| 69 | opr CMP(self, other:LessTh): Comp = EqTo |
|---|
| 70 | opr CMP(self, other:TotalComp): Comp = GreaterTh |
|---|
| 71 | opr LSTH(self, other:LessTh): Boolean = false |
|---|
| 72 | opr LSTH(self, other:TotalComp): Boolean = true |
|---|
| 73 | opr INVERSE(self): TotalComp = GreaterTh |
|---|
| 74 | end |
|---|
| 75 | |
|---|
| 76 | object GreaterTh extends TotalComp |
|---|
| 77 | getter asString(): String = "GreaterTh" |
|---|
| 78 | opr EQL(self, other:GreaterTh): Boolean = do |
|---|
| 79 | println("GreaterTh EQL GreaterTh") |
|---|
| 80 | true |
|---|
| 81 | end |
|---|
| 82 | opr CMP(self, other:GreaterTh): Comp = EqTo |
|---|
| 83 | opr CMP(self, other:TotalComp): Comp = LessTh |
|---|
| 84 | opr LSTH(self, other:Comp): Boolean = false |
|---|
| 85 | opr INVERSE(self): TotalComp = LessTh |
|---|
| 86 | end |
|---|
| 87 | |
|---|
| 88 | object EqTo extends TotalComp |
|---|
| 89 | getter asString(): String = "EqTo" |
|---|
| 90 | opr EQL(self, other:EqTo): Boolean = do |
|---|
| 91 | println("EqTo EQL EqTo") |
|---|
| 92 | true |
|---|
| 93 | end |
|---|
| 94 | opr CMP(self, other:Comp): Comp = INVERSE other |
|---|
| 95 | opr LSTH(self, other:LessTh): Boolean = true |
|---|
| 96 | opr LSTH(self, other:TotalComp): Boolean = false |
|---|
| 97 | opr LEXICO(self, other:TotalComp): TotalComp = other |
|---|
| 98 | opr LEXICO(self, other:()->TotalComp): TotalComp = other() |
|---|
| 99 | opr INVERSE(self): TotalComp = EqTo |
|---|
| 100 | end |
|---|
| 101 | |
|---|
| 102 | trait Equty[\Self extends Equty[\Self\]\] |
|---|
| 103 | opr EQL(self, other:Self): Boolean |
|---|
| 104 | end |
|---|
| 105 | |
|---|
| 106 | (** StdPartialOrder is partial ordering using LSTH,GRTH,LSEQ,GREQ,EQL, and CMP. |
|---|
| 107 | This is primarily for floating-point values. Minimal complete |
|---|
| 108 | definition: either CMP or { LSTH, = }. **) |
|---|
| 109 | trait StdPartialOrder[\Self extends StdPartialOrder[\Self\]\] |
|---|
| 110 | opr CMP(self, other:Self): Comp = |
|---|
| 111 | if self LSTH other then LessTh |
|---|
| 112 | elif other GRTH self then GreaterTh |
|---|
| 113 | else |
|---|
| 114 | println("Equty test for " self " EQL " other) |
|---|
| 115 | if self EQL other then EqTo |
|---|
| 116 | else Unord |
|---|
| 117 | end |
|---|
| 118 | end |
|---|
| 119 | opr LSTH(self, other:Self): Boolean = LessTh EQL (self CMP other) |
|---|
| 120 | opr GRTH(self, other:Self): Boolean = other LSTH self |
|---|
| 121 | opr EQL(self, other:Self): Boolean = do |
|---|
| 122 | println("spo " self " EQL " other) |
|---|
| 123 | EqTo EQL (self CMP other) |
|---|
| 124 | end |
|---|
| 125 | opr LSEQ(self, other:Self): Boolean = other GREQ self |
|---|
| 126 | opr GREQ(self, other:Self): Boolean = (self EQL other) OR: (self GRTH other) |
|---|
| 127 | end |
|---|
| 128 | |
|---|
| 129 | (** StdTotalOrder is the usual total order using LSTH,GRTH,LSEQ,GREQ,EQL, and CMP. |
|---|
| 130 | Most values that define a comparison should do so using this. |
|---|
| 131 | Minimal complete definition: either CMP or LSTH (it's advisable to |
|---|
| 132 | define EQL in the latter case). **) |
|---|
| 133 | trait StdTotalOrder[\Self extends StdTotalOrder[\Self\]\] |
|---|
| 134 | extends StdPartialOrder[\Self\] |
|---|
| 135 | opr CMP(self, other:Self): Comp = |
|---|
| 136 | if self LSTH other then LessTh |
|---|
| 137 | elif other LSTH self then GreaterTh |
|---|
| 138 | else EqTo |
|---|
| 139 | end |
|---|
| 140 | opr GREQ(self, other:Self): Boolean = NOT (self LSTH other) |
|---|
| 141 | end |
|---|
| 142 | |
|---|
| 143 | object Foo extends StdPartialOrder[\Foo\] |
|---|
| 144 | getter asString():String = "Foo" |
|---|
| 145 | opr CMP(self, other:Foo):Comp = EqTo |
|---|
| 146 | end |
|---|
| 147 | |
|---|
| 148 | run():() = do |
|---|
| 149 | (* |
|---|
| 150 | assert((Foo CMP Foo) EQL EqTo, "CMP") |
|---|
| 151 | assert(Foo EQL Foo, "=") |
|---|
| 152 | assert(Foo GREQ Foo, ">=") |
|---|
| 153 | assert(Foo LSEQ Foo, "<=") |
|---|
| 154 | assert(NOT(Foo GRTH Foo), ">") |
|---|
| 155 | *) |
|---|
| 156 | assert(NOT(Foo LSTH Foo), "<") |
|---|
| 157 | end |
|---|
| 158 | |
|---|
| 159 | end |
|---|