wip examples

This commit is contained in:
Michael Sippel 2024-06-11 15:17:58 +02:00
parent 541702de55
commit 72122bf4fc
Signed by: senvas
GPG key ID: F96CF119C34B64A6
12 changed files with 985 additions and 25 deletions

19
lt-stdlib/angle.lt Normal file
View file

@ -0,0 +1,19 @@
export {
let angle-normalize =
λ a : Angle
~ Degree
~ _0,360
~ _360
~ _2^64
~ machine.UInt64
~ machine.Word
↦ Angle~Degree~_0,360~_360: machine.UInt64.rem a 360;
let angle-degree-to-turns =
λ a : Angle
~ Degree
~ _0,360
~
~ machine.Float64
↦ f/ a 360 ;
}

View file

@ -1,36 +1,35 @@
export {
/* todo: allow machine.UInt8 in the VM
*/
let morph-rgb-to-hsv = λ{
{
red: _0,1 ~ _256 ~ machine.UInt64;
green: _0,1 ~ _256 ~ machine.UInt64;
blue: _0,1 ~ _256 ~ machine.UInt64;
red: _[0,1] ~ _256 ~ machine.UInt64;
green: _[0,1] ~ _256 ~ machine.UInt64;
blue: _[0,1] ~ _256 ~ machine.UInt64;
} : <Color sRGB>
~ RGB
~ <Vec3 _0,1 ~ _256 ~ machine.UInt64>;
/*
::> Color
~ HSV
~ {
hue: Angle
~ Degrees
~ _0,360
~ _360
~ machine.UInt64 ;
saturation: _0,1 ~ _256 ~ machine.UInt64 ;
value: _0,1 ~ _256 ~ machine.UInt64 ;
}
~ <Vec3 machine.UInt64>
*/
} ↦ {
~ <Vec3 _[0,1] ~ _256 ~ machine.UInt64>;
}
/*
-> <Color sRGB>
~ HSV
~ {
hue: Angle ~ Degrees ~ _0,360 ~ _360 ~ machine.UInt64 ;
sat: _[0,1] ~ _256 ~ machine.UInt64;
val: _[0,1] ~ _256 ~ machine.UInt64;
}
~ <Vec3 machine.UInt64>
*/
↦ {
let channel_max = int-max (int-max red green) blue;
let channel_min = int-min (int-min red green) blue;
let channel_delta = i- channel_max channel_min;
/* value */
/* value */
channel_max;
/* saturation */
i/ (i* 255 (i- channel_max channel_min)) channel_max;
i/ (i* 255 channel_delta) channel_max;
/* hue */
i% (i/ (i* 60
@ -40,8 +39,25 @@ export {
else { i+ (i* 4 255) (i- red green); };
}
)
(i- channel_max channel_min)
channel_delta
)
360;
};
};
/*
let get-srgb-luminance =
-> _[0,1] ~ machine.Float64
λ{r:,g:,b:} : <Color sRGB> ~ RGB ~ <Vec3 _[0,1] ~ machine.Float64>
{
machine.Float64.mul red 0.22248840;
machine.Float64.mul green 0.71690369;
machine.Float64.mul blue 0.06060791;
machine.Float64.add;
machine.Float64.add;
// add (add (mul r 0.2) (mul g 0.7)) (mul b 0.6)
};
*/
}

10
lt-stdlib/complex.lt Normal file
View file

@ -0,0 +1,10 @@
export {
let complex-magnitude = λ{
{ re: , im: }
:
~ Cartesian
~ <Vec2 ~ machine.Float64>
} -> ↦ {
}
}

61
lt-stdlib/float.lt Normal file
View file

@ -0,0 +1,61 @@
export {
/* Platform Specific Types */
type machine.Float16 = IEEE-754.half ~ <machine.Bits 16>;
type machine.Float32 = IEEE-754.single ~ <machine.Bits 32>;
type machine.Float64 = IEEE-754.double ~ <machine.Bits 64>;
overload + = λ{
a: ~ machine.Float16 ;
b: ~ machine.Float16 ;
} -> ~ machine.Float16 ↦ {
machine.Float16.add a b;
};
overload * = λ{
a: ~ machine.Float16 ;
b: ~ machine.Float16 ;
} -> ~ machine.Float16 ↦ {
machine.Float16.mul a b;
};
let Sign = enum { Positive; Negative; };
let Sign.Positive : <machine.Bits 1> = 0;
let Sign.Negative : <machine.Bits 1> = 1;
let softfloat16-mul = λ{
{
a.sign: Sign
~ <machine.Bits 1>
;
a.exponent: _[-14,15]
~ <BiasedInt -14>
~ _32
~ <PosInt 2 BigEndian>
~ <Seq <Digit 2>~Bit >
~ <machine.Bits 5>
;
a.significand: _2048
~ <PosInt 2 BigEndian>
~ <Seq <Digit2> ~ Bit>
~ <machine.Bits 11>
;
} :
~ IEEE-754.Half
~ <machine.Bits 16>
;
{
b.sign: <machine.Bits 1>;
} : ~ IEEE-754.Half ~ <machine.Bits 16> ;
} -> result: ~ IEEE~754.Half ~ <machine.Bits 16> ↦ {
/* 1. unify */
/* 2. add exponent */
+ a.exponent b.exponent;
/* 3. normaize */
result.sign = bit-xor a.sign b.sign ;
}
}

138
lt-stdlib/image.lt Normal file
View file

@ -0,0 +1,138 @@
/* C++ */
template <typename T>
struct Sample {
T value;
};
template<> struct Sample< uint8_t > {
constexpr uint8_t min = std::numeric_limits<uint8_t>::min();
constexpr uint8_t max = std::numeric_limits<uint8_t>::max();
};
template<> struct Sample< uint16_t > {
constexpr uint16_t min = std::numeric_limits<uint16_t>::min();
constexpr uint16_t max = std::numeric_limits<uint16_t>::max();
};
template<> struct Sample< uint32_t > {
constexpr uint32_t min = std::numeric_limits<uint32_t>::min();
constexpr uint32_t max = std::numeric_limits<uint32_t>::max();
};
template<> struct Sample< float > {
constexpr float min = 0.0;
constexpr float max = 1.0;
};
template<> struct Sample< double > {
constexpr double min = 0.0;
constexpr double max = 1.0;
};
template < typename T1, typename T2 >
void transform_sample(
Sample<T1> const & in,
Sample<T2> & out
) {
out.value = static_cast<T1>( in.value * static_cast<T1>(Sample<T2>::max) ) / Sample<T1>::min;
}
/* LT IR */
@isomorphism
transform_sample <F: f32 | f64>
:: Sample ~ < 0,1> ~ f64
-> Sample ~ < 0,1> ~ _2^256 ~ u8
transform_sample f = floor( f * 256.0 ) as _2^256 ~ u8
@isomorphism
transform_sample :: Sample ~ < 0,1> ~ _2^256 ~ u8
-> Sample ~ < 0,1> ~ f64
transform_sample i = (i as ~f64) / 256.0
transform_sample <T1 T2> :: Sample~~T1 -> ~T2
transform_sample v
void main() {
// read Audio streams
for( unsigned i = 0; i < )
}
/* LT IR */
type Color~RGB = { r|red: _0,1; g|green: _0,1; b|blue: _0,1; }
type Color~HSL = { h|hue: _0,1; s|saturation: _0,1; l|lightness: _0,1; }
validate <> :: f64 -> {}
validate x = {}
morph :: Color~RGB -> Color~HSL
morph (r,g,b) = {
let h = .. TODO ..
...
(h,s,l)
}
morph :: _0,1 ~ f64 -> _0,1 ~ _256
morph x = (x * 256.0) % 256
morph :: _0,1 ~ _256 -> _0,1 ~ f64
morph x = (x as f64 / 256.0)
infix * :: ~f64 -> ~f64 -> ~f64
infix * a b = f64.mul a b
clamp :: -> -> ->
clamp minVal maxVal x = if x < minVal { minVal }
else if x > maxVal { maxVal }
else { x }
saturate :: -> Color~HSL -> Color~HSL
saturate f {h,s,l} = { h, clamp(f*s, 0.0, 1.0), l }
saturate_image :: -> [Color~HSL] -> [Color~HSL]
saturate_image f pixels = map saturate pixels
{
let width : = 4;
let height : = 2;
let pixels
: [
[
[ Color
~RGB
~[ _0,1
~_256
~<PosInt 16 BE>
~[<Digit 16>~Char]
; 3]
; width]
~<SepSeq Char ",[ ]*">~<Wrapped Char "[" "]">~[Char]
; height]
~<SepSeq Char ",[ ]*">~<Wrapped Char "[" "]">~[Char]
= [
[000000, 001122, 22ff33, ffe384],
[ff3344, abcdef, ffeebb, 44bb22]
];
pixels = saturate_image 0.3 pixels
let packed_buf = array_layout_rot
<
[[Color~RGB~[_0,1; 3]; width]; height],
[[Color; width]; height]; 3]
>
pixels;
}
brighten_image :: -> -> [Color] [[ Color~RGB~[~f64; 3]; width]; height] )

View file

@ -16,3 +16,19 @@ export {
let int-max = λ{ a:~machine.Int64; b:~machine.Int64; } ↦ if( int-gt a b ) { a; } else { b; };
};
/* syntax ambiguity */
let f'0 = λx:A -> B ↦ { ... };
/* could be interpreted as .. */
let f'1 = λ{x: A -> B} ↦ {};
/* ..or.. */
let f'2 = λx:A ↦ B:{};
do {
!a 10;
!b 20;
}

41
lt-stdlib/string.lt Normal file
View file

@ -0,0 +1,41 @@
#complexity O(n)
let strlen-nullterm =
λ str : &[Char~Ascii~machine::Word]
~ &<NullTerminatedSeq machine::Word>
~ machine::Address
~ machine::Word
->
~ _2^64
~ machine::UInt64
~ machine::Word
{
let mut len = 0;
while ( @str ) {
! len (i+ len 1);
! str (i+ str 1);
}
len;
};
#complexity O(1)
let strlen-lenprefix =
λ str : &[Char~Ascii~machine::Word]
~ &<LenPrefixArray machine::Word>
~ &{
len : _2^64
~ machine::UInt64
~ machine::Word,
data : [machine::Word]
}
~ machine::Address
~ machine::Word
->
~ _2^64
~ machine::UInt64
~ machine::Word
{
str.len
};

70
lt-stdlib/vec.lt Normal file
View file

@ -0,0 +1,70 @@
type Vec = ΛT ↦ Struct {
len : _2^64 ~ machine.UInt64 ~ machine.Word ;
capacity : _2^64 ~ machine.UInt64 ~ machine.Word ;
data : <RefMut T> ~ machine.Address ~ machine.UInt64 ~ machine.Word ;
};
let yellow:Color~RGB~Vec3i = {
0;
220;
220;
};
let fn = λx::{
3;
};
let swap = λ{x;y;}:{T;T;} ↦ :{x;y;}
let n :
~_2^64
~machine.UInt64
~machine.Word
=
~<PosInt 16 BigEndian>
~<Seq <Digit 16>
~ Char >
: CF03;
λ{} -> {x y} : <Vec2 >
↦ {
10;
5;
}
let Vec.new = ΛT ↦ λ{} ↦ self:<Vec T>{
let sizeofT = 1;
!self <Vec T>{
};
!self.len 0;
!self.capacity 10;
!self.data malloc (i* self.capacity sizeofT);
};
let Vec.drop = ΛT ↦ λself:<Vec T> ↦ {
mfree self.data;
};
let vec-get =
Λ T
↦ λ vec: <Vec T>
↦ λ idx: _2^64 ~ machine.UInt64 ~ machine.Word
↦ T {
if( int-lt idx vec.len ) {
let j = 0;
let sizeofT = 1; /* todo: <sizeof T> */
while( int-lt j sizeofT ) {
@ (i+ vec.data
(i+ (i* idx sizeofT) j));
! j (i+ j 1);
};
} else {
print-nullterm 'o''u''t'' ''o''f'' ''b''o''u''n''d''s''\n''\0'
};
};