Skip to content

Commit

Permalink
added to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
philzook58 committed Jul 2, 2019
1 parent 0ffa54e commit ca3e626
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# rel

A blog post:

http://www.philipzucker.com/a-short-skinny-on-relations-towards-the-algebra-of-programming/


Links:
- www4.di.uminho.pt/~jno/ps/pdbc.pdf JN Oliveira Program Design By Calculation draft
- https://themattchan.com/docs/algprog.pdf algebra of programming textbook
27 changes: 20 additions & 7 deletions src/ListRel.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ tabulate f = [(x, f x) | x <- enumAll]
tabulate2 :: (BEnum a, BEnum b) => (a -> b -> c) -> Rel (a,b) c
tabulate2 f = [((x,y), f x y) | x <- enumAll, y <- enumAll]

rcompose :: Eq b => Rel a b -> Rel b c -> Rel a c
rcompose xs ys = [ (a,c) | (a, b) <- xs, (b', c) <- ys, b' == b]
rcompose :: Eq b => Rel b c -> Rel a b -> Rel a c
rcompose xs ys = [ (a,c) | (a, b) <- ys, (b', c) <- xs, b' == b]

x <<< y = rcompose x y

Expand Down Expand Up @@ -57,6 +57,7 @@ tabulateSearch f = [(a,b) | a <- enumAll, b <- f a]
searchRel :: Eq a => Rel a b -> (a -> [b])
searchRel r a = [b | (a', b) <- r, a == a']


power :: [a] -> [[a]] -- all subsets.
power (x:xs) = (power xs) ++ [ x : xs' | xs' <- power xs] -- x is in or not.

Expand All @@ -66,12 +67,17 @@ rElem = [(a,xs) | xs <- power enumAll, a <- xs]
power' :: Eq a => Rel a b -> Rel a [b]
power' r = [ (a, searchRel r a) | a <- leftSet r]


tabulatePartial :: BEnum a => (a -> Maybe b) -> Rel a b
tabulatePartial f = [(a,b) | a <- enumAll, b <- toList (f a)]

reflectInd :: (BoundedMeetSemiLattice (Rel a b)) => (a -> b -> Bool) -> Rel a b -- )BEnum a, BEnum b)
reflectInd f = filter (uncurry f) top


rOrd' :: (Ord a, BEnum a) =>Rel a a
rOrd' = reflectInd (<=)

-- rElem' = reflectInd elem -- going to build the top of [a]?
-- rOrd' = reflectInd (<=)
instance (Eq a, Eq b, BEnum a, BEnum b) => BoundedMeetSemiLattice (Rel a b) where
Expand Down Expand Up @@ -134,10 +140,10 @@ instance (Enum a, Enum b, Bounded a, Bounded b) => Enum (a,b) where
rfan :: Eq a => Rel a b -> Rel a c -> Rel a (b,c)
rfan f g = [ (a, (b,c)) | (a,b) <- f, (a',c) <- g, a == a']

rfst :: (Enum a, Bounded a, Enum b, Bounded b) => Rel (a,b) a
rfst :: BEnum (a,b) => Rel (a,b) a
rfst = tabulate fst -- map (fst . snd) rid

rsnd :: (Enum a, Bounded a, Enum b, Bounded b) => Rel (a,b) b
rsnd :: BEnum (a,b) => Rel (a,b) b
rsnd = tabulate snd -- map (snd . snd) rid

rleft :: (Enum a, Bounded a) => Rel a (Either a b)
Expand All @@ -151,6 +157,13 @@ reither f g = [(Left a, c) | (a,c) <- f] ++ [(Right b, c) | (b,c) <- g] -- (lma



---
swap r = [(b,a) | (a,b) <- r]
par f g = [((a,b), (c,d)) | (a,c) <- f, (b, d) <- g ]

--- goofy inefficient definitions
dup :: (Eq a, Eq b, BEnum a, BEnum b) => Rel a (a,a)
dup = rfan rid rid
swap ::(Eq a, Eq b, BEnum (a,b)) => Rel (a,b) (b,a)
swap = rfan rsnd rfst
par :: (Eq a, Eq c, BEnum a, BEnum c) => Rel a b -> Rel c d -> Rel (a,c) (b,d)
par f g = rfan (rcompose f rfst) (rcompose g rsnd)

-- [((a,b), (c,d)) | (a,c) <- f, (b, d) <- g ]
4 changes: 2 additions & 2 deletions src/PathRel.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module PathRel where

import ListRel
import Algebra.Lattice
import Algerba.Lattice.Ordered
-- import Algerba.Lattice.Ordered
type PathRel r a b = [(a,b,r)]
import Algebra.PartialOrd
-- import Algebra.PartialOrd

compose' x y = [ ( a,c , r' /\ r) | (b,c,r') <- x, (a,b',r) <- y , b == b']

Expand Down
4 changes: 2 additions & 2 deletions test/Hog.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import qualified Hedgehog.Range as Range
import ListRel hiding ((===))

-- a delapidated attempt at using hedgehog

{-
prop_reverse :: Property
prop_reverse =
property $ do
Expand Down Expand Up @@ -54,7 +54,7 @@ prop_top = property $ do
prop_iso_search = property $ do
r <- forAll simplerel
assert ((tabulateSearch (searchRel r)) `rEq` r)

-}
{-
other properties
Expand Down
17 changes: 10 additions & 7 deletions test/RelTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Test.QuickCheck.All

import ListRel
import Data.Word
import Algebra.Lattice
--dummy stuff


Expand All @@ -22,15 +23,17 @@ prop_ordering x = x == x

type R1 = Rel Bool Ordering



prop_indirect_eq :: R1 -> R1 -> R1 -> Bool
prop_indirect_eq x y z = (y ~~ x) `implies` ((z <~ x) == (z <~ y)) -- hmm. quickcheck is never gonna find y ~~ x

prop_ridleft :: Rel Bool Ordering -> Bool
prop_ridleft x = (rid <<< x) ~~ x

prop_ridright :: Rel Bool Ordering -> Bool
prop_ridright x = (x <<< rid) ~~ x

prop_indirect_eq :: R1 -> R1 -> R1 -> Bool
prop_indirect_eq x y z = (y ~~ x) `implies` ((z <~ x) == (z <~ y)) -- hmm. quickcheck is never gonna find y ~~ x

prop_meet :: R1 -> R1 -> Bool
prop_meet x y = (x /\ y) <~ x

Expand Down Expand Up @@ -64,17 +67,17 @@ prop_trans_iso :: Rel (Bool, Ordering) Word8 -> Bool
prop_trans_iso x = untrans (trans x) == x

prop_rdiv :: Rel Bool Ordering -> Rel Word8 Ordering -> Bool
prop_rdiv g j = ((rdiv g j) <<< j) <~ g
prop_rdiv g j = (j <<< (rdiv g j)) <~ g

prop_con :: R1 -> Bool
prop_con x = con (con x) ~~ x

prop_rdiv' :: Rel Bool Word8 -> Rel Bool Ordering -> Rel Word8 Ordering -> Bool
prop_rdiv' x g j = (x <~ (rdiv g j)) == ((x <<< j) <~ g)
prop_rdiv' x g j = (x <~ (rdiv g j)) == ((j <<< x) <~ g)

prop_rdiv'' :: Rel Bool Ordering -> Rel Bool Ordering -> Rel Ordering Ordering -> Bool
{- prop_rdiv'' :: Rel Bool Ordering -> Rel Bool Ordering -> Rel Ordering Ordering -> Bool
prop_rdiv'' x g j = (x <~ (rdiv g j)) == ((x <<< j) <~ g)

-}
{-
-- monotonicity properties
Expand Down

0 comments on commit ca3e626

Please sign in to comment.