Idris2/libs/base/Data/Nat/Views.idr
Edwin Brady c88bf7af8d Fix import loading
This was taking too long, and adding too many things, because it was
going too deep in the name of having everything accessible at the REPL
and for the compiler. So, it's done a bit differently now, only chasing
everything on a "full" load (i.e., final load at the REPL)

This has some effects:
+ As systems get bigger, load time gets better (on my machine, checking
  Idris.Main now takes 52s from scratch, down from 76s)
+ You might find import errors that you didn't previously get, because
  things were being imported that shouldn't have been. The new way is
  correct!

An unfortunate effect is that sometimes you end up getting "undefined
name" errors even if you didn't explicitly use the name, because
sometimes a module uses a name from another module in a type, which then
gets exported, and eventually needs to be reduced. This mostly happens
because there is a compile time check that should be done which I
haven't implemented yet. That is, public export definitions should only
be allowed to use names that are also public export. I'll get to this
soon.
2020-05-27 15:49:03 +01:00

38 lines
1.2 KiB
Idris

module Data.Nat.Views
import Control.WellFounded
import Data.Nat
||| View for dividing a Nat in half
public export
data Half : Nat -> Type where
HalfOdd : (n : Nat) -> Half (S (n + n))
HalfEven : (n : Nat) -> Half (n + n)
||| View for dividing a Nat in half, recursively
public export
data HalfRec : Nat -> Type where
HalfRecZ : HalfRec Z
HalfRecEven : (n : Nat) -> (rec : Lazy (HalfRec n)) -> HalfRec (n + n)
HalfRecOdd : (n : Nat) -> (rec : Lazy (HalfRec n)) -> HalfRec (S (n + n))
||| Covering function for the `Half` view
public export
half : (n : Nat) -> Half n
half Z = HalfEven Z
half (S k) with (half k)
half (S (S (n + n))) | HalfOdd n = rewrite plusSuccRightSucc (S n) n in
HalfEven (S n)
half (S (n + n)) | HalfEven n = HalfOdd n
public export total
halfRec : (n : Nat) -> HalfRec n
halfRec n with (sizeAccessible n)
halfRec Z | acc = HalfRecZ
halfRec (S n) | acc with (half n)
halfRec (S (S (k + k))) | Access acc | HalfOdd k
= rewrite plusSuccRightSucc (S k) k
in HalfRecEven _ (halfRec (S k) | acc (S k) (LTESucc (LTESucc (lteAddRight _))))
halfRec (S (k + k)) | Access acc | HalfEven k
= HalfRecOdd _ (halfRec k | acc k (LTESucc (lteAddRight _)))