Skip to content

Instantly share code, notes, and snippets.

@ytomino
Created January 12, 2016 22:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytomino/18ffee7b75ba98839251 to your computer and use it in GitHub Desktop.
Save ytomino/18ffee7b75ba98839251 to your computer and use it in GitHub Desktop.
Trying dependent types in nim
type Cell[T] = ref object
car: T
cdr: Cell[T]
type List[T, N] = distinct Cell[T]
proc makeNil[T]: List[T, range[0..0]] = nil
proc makeCons[T, N](car: T; cdr: List[T, N]): auto =
type N1 = range[0..high(N)+1]
var r: Cell[T] = Cell[T](car: car, cdr: Cell[T](cdr))
List[T, N1](r)
const theNil: List[int, range[0..0]] = makeNil[int]()
const theList1 = makeCons(1, theNil)
# % nim c dt.nim
# Hint: system [Processing]
# Hint: dt [Processing]
# dt.nim(5, 14) Hint: 'N' is declared but not used [XDeclaredButNotUsed]
# SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment