Skip to content

Instantly share code, notes, and snippets.

@Fedjmike
Last active December 18, 2015 23:29
Show Gist options
  • Save Fedjmike/5861552 to your computer and use it in GitHub Desktop.
Save Fedjmike/5861552 to your computer and use it in GitHub Desktop.

type tree: {payload: int, void | {l, r: tree*}}

fun treePrint: (root: tree*) -> void {

}

Algebraic data types

  • Declare using named case labels inside struct
  • Fields declared before a case are common to all cases
  • Field overlap ???
  • Operate on using switch statement
    • Follow through makes no sense => illegal (implicit break?)
    • Inside case block, variable (if lvalue) becomes subtyped DO CASES CREATE SCOPE? NO LOL
    • Allow for-like declaration of variables in switch
    • Access to any non-common fields outside of the relavent case illegalConstruct ???
struct tree {
    int payload;
    
case leaf:
case branch:
    tree *left, *right;
};

int getDepth (tree* root) {
    switch (root) {
    case leaf:
        return 1;
        break;
        
    case branch:
        return 1+max(getDepth(root->left),
                     getDepth(root->right));
    }
}

New type decl syntax

var a: x*[3]; //Array of pointers to xs
fun f: (a: x, b: y) -> z {
    h(a)[0](b);
}
var g: (x, y) -> z = f;
fun h: x -> (y -> z)*;

fun swap: (x: :a, y: :b) -> (:b, :a) {
    return {y, x};
}

Polymorphic structs and functions

struct :a:b pair {
    :a first;
    :b second;
}

typedef :a:(:b:c pair) pair :a:b:c triplet;

:b:a pair* flip (:a:b pair* Old) {
    :b:a pair* New = malloc(sizeof(:a:b: pair));
    New->first = Old->second;
    New->second = Old->first;
    return New;
}

flipTriplet ((a, b, c))

:a int pair
int (double int pair) pair
int int pair or :int:int pair? See:

int multiplyPair (:int:int pair* p) {
    return p->first * p->second;
}

Tuples

Reverse comma operator

Evaluates first, evaluates second, returns first. Binds leftly like normal comma.

x` y` z == (x` y)` z

x` y == {auto ret = x; y; return ret}

Uses:

char* str = malloc(...);
return strfn(str, ...)` free(str);
#define swap(x, y) ((x) = ((y)` (y) = (x)))
parserCtx* ctx = parserInit(file, global, searchPaths);
ast* Module = parserModule(ctx);
return (parserResult) {Module, ctx->errors, ctx->warnings}` parserEnd(ctx);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment