backup commit of notes so far

This commit is contained in:
2023-05-14 20:35:31 +01:00
parent 6a381c5b57
commit 33413b2b0f
34 changed files with 1109 additions and 421 deletions

View File

@@ -1,45 +0,0 @@
opaque := \T. T
--[ Typeclass definition (also just a type) ]--
define Add $L:type $R:type $O:type as $L -> $R -> $O
-- HKTC
define Mappable $C:(type -> type) as @T. @U. (T -> U) -> $C T -> $C U
-- Dependency on existing typeclass
define Zippable $C:(type -> type) as @:Mappable $C. (
@T. @U. @V. (T -> U -> V) -> $C T -> $C U -> $C V
)
define Default $T:type as $T
--[ Type definition ]--
define Cons $elem:type as loop \r. Option (Pair T $elem)
nil := @T. from @(Cons T) none
cons := @T. \el:T. (
generalise @(Cons T)
|> (\list. some t[el, into list])
|> categorise @(Cons T)
)
export map := @T. @U. \f:T -> U. (
generalise @(Cons T)
|> loop ( \recurse. \option.
map option \pair. t[f (fst pair), recurse (snd pair)]
)
|> categorise @(Cons U)
)
-- Universal typeclass implementation; no parameters, no overrides, no name for overriding
impl Mappable Cons via map
-- Blanket typeclass implementation; parametric, may override, must have name for overriding
impl (@T. Add (Cons T) (Cons T) (Cons T)) by concatenation over elementwiseAddition via concat
-- Scratchpad
filterBadWords := @C:type -> type. @:Mappable C. \strings:C String. (
map strings \s. if intersects badWords (slice " " s) then none else some s
):(C (Option String))
-- /Scratchpad
main := \x. foo @bar x
foo := @util. \x. util x
export opaque := \T. atom

15
examples/lite/fn.orc Normal file
View File

@@ -0,0 +1,15 @@
export Y := \f.(\x.f (x x))(\x.f (x x))
export loop $r on (...$parameters) with ...$tail =0x5p512=> Y (\$r.
bind_names (...$parameters) (...$tail)
) ...$parameters
-- bind each of the names in the first argument as a parameter for the second argument
bind_names ($name ..$rest) $payload =0x2p1000=> \$name. bind_names (..$rest) $payload
bind_names () (...$payload) =0x1p1000=> ...$payload
export ...$prefix $ ...$suffix:1 =0x1p130=> ...$prefix (...$suffix)
export ...$prefix |> $fn ..$suffix:1 =0x2p130=> $fn (...$prefix) ..$suffix
export (...$argv) => ...$body =0x2p512=> (bind_names (...$argv) (...$body))
$name => ...$body =0x1p512=> (\$name. ...$body)

41
examples/lite/list.orc Normal file
View File

@@ -0,0 +1,41 @@
import option
import super::fn::*
pair := \a.\b. \f. f a b
-- Constructors
export cons := \hd.\tl. option::some (pair hd tl)
export end := option::none
export pop := \list.\default.\f.list default \cons.cons f
-- Operators
export reduce := \list.\acc.\f. (
loop r on (list acc) with
pop list acc \head.\tail. r tail (f acc head)
)
export map := \list.\f. (
loop r on (list) with
pop list end \head.\tail. cons (f head) (r tail)
)
export skip := \list.\n. (
loop r on (list n) with
if n == 0 then list
else pop list end \head.\tail. r tail (n - 1)
)
export take := \list.\n. (
loop r on (list n) with
if n == 0 then end
else pop list end \head.\tail. cons head $ r tail $ n - 1
)
new[...$item, ...$rest:1] =0x2p333=> (cons (...$item) new[...$rest])
new[...$end] =0x1p333=> (cons (...$end) end)
new[] =0x1p333=> end
export ::(new)

View File

@@ -1,9 +1,10 @@
import prelude::*
import std::(parse_float, to_string)
import std::(readline, print)
import std::(readline, print, debug)
import std::(concatenate)
import super::list
import fn::*
export main := do{
--[ export main := do{
cps data = readline;
let a = parse_float data;
cps op = readline;
@@ -19,6 +20,35 @@ export main := do{
);
cps print (to_string result ++ "\n");
0
} ]--
export main := do{
let foo = list::new[1, 2, 3, 4, 5, 6];
let bar = list::map foo n => n * 2;
let sum = bar
|> list::skip 2
|> list::take 3
|> list::reduce 0 (a b) => a + b;
cps print $ to_string sum ++ "\n";
0
}
-- export main := 1 do { 1 ; 2 } 3
--[
export main := do{
let n = 1;
let acc = 1;
loop r on (n acc) with (
if n == 5
then print acc
else r (n + 1) (acc * 2)
)
}
]--
--[
export main := do{
let n = 1;
loop r on (n) with (
debug r
)
}
]--

6
examples/lite/option.orc Normal file
View File

@@ -0,0 +1,6 @@
export some := \v. \d.\f. f v
export none := \d.\f. d
export map := \option.\f. option none f
export flatten := \option. option none \opt. opt
export flatmap := \option.\f. option none \opt. map opt f

1
examples/scratch.hs Normal file
View File

@@ -0,0 +1 @@
main = sequence

View File

@@ -1,72 +0,0 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE BlockArguments #-}
import Prelude((>>=), Maybe( Just, Nothing ), return, fmap)
import Debug.Trace
-- 1
class Add l r o where
add :: l -> r -> o
(+) :: l -> r -> o
(+) = add
-- 2
class Mappable c where
map :: (i -> o) -> c i -> c o
-- 3
class Mappable c => Zippable c where
zip :: (l -> r -> o) -> c l -> c r -> c o
-- 4
class Default t where
def :: t
-- 5
instance (Zippable c, Add l r o)
=> Add (c l) (c r) (c o) where
add :: (Zippable c, Add l r o) => c l -> c r -> c o
add = zip add
-- 6
-- newtype List t = List (Maybe (t, List t))
-- instance Mappable List where
-- map :: (i -> o) -> List i -> List o
-- map f (List o) = List (fmap (\(h, t) -> (f h, map f t)) o)
-- instance Zippable List where
-- zip :: (l -> r -> o) -> List l -> List r -> List o
-- zip f (List l) (List r) = List do
-- (lh, lt) <- l
-- (rh, rt) <- r
-- return (f lh rh, zip f lt rt)
-- instance Add (List e) (List e) (List e) where
-- add (List l) (List r) = List case l of
-- Just (head, tail) -> Just (head, add tail r)
-- Nothing -> r
data List t = Cons t (List t) | End
instance Mappable List where
map :: (i -> o) -> List i -> List o
map _ End = End
map f (Cons head tail) = Cons (f head) (map f tail)
instance Zippable List where
zip :: (l -> r -> o) -> List l -> List r -> List o
zip _ _ End = End
zip _ End _ = End
zip f (Cons lhead ltail) (Cons rhead rtail) =
Cons (f lhead rhead) (zip f ltail rtail)
instance Add (List e) (List e) (List e) where
add End r = r
add (Cons head tail) r = Cons head (add tail r)

View File

@@ -1,65 +0,0 @@
-- 1
define Add $L $R $O
as $L -> $R -> $O
$left:2... + $right:1... =1000=> add ($left...) ($right...)
-- 2
define Mappable $C:type -> type
as @I. @O. (I -> O) -> $C I -> $C O
-- 3
define Zippable $C:type -> type
as @:Mappable $C.
@L. @R. @O. (L -> R -> O) -> $C L -> $C R -> $C O
-- 4
define Default $T:type as $T
-- 5
impl
@C:Type -> Type. @L. @R. @O.
@:(Zippable C). @:(Add L R O).
Add (C L) (C R) (C O)
by elementwiseAdd
via zip add
-- 6
define List $E as Y \r. Option t[ $E, r ]
impl Mappable List
via \f.\list. categorise (
(Y \repeat. \opt. match opt {
Some t[head, tail] =>
Some t[f head, repeat tail];
None => None;
}) (generalise list)
)
impl Zippable List
via \f.\l.\r. categorise (
Y \repeat.\lopt.\ropt. do {
bind t[lhead, ltail] <- lopt;
bind t[rhead, rtail] <- ropt;
t[f lhead rhead, repeat ltail rtail]
}
) (generalise l) (generalise r)
impl @T. Add (List T) (List T) (List T)
by concatListAdd over elementwiseAdd
via \l.\r.categorise Y \repeat.\l. (
match l (
Some t[head, tail] =>
Some t[head, repeat tail];
None => (generalise r)
)
) (generalise l)