Commit 519b15aa authored by Tim Steenvoorden's avatar Tim Steenvoorden
Browse files

implement Lattice for Int and Nat

parent 9e792843
definition module Data.Int definition module Data.Int
from Algebra.Order import class Eq, class Ord from Algebra.Order import class Eq, class Ord
from Algebra.Enum import class Enum
from Algebra.Group import class Semigroup, class Monoid, class Group from Algebra.Group import class Semigroup, class Monoid, class Group
from Algebra.Ring import class Semiring, class Ring, class Domain from Algebra.Ring import class Semiring, class Ring, class Domain
from Algebra.Enum import class Enum from Algebra.Lattice import class MeetSemilattice, class JoinSemilattice, class UpperBounded, class LowerBounded
/// # Definition /// # Definition
...@@ -23,6 +24,11 @@ instance Semiring Int ...@@ -23,6 +24,11 @@ instance Semiring Int
// instance Ring Int // instance Ring Int
instance Domain Int instance Domain Int
instance MeetSemilattice Int
instance JoinSemilattice Int
instance UpperBounded Int
instance LowerBounded Int
/// # Helpers /// # Helpers
inc :: !Int -> Int inc :: !Int -> Int
......
...@@ -3,9 +3,10 @@ implementation module Data.Int ...@@ -3,9 +3,10 @@ implementation module Data.Int
import Control.Function import Control.Function
import Algebra.Order import Algebra.Order
import Algebra.Enum
import Algebra.Group import Algebra.Group
import Algebra.Ring import Algebra.Ring
import Algebra.Enum import Algebra.Lattice
import Clean.Prim import Clean.Prim
...@@ -62,6 +63,7 @@ instance Monoid Int where ...@@ -62,6 +63,7 @@ instance Monoid Int where
instance Group Int where instance Group Int where
(-) x y = prim_subInt x y (-) x y = prim_subInt x y
inverse x = prim_negInt x inverse x = prim_negInt x
instance Semiring Int where instance Semiring Int where
...@@ -86,6 +88,18 @@ instance Domain Int where ...@@ -86,6 +88,18 @@ instance Domain Int where
lcm 0 _ = 0 lcm 0 _ = 0
lcm x y = abs ((x `quot` gcd x y) * y) lcm x y = abs ((x `quot` gcd x y) * y)
instance MeetSemilattice Int where
(/\) x y = prim_minInt x y
instance JoinSemilattice Int where
(\/) x y = prim_maxInt x y
instance UpperBounded Int where
top = undefined
instance LowerBounded Int where
bottom = undefined
/// # Helpers /// # Helpers
inc :: !Int -> Int inc :: !Int -> Int
......
...@@ -2,7 +2,8 @@ definition module Data.Nat ...@@ -2,7 +2,8 @@ definition module Data.Nat
from Algebra.Order import class Eq, class Ord from Algebra.Order import class Eq, class Ord
from Algebra.Group import class Semigroup, class Monoid from Algebra.Group import class Semigroup, class Monoid
from Algebra.Ring import class Semiring from Algebra.Ring import class Semiring, class Domain
from Algebra.Lattice import class MeetSemilattice, class JoinSemilattice, class UpperBounded, class LowerBounded
/// # Definition /// # Definition
...@@ -20,6 +21,11 @@ instance Semigroup Nat ...@@ -20,6 +21,11 @@ instance Semigroup Nat
instance Monoid Nat instance Monoid Nat
instance Semiring Nat instance Semiring Nat
instance MeetSemilattice Nat
instance JoinSemilattice Nat
instance UpperBounded Nat
instance LowerBounded Nat
/// # Special Algebra /// # Special Algebra
(.-) infixl 6 :: !Nat !Nat -> Nat (.-) infixl 6 :: !Nat !Nat -> Nat
...@@ -7,6 +7,7 @@ import Control.Function ...@@ -7,6 +7,7 @@ import Control.Function
import Algebra.Order import Algebra.Order
import Algebra.Group import Algebra.Group
import Algebra.Ring import Algebra.Ring
import Algebra.Lattice
import Clean.Prim import Clean.Prim
...@@ -17,8 +18,8 @@ import Clean.Prim ...@@ -17,8 +18,8 @@ import Clean.Prim
//TODO rewrite in inlined ABC (?) //TODO rewrite in inlined ABC (?)
nat :: !Int -> Nat nat :: !Int -> Nat
nat n nat n
| prim_ltInt n 0 = abort "Data.Nat.nat: negative integer" | n < 0 = prim_abort "Data.Nat.nat: negative integer"
| otherwise = n | otherwise = n
int :: !Nat -> Int int :: !Nat -> Int
int n = prim_noop int n = prim_noop
...@@ -48,10 +49,25 @@ instance Semiring Nat where ...@@ -48,10 +49,25 @@ instance Semiring Nat where
unity = prim_oneInt unity = prim_oneInt
/// ## Lattice
instance MeetSemilattice Nat where
(/\) x y = prim_minInt x y
instance JoinSemilattice Nat where
(\/) x y = prim_maxInt x y
instance UpperBounded Nat where
top = prim_upperInt
instance LowerBounded Nat where
bottom = prim_zeroInt
/// # Special Algebra /// # Special Algebra
//TODO rewrite in inlined ABC (?) //TODO rewrite in inlined ABC (?)
(.-) infixl 6 :: !Nat !Nat -> Nat (.-) infixl 6 :: !Nat !Nat -> Nat
(.-) n m (.-) n m
| prim_ltInt m n = prim_subInt n m | m < n = prim_subInt n m
| otherwise = 0 | otherwise = prim_zeroInt
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment