on master help sign in
Merge remote-tracking branch 'upstream/master'
a3c760fMekeor Melire
idris.cabal+1−1
@@ -67,7 +67,7 @@ Executable idris
67
67
68 Build-depends: base>=4 && <5, parsec, mtl, Cabal, haskeline,
68 Build-depends: base>=4 && <5, parsec, mtl, Cabal, haskeline,
69 containers, process, transformers, filepath, directory,
69 containers, process, transformers, filepath, directory,
70− binary, bytestring, epic>=0.9.2
70+ binary, bytestring, epic>=0.9.2.1
71
71
72 Extensions: MultiParamTypeClasses, FunctionalDependencies,
72 Extensions: MultiParamTypeClasses, FunctionalDependencies,
73 FlexibleInstances, TemplateHaskell
73 FlexibleInstances, TemplateHaskell
lib/prelude/heap.idrnew file+183
@@ -0,0 +1,183 @@
1+--------------------------------------------------------------------------------
2+-- Okasaki-style maxiphobic heaps. See the paper:
3+-- ``Fun with binary heap trees'', Chris Okasaki, Fun of programming, 2003.
4+--------------------------------------------------------------------------------
5+
6+module prelude.heap
7+
8+import builtins
9+
10+import prelude
11+import prelude.algebra
12+import prelude.list
13+import prelude.nat
14+
15+%access public
16+
17+abstract data MaxiphobicHeap : Set -> Set where
18+ Empty : MaxiphobicHeap a
19+ Node : Nat -> MaxiphobicHeap a -> a -> MaxiphobicHeap a -> MaxiphobicHeap a
20+
21+----------------------------------------- ---------------------------------------
22+-- Syntactic tests
23+--------------------------------------------------------------------------------
24+
25+total isEmpty : MaxiphobicHeap a -> Bool
26+isEmpty Empty = True
27+isEmpty _ = False
28+
29+total size : MaxiphobicHeap a -> Nat
30+size Empty = O
31+size (Node s l e r) = s
32+
33+--------------------------------------------------------------------------------
34+-- Basic heaps
35+--------------------------------------------------------------------------------
36+
37+total empty : MaxiphobicHeap a
38+empty = Empty
39+
40+total singleton : a -> MaxiphobicHeap a
41+singleton e = Node 1 Empty e Empty
42+
43+--------------------------------------------------------------------------------
44+-- Inserting items and merging heaps
45+--------------------------------------------------------------------------------
46+
47+private orderBySize : MaxiphobicHeap a -> MaxiphobicHeap a -> MaxiphobicHeap a ->
48+ (MaxiphobicHeap a, MaxiphobicHeap a, MaxiphobicHeap a)
49+orderBySize left centre right =
50+ if size left == largest then
51+ (left, centre, right)
52+ else if size centre == largest then
53+ (centre, left, right)
54+ else
55+ (right, left, centre)
56+ where
57+ largest : Nat
58+ largest = maximum (size left) $ maximum (size centre) (size right)
59+
60+merge : Ord a => MaxiphobicHeap a -> MaxiphobicHeap a -> MaxiphobicHeap a
61+merge Empty right = right
62+merge left Empty = left
63+merge (Node ls ll le lr) (Node rs rl re rr) =
64+ if le < re then
65+ let (largest, b, c) = orderBySize ll lr (Node rs rl re rr) in
66+ Node mergedSize largest le (merge b c)
67+ else
68+ let (largest, b, c) = orderBySize rl rr (Node ls ll le lr) in
69+ Node mergedSize largest re (merge b c)
70+ where
71+ mergedSize : Nat
72+ mergedSize = ls + rs
73+
74+insert : Ord a => a -> MaxiphobicHeap a -> MaxiphobicHeap a
75+insert e = merge $ singleton e
76+
77+--------------------------------------------------------------------------------
78+-- Heap operations
79+--------------------------------------------------------------------------------
80+
81+findMinimum : (h : MaxiphobicHeap a) -> (isEmpty h = False) -> a
82+findMinimum Empty p = ?findMinimumEmptyAbsurd
83+findMinimum (Node s l e r) p = e
84+
85+deleteMinimum : Ord a => (h : MaxiphobicHeap a) -> (isEmpty h = False) -> MaxiphobicHeap a
86+deleteMinimum Empty p = ?deleteMinimumEmptyAbsurd
87+deleteMinimum (Node s l e r) p = merge l r
88+
89+--------------------------------------------------------------------------------
90+-- Conversions to and from lists (and a derived heap sorting algorithm)
91+--------------------------------------------------------------------------------
92+
93+toList : Ord a => MaxiphobicHeap a -> List a
94+toList Empty = []
95+toList (Node s l e r) = toList' (Node s l e r) refl
96+ where
97+ toList' : Ord a => (h : MaxiphobicHeap a) -> (isEmpty h = False) -> List a
98+ toList' heap p = findMinimum heap p :: toList $ deleteMinimum heap p
99+
100+fromList : Ord a => List a -> MaxiphobicHeap a
101+fromList = foldr insert empty
102+
103+sort : Ord a => List a -> List a
104+sort = prelude.heap.toList . prelude.heap.fromList
105+
106+--------------------------------------------------------------------------------
107+-- Class instances
108+--------------------------------------------------------------------------------
109+
110+instance Show a => Show (MaxiphobicHeap a) where
111+ show Empty = "Empty"
112+ show (Node s l e r) = "Node (" ++ show l ++ " " ++ show e ++ " " ++ show r ++ ")"
113+
114+instance Eq a => Eq (MaxiphobicHeap a) where
115+ Empty == Empty = True
116+ (Node ls ll le lr) == (Node rs rl re rr) =
117+ ls == rs && ll == rl && le == re && lr == rr
118+ _ == _ = False
119+
120+instance Ord a => Semigroup (MaxiphobicHeap a) where
121+ (<+>) = merge
122+
123+instance Ord a => Monoid (MaxiphobicHeap a) where
124+ neutral = empty
125+
126+instance Ord a => JoinSemilattice (MaxiphobicHeap a) where
127+ join = merge
128+
129+--------------------------------------------------------------------------------
130+-- Properties
131+--------------------------------------------------------------------------------
132+
133+total absurdBoolDischarge : False = True -> _|_
134+absurdBoolDischarge p = replace {P = disjointTy} p ()
135+ where
136+ total disjointTy : Bool -> Set
137+ disjointTy False = ()
138+ disjointTy True = _|_
139+
140+total isEmptySizeZero : (h : MaxiphobicHeap a) -> (isEmpty h = True) -> size h = O
141+isEmptySizeZero Empty p = refl
142+isEmptySizeZero (Node s l e r) p = ?isEmptySizeZeroNodeAbsurd
143+
144+--------------------------------------------------------------------------------
145+-- Proofs
146+--------------------------------------------------------------------------------
147+
148+isEmptySizeZeroNodeAbsurd = proof {
149+ intros;
150+ refine FalseElim;
151+ refine absurdBoolDischarge;
152+ exact p;
153+}
154+
155+findMinimumEmptyAbsurd = proof {
156+ intros;
157+ refine FalseElim;
158+ refine absurdBoolDischarge;
159+ rewrite p;
160+ trivial;
161+}
162+
163+deleteMinimumEmptyAbsurd = proof {
164+ intros;
165+ refine FalseElim;
166+ refine absurdBoolDischarge;
167+ rewrite p;
168+ trivial;
169+}
170+
171+--------------------------------------------------------------------------------
172+-- Debug
173+--------------------------------------------------------------------------------
174+
175+{- XXX: poor performance when compiled, diverges when used in the REPL, but it
176+ does seem to work correctly!
177+main : IO ()
178+main = do
179+ _ <- print $ main.sort [10, 3, 7, 2, 9, 1, 8, 0, 6, 4, 5]
180+ _ <- print $ main.sort ["orange", "apple", "pear", "lime", "durian"]
181+ _ <- print $ main.sort [("jim", 19, "cs"), ("alice", 20, "english"), ("bob", 50, "engineering")]
182+ return ()
183+-}
src/Core/Evaluate.hs+6−6
@@ -165,7 +165,7 @@ eval ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
165 ev ntimes_in stk top env (P Ref n ty)
165 ev ntimes_in stk top env (P Ref n ty)
166 | (True, ntimes) <- usable n ntimes_in
166 | (True, ntimes) <- usable n ntimes_in
167 = do let val = lookupDefAcc Nothing n atRepl ctxt
167 = do let val = lookupDefAcc Nothing n atRepl ctxt
168− step maxred
168+ when (not atRepl) $ step maxred
169 case val of
169 case val of
170 [(Function _ tm, Public)] ->
170 [(Function _ tm, Public)] ->
171 ev ntimes (n:stk) True env tm
171 ev ntimes (n:stk) True env tm
@@ -184,24 +184,24 @@ eval ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
184 | otherwise = return $ VV i
184 | otherwise = return $ VV i
185 ev ntimes stk top env (Bind n (Let t v) sc)
185 ev ntimes stk top env (Bind n (Let t v) sc)
186 = do v' <- ev ntimes stk top env v --(finalise v)
186 = do v' <- ev ntimes stk top env v --(finalise v)
187− step maxred
187+ when (not atRepl) $ step maxred
188 sc' <- ev ntimes stk top (v' : env) sc
188 sc' <- ev ntimes stk top (v' : env) sc
189 wknV (-1) sc'
189 wknV (-1) sc'
190 ev ntimes stk top env (Bind n (NLet t v) sc)
190 ev ntimes stk top env (Bind n (NLet t v) sc)
191 = do t' <- ev ntimes stk top env (finalise t)
191 = do t' <- ev ntimes stk top env (finalise t)
192 v' <- ev ntimes stk top env (finalise v)
192 v' <- ev ntimes stk top env (finalise v)
193− step maxred
193+ when (not atRepl) $ step maxred
194 sc' <- ev ntimes stk top (v' : env) sc
194 sc' <- ev ntimes stk top (v' : env) sc
195 return $ VBind n (Let t' v') (\x -> return sc')
195 return $ VBind n (Let t' v') (\x -> return sc')
196 ev ntimes stk top env (Bind n b sc)
196 ev ntimes stk top env (Bind n b sc)
197 = do b' <- vbind env b
197 = do b' <- vbind env b
198− step maxred
198+ when (not atRepl) $ step maxred
199 return $ VBind n b' (\x -> ev ntimes stk top (x:env) sc)
199 return $ VBind n b' (\x -> ev ntimes stk top (x:env) sc)
200 where vbind env t = fmapMB (\tm -> ev ntimes stk top env (finalise tm)) t
200 where vbind env t = fmapMB (\tm -> ev ntimes stk top env (finalise tm)) t
201 ev ntimes stk top env (App f a)
201 ev ntimes stk top env (App f a)
202 = do f' <- ev ntimes stk top env f
202 = do f' <- ev ntimes stk top env f
203 a' <- ev ntimes stk False env a
203 a' <- ev ntimes stk False env a
204− step maxred
204+ when (not atRepl) $ step maxred
205 evApply ntimes stk top env [a'] f'
205 evApply ntimes stk top env [a'] f'
206 ev ntimes stk top env (Constant c) = return $ VConstant c
206 ev ntimes stk top env (Constant c) = return $ VConstant c
207 ev ntimes stk top env Erased = return VErased
207 ev ntimes stk top env Erased = return VErased
@@ -209,7 +209,7 @@ eval ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
209
209
210 evApply ntimes stk top env args (VApp f a) =
210 evApply ntimes stk top env args (VApp f a) =
211 evApply ntimes stk top env (a:args) f
211 evApply ntimes stk top env (a:args) f
212− evApply ntimes stk top env args f = do step maxred
212+ evApply ntimes stk top env args f = do when (not atRepl) $ step maxred
213 apply ntimes stk top env f args
213 apply ntimes stk top env f args
214
214
215 apply ntimes stk top env (VBind n (Lam t) sc) (a:as)
215 apply ntimes stk top env (VBind n (Lam t) sc) (a:as)
src/Idris/Primitives.hs+5
@@ -45,6 +45,7 @@ intToStr x = foreign_ tyString "intToStr" [(x, tyInt)]
45charToInt x = x
45charToInt x = x
46intToChar x = x
46intToChar x = x
47intToBigInt x = foreign_ tyBigInt "intToBigInt" [(x, tyInt)]
47intToBigInt x = foreign_ tyBigInt "intToBigInt" [(x, tyInt)]
48+bigIntToInt x = foreign_ tyInt "bigIntToInt" [(x, tyBigInt)]
48strToBigInt x = foreign_ tyBigInt "strToBig" [(x, tyString)]
49strToBigInt x = foreign_ tyBigInt "strToBig" [(x, tyString)]
49bigIntToStr x = foreign_ tyString "bigToStr" [(x, tyBigInt)]
50bigIntToStr x = foreign_ tyString "bigToStr" [(x, tyBigInt)]
50strToFloat x = foreign_ tyFloat "strToFloat" [(x, tyString)]
51strToFloat x = foreign_ tyFloat "strToFloat" [(x, tyString)]
@@ -158,6 +159,8 @@ primitives =
158 ([E.name "x"], intToChar (fun "x")) total,
159 ([E.name "x"], intToChar (fun "x")) total,
159 Prim (UN "prim__intToBigInt") (ty [IType] BIType) 1 (c_intToBigInt)
160 Prim (UN "prim__intToBigInt") (ty [IType] BIType) 1 (c_intToBigInt)
160 ([E.name "x"], intToBigInt (fun "x")) total,
161 ([E.name "x"], intToBigInt (fun "x")) total,
162+ Prim (UN "prim__bigIntToInt") (ty [BIType] IType) 1 (c_bigIntToInt)
163+ ([E.name "x"], bigIntToInt (fun "x")) total,
161 Prim (UN "prim__strToBigInt") (ty [StrType] BIType) 1 (c_strToBigInt)
164 Prim (UN "prim__strToBigInt") (ty [StrType] BIType) 1 (c_strToBigInt)
162 ([E.name "x"], strToBigInt (fun "x")) total,
165 ([E.name "x"], strToBigInt (fun "x")) total,
163 Prim (UN "prim__bigIntToStr") (ty [BIType] StrType) 1 (c_bigIntToStr)
166 Prim (UN "prim__bigIntToStr") (ty [BIType] StrType) 1 (c_bigIntToStr)
@@ -257,6 +260,8 @@ c_charToInt _ = Nothing
257
260
258c_intToBigInt [VConstant (I x)] = Just $ VConstant (BI (fromIntegral x))
261c_intToBigInt [VConstant (I x)] = Just $ VConstant (BI (fromIntegral x))
259c_intToBigInt _ = Nothing
262c_intToBigInt _ = Nothing
263+c_bigIntToInt [VConstant (BI x)] = Just $ VConstant (I (fromInteger x))
264+c_bigIntToInt _ = Nothing
260
265
261c_bigIntToStr [VConstant (BI x)] = Just $ VConstant (Str (show x))
266c_bigIntToStr [VConstant (BI x)] = Just $ VConstant (Str (show x))
262c_bigIntToStr _ = Nothing
267c_bigIntToStr _ = Nothing