Browse Source

update readme

Mistivia 4 months ago
parent
commit
eae9dcdba5
2 changed files with 76 additions and 16 deletions
  1. 7 0
      .gitignore
  2. 69 16
      README.md

+ 7 - 0
.gitignore

@@ -0,0 +1,7 @@
+*.so
+*.o
+*.link
+*.build.sh
+*.install.sh
+*.import.scm
+#*

+ 69 - 16
README.md

@@ -1,31 +1,84 @@
 # Trait
 
-A trait/typeclass system for Scheme.
+A trait/typeclass system for Chicken Scheme.
 
 ## Example
 
+### Eq
+
     (import trait)
     
     (define-record point x y)
     
     (define-trait Eq
-      ;; fallback to equal? by default
-      (same? equal?))
-
+      (==)
+      (/= (lambda (a b) (not (== a b)))))
+    
     (define-trait-impl (Eq number?)
-      (same? =))
+      (== =))
     
     (define-trait-impl (Eq symbol?)
-      (same? eq?))
+      (== eq?))
+    
+    (define-trait-impl (Eq list?)
+      (== equal?))
     
     (define-trait-impl (Eq point?)
-      (same? (lambda (a b) (and (point? b)
-                                (= (point-x a) (point-x b))
-                                (= (point-y a) (point-y b))))))
-    
-    (display (list (same? 'a 'a)
-                   (same? 1 1)
-                   (same? 1 2)
-                   (same? (list 1 2) (list 1 2))
-                   (same? (cons 3 4) (cons 3 4))
-                   (same? (make-point 1 2) (make-point 3 4))))
+      (== (lambda (a b) (and (point? b)
+                             (= (point-x a) (point-x b))
+                             (= (point-y a) (point-y b))))))
+    
+    (display (list (== 'a 'a)
+                   (/= 'a 'a)
+    
+                   (== 1 1)
+                   (/= 1 1)
+    
+                   (== 1 2)
+                   (/= 1 2)
+    
+                   (== (list 1 2) (list 1 2))
+                   (/= (list 1 2) (list 1 2))
+    
+                   (== (make-point 3 4) (make-point 3 4))
+                   (/= (make-point 3 4) (make-point 3 4))))
+
+### Monad
+
+    (import trait)
+    
+    (define-trait Monad
+      (>>=)
+      (return))
+    
+    (define-record nullable is-null value)
+    (define (make-some value)
+      (make-nullable #f value))
+    (define (make-null)
+      (make-nullable #t '()))
+    (define (nullable-type x)
+      (if (nullable-is-null x)
+          'null
+          'some))
+    
+    (define-trait-impl (Monad nullable?)
+      (>>= (lambda (m f)
+             (let ((type (nullable-type m)))
+               (cond ((eq? type 'null)
+                      (make-null))
+                     ((eq? type 'some)
+                      (f (nullable-value m)))))))
+      (return make-some))
+    
+    (define (*2 a)
+      (make-some (* 2 a)))
+    
+    (display (nullable-value (>>= (make-some 3) *2)))
+    (newline)
+    
+    (display ((with-type nullable? return) 42))
+    (newline)
+    
+    (define x (make-some 42))
+    (display ((with-type-of x return) 81))
+    (newline)