lang_2023/tests/stdlib.lang
2023-06-03 19:01:03 +03:00

189 lines
3 KiB
Text

basic \float[#ord #div #str]
basic \int[#ord #idiv #str]
basic \string[#ord #str #char-container #copy]
basic \char[#ord #str #copy]
basic \bool[#ord #str #copy]
basic \unit[#str #copy]
//
decl not : \bool -> \bool
def not : x =
(match x with
| true -> false
| false -> true)
decl ( && ) : \bool -> \bool -> \bool
def ( && ) : x y =
match x with
| true -> (
match y with
| true -> true
| false -> false
)
| false -> false
decl ( || ) : \bool -> \bool -> \bool
def ( || ) : x y =
match x with
| true -> true
| false -> (
match y with
| true -> true
| false -> false
)
//
typeclass \char-container =
& var size : -> \int
& var at : \int -> \char
//
typeclass \move = // TODO
& var ( <- ) : \move -> \unit
typeclass \copy =
& var ( = ) : \copy -> \unit
//
typeclass \sum[#copy] =
& var ( += ) : \sum -> \unit
& var ( -= ) : \sum -> \unit
& var ( + ) : \sum -> \sum
& var ( - ) : \sum -> \sum
& zero : -> \sum
namespace var \sum {
def ( + ) : x = {
var ans = self
; ans += x
return ans
}
def ( - ) : x = {
var ans = self
; ans -= x
return ans
}
}
typeclass \mult[#sum] =
& var ( *= ) : \mult -> \unit
& var ( * ) : \mult -> \mult
namespace var \mult {
def ( * ) : x = {
var ans = self
; ans *= x
return ans
}
}
typeclass \idiv[#mult] =
& var div : \idiv -> \idiv
& var mod : \idiv -> \idiv
namespace var \idiv {
def mod : x = self -. x * self..div: x
}
typeclass \div[#mult] =
& var ( /= ) : \div -> \unit
& var ( / ) : \div -> \div
namespace var \div {
def ( / ) : x = {
var ans = self
; ans /= x
return ans
}
}
//
typeclass \eq =
& var ( == ) : \eq -> \bool
& var ( != ) : \eq -> \bool
namespace var \eq {
def ( != ) : x = not: (self == x)
}
//
struct \order =
| $eq
| $lt
| $gt
typeclass \ord[#eq] =
& var compare : \ord -> \order
& var ( < ) : \ord -> \bool
& var ( >= ) : \ord -> \bool
& var ( > ) : \ord -> \bool
& var ( <= ) : \ord -> \bool
decl min 'a[#ord] : 'a -> 'a -> 'a
def min : x y = if x < y then x else y
decl max 'a[#ord] : 'a -> 'a -> 'a
def max : x y = if x < y then y else x
namespace var \ord {
def compare : x =
if self == x then $eq
elif self < x then $lt
else $gt
def ( >= ) : x = not: (self < x)
def ( > ) : x = x < self
def ( <= ) : x = not: (x < self)
}
//
typeclass \show =
& var show : -> \string
typeclass \read =
& read : \string -> \read
typeclass \str[#show #read]
// typeclass debug-show = // TODO
// & debugdshow : -> \string
//
typeclass \default =
& default : -> \default
//
typeclass \bounded =
& min-bound : -> \bounded
& max-bound : -> \bounded
& var is-max-bound : -> \bool
& var is-min-bound : -> \bool
//
typeclass \enum =
& var succ : -> \optional[enum]
& var pred : -> \optional[enum]
& to-enum : \int -> \enum
& var from-enum : -> \int
//
namespace io {
decl print : \string -> \unit
decl scan : -> \string
}
decl random : -> \int // TODO
//