'Unknown' and three-valued logic (was: Example where Euphoria ...)
- Posted by Juergen Luethje <jluethje at gmx.de> Nov 24, 2002
- 502 views
Derek wrote: > I use a programming language called 'Progress'. It has an explicit 'unknown' > value that you can assign to any variable. It has been a very useful device. I can imagine that very well. At work, I use a statistical programming package that also has something like that, called 'Missing Value' there. In research, it unfortunately is common, that one doesn't get all the data one wants to get, and proper handling of the missing data is important. > The symbol for it in the language is '?'. > > So ... > if theDate = ? then > theDate = today - 1. > > reads... > if the date is not known, set it to yesterday's date. <big snip> At the moment Euphoria only has values that mean 'unknown' in a special context, and have another meaning in another context. For instance opening a non existing file for reading returns the 'handle' -1, which means something like 'invalid handle' or 'handle unknown'. Then, on the next line, -1 can be a normal number, with which I can perform math operations. Not very elegant IMHO. Or when I e.g. have written a function, that can return any number as a valid result, it must return a sequence as flag for 'invalid' or 'unknown'. And what, if my function can return numbers and sequences as valid results? Introducing something like 'nil' to Euphoria would make many things more elegant, more flexible, more powerful -- in one word: more Euphoria-like. Anyway ... Some time ago, I had the idea to write some functions for three-valued logic -- now it has been waked up. Derek, maybe you, and of course anyone else interested, would like to have a look at the following lines, especially at the truth table? I'm not sure if it is correct -- and I also don't know what value to use for 'not unknown'. Best regards, Juergen ------------------------------------------------------------------------ Three-Valued Logic (JuLu, 24. November 2002) ============================================= Truth Table ----------- Abbreviations: T = True F = False U = Unknown A | B | not A | A and B | A or B | A xor B --+---+-------+---------+--------+-------- T | T | F | T | T | F T | F | F | F | T | T T | U | F | U | T | U F | T | T | F | T | T F | F | T | F | F | F F | U | T | F | U | U U | T | | U | T | U U | F | | F | U | U U | U | | U | U | F Euphoria code follows (untested!): ------------------------------------------------------------------------ global constant FALSE = 0, TRUE = not FALSE, UNKNOWN = -1 global type three_valued (object x) if integer(x) and (x = TRUE or x = FALSE or x = UNKNOWN) then return TRUE end if return FALSE end type global function tv_not (three_valued a) if a != UNKNOWN then return not a else -- return value also unknown by me end if end function global function tv_and (three_valued a, three_valued b) if a != UNKNOWN and b != UNKNOWN then return a and b elsif a = FALSE or b = FALSE then return FALSE else return UNKNOWN end if end function global function tv_or (three_valued a, three_valued b) if a != UNKNOWN and b != UNKNOWN then return a or b elsif a = TRUE or b = TRUE then return TRUE else return UNKNOWN end if end function global function tv_xor (three_valued a, three_valued b) if a != UNKNOWN and b != UNKNOWN then return a xor b elsif a = UNKNOWN and b = UNKNOWN then return FALSE else return UNKNOWN end if end function