Last active
December 30, 2015 11:58
-
-
Save robotlolita/7825670 to your computer and use it in GitHub Desktop.
Three-valued logic.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Trilean where | |
data Trilean = Yes | |
| No | |
| Unknown | |
deriving (Eq, Show) | |
(/\) :: Trilean -> Trilean -> Trilean | |
(/\) Yes a = a | |
(/\) Unknown No = No | |
(/\) Unknown _ = Unknown | |
(/\) No _ = No | |
(\/) :: Trilean -> Trilean -> Trilean | |
(\/) Yes _ = Yes | |
(\/) Unknown Yes = Yes | |
(\/) Unknown _ = Unknown | |
(\/) No a = a | |
invert :: Trilean -> Trilean | |
invert Yes = No | |
invert Unknown = Unknown | |
invert No = Yes | |
(→) :: Trilean -> Trilean -> Trilean | |
(→) a b = (invert a) \/ b | |
trif :: Trilean -> a -> a -> a -> a | |
trif Yes a _ _ = a | |
trif No _ _ c = c | |
trif Unknown _ b _ = b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Maybe = require('monads.maybe') | |
var True = Maybe.Just(true); | |
var False = Maybe.Just(false); | |
var Unknown = Maybe.Nothing(); | |
function and(a, b) { | |
return a === True? b | |
: a === Unknown && b === False? False | |
: a === Unknown? Unknown | |
: a === False? False | |
} | |
function or(a, b) { | |
return a === True? True | |
: a === Unknown && b === True? True | |
: a === Unknown? Unknown | |
: a === False? b | |
} | |
function not(a) { | |
return a === True? False | |
: a === Unknown? Unknown | |
: a === False? True | |
} | |
function imply(a, b) { | |
return or(not(a), b) | |
} | |
function trif(a, f, g, h) { | |
return a === True? f() | |
: a === Unknown? g() | |
: a === False? h() | |
} | |
trif(someComputationReturningTrilean(), | |
function(){ /* true */ }, | |
function(){ /* unknown */ }, | |
function(){ /* false */ }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment