on master help sign in
Merge pull request #212 from david-christiansen/inductive-ints
7c393c7Edwin Brady
lib/Data/Sign.idrnew file+6
@@ -0,0 +1,6 @@
1+module Data.Sign
2+
3+data Sign = Plus | Minus
4+
5+class Signed t where
6+ total sign : t -> Sign
lib/Data/Z.idrnew file+144
@@ -0,0 +1,144 @@
1+module Data.Z
2+
3+import Decidable.Equality
4+import Data.Sign
5+
6+%default total
7+%access public
8+
9+
10+-- | An integer is either a positive nat or the negated successor of a nat.
11+-- Zero is chosen to be positive.
12+data Z = Pos Nat | NegS Nat
13+
14+instance Signed Z where
15+ sign (Pos _) = Plus
16+ sign (NegS _) = Minus
17+
18+absZ : Z -> Nat
19+absZ (Pos n) = n
20+absZ (NegS n) = S n
21+
22+instance Show Z where
23+ show (Pos n) = show n
24+ show (NegS n) = "-" ++ show (S n)
25+
26+negZ : Z -> Z
27+negZ (Pos O) = Pos O
28+negZ (Pos (S n)) = NegS n
29+negZ (NegS n) = Pos (S n)
30+
31+negNat : Nat -> Z
32+negNat O = Pos O
33+negNat (S n) = NegS n
34+
35+minusNatZ : Nat -> Nat -> Z
36+minusNatZ n O = Pos n
37+minusNatZ O (S m) = NegS m
38+minusNatZ (S n) (S m) = minusNatZ n m
39+
40+plusZ : Z -> Z -> Z
41+plusZ (Pos n) (Pos m) = Pos (n + m)
42+plusZ (NegS n) (NegS m) = NegS (S (n + m))
43+plusZ (Pos n) (NegS m) = minusNatZ n (S m)
44+plusZ (NegS n) (Pos m) = minusNatZ m (S n)
45+
46+subZ : Z -> Z -> Z
47+subZ n m = plusZ n (negZ m)
48+
49+instance Eq Z where
50+ (Pos n) == (Pos m) = n == m
51+ (NegS n) == (NegS m) = n == m
52+ _ == _ = False
53+
54+
55+instance Ord Z where
56+ compare (Pos n) (Pos m) = compare n m
57+ compare (NegS n) (NegS m) = compare m n
58+ compare (Pos _) (NegS _) = GT
59+ compare (NegS _) (Pos _) = LT
60+
61+
62+multZ : Z -> Z -> Z
63+multZ (Pos n) (Pos m) = Pos $ n * m
64+multZ (NegS n) (NegS m) = Pos $ (S n) * (S m)
65+multZ (NegS n) (Pos m) = negNat $ (S n) * m
66+multZ (Pos n) (NegS m) = negNat $ n * (S m)
67+
68+fromInt : Int -> Z
69+fromInt n = if n < 0
70+ then NegS $ fromInteger {a=Nat} (-n - 1)
71+ else Pos $ fromInteger {a=Nat} n
72+
73+instance Cast Nat Z where
74+ cast n = Pos n
75+
76+instance Num Z where
77+ (+) = plusZ
78+ (-) = subZ
79+ (*) = multZ
80+ abs = cast . absZ
81+ fromInteger = fromInt
82+
83+instance Cast Z Int where
84+ cast (Pos n) = cast n
85+ cast (NegS n) = (-1) * (cast n + 1)
86+
87+instance Cast Int Z where
88+ cast = fromInteger
89+
90+
91+--------------------------------------------------------------------------------
92+-- Properties
93+--------------------------------------------------------------------------------
94+
95+natPlusZPlus : (n : Nat) -> (m : Nat) -> (x : Nat)
96+ -> n + m = x -> (Pos n) + (Pos m) = Pos x
97+natPlusZPlus n m x h = cong h
98+
99+natMultZMult : (n : Nat) -> (m : Nat) -> (x : Nat)
100+ -> n * m = x -> (Pos n) * (Pos m) = Pos x
101+natMultZMult n m x h = cong h
102+
103+doubleNegElim : (z : Z) -> negZ (negZ z) = z
104+doubleNegElim (Pos O) = refl
105+doubleNegElim (Pos (S n)) = refl
106+doubleNegElim (NegS O) = refl
107+doubleNegElim (NegS (S n)) = refl
108+
109+-- Injectivity
110+posInjective : Pos n = Pos m -> n = m
111+posInjective refl = refl
112+
113+negSInjective : NegS n = NegS m -> n = m
114+negSInjective refl = refl
115+
116+posNotNeg : Pos n = NegS m -> _|_
117+posNotNeg refl impossible
118+
119+-- Decidable equality
120+instance DecEq Z where
121+ decEq (Pos n) (NegS m) = No posNotNeg
122+ decEq (NegS n) (Pos m) = No $ negEqSym posNotNeg
123+ decEq (Pos n) (Pos m) with (decEq n m)
124+ | Yes p = Yes $ cong p
125+ | No p = No $ \h => p $ posInjective h
126+ decEq (NegS n) (NegS m) with (decEq n m)
127+ | Yes p = Yes $ cong p
128+ | No p = No $ \h => p $ negSInjective h
129+
130+-- Plus
131+plusZeroLeftNeutralZ : (right : Z) -> 0 + right = right
132+plusZeroLeftNeutralZ (Pos n) = refl
133+plusZeroLeftNeutralZ (NegS n) = refl
134+
135+plusZeroRightNeutralZ : (left : Z) -> left + 0 = left
136+plusZeroRightNeutralZ (Pos n) = cong $ plusZeroRightNeutral n
137+plusZeroRightNeutralZ (NegS n) = refl
138+
139+plusCommutativeZ : (left : Z) -> (right : Z) -> (left + right = right + left)
140+plusCommutativeZ (Pos n) (Pos m) = cong $ plusCommutative n m
141+plusCommutativeZ (Pos n) (NegS m) = refl
142+plusCommutativeZ (NegS n) (Pos m) = refl
143+plusCommutativeZ (NegS n) (NegS m) = cong {f=NegS} $ cong {f=S} $ plusCommutative n m
144+
lib/base.ipkg+1−1
@@ -17,7 +17,7 @@ modules = Builtins, Prelude, IO, System,
17
17
18 Language.Reflection,
18 Language.Reflection,
19
19
20− Data.Morphisms, Data.Bits, Data.Mod2,
20+ Data.Morphisms, Data.Bits, Data.Mod2, Data.Z, Data.Sign,
21
21
22 Control.Monad.Identity, Control.Monad.State, Control.Category,
22 Control.Monad.Identity, Control.Monad.State, Control.Category,
23 Control.Arrow,
23 Control.Arrow,