Skip to content

Commit b1c14b5

Browse files
committed
Add idris code of strictly sorted list
1 parent 3adf3a4 commit b1c14b5

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

idris/StrictlySorted.idr

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
module StrictlySorted
2+
3+
------------------------------------------------------------------
4+
-- Attempts to define a strictly sorted list data type (as a set
5+
-- implementation).
6+
------------------------------------------------------------------
7+
8+
--------------------------------------------------------------------
9+
-- StrictlySorted using (Maybe a) as type argument to capture the
10+
-- first element of the list.
11+
--------------------------------------------------------------------
12+
13+
data StrictlySorted : (a : Type) -> Maybe a -> Type where
14+
Empty : StrictlySorted a Nothing
15+
Singleton : (x : a) -> StrictlySorted a (Just x)
16+
Prepend : Ord a => (x : a)
17+
-> (s : StrictlySorted a (Just y))
18+
-> (prf : (x < y) === True)
19+
-> StrictlySorted a (Just x)
20+
21+
-- Test
22+
empty_ssl : StrictlySorted Int Nothing
23+
empty_ssl = Empty
24+
25+
singleton_ssl : StrictlySorted Int (Just 10)
26+
singleton_ssl = Singleton 10
27+
28+
pair_ssl : StrictlySorted Int (Just 5)
29+
pair_ssl = Prepend 5 (Singleton 10) Refl
30+
31+
-- Fails due to 42 not being less than 10
32+
-- ill_ssl : StrictlySorted Int (Just 42)
33+
-- ill_ssl = Prepend 42 (Singleton 10) Refl
34+
35+
------------------------------------------------------------------------
36+
-- Alternative implementation of StrictlySorted not using (Maybe a) in
37+
-- the type declaration. Not as elegant to define, but easier to use.
38+
-- Note that (Maybe a) is still used in ssh (short for strictly sorted
39+
-- head). It works because (Just x) < Nothing is evaluated to False.
40+
------------------------------------------------------------------------
41+
42+
-- Pre-declaration to avoid circular dependencies
43+
data StrictlySorted' : Type -> Type
44+
0 ssh : StrictlySorted' a -> Maybe a
45+
46+
data StrictlySorted' : (a : Type) -> Type where
47+
Empty' : StrictlySorted' a
48+
Singleton' : (x : a) -> StrictlySorted' a
49+
Prepend' : Ord a => (x : a)
50+
-> (s : StrictlySorted' a)
51+
-> (prf : ((Just x) < ssh s) === True)
52+
-> StrictlySorted' a
53+
54+
-- Post-implementation of ssh to avoid circular dependencies
55+
ssh Empty' = Nothing
56+
ssh (Singleton' x) = Just x
57+
ssh (Prepend' x _ _) = Just x
58+
59+
-- Test
60+
empty_ssl' : StrictlySorted' Int
61+
empty_ssl' = Empty'
62+
63+
singleton_ssl' : StrictlySorted' Int
64+
singleton_ssl' = Singleton' 10
65+
66+
pair_ssl' : StrictlySorted' Int
67+
pair_ssl' = Prepend' 5 (Singleton' 10) Refl
68+
69+
-- Fails due to 42 not being less than 10
70+
-- ill_ssl' : StrictlySorted Int (Just 42)
71+
-- ill_ssl' = Prepend 42 (Singleton 10) Refl
72+
73+
--------------------------------------
74+
-- Alternatives from Micheal Messer
75+
--------------------------------------
76+
77+
-- data StrictlySorted : Ord a -> List a -> Type where
78+
-- Empty : StrictlySorted o []
79+
-- Singleton : StrictlySorted o [x]
80+
-- Prepend : (v : a) -> StrictlySorted o (x :: xs) -> ((<) @{o} v x = True) -> StrictlySorted o (v :: x :: xs)
81+
82+
-- g : Ord a => (xs : List a) -> {auto prf : StrictlySorted %search xs} -> ()
83+
84+
-- t : ()
85+
-- t = g [1,2,3]
86+
87+
---------------------------------------------
88+
-- Another alternative from Micheal Messer
89+
---------------------------------------------
90+
91+
-- data StrictlySorted : (a : Type) -> Ord a -> Type
92+
-- 0 helper : a -> StrictlySorted a o -> Type
93+
94+
-- public export
95+
-- data StrictlySorted : (a : Type) -> Ord a -> Type where
96+
-- Nil : StrictlySorted a o
97+
-- (::) : (v : a) -> (ls : StrictlySorted a o) -> {auto 0 prf : helper v ls} -> StrictlySorted a o
98+
99+
-- helper v [] = ()
100+
-- helper v (x :: xs) = (<) @{o} v x = True
101+
102+
-- g : Ord a => StrictlySorted a %search -> ()
103+
104+
-- t : ()
105+
-- t = g [1,2,3]

0 commit comments

Comments
 (0)