summaryrefslogtreecommitdiff
path: root/modules/language/python/bool.scm
blob: d15c7498e522800186533e68b28cf669e51f8bac (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(define-module (language python bool)
  #:use-module (oop goops)
  #:use-module (language python exceptions)
  #:use-module (oop pf-objects)
  #:export (bool))

(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))

(define-method (bool x)
  (cond
   ((null? x)
    #f)
   ((eq? x None)
    #f)
   (else x)))

(define-method (bool (x <integer>)) (if (= x 0) #f x))
(define-method (bool (x <p>))
  (aif it (ref x '__bool__)
       (it)
       (next-method)))
		     

(define-method (+ (a <boolean>) b)
  (+ (if a 1 0) b))
(define-method (+ b (a <boolean>))
  (+ (if a 1 0) b))
(define-method (* (a <boolean>) b)
  (* (if a 1 0) b))
(define-method (* b (a <boolean>))
  (* (if a 1 0) b))
(define-method (- (a <boolean>) b)
  (- (if a 1 0) b))
(define-method (- b (a <boolean>))
  (- b (if a 1 0)))