Caller-relative imports; smart deduping in imports
This commit is contained in:
115
lib/base.tri
115
lib/base.tri
@ -1,13 +1,8 @@
|
||||
false = t
|
||||
_ = t
|
||||
true = t t
|
||||
k = t t
|
||||
i = t (t k) t
|
||||
s = t (t (k t)) t
|
||||
m = s i i
|
||||
b = s (k s) k
|
||||
c = s (s (k s) (s (k k) s)) (k k)
|
||||
id = \a : a
|
||||
const = \a b : a
|
||||
pair = t
|
||||
if = \cond then else : t (t else (t t then)) t cond
|
||||
|
||||
@ -26,33 +21,6 @@ matchBool = (\ot of : triage
|
||||
(\_ _ : ot)
|
||||
)
|
||||
|
||||
matchList = \a b : triage a _ b
|
||||
|
||||
matchPair = \a : triage _ _ a
|
||||
|
||||
not? = matchBool false true
|
||||
and? = matchBool id (\_ : false)
|
||||
emptyList? = matchList true (\_ _ : false)
|
||||
|
||||
head = matchList t (\head _ : head)
|
||||
tail = matchList t (\_ tail : tail)
|
||||
|
||||
or? = (\x y :
|
||||
matchBool
|
||||
(matchBool (t t) (t t) y)
|
||||
(matchBool (t t) t y)
|
||||
x)
|
||||
|
||||
xor? = (\x y :
|
||||
matchBool
|
||||
(matchBool t (t t) y)
|
||||
(matchBool (t t) t y)
|
||||
x)
|
||||
|
||||
append = y (\self : matchList
|
||||
(\k : k)
|
||||
(\h r k : pair h (self r k)))
|
||||
|
||||
lAnd = (triage
|
||||
(\_ : false)
|
||||
(\_ x : x)
|
||||
@ -63,11 +31,22 @@ lOr = (triage
|
||||
(\_ _ : true)
|
||||
(\_ _ _ : true))
|
||||
|
||||
map_ = y (\self :
|
||||
matchList
|
||||
(\_ : t)
|
||||
(\head tail f : pair (f head) (self tail f)))
|
||||
map = \f l : map_ l f
|
||||
matchPair = \a : triage _ _ a
|
||||
|
||||
not? = matchBool false true
|
||||
and? = matchBool id (\_ : false)
|
||||
|
||||
or? = (\x z :
|
||||
matchBool
|
||||
(matchBool true true z)
|
||||
(matchBool true false z)
|
||||
x)
|
||||
|
||||
xor? = (\x z :
|
||||
matchBool
|
||||
(matchBool false true z)
|
||||
(matchBool true false z)
|
||||
x)
|
||||
|
||||
equal? = y (\self : triage
|
||||
(triage
|
||||
@ -85,21 +64,6 @@ equal? = y (\self : triage
|
||||
(\_ : false)
|
||||
(\bx by : lAnd (self ax bx) (self ay by))))
|
||||
|
||||
lExist? = y (\self x : matchList
|
||||
false
|
||||
(\h z : or? (equal? x h) (self x z)))
|
||||
|
||||
filter_ = y (\self : matchList
|
||||
(\_ : t)
|
||||
(\head tail f : matchBool (t head) i (f head) (self tail f)))
|
||||
filter = \f l : filter_ l f
|
||||
|
||||
foldl_ = y (\self f l x : matchList (\acc : acc) (\head tail acc : self f tail (f acc head)) l x)
|
||||
foldl = \f x l : foldl_ f l x
|
||||
|
||||
foldr_ = y (\self x f l : matchList x (\head tail : f (self x f tail) head) l)
|
||||
foldr = \f x l : foldr_ x f l
|
||||
|
||||
succ = y (\self :
|
||||
triage
|
||||
1
|
||||
@ -108,48 +72,3 @@ succ = y (\self :
|
||||
(t (t t))
|
||||
(\_ tail : t t (self tail))
|
||||
t))
|
||||
|
||||
length = y (\self : matchList
|
||||
0
|
||||
(\_ tail : succ (self tail)))
|
||||
|
||||
reverse = y (\self : matchList
|
||||
t
|
||||
(\head tail : append (self tail) (pair head t)))
|
||||
|
||||
snoc = y (\self x : matchList
|
||||
(pair x t)
|
||||
(\h z : pair h (self x z)))
|
||||
|
||||
count = y (\self x : matchList
|
||||
0
|
||||
(\h z : matchBool
|
||||
(succ (self x z))
|
||||
(self x z)
|
||||
(equal? x h)))
|
||||
|
||||
last = y (\self : matchList
|
||||
t
|
||||
(\hd tl : matchBool
|
||||
hd
|
||||
(self tl)
|
||||
(emptyList? tl)))
|
||||
|
||||
all? = y (\self pred : matchList
|
||||
true
|
||||
(\h z : and? (pred h) (self pred z)))
|
||||
|
||||
any? = y (\self pred : matchList
|
||||
false
|
||||
(\h z : or? (pred h) (self pred z)))
|
||||
|
||||
unique_ = y (\self seen : matchList
|
||||
t
|
||||
(\head rest : matchBool
|
||||
(self seen rest)
|
||||
(pair head (self (pair head seen) rest))
|
||||
(lExist? head seen)))
|
||||
unique = \xs : unique_ t xs
|
||||
|
||||
intersect = \xs ys : filter (\x : lExist? x ys) xs
|
||||
union = \xs ys : unique (append xs ys)
|
||||
|
77
lib/list.tri
Normal file
77
lib/list.tri
Normal file
@ -0,0 +1,77 @@
|
||||
!import "base.tri" !Local
|
||||
|
||||
matchList = \a b : triage a _ b
|
||||
|
||||
emptyList? = matchList true (\_ _ : false)
|
||||
head = matchList t (\head _ : head)
|
||||
tail = matchList t (\_ tail : tail)
|
||||
|
||||
append = y (\self : matchList
|
||||
(\k : k)
|
||||
(\h r k : pair h (self r k)))
|
||||
|
||||
lExist? = y (\self x : matchList
|
||||
false
|
||||
(\h z : or? (equal? x h) (self x z)))
|
||||
|
||||
map_ = y (\self :
|
||||
matchList
|
||||
(\_ : t)
|
||||
(\head tail f : pair (f head) (self tail f)))
|
||||
map = \f l : map_ l f
|
||||
|
||||
filter_ = y (\self : matchList
|
||||
(\_ : t)
|
||||
(\head tail f : matchBool (t head) id (f head) (self tail f)))
|
||||
filter = \f l : filter_ l f
|
||||
|
||||
foldl_ = y (\self f l x : matchList (\acc : acc) (\head tail acc : self f tail (f acc head)) l x)
|
||||
foldl = \f x l : foldl_ f l x
|
||||
|
||||
foldr_ = y (\self x f l : matchList x (\head tail : f (self x f tail) head) l)
|
||||
foldr = \f x l : foldr_ x f l
|
||||
|
||||
length = y (\self : matchList
|
||||
0
|
||||
(\_ tail : succ (self tail)))
|
||||
|
||||
reverse = y (\self : matchList
|
||||
t
|
||||
(\head tail : append (self tail) (pair head t)))
|
||||
|
||||
snoc = y (\self x : matchList
|
||||
(pair x t)
|
||||
(\h z : pair h (self x z)))
|
||||
|
||||
count = y (\self x : matchList
|
||||
0
|
||||
(\h z : matchBool
|
||||
(succ (self x z))
|
||||
(self x z)
|
||||
(equal? x h)))
|
||||
|
||||
last = y (\self : matchList
|
||||
t
|
||||
(\hd tl : matchBool
|
||||
hd
|
||||
(self tl)
|
||||
(emptyList? tl)))
|
||||
|
||||
all? = y (\self pred : matchList
|
||||
true
|
||||
(\h z : and? (pred h) (self pred z)))
|
||||
|
||||
any? = y (\self pred : matchList
|
||||
false
|
||||
(\h z : or? (pred h) (self pred z)))
|
||||
|
||||
unique_ = y (\self seen : matchList
|
||||
t
|
||||
(\head rest : matchBool
|
||||
(self seen rest)
|
||||
(pair head (self (pair head seen) rest))
|
||||
(lExist? head seen)))
|
||||
unique = \xs : unique_ t xs
|
||||
|
||||
intersect = \xs ys : filter (\x : lExist? x ys) xs
|
||||
union = \xs ys : unique (append xs ys)
|
Reference in New Issue
Block a user