{"size": 2936, "ext": "js", "max_stars_repo_path": "examples/msort.js", "max_stars_repo_name": "rdesfo/esverify", "max_stars_repo_stars_event_min_datetime": "2016-12-01T02:45:55.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-29T07:14:16.000Z", "max_issues_repo_path": "examples/msort.js", "max_issues_repo_name": "rdesfo/esverify", "max_issues_repo_issues_event_min_datetime": "2018-08-30T19:08:46.000Z", "max_issues_repo_issues_event_max_datetime": "2021-06-14T00:03:50.000Z", "max_forks_repo_path": "examples/msort.js", "max_forks_repo_name": "rdesfo/esverify", "max_forks_repo_forks_event_min_datetime": "2018-12-01T13:45:11.000Z", "max_forks_repo_forks_event_max_datetime": "2019-10-01T12:21:08.000Z"}
{"max_stars_count": 102, "max_issues_count": 41, "max_forks_count": 6, "avg_line_length": 29.0693069307, "max_line_length": 80, "alphanum_fraction": 0.5967302452}
// Merge Sort
class IntList {
  constructor (head, tail) {
    this.head = head;
    this.tail = tail;
  }
  invariant () {
    return typeof(this.head) === 'number' &&
           (this.tail === null || this.tail instanceof IntList);
  }
}

class IntListPartition {
  constructor (left, right) {
    this.left = left;
    this.right = right;
  }
  invariant () {
    return (this.left === null || this.left instanceof IntList) &&
           (this.right === null || this.right instanceof IntList);
  }
}

function partition (lst, fst, snd, alternate) {
  requires(lst === null || lst instanceof IntList);
  requires(fst === null || fst instanceof IntList);
  requires(snd === null || snd instanceof IntList);
  requires(typeof(alternate) === 'boolean');
  ensures(res => res instanceof IntListPartition);
  ensures(pure());

  if (lst === null) {
    return new IntListPartition(fst, snd);
  } else if (alternate) {
    return partition(lst.tail, new IntList(lst.head, fst), snd, false);
  } else {
    return partition(lst.tail, fst, new IntList(lst.head, snd), true);
  }
}

function isSorted (list) {
  requires(list === null || list instanceof IntList);
  ensures(res => typeof(res) === 'boolean');
  ensures(pure());

  return list === null || list.tail === null ||
        list.head <= list.tail.head && isSorted(list.tail);
}

function merge (left, right) {
  requires(left === null || left instanceof IntList);
  requires(isSorted(left));
  requires(right === null || right instanceof IntList);
  requires(isSorted(right));
  ensures(res => res === null || res instanceof IntList);
  ensures(res => isSorted(res));
  ensures(res => (left === null && right === null) === (res === null));
  ensures(res => !(left !== null && (right === null || right.head >= left.head))
                  ||
                (res !== null && res.head === left.head));
  ensures(res => !(right !== null && (left === null || right.head < left.head))
                  ||
                (res !== null && res.head === right.head));
  ensures(pure());

  if (left === null) {
    return right;
  } else if (right === null) {
    return left;
  } else if (left.head <= right.head) {
    isSorted(left);
    isSorted(left.tail);
    const merged = merge(left.tail, right);
    const res = new IntList(left.head, merged);
    isSorted(res);
    return res;
  } else {
    isSorted(right);
    isSorted(right.tail);
    const merged = merge(left, right.tail);
    const res = new IntList(right.head, merged);
    isSorted(res);
    return res;
  }
}

function sort (list) {
  requires(list === null || list instanceof IntList);
  ensures(res => res === null || res instanceof IntList);
  ensures(res => isSorted(res));
  ensures(pure());

  if (list === null || list.tail === null) {
    isSorted(list);
    assert(isSorted(list));
    return list;
  }
  const part = partition(list, null, null, false);
  return merge(sort(part.left), sort(part.right));
}
