Skip to content

Instantly share code, notes, and snippets.

@wongjiahau
Last active December 23, 2019 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wongjiahau/21364e2e3b9ef70052b96ca4e3b730ee to your computer and use it in GitHub Desktop.
Save wongjiahau/21364e2e3b9ef70052b96ca4e3b730ee to your computer and use it in GitHub Desktop.
Jia Hau's syntax idea
// 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