Skip to content

Instantly share code, notes, and snippets.

@divs1210
Last active August 19, 2021 17:52
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 divs1210/b70fceb6f8f2b14e9bddb4a0b946031e to your computer and use it in GitHub Desktop.
Save divs1210/b70fceb6f8f2b14e9bddb4a0b946031e to your computer and use it in GitHub Desktop.
Hypothetical Programming Language of My Dreams

Mylang

Features

Functional

  • like Clojure / Elixir
  • not fanatic like Haskell

Algol-derived syntax

  • syntax like JS / Python / Ruby / Elixir
  • avoid lisp syntax due to stigma
  • avoid Haskell syntax because it sucks
  • regular, predictable syntax like lisps - especially Clojure

Garbage collected

  • no memory management headaches like Rust / C / C++

Gradual typing

  • start without types, add types to any extent, even dependent types
  • typechecking should be done at compile time to whatever extent possible
  • the remaining checks should be left for runtime
  • enable/disable typechecking for forms/functions

Numerics

  • awesome like Scheme, supporting bignums, complex numbers, ratios, etc.
  • fast-enough for general use
  • bindings to a fast n-dimensional-vector lib for performance sensitive code

Immutable collections

  • like Clojure: list, vector, ordered-hash-map, ordered-set

Polymorphic functions

  • every top-level public function is a multimethod
  • maybe protocols implemented as a special type of multimethods

Form application syntax

+(1, 2, 3)

is equivalent to

1.+(2, 3)

If only one arg, the dot and brackets can be elided

1 + 2 + 3

is equivalent to

1.+(2).+(3)

Compile time evaluation

  • constant folding
  • any form / function whose args are compile-time constants should be evaulated and inlined

Partial application

let f = fn((m, n), 2 * m + n)
let fm2 = f(2, _)
let fn2 = f(_, 2)

should compile to

let f = fn((m, n), 2 * m + n)
let fm2 = fn((n), 4 + n)
let fn2 = fn((m), 2 * m + 2)

Extensible compiler

  • something between macros and GHC extensions
  • can add language features, hopefully in the language itself

Implementation ideas

Compile to Chez Scheme

  • fast
  • great recursion capabilities
  • native executables
  • continuations
  • c ffi

Interpret with Truffle/Graal

  • superfast
  • runtime optimization, such as dependent type checks
  • java ffi
  • slow, bloated native executables with some trouble, losing java ffi and with bad gc

Code Examples

Overview

example.mylang

ns!(example)

defn!(fact, {}, [n] =>
  cond(
    n == 0 => 1,
    :else  => n * fact(n - 1)
  )
)

defn!(main!, {:public true}, args =>
  do(
    println!(fact(5)),
    println!(fact("a"))
  )
)

Exegesis

  • side-effeting forms and functions end with a bang !
  • => is parsed as , and is just sugar
  • fact and main! are default methods of multimethods - specialized methods can be added later, even in another ns
  • the second arg to defn! is a map containing metadata
  • the formal param of main!, args, will contain all the passed args, whereas fact destructures the param vector to get a single param n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment