aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--samples/rosetta/universal_lambda_machine.bruijn28
-rw-r--r--std/Combinator.bruijn2
-rw-r--r--std/Meta.bruijn30
-rw-r--r--std/Number/Conversion.bruijn12
4 files changed, 48 insertions, 24 deletions
diff --git a/samples/rosetta/universal_lambda_machine.bruijn b/samples/rosetta/universal_lambda_machine.bruijn
index 2f003ce..60ba551 100644
--- a/samples/rosetta/universal_lambda_machine.bruijn
+++ b/samples/rosetta/universal_lambda_machine.bruijn
@@ -1,6 +1,6 @@
:import std/Combinator .
:import std/Number/Binary .
-:import std/Meta .
+:import std/Meta M
:import std/List .
# converts string to list of bits
@@ -13,7 +13,27 @@ blc→str map [0 '0' '1']
:test (blc→str ([[1]] : ([[1]] : ([[0]] : {}[[1]])))) ("0010")
-# reduces BLC string to BLC string
-main str→blc → blc→meta → β* → meta→blc → blc→str
+# evaluates BLC string
+main str→blc → M.blc→meta+rest → &M.eval → blc→str
-:test (main "0010") ("0010")
+# --- tests ---
+
+id "0010"
+
+# 342 bit IO example
+io "010100011010000000011000010110011110000010010111110111100001010110000000011000011111000000101111110110010111111011001011110100111010111100010000001011100101010001101000000000010110000101011111101111100000010101111011111011111100001011000000101111111010110111000000111111000010110111101110011110100000010110000011011000100000101111000111001110"
+
+# quine example
+quine "000101100100011010000000000001011011110010111100111111011111011010000101100100011010000000000001011011110010111100111111011111011010"
+
+# 100 doors example
+doors "0001000100010101000110100000010110000011001110110010100011010000000000101111111000000101111101011001011001000110100001111100110100101111101111000000001011111111110110011001111111011100000000101111110000001011111010110011011100101011000000101111011001011110011110011110110100000000001011011100111011110000000001000000111001110100000000101101110110"
+
+# sieve of Eratosthenes example
+primes "00010001100110010100011010000000010110000010010001010111110111101001000110100001110011010000000000101101110011100111111101111000000001111100110111000000101100000110110"
+
+:test (main id) (empty)
+:test (main io) ("11010")
+:test (main quine) (quine)
+:test (take (+20) (main doors)) ("10010000100000010000")
+:test (take (+20) (main primes)) ("00110101000101000101")
diff --git a/std/Combinator.bruijn b/std/Combinator.bruijn
index 81db105..7199c77 100644
--- a/std/Combinator.bruijn
+++ b/std/Combinator.bruijn
@@ -157,6 +157,8 @@ s [[[2 0 (1 0)]]] ⧗ (a → b → c) → (a → b) → a → c
# thrush combinator: flipped $
t [[0 1]] ⧗ a → (a → b) → b
+&‣ t
+
…&… t
# turing combinator
diff --git a/std/Meta.bruijn b/std/Meta.bruijn
index 5f8da84..3b4ccbd 100644
--- a/std/Meta.bruijn
+++ b/std/Meta.bruijn
@@ -94,24 +94,26 @@ blc1 [[0]] ⧗ LcBit
blc0 [[1]] ⧗ LcBit
# converts blc index to meta index + rest
-blc→unary y [[[rec]]] (+0u) ⧗ (List LcBit) → (Pair Meta (List LcBit))
+blc→unary+rest y [[[rec]]] (+0u) ⧗ (List LcBit) → (Pair Meta (List LcBit))
rec 0 [[1 case-end case-rec]]
case-rec 4 ++3 0
case-end (idx --3) : 0
-:test (blc→unary (blc1 : {}blc0)) (`0 : [[0]])
-:test (blc→unary (blc1 : (blc1 : {}blc0))) (`1 : [[0]])
-:test (blc→unary (blc1 : (blc1 : (blc1 : {}blc0)))) (`2 : [[0]])
-:test (blc→unary (blc1 : (blc1 : (blc1 : (blc0 : {}blc1))))) (`2 : {}blc1)
+:test (blc→unary+rest (blc1 : {}blc0)) (`0 : [[0]])
+:test (blc→unary+rest (blc1 : (blc1 : {}blc0))) (`1 : [[0]])
+:test (blc→unary+rest (blc1 : (blc1 : (blc1 : {}blc0)))) (`2 : [[0]])
+:test (blc→unary+rest (blc1 : (blc1 : (blc1 : (blc0 : {}blc1))))) (`2 : {}blc1)
# converts blc list to meta term
# TODO: use CSP instead?
-blc→meta head ∘ (y [[rec]]) ⧗ (List LcBit) → Meta
+blc→meta+rest y [[rec]] ⧗ (List LcBit) → (Pair Meta (List LcBit))
rec 0 [[1 case-0 case-1]]
case-0 ^0 case-00 case-01
case-00 3 ~0 [[(abs 1) : 0]]
case-01 3 ~0 [[5 0 [[(app 3 1) : 0]]]]
- case-1 blc→unary 2
+ case-1 blc→unary+rest 2
+
+blc→meta head ∘ blc→meta+rest ⧗ (List LcBit) → Meta
:test (blc→meta (blc0 : (blc0 : (blc1 : {}blc0)))) (`[0])
:test (blc→meta (blc0 : (blc0 : (blc0 : (blc0 : (blc0 : (blc1 : (blc1 : (blc1 : (blc0 : (blc1 : (blc1 : {}blc0)))))))))))) (`[[1 1]])
@@ -254,21 +256,21 @@ map [[0 case-idx case-app case-abs]] ⧗ (Meta → Meta) → Meta → Meta
# inspired by Helmut Brandl's and Steve Goguen's work
# uses ternary for lower space complexity
encode fold idx-cb app-cb abs-cb ⧗ Meta → Number
- idx-cb (pair-ternary (+0)) ∘ unary-to-ternary
+ idx-cb (pair-ternary (+0)) ∘ unary→ternary
app-cb (pair-ternary (+1)) ∘∘ pair-ternary
abs-cb pair-ternary (+2)
-:test (ternary-to-unary (encode `0)) ((+0u))
-:test (ternary-to-unary (encode `1)) ((+1u))
-:test (ternary-to-unary (encode `(0 0))) ((+3u))
-:test (ternary-to-unary (encode `[1])) ((+7u))
-:test (ternary-to-unary (encode `[0])) ((+8u))
+:test (ternary→unary (encode `0)) ((+0u))
+:test (ternary→unary (encode `1)) ((+1u))
+:test (ternary→unary (encode `(0 0))) ((+3u))
+:test (ternary→unary (encode `[1])) ((+7u))
+:test (ternary→unary (encode `[0])) ((+8u))
# decodes ternary encoding back to meta term
# TODO: improve performance (maybe with different unpairing function or better sqrt)
decode y [[unpair-ternary 0 go]] ⧗ Number → Meta
go [[(case-idx : (case-app : {}case-abs)) !! 1 0]]
- case-idx idx ∘ ternary-to-unary
+ case-idx idx ∘ ternary→unary
case-app [unpair-ternary 0 (ψ app 4)]
case-abs abs ∘ 3
diff --git a/std/Number/Conversion.bruijn b/std/Number/Conversion.bruijn
index fe7e63c..41ddf38 100644
--- a/std/Number/Conversion.bruijn
+++ b/std/Number/Conversion.bruijn
@@ -5,13 +5,13 @@
:import std/Number/Ternary T
# converts unary numbers to ternary
-unary-to-ternary [0 T.inc (+0)] ⧗ Unary → Ternary
+unary→ternary [0 T.inc (+0)] ⧗ Unary → Ternary
-:test (unary-to-ternary (+0u)) ((+0))
-:test (unary-to-ternary (+2u)) ((+2))
+:test (unary→ternary (+0u)) ((+0))
+:test (unary→ternary (+2u)) ((+2))
# converts ternary numbers to unary
-ternary-to-unary [T.apply 0 U.inc (+0u)] ⧗ Ternary → Unary
+ternary→unary [T.apply 0 U.inc (+0u)] ⧗ Ternary → Unary
-:test (ternary-to-unary (+0)) ((+0u))
-:test (ternary-to-unary (+2)) ((+2u))
+:test (ternary→unary (+0)) ((+0u))
+:test (ternary→unary (+2)) ((+2u))