Repository: idris-lang/Idris-dev

Issue #172: fromList and exponential compile time
Created: 2013-01-30T14:49:02Z
The following program
> module Main

> n : Nat
> n = 100

> nats : Vect Nat n
> nats = fromList [1..n]

> main : IO ()
> main = putStrLn (show (cast n))

executes in constant time in n as one would expect. Compilation, however, takes exponential time:
   n      user time
 100      0m0.728s
 200      0m1.304s 
 400      0m3.352s
 800      0m14.737s
1600      1m30.258s

In fact, at n = 3200, compilation virtually stalls (on my laptop) after having eaten up the whole memory (8GB). Notice that replacing
> nats : Vect Nat n
> nats = fromList [1..n]

with
> nats : List Nat
> nats = [1..n]

yields constant compile times. Is it possible to construct vectors of natural numbers of length n in at most linear time (in n) ?

Duplicate issue #761: Type checking complexity and deferred definitions
Created: 2014-01-02T15:13:12Z
The following code seems to type check in linear time in "maxColumn"
> module Test

> %assert_total

> Blt : Nat -> Type
> Blt b = (n : Nat ** so (n < b))
> toNat : Blt b -> Nat
> toNat = getWitness
> toVect : {b : Nat} -> (Blt b -> a) -> Vect b a

> idx : Vect n alpha -> Blt n -> alpha
> X : Nat -> Type
> viable : (n : Nat) -> X t -> Bool
> nX : (t : Nat) -> (n : Nat) -> Nat 
> xedni : (n : Nat) -> Blt (nX t n) -> (x : X t ** so (viable n x))
> maxColumn : Nat
> maxColumn = 16
> X t = Blt (S maxColumn)
> states : (t : Nat) -> Vect (S maxColumn) (X t)
> states t = toVect (\ i => i)
> cxrvs : (t : Nat) -> (n : Nat) -> (i : Nat ** Vect i (X t))
> cxrvs t n = filter (\ x => viable {t = t} n x) (states t)
> nX t n = getWitness (cxrvs t n)
> xrvs : (t : Nat) -> (n : Nat) -> Vect (nX t n) (X t)
> xedni {t} n i = (idx (xrvs t n) i ** believe_me oh)

> toVect {b = Z} _ = Nil
> toVect {b = S b'} {a = a} f = ((f (Z ** oh)) :: toVect f') where
>   f' : Blt b' -> a
>   f' (k ** q) = f (S k ** believe_me q) 

for values of "maxColumn" up to about 1000. What would you expect to be the type check complexity if one moves the definition of "toVect" immediately after its declaration point ?  In other words, what do you expect to be the type check complexity of
> module Test

> %assert_total

> Blt : Nat -> Type
> Blt b = (n : Nat ** so (n < b))
> toNat : Blt b -> Nat
> toNat = getWitness
> toVect : {b : Nat} -> (Blt b -> a) -> Vect b a
> toVect {b = Z} _ = Nil
> toVect {b = S b'} {a = a} f = ((f (Z ** oh)) :: toVect f') where
>   f' : Blt b' -> a
>   f' (k ** q) = f (S k ** believe_me q) 

> idx : Vect n alpha -> Blt n -> alpha
> X : Nat -> Type
> viable : (n : Nat) -> X t -> Bool
> nX : (t : Nat) -> (n : Nat) -> Nat 
> xedni : (n : Nat) -> Blt (nX t n) -> (x : X t ** so (viable n x))
> maxColumn : Nat
> maxColumn = 16
> X t = Blt (S maxColumn)
> states : (t : Nat) -> Vect (S maxColumn) (X t)
> states t = toVect (\ i => i)
> cxrvs : (t : Nat) -> (n : Nat) -> (i : Nat ** Vect i (X t))
> cxrvs t n = filter (\ x => viable {t = t} n x) (states t)
> nX t n = getWitness (cxrvs t n)
> xrvs : (t : Nat) -> (n : Nat) -> Vect (nX t n) (X t)
> xedni {t} n i = (idx (xrvs t n) i ** believe_me oh)

in "maxColumn" ?
I'm asking the question because, on my laptop, I cannot type check the second example for "maxColumn = 32". The type checker eats up the whole memory, starts swapping and type checking stalls.
Is this the expected behavior ? Does this imply that, in general, function definitions should be deferred as much as possible ?

Closing comment (2016-04-05T16:56:45Z): I will close this since it seems to be closely related to #172.