Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor Uninhabited implementation for Elem types #3412

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG_NEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ This CHANGELOG describes the merged but unreleased changes. Please see [CHANGELO

* Added `Data.IORef.atomically` for the chez backend.

* Refactor `Uninhabited` implementation for `Data.List.Elem`, `Data.List1.Elem`, `Data.SnocList.Elem` and `Data.Vect.Elem`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding a brief sentence explaining why / explaining what the refactor achieves?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


#### Contrib

* `Data.List.Lazy` was moved from `contrib` to `base`.
Expand Down
4 changes: 2 additions & 2 deletions libs/base/Data/List/Elem.idr
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ data Elem : a -> List a -> Type where
There : Elem x xs -> Elem x (y :: xs)

export
Uninhabited (Here = There e) where
Uninhabited (Here {x} {xs} = There {x = x'} {y} {xs} e) where
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going fully heterogeneous, why not have two different xs on each sides?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. xs are now different

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since xs are now different maybe we should add something like that?
Either (Not (x = x')) (Not xs = xs') => Uninhabited (Here {x} {xs} = Here {x = x'} {xs = xs'}) where
Basically, if x or xs are different, Here and There will be different as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either (Not (x = x')) (Not xs = xs') => Uninhabited (Here {x} {xs} = Here {x = x'} {xs = xs'})

I think this can be proven for any Biinjective, not only for this particular one

uninhabited Refl impossible

export
Uninhabited (There e = Here) where
Uninhabited (There {x} {y} {xs} e = Here {x = x'} {xs}) where
uninhabited Refl impossible

export
Expand Down
5 changes: 2 additions & 3 deletions libs/base/Data/List1/Elem.idr
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ data Elem : a -> List1 a -> Type where
There : Elem x xs -> Elem x (y ::: xs)

export
Uninhabited (List1.Elem.Here = List1.Elem.There e) where
Uninhabited (List1.Elem.Here {x} {xs} = List1.Elem.There {x = x'} {y} {xs} e) where
uninhabited Refl impossible

export
Uninhabited (List1.Elem.There e = List1.Elem.Here) where
Uninhabited (List1.Elem.There {x} {y} {xs} e = List1.Elem.Here {x = x'} {xs}) where
uninhabited Refl impossible

export
Expand Down Expand Up @@ -84,4 +84,3 @@ export
elemMap : (0 f : a -> b) -> List1.Elem.Elem x xs -> List1.Elem.Elem (f x) (map f xs)
elemMap f Here = Here
elemMap f (There el) = There $ elemMap f el

4 changes: 2 additions & 2 deletions libs/base/Data/SnocList/Elem.idr
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ data Elem : a -> SnocList a -> Type where
There : {0 x, y : a} -> Elem x sx -> Elem x (sx :< y)

export
Uninhabited (Here = There e) where
Uninhabited (Here {x} {sx} = There {x = x'} {y} {sx} e) where
uninhabited Refl impossible

export
Uninhabited (There e = Here) where
Uninhabited (There {x} {y} {sx} e = Here {x = x'} {sx}) where
uninhabited Refl impossible

export
Expand Down
4 changes: 2 additions & 2 deletions libs/base/Data/Vect/Elem.idr
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ data Elem : a -> Vect k a -> Type where
There : (later : Elem x xs) -> Elem x (y::xs)

export
Uninhabited (Here = There e) where
Uninhabited (Here {x} {xs} = There {x = x'} {y} {xs} e) where
uninhabited Refl impossible

export
Uninhabited (There e = Here) where
Uninhabited (There {x} {y} {xs} e = Here {x = x'} {xs}) where
uninhabited Refl impossible

export
Expand Down
21 changes: 21 additions & 0 deletions tests/base/data_list003/Elem.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Data.List
import Data.List.Elem

decHomogeneousElem : (el1 : Elem x xs) -> (el2 : Elem x xs) -> Dec (el1 = el2)
decHomogeneousElem Here Here = Yes Refl
decHomogeneousElem (There _) Here = No uninhabited
decHomogeneousElem Here (There _) = No uninhabited
decHomogeneousElem (There el1') (There el2') = case decHomogeneousElem el1' el2' of
Yes Refl => Yes Refl
No contra => No $ \Refl => contra Refl

decHeterogeneousElem : (el1 : Elem x xs) -> (el2 : Elem y xs) -> Dec (el1 = el2)
decHeterogeneousElem Here Here = Yes Refl
decHeterogeneousElem (There _) Here = No uninhabited
decHeterogeneousElem Here (There _) = No uninhabited
decHeterogeneousElem (There el1') (There el2') = case decHeterogeneousElem el1' el2' of
Yes Refl => Yes Refl
No contra => No $ \Refl => contra Refl

main : IO ()
main = printLn "OK"
1 change: 1 addition & 0 deletions tests/base/data_list003/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"OK"
3 changes: 3 additions & 0 deletions tests/base/data_list003/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
. ../../testutils.sh

run Elem.idr
39 changes: 39 additions & 0 deletions tests/base/data_listone001/Elem.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Data.List
import Data.List.Elem
import Data.List1
import Data.List1.Elem

decHomogeneousElemList : (el1 : Data.List.Elem.Elem x xs) -> (el2 : Data.List.Elem.Elem x xs) -> Dec (el1 = el2)
decHomogeneousElemList Here Here = Yes Refl
decHomogeneousElemList (There _) Here = No uninhabited
decHomogeneousElemList Here (There _) = No uninhabited
decHomogeneousElemList (There el1') (There el2') = case decHomogeneousElemList el1' el2' of
Yes Refl => Yes Refl
No contra => No $ \Refl => contra Refl

decHeterogeneousElemList : (el1 : Data.List.Elem.Elem x xs) -> (el2 : Data.List.Elem.Elem y xs) -> Dec (el1 = el2)
decHeterogeneousElemList Here Here = Yes Refl
decHeterogeneousElemList (There _) Here = No uninhabited
decHeterogeneousElemList Here (There _) = No uninhabited
decHeterogeneousElemList (There el1') (There el2') = case decHeterogeneousElemList el1' el2' of
Yes Refl => Yes Refl
No contra => No $ \Refl => contra Refl

decHomogeneousElem : (el1 : Data.List1.Elem.Elem x xs) -> (el2 : Data.List1.Elem.Elem x xs) -> Dec (el1 = el2)
decHomogeneousElem Here Here = Yes Refl
decHomogeneousElem (There _) Here = No uninhabited
decHomogeneousElem Here (There _) = No uninhabited
decHomogeneousElem (There el1') (There el2') = case decHomogeneousElemList el1' el2' of
Yes Refl => Yes Refl
No contra => No $ \Refl => contra Refl

decHeterogeneousElem : (el1 : Data.List1.Elem.Elem x xs) -> (el2 : Data.List1.Elem.Elem y xs) -> Dec (el1 = el2)
decHeterogeneousElem Here Here = Yes Refl
decHeterogeneousElem (There _) Here = No uninhabited
decHeterogeneousElem Here (There _) = No uninhabited
decHeterogeneousElem (There el1') (There el2') = case decHeterogeneousElemList el1' el2' of
Yes Refl => Yes Refl
No contra => No $ \Refl => contra Refl

main : IO ()
main = printLn "OK"
1 change: 1 addition & 0 deletions tests/base/data_listone001/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"OK"
3 changes: 3 additions & 0 deletions tests/base/data_listone001/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
. ../../testutils.sh

run Elem.idr
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions tests/base/data_snoclist002/Elem.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Data.SnocList
import Data.SnocList.Elem

decHomogeneousElem : (el1 : Elem x xs) -> (el2 : Elem x xs) -> Dec (el1 = el2)
decHomogeneousElem Here Here = Yes Refl
decHomogeneousElem (There _) Here = No uninhabited
decHomogeneousElem Here (There _) = No uninhabited
decHomogeneousElem (There el1') (There el2') = case decHomogeneousElem el1' el2' of
Yes Refl => Yes Refl
No contra => No $ \Refl => contra Refl

decHeterogeneousElem : (el1 : Elem x xs) -> (el2 : Elem y xs) -> Dec (el1 = el2)
decHeterogeneousElem Here Here = Yes Refl
decHeterogeneousElem (There _) Here = No uninhabited
decHeterogeneousElem Here (There _) = No uninhabited
decHeterogeneousElem (There el1') (There el2') = case decHeterogeneousElem el1' el2' of
Yes Refl => Yes Refl
No contra => No $ \Refl => contra Refl

main : IO ()
main = printLn "OK"
1 change: 1 addition & 0 deletions tests/base/data_snoclist002/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"OK"
3 changes: 3 additions & 0 deletions tests/base/data_snoclist002/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
. ../../testutils.sh

run Elem.idr
21 changes: 21 additions & 0 deletions tests/base/data_vect002/Elem.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Data.Vect
import Data.Vect.Elem

decHomogeneousElem : (el1 : Elem x xs) -> (el2 : Elem x xs) -> Dec (el1 = el2)
decHomogeneousElem Here Here = Yes Refl
decHomogeneousElem (There _) Here = No uninhabited
decHomogeneousElem Here (There _) = No uninhabited
decHomogeneousElem (There el1') (There el2') = case decHomogeneousElem el1' el2' of
Yes Refl => Yes Refl
No contra => No $ \Refl => contra Refl

decHeterogeneousElem : (el1 : Elem x xs) -> (el2 : Elem y xs) -> Dec (el1 = el2)
decHeterogeneousElem Here Here = Yes Refl
decHeterogeneousElem (There _) Here = No uninhabited
decHeterogeneousElem Here (There _) = No uninhabited
decHeterogeneousElem (There el1') (There el2') = case decHeterogeneousElem el1' el2' of
Yes Refl => Yes Refl
No contra => No $ \Refl => contra Refl

main : IO ()
main = printLn "OK"
1 change: 1 addition & 0 deletions tests/base/data_vect002/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"OK"
3 changes: 3 additions & 0 deletions tests/base/data_vect002/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
. ../../testutils.sh

run Elem.idr
Loading