Skip to content

Instantly share code, notes, and snippets.

@wokalski
Last active January 14, 2019 13:10
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 wokalski/8f765caf542158a87fcf13b88fe577a5 to your computer and use it in GitHub Desktop.
Save wokalski/8f765caf542158a87fcf13b88fe577a5 to your computer and use it in GitHub Desktop.
/*
module Gettable = {
type empty;
type index('full, 'nth) =
| Zero: index('nth => 'more, 'nth)
| Succ(index('full, 'nth)): index('more => 'full, 'nth);
type t('ty) =
| Nil: t(empty)
| Cons('a, t('ty)): t('a => 'ty);
let rec nth: type l v. (t(l), index(l, v)) => v =
(l, i) =>
switch (l, i) {
| (Cons(v, _), Zero) => (v: v)
| (Cons(_, l), Succ(i)) => nth(l, i)
| _ => failwith("A")
};
/*
let use: type l v n. (option(t(l)), index(l, v), t(n), v) => v =
(l, index, next, default) =>
switch (l) {
| Some(l) => nth(l, index)
| None => default
};
*/
let dropFirst: type elem tail. t(elem => tail) => (elem, t(tail)) =
(Cons(elem, tail)) => (elem, tail);
};
module Appendable = {
type t('contents, 'unificationVariable) =
| Nil: t('unificationVariable, 'unificationVariable)
| Cons('a, t('contents, 'uv)): t('a => 'contents, 'uv);
let rec nthlist2: type l uv v. (t(l, uv), Gettable.index(l, v)) => v =
(l, i) =>
switch (l, i) {
| (Cons(_, l), Succ(i)) => nthlist2(l, i)
| (Cons(v, _), Zero) => (v: v)
| (Nil, _) => failwith("nth too big index!")
};
let rec addElement:
type l elem uv uv2. (t(l, elem), t(elem, uv2)) => t(l, uv2) =
(list, elem) =>
switch (list) {
| Nil => elem
| Cons(v, t) => Cons(v, addElement(t, elem))
};
external ofGettable: Gettable.t('l) => t('l, 'uv) = "%identity";
external toGettable: t('l, 'uv) => Gettable.t('l) = "%identity";
};
let index = Gettable.Zero;
let gettable = Gettable.(Cons(1, Cons('a', Nil)));
let x = Gettable.nth(gettable, index);
let appendable = Appendable.ofGettable(gettable);
let x' = Appendable.nthlist2(appendable, Succ(index));
let appendable = Appendable.addElement(appendable, Cons("chuJ", Nil));
let gettable' = Appendable.toGettable(appendable);
let x'' = Gettable.nth(gettable', index);
let x'' = Gettable.nth(gettable', Succ(Succ(Zero)));
*/
module type S = {type t('a);};
module HigherHeterogenousList = (Elem: S) => {
type index('full, 'nth) =
| Zero: index('nth => 'more, Elem.t('nth))
| Succ(index('full, 'nth)): index('more => 'full, 'nth);
type t('ty) =
| Nil: t(unit)
| Cons(Elem.t('a), t('ty)): t('a => 'ty);
let rec nth: type l v. (t(l), index(l, v)) => v =
(l, i) =>
switch (l, i) {
| (Cons(v, _), Zero) => (v: v)
| (Cons(_, l), Succ(i)) => nth(l, i)
| _ => failwith("A")
};
let nthAndAppend: type l v n. ((option(t(l)), index(l, v)), v) => v =
((hlist, index), default) =>
switch (hlist) {
| Some(l) => nth(l, index)
| None => default
};
type opaqueElem =
| OpaqueElement(Elem.t('a)): opaqueElem;
let rec iter: type l e. (~f: opaqueElem => unit, t(l)) => unit =
(~f, l) =>
switch (l) {
| Cons(elem, t) =>
f(OpaqueElement(elem));
iter(~f, t);
| Nil => ()
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment