on master help sign in
Add views for recursion over Int/Integer
commitf2ab3621cf5fff819e4092de55020c5359a8cce3 authorEdwin Brady <ecb10@st-andrews.ac.uk> dateTue 17 May 2016, 20:24 (10 years ago) parent986e616
2 files changed+37−2
libs/base/Data/Primitives/Views.idr+26 src/Idris/Elab/Clause.hs+11−2
libs/base/Data/Primitives/Views.idr+26
@@ -24,6 +24,19 @@ namespace Integer
2424 believe_me (DivBy {d} {div = dividend} {rem = remainder}
2525 (believe_me (Refl {x = True})))
2626
27+ ||| View for recursion over Integers
28+ data IntegerRec : Integer -> Type where
29+ IntegerZ : IntegerRec 0
30+ IntegerSucc : IntegerRec (n - 1) -> IntegerRec n
31+ IntegerPred : IntegerRec ((-n) + 1) -> IntegerRec (-n)
32+
33+ ||| Covering function for `IntegerRec`
34+ integerRec : (x : Integer) -> IntegerRec x
35+ integerRec 0 = IntegerZ
36+ integerRec x = if x > 0 then IntegerSucc (assert_total (integerRec (x - 1)))
37+ else believe_me (IntegerPred {n=-x}
38+ (assert_total (believe_me (integerRec (x + 1)))))
39+
2740namespace Int
2841 ||| View for expressing a number as a multiplication + a remainder
2942 public export
@@ -47,3 +60,16 @@ namespace Int
4760 believe_me (DivBy {d} {div = dividend} {rem = remainder}
4861 (believe_me (Refl {x = True})))
4962
63+ ||| View for recursion over Ints
64+ data IntRec : Int -> Type where
65+ IntZ : IntRec 0
66+ IntSucc : IntRec (n - 1) -> IntRec n
67+ IntPred : IntRec ((-n) + 1) -> IntRec (-n)
68+
69+ ||| Covering function for `IntRec`
70+ intRec : (x : Int) -> IntRec x
71+ intRec 0 = IntZ
72+ intRec x = if x > 0 then IntSucc (assert_total (intRec (x - 1)))
73+ else believe_me (IntPred {n=-x}
74+ (assert_total (believe_me (intRec (x + 1)))))
75+
src/Idris/Elab/Clause.hs+11−2
@@ -1041,9 +1041,18 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in pn_in withblock)
10411041
10421042 lhs_arity = arity toplhs
10431043
1044− updateApp wtm@(PWithApp fcw tm warg) =
1044+ currentFn fname (PAlternative _ _ as)
1045+ | Just tm <- getApp as = tm
1046+ where getApp (tm@(PApp _ (PRef _ _ f) _) : as)
1047+ | f == fname = Just tm
1048+ getApp (_ : as) = getApp as
1049+ getApp [] = Nothing
1050+ currentFn _ tm = tm
1051+
1052+ updateApp wtm@(PWithApp fcw tm_in warg) =
1053+ let tm = currentFn fname tm_in in
10451054 case matchClause ist toplhs tm of
1046− Left _ -> PElabError (Msg (show fc ++ ":with application does not match top level"))
1055+ Left _ -> PElabError (Msg (show fc ++ ":with application does not match top level "))
10471056 Right mvars ->
10481057 let ns = map (keepMvar (map fst mvars) fcw) ns_in
10491058 ns' = map (keepMvar (map fst mvars) fcw) ns_in'