// In [LANG], everything is made up of sets. Yes, everything: Numbers, strings, functions, etc.
//
// Numbers are sets
//
// A function is a set of relations. Relations are ordered pairs. Ordered pairs are sets.
//
// A function can also act as a list. A list-functioon is a set of relations that have a Nat as input and and an output.
// Example of a list:
{
0 -> 10,
1 -> 20,
2 -> 30
}
// String is the set of all strings. A string is a list of characters (Unicode code points).
// Example of a string:
{
0 -> 'W'
1 -> 'i'
2 -> 'l'
3 -> 'k'
4 -> 'o'
}
// Is the same as:
['W', 'i', 'l', 'k', 'o']
// Above is exactly the same as:
"Wilko"
// Code points must use single characters, meaning these are equivalent:
"a" {'a'}
// But the following is not equivalent to the above:
'a'
// If you have a relation
"a" -> ..
a -> ..
// This relation can be applied (if in scope), by referring to the string argument without quotes:
a
// Or by passing the string as an argument to the Scope set. The scope set is a special set that contains all relations currently
// in scope. The Scope set is always in scope. TODO: Contradiction, Scope includes Scope??
Scope "a"
// The Scope set can also be referred to as $. (""−>Scope) "a"
$"a"
// IMAGINE:
-> {
"a" -> 3
}
// Applied:
$ "a"
// More examples:
"b" ->
// Here, in the "body" of the relation, there's an implicit relation in scope: "x" -> (whichever Int is the input)
@x : Int -> x + x
b 30 // Result: 60
// A file represents a relation: The filename (path) is an atom. The file contents can be any element. Together
// they form the relation "FileName" -> FileContents. The extension is excluded from the FileName string.
// The file name "myFile" is a string.
// FILE myFile.elem
// FILE relation: "myFile" -> 1
// FILE contents:
1
// FILE myOtherFile.elem
// FILE relation: "myOtherFile" -> {0, "abc", 3.5}
// FILE contents:
{0, "abc", 3.5}
// FILE someFile.elem
// FILE relation: someFile -> (x : Int -> x * x)
// FILE contents:
@x : Int -> (x * x)
// FILE Main.elem
// FILE relation: "someFile" -> {..}
// FILE contents:
{
// "Import"
myOtherFile -> $SubDir myOtherFile,
// TODO: Main will juse be a file-relation "Main" ->
// Remember: This is a relation of the string "main" to a set containing some relations.
"main" -> {
// Since relations are unnamed, everything gets called implicitely through the Scope ()set"U" "ProjectName" "Main" "fib" 1
1 -> (((($"U" "ProjectName") "Main") "fib") 1)
// Implicitely:
1 -> Main fib 1
// More implicit:
1 -> fib 1
},
"Nat" -> {...(@x : {...@i : Int -> i >= 0 })},
"example" ->
@y : Int -> ..,
"func" ->
@x : Int -> $"example" x,
"func" ->
@x : {...@i : Int -> i <= 5} ->
func 7
$"func" 8
"func" 9 // ERROR
// (actually in this case it would be an error for a different reason,
// because a string is a set with relations, it would return the
// 6th character (which does not exist in this string))
// Statements are just functions chained.
// TODO REDO
funcWithStatements ->
@x : Int ->
// Can apply without any argument as well. TODO?
|> @s = {x, @y : Int -> 30 * y} -> s
|> {@x, @f} -> (fx)∗(fx)∣>@z−> ($ z * z) * 10,
}
// IMAGINE:
// The "universe" set contains everything that's declared in the program.
"Universe" -> {
"ProjectName" -> {
myFile,
someFile,
"Main" -> {..},
"Math" -> {
"Lib" -> {
"min" ->
@x : Int -> @y : Int -> ..
}
}
}
}
U -> Universe
// Import: TODO
_ -> ./std/Core
_ -> {
main,
main -> fib 30,
fib,
fib ->
n : Int -> n |>
_ : {0, 1} -> fib n - 1
_ -> fib (plus(min n 1) + 1)
}
// Handling side effects with handles:
//
// A relation, given the same input, always gives the sames output.
//
// This is not the case for effectful relations.
// Effectful relations have a handle as input, which means the output is not guaranteed to be the same everytime.
// Effectful relations are defined with ~> instead of ->.
"Console" -> {
"readLine" -> (None ~> ^String)
"printLine" -> (@text : String ~> ^$Unit)
}
// Effectful relations can only be called in in UProjectName Mainmain
"Effect" -> {..} // Natively defined. Contains all effect handlers, defined natively.
$ "Effect" "Console"
// Equivalent to:
~"Console"
// Main.elem
{
"main" ->
// Because an effectful relation is used, main must become effectful as well.
@args : String ~> ~Console printLine "Hello, world!"
}
// TODO: Make set types pattern match
// $x could be anything. Assigned to type parameter 'a' ?
@x -> @x
// @x is an element from the Intset@x:Int -> $ x "*" x
// @x is an element from the set {0, 1, 2, 3}. Basically a union type.
@x : {0, 1, 2, 3} -> $ x "*" x
"Even" -> {...@y : Int−>y |> rem 2 |> eq 0}
// MAYBE?: Use different symbol for variable binding before and after :
@x : {...@y : Int−>y |> rem 2 |> eq 0} -> ..
@x : $Even -> ..
@x : Even & Nat -> ..
@x : & EvenNat -> ..
@x : {{0, 1, @y, z}} -> .. // x is bound to {0, 1, ..}, yisboundtowhicheverotherelementwasinx.
// Define infix relation:
{
// Normal relation:
"SomeName" ->
%x : Int−>Int -> x+y,
// Applied:
$SomeName 1 2,
// Infix relation:
@x : Int -> _ : {"SomeName"} -> @y : Int -> x+y,
$0 "SomeName" 3,
"MyInt" -> 0,
// TODO: Maybe $ is not necessary, consider this:
// Defining a relation like
"SomeName" -> $x : $Int -> $x * $x,
// Means that the string "SomeName" can now accept an Int argument.
// Applied:
SomeName 3,
// PROBLEM: What to do with relations bound to an atom with 'no arguments' (the atom itself would be the argument).
"SomeName" -> "asdasd",
"SomeName" // UNCLEAR: Does this expression evaluate to "SomeName" or "asdasd"?
// POSSIBLE SOLUTION:
"SomeName" _
_ SomeName
U SomeName
$ SomeName
$SomeName
// POSSIBLE SOLUTION:
"SomeName" // evaluates to "SomeName"
SomeName // evaluates to "asdasd"
// Meaning that strings must always be with quotes, and that a string without quotes would be as if it's
// applied from the U set. Below are all equivalent:
U "SomeName"
$"SomeName"
SomeName
1 -> 3,
// PROBLEM:
// How does this work with number atoms?
1 // evaluates to 1
1 // evaluates to.. 1
// SOLUTION:
// Must be invoked using the scope set
U 1 // Evaluates to 3
$1 // Evaluates to 3
'a' -> 'b'
// PROBLEM:
// How does this work with character atoms?
'a' // evaluates to 'a'
'a' // evaluates to.. 'a'
// SOLUTION:
// Must be invoked using the scope set
U 'a' // Evaluates to 'b'
$'a' // Evaluates to 'b'
// PROBLEM: Infix is still not possible without $, except on strings
'a' -> "+" -> 'b'
'a' + 'b' // ERROR: Atom cannot be applied
$ 'a' + 'b'
// SOLUTION:
// Don't care?
// Intersection of set definition:
// Infix:
@left <: {{...}} -> "&" -> @right <: {{...}} -> // Natively defined
// PROBLEM: Infix function name must be a string, since it's not in scope.
// SOLUTION: Create relation to itself:
"&" -> "&"
$ MySet & MyOtherSet, // Evaluates to {1, 2, 3, 4, 5, 6}
$ $"MySet" $"&" $"MyOtherSet", // Evaluates to {1, 2, 3, 4, 5, 6}
// Normal:
"intersect" ->
@left <: {{...}} -> @right <: {{...}} -> // Natively defined
MySet -> {1, 2, 3},
MyOtherSet -> {4, 5, 6},
// TODO: Fluent APIs with constant string args
"when" ->
b : {0, 1}
// TODO: Holes (_) filled & unfilled
// Bool definition:
"true" -> 1,
"false" -> 0,
"Bool" -> {true, false},
"!" -> {
true -> false,
false -> true
},
// Applied:
!true,
// Also applied:
$ "!" true,
// Or even:
$ "!" ($ "true"),
// Name relations that contain non-alphanumeric characters (defined by Unicode), cannot be referenced without quotes:
"asd^!abc" -> {1, 2, 3},
asd^!abc,
// Above evaluates to:
($ "asd") ($ "^") ($ "!") ($ "abc"),
// To reference the name relations, use:
$ "asd^!abc",
// Importing:
...($ "SubDir" "SomeFile")
...(SubDir "SomeFile"),
// String format:
"format" -> {
@x : Any -> {
EOT -> toString x
@y : Any -> format (concat (toString x) (toString y))
}
},
format 0 // RESULT: (@y -> format (concat (toString 0) (toString y)))
format 0 1 // RESULT: (@y -> format (concat (toString "01") (toString y)))
format 0 1 EOT // RESULT: toString "01" // RESULT: "01"
"toString" ->
@x : Int -> .. // Native?
"concat" ->
@left : String -> @right : String -> // RESULT: left + right
"myFunc" -> {
// There should be a LINT, if the source-order does not match the specificity of the inputs of the relations.
x : Int -> ($ x * x)
x : Any -> ..
},
// When a set of relations (a function) has overlapping inputs, the more specific relation is chosen.
// In this case, the first (in source-order) relation is invoked.
myFunc 1,
"EOT" -> codePointAt 4,
"⌁" -> EOT,
"codePointAt" ->
n : Nat -> ..
hex : String -> ..
"specifySize" ->
someSet : {3#{...Int}} ->
"0" -> {},
"1" -> {0}, // { {} }
"2" -> {0, 1}, // { {}, { {} } }
"-" ->
@nat : Nat -> {{0}, {0, nat}}
// Tuple syntax: (ordered pair):
[a, b]
// Equivalent:
{{a}, {a, b}}
[b, a]
{{b}, {b, a}}
[a, b, c]
{{a}, {a, b}, {a, b, c}} // TODO: Correct?
x : Nat -> ($ x + x),
// Equivalent to:
{
[0, + 0 0],
[1, + 1 1],
[2, + 2 2],
[3, + 3 3],
...
},
// Equivalent to:
{
{{0}, {0, + 0 0}},
{{1}, {1, + 1 1}},
{{2}, {2, + 2 2}},
{{3}, {3, + 3 3}},
...
},
// If you want, you can define functions as above, but it can be cumbersome:
{
{{0}, {0, + 0 0}},
{{1}, {1, + 1 1}},
{{2}, {2, + 2 2}},
{{3}, {3, + 3 3}},
// NOTE: Not continued! This all there is.
},
// Above is equivalent to:
@x : {0, 1, 2, 3} -> + x x,
// 2 functions being made one:
// Notice how we have to spread them into the outer set, otherwise the set would contain 2 sets, both with 2 relations.
{
...@x : {0, 1} -> + x x,
...@x : {2, 3} -> * x x
},
// Equivalent:
{
...{
{{0}, {0, + 0 0}},
{{1}, {1, + 1 1}},
},
...{
{{2}, {2, * 2 2}},
{{3}, {3, * 3 3}},
}
},
// Equivalent:
{
...{
[0, + 0 0],
[1, + 1 1],
},
...{
[2, * 2 2],
[3, * 3 3],
}
},
// Equivalent:
{
[0, + 0 0],
[1, + 1 1],
[2, * 2 2],
[3, * 3 3]
},
// Unicode code point at [x].
"U" ->
x : Nat -> {{}, x},
// Applied:
U x00E1,
U+00E1,
// And with some sugar:
'a',
// IMAGINE:
// A "file" with all hex numbers
// LINT: If both importing hex and decimal relations
"00E1" -> 225,
// IMAGINE:
// A "file" with all hex numbers, but with a prefix:
"x00E1" -> 225,
// IMAGINE:
// A "file" with all hex numbers, but with a plus prefix:
"+00E1" -> 225
// This whole relation will not be defined in source code.
"native" ->
// Since there are only two "things" (elements) in Elementary, sets and relations, :! Relation matches sets.
x :! Relation ->
}
// IMAGINE:
{
"myFunc" -> 1 -> * 1 1,
// Function
{
// Relation 1 (which is an ordered pair)
{
{"myFunc"},
{
"myFunc",
{
{{1}, {1, * 1 1}}
}
}
}
},
myFunc 1 // Returns 1 (from * 1 1)
// Function with 1 relation:
{[0, 1]} 1// Returns 1,
// If the function is defined in the same set, it's in scope:
0 // Returns 1
// Because of the above, a LINT warning should be display if there's a function in the top-level set without a name.
// In other words, if there's a function in the top-level set who's argument is not a string literal, it's probably
// a mistake and a warning should be displayed.
| 2 2
// Result of above:
| { {}, { {} } } { {}, { {} } }
"0" -> {}
"1" -> { {} } // {0}
"2" -> { {}, { {} } } // {0, 1}
"3" -> { {}, { {} }, { {}, { {} } } }
// REDEFINE ->
// -> is syntactic sugar for a relation: A single input with a single output. No names can be bound.
// A relation is actually an ordered pair (which is actually a certain kind of set).
"asd" -> 10
["asd", 10]
{{"asd"}, {"asd", 10}}
// A relation can be "invoked":
// ASSUME
$ "asd" // Returns 10
// =>
// => is syntactic sugar for a function: A set of relations. Can use name binding.
@a : {0, 1, 2} => * a 2,
// The above function implies the following relations:
0 -> * 0 2
1 -> * 1 2
2 -> * 2 2
// The function is equivalent to:
{
{{0}, {0, * 0 2}},
{{1}, {1, * 1 2}},
{{2}, {2, * 2 2}},
}
"myFunc" ->
@a : {0, 1, 2} => * a 2,
// Equivalent:
{
{"myFunc"},
{"myFunc", {
{{0}, {0, * 0 2}},
{{1}, {1, * 1 2}},
{{2}, {2, * 2 2}},
}
}
}
"S" ->
@n : Nat => |> n {
0 -> 0,
// No pattern necessary, we know the type is Nat - {0}.
// Note that this is not based on the ORDER of the relations, since it's a set there is no order.
// It's purely based on the elements of the set.
...@n => | n {n}
},
// TODO: The relation -> operator (and => for functions) is defined using the language. How?
// TODO: How to make infix possible (without adding new language features):
@a => (("|" -> @b) -> ..)// Native
// SOLUTION?:
// Define infix relations with (
"(" ->
@a => "|" -> @b => ")" -> .. /* Natve */
(someSet | otherSet)
// PROBLEM: Parentheses were used to disambiguate
// SOLUTION: Define catch-all defintion
"(" -> @a => ")" -> a
// If there are multiple relations (or functions?) with the same input, and both have function as output that have
// inputs, those function will become one in scope.
// Above two relations become:
// ERROR: One more step needed
"(" -> {
...@a => "|" -> @b => ")" -> .. /* Natve */,
...@a => ")" -> a
},
// If the function has overlapping inputs, and the function return a relation/function, the functions are combined:
"(" -> @a => {
"|" -> @b => ")" -> .. /* Natve */,
")" -> a
},
")" -> ")",
"|" -> "|"
( 20 // With the current scope, either `)` or `|` can be passed
// Set builder
{|@a : Int => a <= 10|}
// If function input have the exact same specificity,
// If there are 2 relations with the same input in scope, that's an error.
"abc" -> 10
"abc" -> 12
abc // Result: ??
// Define relation syntax through the language itself:
// It is defined, but not using the language's syntax.
// Should be displayed in pseudo-syntax when hovering over `=>` or `->`
@a => "->" -> @b
"(" ->
@a => _tupleOf a
"_tupleOf" ->
@a -> {
")" -> a
"," -> @b => _tupleOf {{a}, {a, b}}
}
$ "("
$ "(" 10 // Result: _tupleValue 10
$ "(" 10 "," // Result: _tupleValue 10 ","
$ "(" 10 "," 20 // Result: _tupleValue 10 "," (_tupleValue {{10}, {10, 20}})
$ "(" 10 "," 20 ")" // Result: {{10}, {10, 20}}
$ "(" 10 "," 20 "," // Result: _tupleValue 10 "," (_tupleValue {{10}, {10, 20}} ",")
$ "(" 10 "," 20 "," 30 // Result: _tupleValue 10 "," (_tupleValue {{10}, {10, 20}} "," (_tupleValue {{{{10}, {10, 20}}}, {{{10}, {10, 20}}, 30}}))
$ "(" 10 "," 20 "," 30 ")"
// Or:
(10, 20, 30)
$ "(" 10 ")" // 10
(10) // 10
[a, b]
{{a}, {a, b}}
[a, b, c]
{{a}, {a, b}, {a, b, c}}
// MAYBE: The only character that is pre-defined is:
∅
// To denote the empty set.
// If used with this symbol, the empty set is actually a relation that accepts argument that can be "put in" in the set
∅ ∅ // { {} }
∅ ∅ ∅ // { { {} } }
"{" ->
@a => _setOf a
"_setOf" ->
@a -> {
"}" -> // ?? SETOF a
"," -> @b => _setOf {a, b}
}
$ "("
$ "(" 10 // Result: _tupleValue 10
$ "(" 10 "," // Result: _tupleValue 10 ","
$ "(" 10 "," 20 // Result: _tupleValue 10 "," (_tupleValue {{10}, {10, 20}})
$ "(" 10 "," 20 ")" // Result: {{10}, {10, 20}}
$ "(" 10 "," 20 "," // Result: _tupleValue 10 "," (_tupleValue {{10}, {10, 20}} ",")
$ "(" 10 "," 20 "," 30 // Result: _tupleValue 10 "," (_tupleValue {{10}, {10, 20}} "," (_tupleValue {{{{10}, {10, 20}}}, {{{10}, {10, 20}}, 30}}))
$ "(" 10 "," 20 "," 30 ")"
"@" ->
// END MAYBE
"format" -> {
@x -> {
EOT -> toString x,
@y -> format (concat (toString x) (toString y))
}
},
format 0 // RESULT: (@y -> format (concat (toString 0) (toString y)))
format 0 1 // RESULT: (@y -> format (concat (toString "01") (toString y)))
format 0 1 EOT // RESULT: toString "01" // RESULT: "01"
"test" ->
@a =>
"+" -> * a a
// TODO: Define tuples like this?
}
---------
Above is an rough example, scratch-pad of my made up language based purely on sets and relations. I am trying to even define the syntax using sets and relations. I managed to do it for tuples (a, b, c). I'm trying to do it even for strings (stuff between ") and characters ('). And even sets {a, b, c}. Although I'm note sure if it's possible, since it seems like it could get self referential. What's the minimum amount of rules a programming language would need to do this, and can you show me?