Skip to content

Commit 4033348

Browse files
committed
Merge pull request #64 from purescript/class-updates
Class updates
2 parents a215586 + 9c8176c commit 4033348

17 files changed

+217
-199
lines changed

src/Data/BooleanAlgebra.purs

+11-54
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,20 @@
11
module Data.BooleanAlgebra
2-
( class BooleanAlgebra, conj, disj, not
3-
, (&&), (||)
2+
( class BooleanAlgebra
3+
, module Data.HeytingAlgebra
44
) where
55

6-
import Data.Bounded (class Bounded)
7-
import Data.Unit (Unit, unit)
6+
import Data.HeytingAlgebra (class HeytingAlgebra, ff, tt, implies, conj, disj, not)
7+
import Data.Unit (Unit)
88

99
-- | The `BooleanAlgebra` type class represents types that behave like boolean
1010
-- | values.
1111
-- |
12-
-- | Instances should satisfy the following laws in addition to the `Bounded`
13-
-- | laws:
12+
-- | Instances should satisfy the following laws in addition to the
13+
-- | `HeytingAlgebra` law:
1414
-- |
15-
-- | - Associativity:
16-
-- | - `a || (b || c) = (a || b) || c`
17-
-- | - `a && (b && c) = (a && b) && c`
18-
-- | - Commutativity:
19-
-- | - `a || b = b || a`
20-
-- | - `a && b = b && a`
21-
-- | - Distributivity:
22-
-- | - `a && (b || c) = (a && b) || (a && c)`
23-
-- | - `a || (b && c) = (a || b) && (a || c)`
24-
-- | - Identity:
25-
-- | - `a || bottom = a`
26-
-- | - `a && top = a`
27-
-- | - Idempotent:
28-
-- | - `a || a = a`
29-
-- | - `a && a = a`
30-
-- | - Absorption:
31-
-- | - `a || (a && b) = a`
32-
-- | - `a && (a || b) = a`
33-
-- | - Annhiliation:
34-
-- | - `a || top = top`
35-
-- | - Complementation:
36-
-- | - `a && not a = bottom`
37-
-- | - `a || not a = top`
38-
class Bounded a <= BooleanAlgebra a where
39-
conj :: a -> a -> a
40-
disj :: a -> a -> a
41-
not :: a -> a
15+
-- | - Excluded middle:
16+
-- | - `a || not a = tt`
17+
class HeytingAlgebra a <= BooleanAlgebra a
4218

43-
infixr 3 conj as &&
44-
infixr 2 disj as ||
45-
46-
instance booleanAlgebraBoolean :: BooleanAlgebra Boolean where
47-
conj = boolConj
48-
disj = boolDisj
49-
not = boolNot
50-
51-
instance booleanAlgebraUnit :: BooleanAlgebra Unit where
52-
conj _ _ = unit
53-
disj _ _ = unit
54-
not _ = unit
55-
56-
instance booleanAlgebraFn :: BooleanAlgebra b => BooleanAlgebra (a -> b) where
57-
conj fx fy a = fx a `conj` fy a
58-
disj fx fy a = fx a `disj` fy a
59-
not fx a = not (fx a)
60-
61-
foreign import boolConj :: Boolean -> Boolean -> Boolean
62-
foreign import boolDisj :: Boolean -> Boolean -> Boolean
63-
foreign import boolNot :: Boolean -> Boolean
19+
instance booleanAlgebraBoolean :: BooleanAlgebra Boolean
20+
instance booleanAlgebraUnit :: BooleanAlgebra Unit

src/Data/Bounded.purs

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
module Data.Bounded (class Bounded, bottom, top) where
2-
1+
module Data.Bounded
2+
( class Bounded
3+
, bottom
4+
, top
5+
, module Data.Ord
6+
) where
7+
8+
import Data.Ord (class Ord, Ordering(..), compare, (<), (<=), (>), (>=))
39
import Data.Unit (Unit, unit)
410

5-
-- | The `Bounded` type class represents types that have an upper and lower
6-
-- | boundary.
11+
-- | The `Bounded` type class represents totally ordered types that have an
12+
-- | upper and lower boundary.
713
-- |
8-
-- | Although there are no "internal" laws for `Bounded`, every value of `a`
9-
-- | should be considered less than or equal to `top` by some means, and greater
10-
-- | than or equal to `bottom`.
14+
-- | Instances should satisfy the following law in addition to the `Ord` laws:
1115
-- |
12-
-- | The lack of explicit `Ord` constraint allows flexibility in the use of
13-
-- | `Bounded` so it can apply to total and partially ordered sets, boolean
14-
-- | algebras, etc.
15-
class Bounded a where
16+
-- | - Bounded: `bottom <= a <= top`
17+
class Ord a <= Bounded a where
1618
top :: a
1719
bottom :: a
1820

@@ -35,10 +37,10 @@ instance boundedChar :: Bounded Char where
3537
foreign import topChar :: Char
3638
foreign import bottomChar :: Char
3739

40+
instance boundedOrdering :: Bounded Ordering where
41+
top = GT
42+
bottom = LT
43+
3844
instance boundedUnit :: Bounded Unit where
3945
top = unit
4046
bottom = unit
41-
42-
instance boundedFn :: Bounded b => Bounded (a -> b) where
43-
top _ = top
44-
bottom _ = bottom

src/Data/BoundedOrd.purs

-24
This file was deleted.

src/Data/CommutativeRing.purs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Data.CommutativeRing
2+
( class CommutativeRing
3+
, module Data.Ring
4+
, module Data.Semiring
5+
) where
6+
7+
import Data.Ring (class Ring)
8+
import Data.Semiring (class Semiring, add, mul, one, zero, (*), (+))
9+
import Data.Unit (Unit)
10+
11+
-- | The `CommutativeRing` class is for rings where multiplication is
12+
-- | commutative.
13+
-- |
14+
-- | Instances must satisfy the following law in addition to the `Ring`
15+
-- | laws:
16+
-- |
17+
-- | - Commutative multiplication: `a * b = b * a`
18+
class Ring a <= CommutativeRing a
19+
20+
instance commutativeRingInt :: CommutativeRing Int
21+
instance commutativeRingNumber :: CommutativeRing Number
22+
instance commutativeRingUnit :: CommutativeRing Unit

src/Data/DivisionRing.purs

-25
This file was deleted.

src/Data/ModuloSemiring.js src/Data/EuclideanRing.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
"use strict";
22

3-
// module Data.ModuloSemiring
3+
// module Data.EuclideanRing
4+
5+
exports.intDegree = function (x) {
6+
return Math.abs(x);
7+
};
48

59
exports.intDiv = function (x) {
610
return function (y) {

src/Data/EuclideanRing.purs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module Data.EuclideanRing
2+
( class EuclideanRing, degree, div, mod, (/)
3+
, module Data.CommutativeRing
4+
, module Data.Ring
5+
, module Data.Semiring
6+
) where
7+
8+
import Data.CommutativeRing (class CommutativeRing)
9+
import Data.Ring (class Ring, sub, (-))
10+
import Data.Semiring (class Semiring, add, mul, one, zero, (*), (+))
11+
import Data.Unit (Unit, unit)
12+
13+
-- | The `EuclideanRing` class is for commutative rings that support division.
14+
-- |
15+
-- | Instances must satisfy the following law in addition to the `Ring`
16+
-- | laws:
17+
-- |
18+
-- | - Integral domain: `a /= 0` and `b /= 0` implies `a * b /= 0`
19+
-- | - Multiplicative Euclidean function: ``a = (a / b) * b + (a `mod` b)``
20+
-- | where `degree a > 0` and `degree a <= degree (a * b)`
21+
class CommutativeRing a <= EuclideanRing a where
22+
degree :: a -> Int
23+
div :: a -> a -> a
24+
mod :: a -> a -> a
25+
26+
infixl 7 div as /
27+
28+
instance euclideanRingInt :: EuclideanRing Int where
29+
degree = intDegree
30+
div = intDiv
31+
mod = intMod
32+
33+
instance euclideanRingNumber :: EuclideanRing Number where
34+
degree _ = 1
35+
div = numDiv
36+
mod _ _ = 0.0
37+
38+
instance euclideanRingUnit :: EuclideanRing Unit where
39+
degree _ = 1
40+
div _ _ = unit
41+
mod _ _ = unit
42+
43+
foreign import intDegree :: Int -> Int
44+
foreign import intDiv :: Int -> Int -> Int
45+
foreign import intMod :: Int -> Int -> Int
46+
47+
foreign import numDiv :: Number -> Number -> Number

src/Data/Field.purs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module Data.Field
2+
( class Field
3+
, module Data.CommutativeRing
4+
, module Data.EuclideanRing
5+
, module Data.Ring
6+
, module Data.Semiring
7+
) where
8+
9+
import Data.CommutativeRing (class CommutativeRing)
10+
import Data.EuclideanRing (class EuclideanRing, degree, div, mod, (/))
11+
import Data.Ring (class Ring, negate, sub)
12+
import Data.Semiring (class Semiring, add, mul, one, zero, (*), (+))
13+
import Data.Unit (Unit)
14+
15+
-- | The `Field` class is for types that are commutative fields.
16+
-- |
17+
-- | Instances must satisfy the following law in addition to the
18+
-- | `CommutativeRing` and `EuclideanRing` laws:
19+
-- |
20+
-- | - Non-zero multiplicative inverse: ``a `mod` b = 0` for all `a` and `b`
21+
class (CommutativeRing a, EuclideanRing a) <= Field a
22+
23+
instance fieldNumber :: Field Number
24+
instance fieldUnit :: Field Unit

src/Data/BooleanAlgebra.js src/Data/HeytingAlgebra.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
// module Data.BooleanAlgebra
3+
// module Data.HeytingAlgebra
44

55
exports.boolConj = function (b1) {
66
return function (b2) {

src/Data/HeytingAlgebra.purs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
module Data.HeytingAlgebra
2+
( class HeytingAlgebra, tt, ff, implies, conj, disj, not
3+
, (&&), (||)
4+
) where
5+
6+
import Data.Unit (Unit, unit)
7+
8+
-- | The `HeytingAlgebra` type class represents types are bounded lattices with
9+
-- | an implication operator such that the following laws hold:
10+
-- |
11+
-- | - Associativity:
12+
-- | - `a || (b || c) = (a || b) || c`
13+
-- | - `a && (b && c) = (a && b) && c`
14+
-- | - Commutativity:
15+
-- | - `a || b = b || a`
16+
-- | - `a && b = b && a`
17+
-- | - Absorption:
18+
-- | - `a || (a && b) = a`
19+
-- | - `a && (a || b) = a`
20+
-- | - Idempotent:
21+
-- | - `a || a = a`
22+
-- | - `a && a = a`
23+
-- | - Identity:
24+
-- | - `a || ff = a`
25+
-- | - `a && tt = a`
26+
-- | - Implication:
27+
-- | - ``a `implies` a = tt``
28+
-- | - ``a && (a `implies` b) = a && b``
29+
-- | - ``b && (a `implies` b) = b``
30+
-- | - ``a `implies` (b && c) = (a `implies` b) && (a `implies` c)``
31+
-- | - Complemented:
32+
-- | - ``not a = a `implies` ff``
33+
class HeytingAlgebra a where
34+
ff :: a
35+
tt :: a
36+
implies :: a -> a -> a
37+
conj :: a -> a -> a
38+
disj :: a -> a -> a
39+
not :: a -> a
40+
41+
infixr 3 conj as &&
42+
infixr 2 disj as ||
43+
44+
instance heytingAlgebraBoolean :: HeytingAlgebra Boolean where
45+
ff = false
46+
tt = true
47+
implies a b = not a || b
48+
conj = boolConj
49+
disj = boolDisj
50+
not = boolNot
51+
52+
instance heytingAlgebraUnit :: HeytingAlgebra Unit where
53+
ff = unit
54+
tt = unit
55+
implies _ _ = unit
56+
conj _ _ = unit
57+
disj _ _ = unit
58+
not _ = unit
59+
60+
instance heytingAlgebraFunction :: HeytingAlgebra b => HeytingAlgebra (a -> b) where
61+
ff _ = ff
62+
tt _ = tt
63+
implies f g a = f a `implies` g a
64+
conj f g a = f a && g a
65+
disj f g a = f a || g a
66+
not f a = not (f a)
67+
68+
foreign import boolConj :: Boolean -> Boolean -> Boolean
69+
foreign import boolDisj :: Boolean -> Boolean -> Boolean
70+
foreign import boolNot :: Boolean -> Boolean

src/Data/ModuloSemiring.purs

-37
This file was deleted.

0 commit comments

Comments
 (0)