Skip to content

Instantly share code, notes, and snippets.

@gallais
Created December 4, 2015 16:23
Show Gist options
  • Save gallais/47d628be691e0e0f06c1 to your computer and use it in GitHub Desktop.
Save gallais/47d628be691e0e0f06c1 to your computer and use it in GitHub Desktop.
"Difference Nats" and addition by recursion on the first / second argument
type z = Z
type +'a s = S
type _ nat =
| Z : ('l * 'l) nat
| S : ('l * 'm) nat -> ('l * 'm s) nat
let rec add : type l m n . (l*m) nat -> (m*n) nat -> (l*n) nat =
fun i1 i2 -> match i2 with
| Z -> i1
| S i -> S (add i1 i) (* OK *)
let rec shift : type l m. (l*m) nat -> (l s * m s) nat = function
| Z -> Z
| S i -> S (shift i)
let rec add2 : type l m n . (l*m) nat -> (m*n) nat -> (l*n) nat =
fun i1 i2 -> match i2 with
| Z -> i1
| S i -> add2 (S i1) (shift i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment