bool.anubis 3.43 KB

                       The Anubis project.

                 Utilities around the booleans.

   (reformated by AP, 2014/07/31)
   

   *** (1) Logical operations for booleans. 

public define macro   Bool       Bool x | Bool y.            the disjunction (logical 'or')
public define macro   Bool       Bool x & Bool y.            the conjunction (logical 'and')
 
   The above two macros are already defined in 'library/predefined.anubis'. 
   
public define macro   Bool       xor(Bool x, Bool y).        the exclusive disjunction
public define macro   Bool       not (Bool x).               the negation    (logical 'not')
public define macro   Bool       ~ Bool x.                   the negation again, with a shorter syntax

   *** (2) Logical operations for boolean predicates. 
   
   Same as above, but applying to data of type $T -> Bool ('predicates') 
   instead of data of type Bool ('booleans'), and of course returning predicates. 
   
public define    $T -> Bool       $T -> Bool p | $T -> Bool q.     
public define    $T -> Bool       $T -> Bool p & $T -> Bool q.     
public define    $T -> Bool       xor($T -> Bool p, $T -> Bool q). 
public define    $T -> Bool       not ($T -> Bool  p). 
public define    $T -> Bool       ~ $T -> Bool  p. 

   *** (3) Logical 'or' and 'and' for a predicate applied to a list of elements. 

   'mapand(p,l)' is true if 'p' returns true for all elements of 'l'. 
   'mapor(p,l)'  is true if 'p' returns true for at least one element of 'l'. 
   
public define          Bool       mapand    ($T -> Bool p, List($T) l). 
public define          Bool       mapor     ($T -> Bool p, List($T) l). 

   Of course, 'mapand(p,[])' is true and 'mapor(p,[])' is false. 

   *** (4) Non equality (with boolean value). 
   
   This is just the negation of equality. 

 public define macro   Bool      $T x /= $T y.               true if x is not equal to y
 public define macro   Bool      $T x != $T y.               the same one with the C syntax


   --- That's all for the public part ! ------------------------------
   

   
 public define macro Bool     // moved to predefined.anubis
  $T x /= $T y
   =
  if x = y then false else true. 
   
public define macro Bool
  $T x != $T y
   =
  if x = y then false else true. 
   
public define macro Bool 
  not
    (
      Bool x
    ) =
  if x then false else true. 

public define macro Bool 
  ~ Bool x
    =
  if x then false else true. 

public define macro Bool
  xor
    (
      Bool x,
      Bool y
    ) =
   if x = y then false else true. 
   


public define $T -> Bool
   $T -> Bool f | $T -> Bool g 
     =
   ($T x) |-> if f(x) then true else g(x). 
   
public define $T -> Bool
   $T -> Bool f & $T -> Bool g 
     =
   ($T x) |-> if f(x) then g(x) else false. 
   
public define $T -> Bool
   xor
   (
     $T -> Bool    p, 
     $T -> Bool    q
   ) =   
   ($T x) |-> if p(x) = q(x) then false else true. 
   
   
public define $T -> Bool
   not
     (
       $T -> Bool f
     ) =
   ($T t) |-> not(f(t)). 
   

public define Bool
   mapand
     (
       $T -> Bool   test, 
       List($T)     l
     ) =
   if l is 
     {
       [ ] then true, 
       [h . t] then 
         if test(h)
         then mapand(test,t)
         else false
     }.
        
public define Bool
   mapor
     (
       $T -> Bool    test, 
       List($T)      l
     ) =
   if l is 
     {
       [ ] then false, 
       [h . t] then 
         if test(h) 
         then true
         else mapor(test,t)
     }.