-
-
Save wongjiahau/21364e2e3b9ef70052b96ca4e3b730ee to your computer and use it in GitHub Desktop.
Jia Hau's syntax idea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This language is based on the idea of json-like objects | |
// This is a comment | |
// Tagged union | |
// Like OCaml, the tag is not associated with the type name, in this case `nat` | |
// Essentially, `nat` is just a alias for the tagged union | |
type Nat = {#zero} | {#succ, of: nat} | |
// Variable & object initialization | |
people = {name='John', hobby='type theory'} | |
// Object deconstruction | |
{x, y := a} = {x := '1', y := '2'} // Expose x and a to current closure | |
// Typing a variable | |
{j}: {j: Nat} = {j := {#zero}} | |
// Object deconstruction with type assertion and re-aliasing | |
{j: Nat = k} = {j := 'hi'} // Compiler will throw error | |
// Function declaration with pattern matching | |
add: Function { | |
in: {value: Nat, by: Nat}, | |
out: Nat | |
} = { | |
{value, by = {#zero}} -> value, | |
{value, by = {#succ, of: x}} -> {#succ, of=add{value, by=x}} | |
} | |
// Function application | |
answer = add {value={#zero}, by={#succ, of={#zero}}} | |
// Currying and Uniform Calling Syntax (UCS) | |
// The following are equivalent | |
add{value:= x, by:= y} | |
add{value:= x, by:= _} {by:= y} | |
add{value:= _, by:= y} {value:= x} | |
x.add{by:= y} | |
y.add{value:= x} | |
// Generic types | |
type List := { | |
{of: type = a} = {#nil} | {#cons, current: a, next: a.list} | |
} | |
// Parametric polymorphism and type inference | |
length: Function { | |
in: {of: List {of: infer _}}, | |
out: nat | |
} = { | |
{#nil} -> {#zero}, | |
{#cons, current, next} -> {#succ, of={#zero}}.add{by: next.length} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment