on master help sign in
Updated README
commit2fec7d986ba158ccf29ebc1489e152ccacd5d1ad authorArmando Santos <armandoifsantos@gmail.com> dateWed 29 Jan 2020, 00:47 (7 years ago) parent6f1b6e5
2 files changed+90−15
README.md+80−5 package.yaml+10−10
README.md+80−5
@@ -16,17 +16,20 @@ See the candidate package in hackage [here](https://hackage.haskell.org/package/
1616
1717This library offers:
1818
19−- An internal representation that uses normal datatypes (`Void`, `()`, `Either`) for matrix dimensions and two
19+- An internal module representation that uses normal datatypes (`Void`, `()`, `Either`) for matrix dimensions and two
2020 type families `FromNat` and `Count` to make it easier to work with these type of matrices.
21−- A module that uses generalised types for the dimensions and is just a newtype wrapper
21+- An interfacing module that uses generalised types for the dimensions and is just a newtype wrapper
2222 around the internal matrix data type.
23−- Other that uses type level naturals for the dimensions and is just a newtype wrapper
23+- An interfacing module that uses type level naturals for the dimensions and is just a newtype wrapper
2424 around the internal matrix data type.
25+- An interfacing module that restricts the matrix contents to being only 0's or 1's (True
26+ and False). This module is called 'Relation' and offers many of the combinators seen in
27+ AoP and Relational Calculus/Algebra.
2528- A very simple Probabilistic Programming module and several functions and data types to
2629 make it easier to deal with sample space.
2730
2831Given this, this new matrix formulation compared to other libraries one has much more advantages, such as:
29−
32+
3033- Has an inductive definition enabling writing matrix manipulation functions in a much more elegant and calculational way;
3134- Statically typed dimensions;
3235- Polymorphic data type dimensions;
@@ -35,7 +38,7 @@ Given this, this new matrix formulation compared to other libraries one has much
3538- Fast type natural conversion via `FromNat` type family;
3639- Matrix `Junc` and `Split`-ing in O(1);
3740- Matrix composition takes advantage of divide-and-conquer and fusion laws.
38−
41+
3942Unfortunately, this approach does not solve the issue of type dimensions being in some way constrainted making it impossible to write Arrow instances, for example. Type inference isn't perfect, when it comes to infer the types of matrices which dimensions are computed using type level naturals multiplication, the compiler needs type annotations in order to succeed.
4043
4144## Notes
@@ -118,6 +121,73 @@ grass_wet = row [0,1] . kp1
118121rainning :: Matrix Double (Bool, (Bool, Bool)) One
119122rainning = row [0,1] . kp2 . kp2
120123
124+-- Alcuin Puzzle
125+
126+data Being = Farmer | Fox | Goose | Beans
127+ deriving (Bounded, Enum, Eq, Show, Generic)
128+
129+data Bank = LeftB | RightB
130+ deriving (Bounded, Enum, Eq, Show, Generic)
131+
132+eats :: Being -> Being -> Bool
133+eats Fox Goose = True
134+eats Goose Beans = True
135+eats _ _ = False
136+
137+eatsR :: R.Relation Being Being
138+eatsR = R.toRel eats
139+
140+cross :: Bank -> Bank
141+cross LeftB = RightB
142+cross RightB = LeftB
143+
144+crossR :: R.Relation Bank Bank
145+crossR = R.fromF' cross
146+
147+-- | Initial state, everyone in the left bank
148+locationLeft :: Being -> Bank
149+locationLeft _ = LeftB
150+
151+locationLeftR :: R.Relation Being Bank
152+locationLeftR = R.fromF' locationLeft
153+
154+-- | Initial state, everyone in the right bank
155+locationRight :: Being -> Bank
156+locationRight _ = RightB
157+
158+locationRightR :: R.Relation Being Bank
159+locationRightR = R.fromF' locationRight
160+
161+-- Properties
162+
163+-- Being at the same bank
164+sameBank :: R.Relation Being Bank -> R.Relation Being Being
165+sameBank = R.ker
166+
167+-- Risk of somebody eating somebody else
168+canEat :: R.Relation Being Bank -> R.Relation Being Being
169+canEat w = sameBank w `R.intersection` eatsR
170+
171+-- "Starvation" property.
172+inv :: R.Relation Being Bank -> Bool
173+inv w = (w `R.comp` canEat w) `R.sse` (w `R.comp` farmer)
174+ where
175+ farmer :: R.Relation Being Being
176+ farmer = R.fromF' (const Farmer)
177+
178+-- Arbitrary state
179+bankState :: Being -> Bank -> Bool
180+bankState Farmer LeftB = True
181+bankState Fox LeftB = True
182+bankState Goose RightB = True
183+bankState Beans RightB = True
184+bankState _ _ = False
185+
186+bankStateR :: R.Relation Being Bank
187+bankStateR = R.toRel bankState
188+
189+-- Main
190+
121191main :: IO ()
122192main = do
123193 putStrLn "Monty Hall Problem solution:"
@@ -132,6 +202,8 @@ main = do
132202 prettyPrint (grass_wet . state)
133203 putStrLn "\n Probability of rain:"
134204 prettyPrint (rainning . state)
205+ putStrLn "\n Is the arbitrary state a valid state? (Alcuin Puzzle)"
206+ print (inv bankStateR)
135207```
136208
137209```Shell
@@ -190,4 +262,7 @@ Monty Hall Problem solution:
190262┌ ┐
191263│ 0.2 │
192264└ ┘
265+
266+Is the arbitrary state a valid state? (Alcuin Puzzle)
267+False
193268```
package.yaml+10−10
@@ -25,16 +25,16 @@ dependencies:
2525library:
2626 source-dirs: src
2727
28−executables:
29− laop-exe:
30− main: Main.hs
31− source-dirs: app
32− ghc-options:
33− - -threaded
34− - -rtsopts
35− - -with-rtsopts=-N
36− dependencies:
37− - laop
28+#executables:
29+# laop-exe:
30+# main: Main.hs
31+# source-dirs: app
32+# ghc-options:
33+# - -threaded
34+# - -rtsopts
35+# - -with-rtsopts=-N
36+# dependencies:
37+# - laop
3838
3939tests:
4040 laop-test: