;; lib/maude/tests/owise.sx — owise (otherwise) equations. (define mow-pass 0) (define mow-fail 0) (define mow-failures (list)) (define mow-check! (fn (name got expected) (if (= got expected) (set! mow-pass (+ mow-pass 1)) (do (set! mow-fail (+ mow-fail 1)) (append! mow-failures (str name " expected: " expected " got: " got)))))) ;; The owise catch-all is declared FIRST, yet must only fire when no ordinary ;; equation applies — proving owise is order-independent, not just last-match. (define mow-lookup (mau/parse-module "fmod LOOKUP is\n sorts Key Val .\n ops k1 k2 k3 : -> Key .\n ops v1 v2 none : -> Val .\n op lookup : Key -> Val .\n var K : Key .\n eq lookup(K) = none [owise] .\n eq lookup(k1) = v1 .\n eq lookup(k2) = v2 .\nendfm")) (mow-check! "owise-parsed" (get (first (mau/module-eqs mow-lookup)) :owise) true) (mow-check! "ordinary-not-owise" (get (nth (mau/module-eqs mow-lookup) 1) :owise) false) (mow-check! "lookup-hit-1" (mau/creduce->str mow-lookup "lookup(k1)") "v1") (mow-check! "lookup-hit-2" (mau/creduce->str mow-lookup "lookup(k2)") "v2") (mow-check! "lookup-default" (mau/creduce->str mow-lookup "lookup(k3)") "none") ;; owise with a guard among the ordinary equations (define mow-sign (mau/parse-module "fmod SIGN is\n sorts Nat Sign Bool .\n op 0 : -> Nat .\n op s_ : Nat -> Nat .\n op true : -> Bool .\n op false : -> Bool .\n op _>_ : Nat Nat -> Bool .\n op pos : -> Sign .\n op zero : -> Sign .\n op sign : Nat -> Sign .\n var N : Nat .\n eq 0 > N = false .\n eq s N > 0 = true .\n eq s N > s M = N > M .\n eq sign(N) = pos [owise] .\n eq sign(0) = zero .\n vars M : Nat .\nendfm")) (mow-check! "sign-zero" (mau/creduce->str mow-sign "sign(0)") "zero") (mow-check! "sign-pos" (mau/creduce->str mow-sign "sign(s s 0)") "pos") ;; without owise, an overlapping catch-all declared first would shadow others (define mow-noowise (mau/parse-module "fmod NOOW is\n sorts Key Val .\n ops k1 k2 : -> Key .\n ops v1 def : -> Val .\n op f : Key -> Val .\n var K : Key .\n eq f(K) = def .\n eq f(k1) = v1 .\nendfm")) ;; here f(k1) hits the first (catch-all) equation -> def (no owise tag) (mow-check! "noowise-shadows" (mau/creduce->str mow-noowise "f(k1)") "def") (define mau-owise-tests-run! (fn () {:failures mow-failures :total (+ mow-pass mow-fail) :passed mow-pass :failed mow-fail}))