Skip to content

Commit

Permalink
[ new ] Data.SnocList.HasLength from compiler libs (#3299)
Browse files Browse the repository at this point in the history
  • Loading branch information
gallais committed Jun 5, 2024
1 parent 0742b3b commit 004f1fd
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions libs/base/Data/Singleton.idr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ public export
data Singleton : a -> Type where
Val : (x : a) -> Singleton x

public export %inline
reindex : (0 _ : x === y) -> Singleton x -> Singleton y
reindex Refl x = x

public export %inline
unVal : Singleton {a} x -> a
unVal $ Val x = x
Expand Down
70 changes: 70 additions & 0 deletions libs/base/Data/SnocList/HasLength.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module Data.SnocList.HasLength

import Data.Nat
import Data.SnocList

---------------------------------------
-- horrible hack
import Data.List.HasLength as L

public export
LHasLength : Nat -> List a -> Type
LHasLength = L.HasLength
%hide L.HasLength
---------------------------------------

public export
data HasLength : Nat -> SnocList a -> Type where
Z : HasLength Z [<]
S : HasLength n sa -> HasLength (S n) (sa :< a)

export
hasLength : HasLength n sx -> length sx === n
hasLength Z = Refl
hasLength (S p) = cong S (hasLength p)

export
map : (f : a -> b) -> HasLength n xs -> HasLength n (map f xs)
map f Z = Z
map f (S hl) = S (map f hl)

export
sucL : HasLength n sx -> HasLength (S n) ([<x] ++ sx)
sucL Z = S Z
sucL (S n) = S (sucL n)

export
hlAppend : HasLength m sx -> HasLength n sy -> HasLength (n + m) (sx ++ sy)
hlAppend sx Z = sx
hlAppend sx (S sy) = S (hlAppend sx sy)

export
hlFish : HasLength m sx -> LHasLength n ys -> HasLength (n + m) (sx <>< ys)
hlFish x Z = x
hlFish {n = S n} x (S y) = rewrite plusSuccRightSucc n m in hlFish (S x) y

export
mkHasLength : (sx : SnocList a) -> HasLength (length sx) sx
mkHasLength [<] = Z
mkHasLength (sx :< _) = S (mkHasLength sx)

export
hlChips : HasLength m sx -> LHasLength n ys -> LHasLength (m + n) (sx <>> ys)
hlChips Z y = y
hlChips {m = S m} {n} (S x) y
= rewrite plusSuccRightSucc m n in
hlChips x (S y)

export
cast : {sy : _} -> (0 _ : SnocList.length sx = SnocList.length sy) ->
HasLength m sx -> HasLength m sy
cast {sy = [<]} eq Z = Z
cast {sy = sy :< _} eq (S p) = S (cast (injective eq) p)

hlReverseOnto : HasLength m acc -> HasLength n sx -> HasLength (m + n) (reverseOnto acc sx)
hlReverseOnto p Z = rewrite plusZeroRightNeutral m in p
hlReverseOnto {n = S n} p (S q) = rewrite sym (plusSuccRightSucc m n) in hlReverseOnto (S p) q

export
hlReverse : HasLength m acc -> HasLength m (reverse acc)
hlReverse = hlReverseOnto Z
1 change: 1 addition & 0 deletions libs/base/base.ipkg
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ modules = Control.App,
Data.Singleton,
Data.SnocList,
Data.SnocList.Elem,
Data.SnocList.HasLength,
Data.SnocList.Operations,
Data.SnocList.Quantifiers,
Data.So,
Expand Down

0 comments on commit 004f1fd

Please sign in to comment.