packages feed

egison-3.10.3: lib/core/base.egi

;;;;;
;;;;;
;;;;; Base
;;;;;
;;;;;

(define $eq
  (matcher
    {[,$val []
      {[$tgt (if (eq? val tgt)
                 {[]}
                 {})]}]
     [$ [something]
      {[$tgt {tgt}]}]
     }))
  
(define $bool eq)
(define $char eq)
(define $integer eq)
(define $float eq)

;;
;; Utility
;;
(define $id 1#%1)

(define $fst 2#%1)

(define $snd 2#%2)

(define $apply
  (lambda [$f $x]
    (f x)))

(define $b.compose
  (lambda [$f $g]
    (lambda $x
      (g (f x)))))

(define $compose
  (cambda $fs
    (lambda $x
      (foldl 2#(%2 %1) x fs))))

(define $flip (lambda [$fn] (lambda [$x $y] (fn y x))))

(define $ref
  (lambda [%xa $is]
    (match is (list integer)
      {[<nil> xa]
       [<cons $i $rs> (ref (array-ref xa i) rs)]})))

(define $eq?/m
  (lambda [$a $x $y]
    (match x a
      {[,y #t]
       [_ #f]})))

;;
;; Boolean
;;
(define $and (cambda $bs (foldl b.and #t bs)))
(define $or (cambda $bs (foldl b.or #f bs)))

(define $b.and
  (lambda [$b1 $b2]
    (if b1
        b2
        #f)))

(define $b.or
  (lambda [$b1 $b2]
    (if b1
        #t
        b2)))

(define $not
  (lambda [$b]
    (match b bool
      {[,#t #f]
       [,#f #t]})))

;;
;; Unordered Pair
;;

(define $unordered-pair
  (lambda [$m]
    (matcher
      {[[$ $] [m m]
        {[[$x $y] {[x y] [y x]}]}]
       [$ [eq]
        {[$tgt {tgt}]}]
       })))