Quite a few more bang files for testing.

This commit is contained in:
2011-02-03 20:26:09 -05:00
parent bc435d83f8
commit 17ca2f7899
6 changed files with 396 additions and 20 deletions

11
bsrc/Data/Bool.bs Normal file
View File

@@ -0,0 +1,11 @@
module Data.Bool
datatype Bool = True | False;
export (&)(x :: Bool, y :: Bool) :: Bool = prim%and;
export (|)(x :: Bool, y :: Bool) :: Bool = prim%or;
export (^)(x :: Bool, y :: Bool) :: Bool = prim%xor;
export (not)(x :: Bool) :: Bool = prim%not;
export otherwise :: Bool = True;

View File

@@ -7,42 +7,49 @@ export datatype List a =
export (++)(a :: [a], b :: [a]) :: [a]
{
case a of
[] -> b
(af:ar) -> case b of
[] -> a
_ -> af:(ar ++ b)
case a {
[] -> b;
(af:ar) ->
case b {
[] -> a;
_ -> af:(ar ++ b);
}
}
};
export null(ls :: [a]) :: Bool
{
case ls of
[] -> True
_ -> False
case ls {
[] -> True;
_ -> False;
}
};
export length(ls :: [a]) :: Int
{
case ls of
[] -> 0
_:rest -> length(rest)
case ls {
[] -> 0;
(_:rest) -> 1 + length(rest);
}
};
export reverse(ls :: [a]) :: [a]
{
helper(xs,acc) = {
case xs of
[] -> acc
(a:rest) -> helper(rest, (a:acc))
}
let helper(xs,acc) = {
case xs {
[] -> acc;
(a:rest) -> helper(rest, (a:acc));
}
};
helper(ls, []);
};
export restrict(Eq a) find(f :: a -> Bool, ls :: [a]) :: Maybe a
{
case ls of
[] -> False
(a:_) | f a -> a
(_:rest) -> find(f, rest)
case ls {
[] -> False;
(a:_) | f(a) -> a;
(_:rest) -> find(f, rest);
}
};

93
bsrc/Data/Number.bs Normal file
View File

@@ -0,0 +1,93 @@
module Data.Number
import Data.Object
export restrict(Eq a) Num a {
(+)(x :: a, y :: a) :: a;
(*)(x :: a, y :: a) :: a;
(-)(x :: a, y :: a) :: a;
};
export restrict(Num a) Signed a {
negate(x :: a) :: a;
abs(x :: a) :: a;
signum(x :: a) :: a;
};
export restrict(Num a) Unsigned a {
};
export class Bounded a {
minBound :: a;
maxBound :: a;
};
export datatype Ordering = Before | Equal | After;
export class restrict(Eq a) Ord a {
compare(_ :: a, _ :: a) :: Ordering;
(<)(x :: a, y :: b) :: Bool
{
case compare(x,y) {
Before -> True;
_ -> False;
}
}
(>)(x :: a, y :: b) :: Bool
{
case compare(x,y) {
After -> True;
_ -> False;
}
}
(<=)(x :: a, y :: b) :: Bool
{
case compare(x,y) {
After -> False;
_ -> True;
}
}
(>=)(x :: a, y :: b) :: Bool
{
case compare(x,y) {
Before -> False;
_ -> True;
}
}
max(x :: a, y :: b) :: a
{
case compare(x,y) {
Before -> y;
_ -> x;
}
}
min(x :: a, y :: b) :: b
{
case compare(x,y) {
After -> y;
_ -> x;
}
}
export class Enum a {
succ(a) :: a;
pred(a) :: a;
toEnum(Integer) :: a;
fromEnum(a) :: Integer;
enumFromTo(a, a) :: [a];
enumFromThenTo(a, a, a) :: [a];
}
export restrict(Ord a, Num a)
createEnum(s :: a, e :: a, m :: a) :: [a] = s : createEnum(s + m, e, m);
export restrict(Ord a, Num a)
diff(x :: a, y :: a) :: a = if x > y then x - y else y - x;

106
bsrc/Data/Number/Machine.bs Normal file
View File

@@ -0,0 +1,106 @@
module Data.Number.Machine
type Int = prim%Int;
type Word = prim%Word;
export instance Eq Int {
(==)(x,y) = prim%inteq(x,y);
(/=)(x,y) = prim%intneq(x,y);
}
export instance Eq Word {
(==)(x,y) = prim%wordeq(x,y);
(/=)(x,y) = prim%wordneq(x,y);
}
export instance Num Int {
(+)(x,y) = prim%intplus(x,y);
(-)(x,y) = prim%intminus(x,y);
(*)(x,y) = prim%intmult(x,y);
}
export instance Num Word {
(+)(x,y) = prim%wordplus(x,y);
(-)(x,y) = prim%wordminus(x,y);
(*)(x,y) = prim%wordmult(x,y);
}
export instance Signed Int {
negate = prim%intnegate;
abs = prim%intabs;
signum(x) = case x {
_ | x < 0 -> -1;
| x == 0 -> 0;
| otherwise -> 1;
}
}
export instance Unsigned Word;
export instance Bounded Int {
minBound = prim%intminbound;
maxBound = prim%intmaxbound;
}
export instance Bounded Word {
minBound = 0;
maxBound = prim%wordmaxbound;
}
export instance Ord Int {
compare(x,y) = if x == y
then Equal
else if x < y
then Before
else After
(<) = prim%intlt
(<=) = prim%intlte
(>) = prim%intgt
(>=) = prim%intgte
max(x,y) = if x > y then x else y;
min(x,y) = if x < y then x else y;
}
export instance Ord Word {
compare(x,y) = if x == y
then Equal
else if x < y
then Before
else After
(<) = prim%wordlt
(<=) = prim%wordlte
(>) = prim%wordgt
(>=) = prim%wordgte
max(x,y) = if x > y then x else y;
min(x,y) = if x < y then x else y;
}
export instance Enum Int {
succ(x) = x + 1;
pred(x) = x - 1;
toEnum(x) = if x > safeConvert(maxBound :: Int)
then maxBound
else if x < safeConvert(minBound :: Int)
then minBound
else unsafeConvert(x)
fromEnum(x) = safeConvert(x)
enumFromTo(x,y) = if x > y then createEnum(x,y,-1) else createEnum(x,y,1);
enumFromThenTo(x,y) = let direction = if x > y then -1 else 1
amt = diff x y
in createEnum(x,y,amt * direction)
}
export instance Enum Word {
succ(x) = x + 1;
pred(x) = x - 1;
toEnum(x) = if x > safeConvert(maxBound :: Word)
then maxBound
else if x < safeConvert(minBound :: Word)
then minBound
else unsafeConvert(x)
fromEnum(x) = safeConvert(x)
enumFromTo(x,y) = if x > y then createEnum(x,y,-1) else createEnum(x,y,1);
enumFromThenTo(x,y) = let direction = if x > y then -1 else 1
amt = diff x y
in createEnum(x,y,amt * direction)
}

27
bsrc/Data/Object.bs Normal file
View File

@@ -0,0 +1,27 @@
module Data.Object
import Data.Bool;
export eq(x :: a, y :: a) :: Bool = prim%eq(x,y);
class Eq a {
(==)(x :: a, y :: a) :: Bool = not (x /= y);
(/=)(x :: a, y :: a) :: Bool = not (x == y);
}
instance Eq () {
(==)(x,y) = True;
(/=)(x,y) = False;
}
instance Eq Bool {
(/=)(x,y) = x ^ y;
}
class SafelyConvertable a b {
safeConvert :: a -> b;
}
class UnsafelyConvertable a b {
unsafeConvert :: a -> b;
}