on master help sign in
.ghc.environment.x86_64-linux-8.6.5deleted−17
since splitsnapshots
unifiedsplit
@@ -1,17 +0,0 @@
1−-- This is a GHC environment file written by cabal. This means you can
2−-- run ghc or ghci and get the environment of the project as a whole.
3−-- But you still need to use cabal repl $target to get the environment
4−-- of specific components (libs, exes, tests etc) because each one can
5−-- have its own source dirs, cpp flags etc.
6−--
7−clear-package-db
8−global-package-db
9−package-db /home/bolt/.cabal/store/ghc-8.6.5/package.db
10−package-db packagedb/ghc-8.6.5
11−package-id laop-0.1.0.0-inplace
12−package-id base-4.12.0.0
13−package-id ghc-prim-0.5.3
14−package-id rts
15−package-id integer-gmp-1.0.2.0
16−package-id deepseq-1.4.4.0
17−package-id array-0.5.3.0
.github/workflows/ci.ymlnew file+43
@@ -0,0 +1,43 @@
1+name: CI
2+
3+# Trigger the workflow on push or pull request, but only for the master branch
4+on:
5+ pull_request:
6+ push:
7+ branches: [master]
8+
9+jobs:
10+ build:
11+ name: ghc ${{ matrix.ghc }}
12+ runs-on: ubuntu-16.04
13+ strategy:
14+ matrix:
15+ cabal: ["3.0"]
16+ ghc:
17+ - "8.6.5"
18+ - "8.8.3"
19+
20+ steps:
21+ - uses: actions/checkout@v2
22+ if: github.event.action == 'opened' || github.event.action == 'synchronize' || github.event.ref == 'refs/heads/master'
23+
24+ - uses: actions/setup-haskell@v1
25+ name: Setup Haskell
26+ with:
27+ ghc-version: ${{ matrix.ghc }}
28+ cabal-version: ${{ matrix.cabal }}
29+
30+ - uses: actions/cache@v1
31+ name: Cache ~/.cabal/store
32+ with:
33+ path: ~/.cabal/store
34+ key: ${{ runner.os }}-${{ matrix.ghc }}-cabal
35+
36+ - name: Build
37+ run: |
38+ cabal v2-update
39+ cabal v2-build --enable-tests --enable-benchmarks
40+
41+ - name: Test
42+ run: |
43+ cabal v2-test --enable-tests
.gitignore+54−2
@@ -1,3 +1,55 @@
1+### Haskell
2+dist
3+dist-*
4+cabal-dev
5+*.o
6+*.hi
7+*.chi
8+*.chs.h
9+*.dyn_o
10+*.dyn_hi
11+*.prof
12+*.aux
13+*.hp
14+*.eventlog
15+.virtualenv
16+.hsenv
17+.hpc
18+.cabal-sandbox/
19+cabal.sandbox.config
20+cabal.config
21+cabal.project.local
22+.ghc.environment.*
23+.HTF/
24+# Stack
1.stack-work/
25.stack-work/
2−laop.cabal
26+stack.yaml.lock
27+
28+### IDE/support
29+# Vim
30+[._]*.s[a-v][a-z]
31+[._]*.sw[a-p]
32+[._]s[a-v][a-z]
33+[._]sw[a-p]
3−*~
34+*~
35+tags
36+
37+# IntellijIDEA
38+.idea/
39+.ideaHaskellLib/
40+*.iml
41+
42+# Atom
43+.haskell-ghc-mod.json
44+
45+# VS
46+.vscode/
47+
48+# Emacs
49+*#
50+.dir-locals.el
51+TAGS
52+
53+# other
54+.DS_Store
55+hackage.sh
.travis.ymlnew file+47
@@ -0,0 +1,47 @@
1+sudo: true
2+language: haskell
3+
4+git:
5+ depth: 5
6+
7+cabal: "3.0"
8+
9+cache:
10+ directories:
11+ - "$HOME/.cabal/store"
12+ - "$HOME/.stack"
13+ - "$TRAVIS_BUILD_DIR/.stack-work"
14+
15+matrix:
16+ include:
17+ - ghc: 8.6.5
18+ - ghc: 8.8.3
19+
20+ - ghc: 8.8.3
21+ env: STACK_YAML="$TRAVIS_BUILD_DIR/stack-8.8.3.yaml"
22+
23+ - ghc: 8.6.5
24+ env: STACK_YAML="$TRAVIS_BUILD_DIR/stack.yaml"
25+
26+
27+install:
28+ - |
29+ if [ -z "$STACK_YAML" ]; then
30+ cabal v2-update
31+ cabal v2-build --enable-tests --enable-benchmarks
32+ else
33+ curl -sSL https://get.haskellstack.org/ | sh
34+ stack --version
35+ stack build --system-ghc --test --bench --no-run-tests --no-run-benchmarks
36+ fi
37+
38+script:
39+ - |
40+ if [ -z "$STACK_YAML" ]; then
41+ cabal v2-test --enable-tests
42+ else
43+ stack test --system-ghc
44+ fi
45+
46+notifications:
47+ email: false
CHANGELOG.mdnew file+17
@@ -0,0 +1,17 @@
1+# Changelog
2+
3+`laop` uses [PVP Versioning][1].
4+The changelog is available [on GitHub][2].
5+
6+## 0.1.1.1
7+
8+* Bump base version to work with cabal
9+
10+## 0.1.1.0
11+
12+* Package is more organized and now has tests and benchmarks separated
13+* Repository is more organized and clean
14+* Added CI
15+
16+[1]: https://pvp.haskell.org
17+[2]: https://github.com/bolt12/laop2/releases
ChangeLog.mddeleted−3
@@ -1,3 +0,0 @@
1−# Changelog for laop
2−
3−## Unreleased changes
LICENSE+17−26
@@ -1,30 +1,21 @@
1−Copyright Armando Santos (c) 2019
1+MIT License
2
2
3−All rights reserved.
3+Copyright (c) 2020 Armando Santos
4
4
5−Redistribution and use in source and binary forms, with or without
5+Permission is hereby granted, free of charge, to any person obtaining a copy
6+of this software and associated documentation files (the "Software"), to deal
7+in the Software without restriction, including without limitation the rights
8+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+copies of the Software, and to permit persons to whom the Software is
6−modification, are permitted provided that the following conditions are met:
10+furnished to do so, subject to the following conditions:
7
11
8− * Redistributions of source code must retain the above copyright
12+The above copyright notice and this permission notice shall be included in all
9− notice, this list of conditions and the following disclaimer.
13+copies or substantial portions of the Software.
10
14
11− * Redistributions in binary form must reproduce the above
15+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12− copyright notice, this list of conditions and the following
13− disclaimer in the documentation and/or other materials provided
14− with the distribution.
15−
16− * Neither the name of Armando Santos nor the names of other
17− contributors may be used to endorse or promote products derived
18− from this software without specific prior written permission.
19−
20−THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21−"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22−LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23−A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
17+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24−OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25−SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26−LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27−DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28−THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
19+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29−(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
20+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30−OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21+SOFTWARE.
README.md+34−30
@@ -1,5 +1,11 @@
1# laop - Linear Algebra of Programming library
1# laop - Linear Algebra of Programming library
2
2
3+[![GitHub CI](https://github.com/bolt12/laop/workflows/CI/badge.svg)](https://github.com/bolt12/laop/actions)
4+[![Build status](https://img.shields.io/travis/bolt12/laop.svg?logo=travis)](https://travis-ci.org/bolt12/laop)
5+[![Hackage](https://img.shields.io/hackage/v/laop.svg?logo=haskell)](https://hackage.haskell.org/package/laop)
6+[![Stackage Lts](http://stackage.org/package/laop/badge/lts)](http://stackage.org/lts/package/laop)
7+[![Stackage Nightly](http://stackage.org/package/laop/badge/nightly)](http://stackage.org/nightly/package/laop)
8+
3The LAoP discipline generalises relations and functions treating them as
9The LAoP discipline generalises relations and functions treating them as
4Boolean matrices and in turn consider these as arrows.
10Boolean matrices and in turn consider these as arrows.
5
11
@@ -10,40 +16,38 @@ motivation behind the library, the underlying theory, and implementation details
10This module offers many of the combinators mentioned in the work of
16This module offers many of the combinators mentioned in the work of
11[Macedo (2012)](https://repositorium.sdum.uminho.pt/handle/1822/22894) and [Oliveira (2012)](https://pdfs.semanticscholar.org/ccf5/27fa9179081223bffe8067edd81948644fc0.pdf).
17[Macedo (2012)](https://repositorium.sdum.uminho.pt/handle/1822/22894) and [Oliveira (2012)](https://pdfs.semanticscholar.org/ccf5/27fa9179081223bffe8067edd81948644fc0.pdf).
12
18
13−See the package in hackage [here](https://hackage.haskell.org/package/laop-0.1.0.5)
19+See the package in hackage [here](https://hackage.haskell.org/package/laop-0.1.1.0)
14−
15−There are a couple of repos with a WIP of benchmarks comparing [matrix composition](https://github.com/bolt12/laop-benchmarks) and a real use case of [data analytics](https://github.com/bolt12/laop-analytics).
16
20
17−A Functional Pearl is also on its way, hopefully will be the reference document for this library.
21+A Functional Pearl has been written and can be regarded as the [reference document](https://github.com/bolt12/tymfgg-pearl) for this library.
18
22
19## Features
23## Features
20
24
21−This library offers:
25+This library offers 3 main matrix programming modules:
22−
26+
23−- An internal module representation that uses normal datatypes (`Void`, `()`, `Either`) for matrix dimensions and two
27+- One in which matrices are typed with type level natural numbers;
24− type families `FromNat` and `Count` to make it easier to work with these type of matrices.
28+- One in which matrices are typed with arbitrary generic data types;
25−- An interfacing module that uses generalised types for the dimensions and is just a newtype wrapper
29+- One in which matrices are regarded as relations, i.e. boolean matrices;
26− around the internal matrix data type.
30+
27−- An interfacing module that uses type level naturals for the dimensions and is just a newtype wrapper
31+There's also an experimental module that uses matrices to represent probability
28− around the internal matrix data type.
32+distributions.
29−- An interfacing module that restricts the matrix contents to being only 0's or 1's (True
33+
30− and False). This module is called 'Relation' and offers many of the combinators seen in
34+The most interesting feature is that matrices are represented as an inductive data type.
31− AoP and Relational Calculus/Algebra.
35+Given this formulation, matrix algorithms can be expressed in a much more elegant and
32−- A very simple Probabilistic Programming module and several functions and data types to
36+calculational style than the traditional vector of vectors representation. This data type
33− make it easier to deal with sample space.
37+guarantees that a matrix will always have valid dimensions. It can also express block matrix
34−
38+computations naturally, which leads to total, efficient and statically typed manipulation
35−Given this, this new matrix formulation compared to other libraries one has much more advantages, such as:
39+and transformation functions.
36−
40+
37−- Has an inductive definition enabling writing matrix manipulation functions in a much more elegant and calculational way;
41+Like matrix multiplication, other common operations, such as matrix transposition, benefit from a block-oriented structure that leads to a simple and natural divide-and-conquer algorithmic solution. Performance wise, this means that without much effort we can obtain optimal cache-oblivious algorithms.
38−- Statically typed dimensions;
42+
39−- Polymorphic data type dimensions;
43+Given this, this new matrix formulation compared to other libraries:
40−- Polymorphic matrix content;
44+
41−- Awesome dimension type inference;
45+- Is more compositional and polymorphic and does not have partial matrix manipulation functions (hence less chances for usage errors);
42−- Fast type natural conversion via `FromNat` type family;
46+- Our implementation of matrices enables simple manipulation of submatrices, making it particularly suitable for formal verification and equation reasoning, using the mathematical framework defined by the linear algebra of programming. Furthermore, the data type constructors ensure that the matrices of this kind are sound, i.e. malformed matrices with incorrect dimensions of the sort, can not be constructed.
43−- Matrix `Junc` and `Split`-ing in O(1);
47+
44−- Matrix composition takes advantage of divide-and-conquer and fusion laws.
48+## Known issues
45−
49+
46−Unfortunately, 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.
50+Unfortunately, due to the use of type-level programming features, this approach sometimes requires type dimensions to be constrainted, in some way, impossible to write idiomatic 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. And not all kinds of programs can be modeled using matrices, namely programs that deal with arbitrary infinite data types such as lists or integers (although there are some workarounds).
47
51
48## Notes
52## Notes
49
53
Setup.hsdeleted−2
@@ -1,2 +0,0 @@
1−import Distribution.Simple
2−main = defaultMain
app/Main.hsdeleted−7
@@ -1,7 +0,0 @@
1−module Main where
2−
3−import Examples
4−import Benchmarks
5−
6−main :: IO ()
7−main = benchmark
appveyor.ymlnew file+34
@@ -0,0 +1,34 @@
1+clone_folder: "c:\\WORK"
2+clone_depth: 5
3+
4+# Do not build feature branch with open Pull Requests
5+skip_branch_with_pr: true
6+
7+platform:
8+ - x86_64
9+
10+cache:
11+ - "C:\\SR"
12+ - dist-newstyle
13+
14+environment:
15+ global:
16+ CABOPTS: --store-dir=C:\\SR
17+
18+ matrix:
19+ - GHCVER: 8.6.5
20+
21+install:
22+ - choco source add -n mistuke -s https://www.myget.org/F/mistuke/api/v2
23+ - choco install -y cabal --version 2.4.1.0
24+ - choco install -y ghc --version 8.6.3
25+ - refreshenv
26+
27+before_build:
28+ - cabal --version
29+ - ghc --version
30+ - cabal %CABOPTS% v2-update
31+
32+build_script:
33+ - cabal %CABOPTS% v2-build --enable-tests
34+ - cabal %CABOPTS% v2-test --enable-tests
app/Benchmarks.hs → benchmark/Main.hsrenamed+10−13
@@ -8,15 +8,12 @@
8{-# LANGUAGE AllowAmbiguousTypes #-}
8{-# LANGUAGE AllowAmbiguousTypes #-}
9{-# LANGUAGE ScopedTypeVariables #-}
9{-# LANGUAGE ScopedTypeVariables #-}
10
10
11−module Benchmarks
12− ( benchmark
13− )
14− where
11+module Main (main) where
15
12
16import LAoP.Dist.Internal
13import LAoP.Dist.Internal
17import LAoP.Utils
14import LAoP.Utils
18−import LAoP.Matrix.Type hiding (CountableN, Countable, FromListsN)
15+import LAoP.Matrix.Type hiding (CountableN, Countable, FLN)
19−import qualified LAoP.Matrix.Type as T (FromListsN)
16+import qualified LAoP.Matrix.Type as T (FLN)
20import Criterion.Main
17import Criterion.Main
21import Control.DeepSeq
18import Control.DeepSeq
22import Test.QuickCheck
19import Test.QuickCheck
@@ -26,13 +23,13 @@ import Prelude hiding (id, (.))
26
23
27selectM2 ::
24selectM2 ::
28 ( Num e,
25 ( Num e,
29− T.FromListsN e b b,
26+ T.FLN b b,
30 CountableN b
27 CountableN b
31 ) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
28 ) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
32selectM2 m y = join y iden `comp` m
29selectM2 m y = join y iden `comp` m
33
30
34selectD2 ::
31selectD2 ::
35− ( FromListsN b b,
32+ ( FLN b b,
36 CountableN b
33 CountableN b
37 ) => Dist (Either a b) -> Matrix Prob a b -> Dist b
34 ) => Dist (Either a b) -> Matrix Prob a b -> Dist b
38selectD2 (D d) m = D (join m iden `comp` d)
35selectD2 (D d) m = D (join m iden `comp` d)
@@ -40,7 +37,7 @@ selectD2 (D d) m = D (join m iden `comp` d)
40-- Composition with iden
37-- Composition with iden
41compId m = comp iden m
38compId m = comp iden m
42
39
43−randomDist :: forall a . (Countable a, FromListsN () a) => Gen (Dist a)
40+randomDist :: forall a . (Countable a, FLN () a) => Gen (Dist a)
44randomDist = do
41randomDist = do
45 let size = fromInteger (natVal (Proxy :: Proxy (Count a)))
42 let size = fromInteger (natVal (Proxy :: Proxy (Count a)))
46 l <- vectorOf size (arbitrary :: Gen Prob)
43 l <- vectorOf size (arbitrary :: Gen Prob)
@@ -49,7 +46,7 @@ randomDist = do
49 m = fromLists lr
46 m = fromLists lr
50 return (D m)
47 return (D m)
51
48
52−randomMatrix :: forall a b . (CountableDimensions a b, FromListsN a b) => Gen (Matrix Prob a b)
49+randomMatrix :: forall a b . (CountableDims a b, FLN a b) => Gen (Matrix Prob a b)
53randomMatrix = do
50randomMatrix = do
54 let cols = fromInteger (natVal (Proxy :: Proxy (Count a)))
51 let cols = fromInteger (natVal (Proxy :: Proxy (Count a)))
55 rows = fromInteger (natVal (Proxy :: Proxy (Count b)))
52 rows = fromInteger (natVal (Proxy :: Proxy (Count b)))
@@ -108,7 +105,7 @@ normalize3 as l = let s = sum l
108 probs = map (/ s) l
105 probs = map (/ s) l
109 in zip as probs
106 in zip as probs
110
107
111−randomDistF :: forall a b . (CountableDimensions a b, CoArbitrary a, Arbitrary b) => Gen (Dist' (a -> b))
108+randomDistF :: forall a b . (CountableDims a b, CoArbitrary a, Arbitrary b) => Gen (Dist' (a -> b))
112randomDistF = do
109randomDistF = do
113 let a = fromInteger (natVal (Proxy :: Proxy (Count a)))
110 let a = fromInteger (natVal (Proxy :: Proxy (Count a)))
114 b = fromInteger (natVal (Proxy :: Proxy (Count b)))
111 b = fromInteger (natVal (Proxy :: Proxy (Count b)))
@@ -147,8 +144,8 @@ setupEnv2 = do
147 distList2 <- generate (resize 1 (randomDistF @(Natural 0 100) @(Natural 0 100)))
144 distList2 <- generate (resize 1 (randomDistF @(Natural 0 100) @(Natural 0 100)))
148 return (m21, m40, dist, dist2, distList1, distList2)
145 return (m21, m40, dist, dist2, distList1, distList2)
149
146
150−benchmark :: IO ()
147+main :: IO ()
151−benchmark = defaultMain [
148+main = defaultMain [
152 env setupEnv $ \ ~(m11, m12, m21, m22, m31, m32) -> bgroup "Matrix Composition" [
149 env setupEnv $ \ ~(m11, m12, m21, m22, m31, m32) -> bgroup "Matrix Composition" [
153 bgroup "10x10" [
150 bgroup "10x10" [
154 bench "WHNF - 10x10" $ whnf (comp m11) m12
151 bench "WHNF - 10x10" $ whnf (comp m11) m12
hackage.shdeleted−10
@@ -1,10 +0,0 @@
1−#!/bin/sh
2−set -e
3−
4−dir=$(mktemp -d dist-docs.XXXXXX)
5−trap 'rm -r "$dir"' EXIT
6−
7−# assumes cabal 2.4 or later
8−cabal v2-haddock --builddir="$dir" --haddock-for-hackage --enable-doc
9−
10−cabal upload -d $dir/*-docs.tar.gz --publish
laop.cabalnew file+89
@@ -0,0 +1,89 @@
1+cabal-version: 2.4
2+name: laop
3+version: 0.1.1.1
4+synopsis: Matrix programming library
5+description: Matrix programming library that uses a type-safe inductive definition
6+ inspired by the Linear Algebra of Programming
7+homepage: https://github.com/bolt12/laop
8+bug-reports: https://github.com/bolt12/laop/issues
9+license: MIT
10+license-file: LICENSE
11+author: Armando Santos
12+maintainer: Armando Santos <armandoifsantos@gmail.com>
13+copyright: 2020 Armando Santos
14+category: Data
15+build-type: Simple
16+extra-doc-files: README.md
17+ CHANGELOG.md
18+tested-with: GHC == 8.6.5
19+ GHC == 8.8.3
20+
21+source-repository head
22+ type: git
23+ location: https://github.com/bolt12/laop.git
24+
25+common common-options
26+ build-depends: base >= 4.12.0.0 && < 5,
27+ deepseq
28+
29+ ghc-options: -Wall
30+ -Wcompat
31+ -Widentities
32+ -Wincomplete-uni-patterns
33+ -Wincomplete-record-updates
34+ if impl(ghc >= 8.0)
35+ ghc-options: -Wredundant-constraints
36+ if impl(ghc >= 8.2)
37+ ghc-options: -fhide-source-paths
38+ if impl(ghc >= 8.4)
39+ ghc-options: -Wmissing-export-lists
40+ -Wpartial-fields
41+ if impl(ghc >= 8.8)
42+ ghc-options: -Wmissing-deriving-strategies
43+
44+ default-language: Haskell2010
45+
46+library
47+ import: common-options
48+ hs-source-dirs: src
49+ exposed-modules: LAoP.Dist
50+ LAoP.Dist.Internal
51+ LAoP.Matrix.Internal
52+ LAoP.Matrix.Nat
53+ LAoP.Matrix.Type
54+ LAoP.Relation
55+ LAoP.Relation.Internal
56+ LAoP.Utils
57+ LAoP.Utils.Internal
58+
59+test-suite laop-test
60+ import: common-options
61+ type: exitcode-stdio-1.0
62+ hs-source-dirs: test
63+ other-modules: Examples.Readme
64+ main-is: Spec.hs
65+ build-depends: laop
66+ ghc-options: -threaded
67+ -rtsopts
68+ -with-rtsopts=-N
69+ -funfolding-use-threshold=16
70+ -fexcess-precision
71+ -optc-O3
72+ -optc-ffast-math
73+
74+benchmark laop-benchmark
75+ import: common-options
76+ type: exitcode-stdio-1.0
77+ hs-source-dirs: benchmark
78+ main-is: Main.hs
79+ build-depends: criterion
80+ , QuickCheck
81+ , deepseq
82+ , laop
83+ ghc-options: -threaded
84+ -rtsopts
85+ -with-rtsopts=-N
86+ -funfolding-use-threshold=16
87+ -fexcess-precision
88+ -optc-O3
89+ -optc-ffast-math
package.yamldeleted−57
@@ -1,57 +0,0 @@
1−name: laop
2−version: 0.1.0.7
3−github: "bolt12/laop"
4−license: BSD3
5−author: "Armando Santos"
6−maintainer: "armandoifsantos@gmail.com"
7−copyright: "MIT"
8−
9−extra-source-files:
10−- README.md
11−- ChangeLog.md
12−
13−# Metadata used when publishing your package
14−# synopsis: Short description of your package
15−category: Data
16−
17−# To avoid duplicated efforts in documentation and dealing with the
18−# complications of embedding Haddock markup inside cabal files, it is
19−# common to point users to the README.md file.
20−description: Please see the README on GitHub at <https://github.com/bolt12/laop#readme>
21−
22−dependencies:
23−- base >= 4.7 && < 5
24−- deepseq
25−
26−library:
27− source-dirs: src
28−
29−executables:
30− laop-exe:
31− main: Main.hs
32− source-dirs: app
33− ghc-options:
34− - -threaded
35− - -rtsopts
36− - -with-rtsopts=-N
37− - -funfolding-use-threshold=16
38− - -fexcess-precision # Good for doubles
39− - -optc-O3 # Good for doubles
40− - -optc-ffast-math # Good for doubles
41− - -O2
42− dependencies:
43− - laop
44− - criterion
45− - deepseq
46− - QuickCheck
47−
48−tests:
49− laop-test:
50− main: Spec.hs
51− source-dirs: test
52− ghc-options:
53− - -threaded
54− - -rtsopts
55− - -with-rtsopts=-N
56− dependencies:
57− - laop
src/LAoP/Dist.hs+2−2
@@ -39,8 +39,8 @@ module LAoP.Dist
39 -- * Constraint type synonyms
39 -- * Constraint type synonyms
40 Countable,
40 Countable,
41 CountableN,
41 CountableN,
42− CountableDimensionsN,
42+ CountableDimsN,
43− FromListsN,
43+ FLN,
44 Liftable,
44 Liftable,
45 TrivialP,
45 TrivialP,
46
46
src/LAoP/Dist/Internal.hs+43−43
@@ -16,8 +16,8 @@ module LAoP.Dist.Internal
16
16
17 Countable,
17 Countable,
18 CountableN,
18 CountableN,
19− CountableDimensionsN,
19+ CountableDimsN,
20− FromListsN,
20+ FLN,
21 Liftable,
21 Liftable,
22 TrivialP,
22 TrivialP,
23
23
@@ -43,7 +43,7 @@ module LAoP.Dist.Internal
43 )
43 )
44 where
44 where
45
45
46−import LAoP.Matrix.Type hiding (TrivialP, Countable, CountableDimensions, CountableN, CountableDimensionsN, Liftable, FromListsN)
46+import LAoP.Matrix.Type hiding (TrivialP, Countable, CountableDims, CountableN, CountableDimsN, Liftable, FLN)
47import Prelude hiding (id, (.))
47import Prelude hiding (id, (.))
48import qualified LAoP.Matrix.Internal as I
48import qualified LAoP.Matrix.Internal as I
49import LAoP.Utils
49import LAoP.Utils
@@ -62,18 +62,18 @@ newtype Dist a = D (Matrix Prob () a)
62 deriving (Show, Num, Eq, Ord, NFData) via (Matrix Prob () a)
62 deriving (Show, Num, Eq, Ord, NFData) via (Matrix Prob () a)
63
63
64-- | Constraint type synonyms to keep the type signatures less convoluted
64-- | Constraint type synonyms to keep the type signatures less convoluted
65−type Countable a = KnownNat (I.Count a)
65+type Countable a = KnownNat (I.Count a)
66−type CountableN a = KnownNat (I.Count (I.Normalize a))
66+type CountableN a = KnownNat (I.Count (I.Normalize a))
67−type CountableDimensionsN a b = (CountableN a, CountableN b)
67+type CountableDimsN a b = (CountableN a, CountableN b)
68−type FromListsN a b = I.FromLists Prob (I.Normalize a) (I.Normalize b)
68+type FLN a b = I.FL (I.Normalize a) (I.Normalize b)
69−type Liftable a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num Prob, Ord Prob)
69+type Liftable a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num Prob, Ord Prob)
70−type TrivialP a b = Normalize (a, b) ~ Normalize (Normalize a, Normalize b)
70+type TrivialP a b = Normalize (a, b) ~ Normalize (Normalize a, Normalize b)
71
71
72-- | Functor instance
72-- | Functor instance
73fmapD ::
73fmapD ::
74 ( Liftable a b,
74 ( Liftable a b,
75− CountableDimensionsN a b,
75+ CountableDimsN a b,
76− FromListsN b a
76+ FLN b a
77 )
77 )
78 =>
78 =>
79 (a -> b) -> Dist a -> Dist b
79 (a -> b) -> Dist a -> Dist b
@@ -85,17 +85,17 @@ unitD = D (one 1)
85
85
86-- | Applicative/Monoidal instance 'mult' function
86-- | Applicative/Monoidal instance 'mult' function
87multD ::
87multD ::
88− ( CountableDimensionsN a b,
88+ ( CountableDimsN a b,
89 CountableN (a, b),
89 CountableN (a, b),
90− FromListsN (a, b) a,
90+ FLN (a, b) a,
91− FromListsN (a, b) b,
91+ FLN (a, b) b,
92 TrivialP a b
92 TrivialP a b
93 ) => Dist a -> Dist b -> Dist (a, b)
93 ) => Dist a -> Dist b -> Dist (a, b)
94multD (D a) (D b) = D (kr a b)
94multD (D a) (D b) = D (kr a b)
95
95
96-- | Selective instance function
96-- | Selective instance function
97selectD ::
97selectD ::
98− ( FromListsN b b,
98+ ( FLN b b,
99 CountableN b
99 CountableN b
100 ) => Dist (Either a b) -> Matrix Prob a b -> Dist b
100 ) => Dist (Either a b) -> Matrix Prob a b -> Dist b
101selectD (D d) m = D (selectM d m)
101selectD (D d) m = D (selectM d m)
@@ -104,17 +104,17 @@ selectD (D d) m = D (selectM d m)
104-- functions to apply to a given argument;
104-- functions to apply to a given argument;
105branchD ::
105branchD ::
106 ( Num e,
106 ( Num e,
107− CountableDimensionsN a b,
107+ CountableDimsN a b,
108− CountableDimensionsN c (Either b c),
108+ CountableDimsN c (Either b c),
109− FromListsN c b,
109+ FLN c b,
110− FromListsN a b,
110+ FLN a b,
111− FromListsN a a,
111+ FLN a a,
112− FromListsN b b,
112+ FLN b b,
113− FromListsN c c,
113+ FLN c c,
114− FromListsN b a,
114+ FLN b a,
115− FromListsN b c,
115+ FLN b c,
116− FromListsN (Either b c) b,
116+ FLN (Either b c) b,
117− FromListsN (Either b c) c
117+ FLN (Either b c) c
118 )
118 )
119 => Dist (Either a b) -> Matrix Prob a c -> Matrix Prob b c -> Dist c
119 => Dist (Either a b) -> Matrix Prob a c -> Matrix Prob b c -> Dist c
120branchD x l r = f x `selectD` g l `selectD` r
120branchD x l r = f x `selectD` g l `selectD` r
@@ -124,11 +124,11 @@ branchD x l r = f x `selectD` g l `selectD` r
124
124
125-- | Branch on a Boolean value, skipping unnecessary computations.
125-- | Branch on a Boolean value, skipping unnecessary computations.
126ifD ::
126ifD ::
127− ( CountableDimensionsN a (Either () a),
127+ ( CountableDimsN a (Either () a),
128− FromListsN a a,
128+ FLN a a,
129− FromListsN a (),
129+ FLN a (),
130− FromListsN () a,
130+ FLN () a,
131− FromListsN (Either () a) a
131+ FLN (Either () a) a
132 )
132 )
133 => Dist Bool -> Dist a -> Dist a -> Dist a
133 => Dist Bool -> Dist a -> Dist a -> Dist a
134ifD x (D t) (D e) = branchD x' t e
134ifD x (D t) (D e) = branchD x' t e
@@ -136,7 +136,7 @@ ifD x (D t) (D e) = branchD x' t e
136 x' = bool (Right ()) (Left ()) `fmapD` x
136 x' = bool (Right ()) (Left ()) `fmapD` x
137
137
138-- | Monad instance 'return' function
138-- | Monad instance 'return' function
139−returnD :: forall a . (Enum a, FromListsN () a, Countable a) => a -> Dist a
139+returnD :: forall a . (Enum a, FLN () a, Countable a) => a -> Dist a
140returnD a = D (col l)
140returnD a = D (col l)
141 where
141 where
142 i = fromInteger $ natVal (Proxy :: Proxy (Count a))
142 i = fromInteger $ natVal (Proxy :: Proxy (Count a))
@@ -151,7 +151,7 @@ bindD (D d) m = D (m `comp` d)
151(??) ::
151(??) ::
152 ( Enum a,
152 ( Enum a,
153 Countable a,
153 Countable a,
154− FromListsN () a
154+ FLN () a
155 ) => (a -> Bool) -> Dist a -> Prob
155 ) => (a -> Bool) -> Dist a -> Prob
156(??) p d =
156(??) p d =
157 let l = toValues d
157 let l = toValues d
@@ -161,11 +161,11 @@ bindD (D d) m = D (m `comp` d)
161-- Distribution Construction
161-- Distribution Construction
162
162
163-- | Constructs a Bernoulli distribution
163-- | Constructs a Bernoulli distribution
164−choose :: (FromListsN () a) => Prob -> Dist a
164+choose :: (FLN () a) => Prob -> Dist a
165choose prob = D (col [prob, 1 - prob])
165choose prob = D (col [prob, 1 - prob])
166
166
167-- | Creates a distribution given a shape function
167-- | Creates a distribution given a shape function
168−shape :: (FromListsN () a) => (Prob -> Prob) -> [a] -> Dist a
168+shape :: (FLN () a) => (Prob -> Prob) -> [a] -> Dist a
169shape _ [] = error "Probability.shape: empty list"
169shape _ [] = error "Probability.shape: empty list"
170shape f xs =
170shape f xs =
171 let incr = 1 / fromIntegral (length xs - 1)
171 let incr = 1 / fromIntegral (length xs - 1)
@@ -173,23 +173,23 @@ shape f xs =
173 in fromFreqs (zip xs ps)
173 in fromFreqs (zip xs ps)
174
174
175-- | Constructs a Linear distribution
175-- | Constructs a Linear distribution
176−linear :: (FromListsN () a) => [a] -> Dist a
176+linear :: (FLN () a) => [a] -> Dist a
177linear = shape id
177linear = shape id
178
178
179-- | Constructs an Uniform distribution
179-- | Constructs an Uniform distribution
180−uniform :: (FromListsN () a) => [a] -> Dist a
180+uniform :: (FLN () a) => [a] -> Dist a
181uniform = shape (const 1)
181uniform = shape (const 1)
182
182
183-- | Constructs an Negative Exponential distribution
183-- | Constructs an Negative Exponential distribution
184−negExp :: (FromListsN () a) => [a] -> Dist a
184+negExp :: (FLN () a) => [a] -> Dist a
185negExp = shape (\x -> exp (-x))
185negExp = shape (\x -> exp (-x))
186
186
187-- | Constructs an Normal distribution
187-- | Constructs an Normal distribution
188−normal :: (FromListsN () a) => [a] -> Dist a
188+normal :: (FLN () a) => [a] -> Dist a
189normal = shape (normalCurve 0.5 0.5)
189normal = shape (normalCurve 0.5 0.5)
190
190
191-- | Transforms a 'Dist' into a list of pairs.
191-- | Transforms a 'Dist' into a list of pairs.
192−toValues :: forall a . (Enum a, Countable a, FromListsN () a) => Dist a -> [(a, Prob)]
192+toValues :: forall a . (Enum a, Countable a, FLN () a) => Dist a -> [(a, Prob)]
193toValues (D d) =
193toValues (D d) =
194 let rows = fromInteger (natVal (Proxy :: Proxy (Count a)))
194 let rows = fromInteger (natVal (Proxy :: Proxy (Count a)))
195 probs = toList d
195 probs = toList d
@@ -197,7 +197,7 @@ toValues (D d) =
197 in res
197 in res
198
198
199-- | Pretty a distribution
199-- | Pretty a distribution
200−prettyDist :: forall a. (Show a, Enum a, Countable a, FromListsN () a) => Dist a -> String
200+prettyDist :: forall a. (Show a, Enum a, Countable a, FLN () a) => Dist a -> String
201prettyDist d =
201prettyDist d =
202 let values = sortBy (\(a, p1) (b, p2) -> compare p2 p1) (toValues @a d)
202 let values = sortBy (\(a, p1) (b, p2) -> compare p2 p1) (toValues @a d)
203 w = maximum (map (length . show . fst) values)
203 w = maximum (map (length . show . fst) values)
@@ -209,12 +209,12 @@ prettyDist d =
209 showR n x = show x ++ " "
209 showR n x = show x ++ " "
210
210
211-- | Pretty Print a distribution
211-- | Pretty Print a distribution
212−prettyPrintDist :: forall a . (Show a, Enum a, Countable a, FromListsN () a) => Dist a -> IO ()
212+prettyPrintDist :: forall a . (Show a, Enum a, Countable a, FLN () a) => Dist a -> IO ()
213prettyPrintDist = putStrLn . prettyDist @a
213prettyPrintDist = putStrLn . prettyDist @a
214
214
215-- Auxiliary functions
215-- Auxiliary functions
216
216
217−fromFreqs :: (FromListsN () a) => [(a,Prob)] -> Dist a
217+fromFreqs :: (FLN () a) => [(a,Prob)] -> Dist a
218fromFreqs xs = D (col (map (\(x,p) -> p/q) xs))
218fromFreqs xs = D (col (map (\(x,p) -> p/q) xs))
219 where q = sum $ map snd xs
219 where q = sum $ map snd xs
220
220
src/LAoP/Matrix/Internal.hs+130−191
@@ -30,7 +30,7 @@
30-- motivation behind the library, the underlying theory, and implementation details.
30-- motivation behind the library, the underlying theory, and implementation details.
31--
31--
32-- This module offers many of the combinators mentioned in the work of
32-- This module offers many of the combinators mentioned in the work of
33−-- Macedo (2012) and Oliveira (2012).
33+-- Macedo (2012) and Oliveira (2012).
34--
34--
35-- This is an Internal module and it is no supposed to be imported.
35-- This is an Internal module and it is no supposed to be imported.
36--
36--
@@ -43,23 +43,23 @@ module LAoP.Matrix.Internal
43 --
43 --
44 -- There exists two type families that make it easier to write
44 -- There exists two type families that make it easier to write
45 -- matrix dimensions: 'FromNat' and 'Count'. This approach
45 -- matrix dimensions: 'FromNat' and 'Count'. This approach
46− -- leads to a very straightforward implementation
46+ -- leads to a very straightforward implementation
47− -- of LAoP combinators.
47+ -- of LAoP combinators.
48
48
49 -- * Type safe matrix representation
49 -- * Type safe matrix representation
50 Matrix (..),
50 Matrix (..),
51
51
52 -- * Constraint type aliases
52 -- * Constraint type aliases
53 Countable,
53 Countable,
54− CountableDimensions,
54+ CountableDims,
55 CountableN,
55 CountableN,
56− CountableDimensionsN,
56+ CountableDimsN,
57− FromListsN,
57+ FL,
58+ FLN,
58 Liftable,
59 Liftable,
59 Trivial,
60 Trivial,
60
61
61 -- * Primitives
62 -- * Primitives
62− empty,
63 one,
63 one,
64 join,
64 join,
65 fork,
65 fork,
@@ -86,7 +86,9 @@ module LAoP.Matrix.Internal
86 -- * Misc
86 -- * Misc
87 -- ** Get dimensions
87 -- ** Get dimensions
88 columns,
88 columns,
89+ columns',
89 rows,
90 rows,
91+ rows',
90
92
91 -- ** Matrix Transposition
93 -- ** Matrix Transposition
92 tr,
94 tr,
@@ -174,6 +176,7 @@ import Data.Kind
174import Data.List
176import Data.List
175import Data.Maybe
177import Data.Maybe
176import Data.Proxy
178import Data.Proxy
179+import Data.Typeable
177import Data.Void
180import Data.Void
178import GHC.TypeLits
181import GHC.TypeLits
179import Data.Type.Equality
182import Data.Type.Equality
@@ -184,7 +187,6 @@ import qualified Prelude (id, (.))
184
187
185-- | LAoP (Linear Algebra of Programming) Inductive Matrix definition.
188-- | LAoP (Linear Algebra of Programming) Inductive Matrix definition.
186data Matrix e cols rows where
189data Matrix e cols rows where
187− Empty :: Matrix e Void Void
188 One :: e -> Matrix e () ()
190 One :: e -> Matrix e () ()
189 Join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
191 Join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
190 Fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
192 Fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
@@ -214,7 +216,6 @@ type family Count (d :: Type) :: Nat where
214--
216--
215-- Thanks to Li-Yao Xia this type family is super fast.
217-- Thanks to Li-Yao Xia this type family is super fast.
216type family FromNat (n :: Nat) :: Type where
218type family FromNat (n :: Nat) :: Type where
217− FromNat 0 = Void
218 FromNat 1 = ()
219 FromNat 1 = ()
219 FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2))
220 FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2))
220
221
@@ -231,27 +232,26 @@ type family Normalize (d :: Type) :: Type where
231-- | Constraint type synonyms to keep the type signatures less convoluted
232-- | Constraint type synonyms to keep the type signatures less convoluted
232type Countable a = KnownNat (Count a)
233type Countable a = KnownNat (Count a)
233type CountableN a = KnownNat (Count (Normalize a))
234type CountableN a = KnownNat (Count (Normalize a))
234−type CountableDimensions a b = (Countable a, Countable b)
235+type CountableDims a b = (Countable a, Countable b)
235−type CountableDimensionsN a b = (CountableN a, CountableN b)
236+type CountableDimsN a b = (CountableN a, CountableN b)
237+type FL a b = FromLists a b
236−type FromListsN e a b = FromLists e (Normalize a) (Normalize b)
238+type FLN a b = FromLists (Normalize a) (Normalize b)
237type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
239type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
238type Trivial a = FromNat (Count a) ~ a
240type Trivial a = FromNat (Count a) ~ a
239
241
240-- | It is possible to implement a constrained version of the category type
242-- | It is possible to implement a constrained version of the category type
241-- class.
243-- class.
242instance (Num e) => Category (Matrix e) where
244instance (Num e) => Category (Matrix e) where
243− type Object (Matrix e) a = (FromLists e a a, Countable a)
245+ type Object (Matrix e) a = (FL a a, Countable a)
244− id = iden
246+ id = iden
245 (.) = comp
247 (.) = comp
246
248
247instance NFData e => NFData (Matrix e cols rows) where
249instance NFData e => NFData (Matrix e cols rows) where
248− rnf Empty = ()
249 rnf (One e) = rnf e
250 rnf (One e) = rnf e
250 rnf (Join a b) = rnf a `seq` rnf b
251 rnf (Join a b) = rnf a `seq` rnf b
251 rnf (Fork a b) = rnf a `seq` rnf b
252 rnf (Fork a b) = rnf a `seq` rnf b
252
253
253instance Eq e => Eq (Matrix e cols rows) where
254instance Eq e => Eq (Matrix e cols rows) where
254− Empty == Empty = True
255 (One a) == (One b) = a == b
255 (One a) == (One b) = a == b
256 (Join a b) == (Join c d) = a == c && b == d
256 (Join a b) == (Join c d) = a == c && b == d
257 (Fork a b) == (Fork c d) = a == c && b == d
257 (Fork a b) == (Fork c d) = a == c && b == d
@@ -259,25 +259,21 @@ instance Eq e => Eq (Matrix e cols rows) where
259 x@(Join _ _) == y@(Fork _ _) = abideJF x == y
259 x@(Join _ _) == y@(Fork _ _) = abideJF x == y
260
260
261instance Num e => Num (Matrix e cols rows) where
261instance Num e => Num (Matrix e cols rows) where
262−
263 a + b = zipWithM (+) a b
262 a + b = zipWithM (+) a b
264
263
265 a - b = zipWithM (-) a b
264 a - b = zipWithM (-) a b
266
265
267 a * b = zipWithM (*) a b
266 a * b = zipWithM (*) a b
268
267
269− abs Empty = Empty
270 abs (One a) = One (abs a)
268 abs (One a) = One (abs a)
271 abs (Join a b) = Join (abs a) (abs b)
269 abs (Join a b) = Join (abs a) (abs b)
272 abs (Fork a b) = Fork (abs a) (abs b)
270 abs (Fork a b) = Fork (abs a) (abs b)
273
271
274− signum Empty = Empty
275 signum (One a) = One (signum a)
272 signum (One a) = One (signum a)
276 signum (Join a b) = Join (signum a) (signum b)
273 signum (Join a b) = Join (signum a) (signum b)
277 signum (Fork a b) = Fork (signum a) (signum b)
274 signum (Fork a b) = Fork (signum a) (signum b)
278
275
279instance Ord e => Ord (Matrix e cols rows) where
276instance Ord e => Ord (Matrix e cols rows) where
280− Empty <= Empty = True
281 (One a) <= (One b) = a <= b
277 (One a) <= (One b) = a <= b
282 (Join a b) <= (Join c d) = (a <= c) && (b <= d)
278 (Join a b) <= (Join c d) = (a <= c) && (b <= d)
283 (Fork a b) <= (Fork c d) = (a <= c) && (b <= d)
279 (Fork a b) <= (Fork c d) = (a <= c) && (b <= d)
@@ -286,10 +282,6 @@ instance Ord e => Ord (Matrix e cols rows) where
286
282
287-- Primitives
283-- Primitives
288
284
289−-- | Empty matrix constructor
290−empty :: Matrix e Void Void
291−empty = Empty
292−
293-- | Unit matrix constructor
285-- | Unit matrix constructor
294one :: e -> Matrix e () ()
286one :: e -> Matrix e () ()
295one = One
287one = One
@@ -320,40 +312,39 @@ infixl 2 ===
320--
312--
321-- Given that it is not possible to branch on types at the term level type
313-- Given that it is not possible to branch on types at the term level type
322-- classes are needed very much like an inductive definition but on types.
314-- classes are needed very much like an inductive definition but on types.
323−class FromLists e cols rows where
315+class FromLists cols rows where
324 -- | Build a matrix out of a list of list of elements. Throws a runtime
316 -- | Build a matrix out of a list of list of elements. Throws a runtime
325 -- error if the dimensions do not match.
317 -- error if the dimensions do not match.
326 fromLists :: [[e]] -> Matrix e cols rows
318 fromLists :: [[e]] -> Matrix e cols rows
327
319
328−instance FromLists e Void Void where
320+instance FromLists () () where
329− fromLists [] = Empty
330− fromLists _ = error "Wrong dimensions"
331−
332−instance {-# OVERLAPPING #-} FromLists e () () where
333 fromLists [[e]] = One e
321 fromLists [[e]] = One e
334 fromLists _ = error "Wrong dimensions"
322 fromLists _ = error "Wrong dimensions"
335
323
336−instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () cols) () where
324+instance (FromLists cols ()) => FromLists (Either () cols) () where
337 fromLists [h : t] = Join (One h) (fromLists [t])
325 fromLists [h : t] = Join (One h) (fromLists [t])
338 fromLists _ = error "Wrong dimensions"
326 fromLists _ = error "Wrong dimensions"
339
327
328+instance {-# OVERLAPPABLE #-}
340−instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), Countable a) => FromLists e (Either a b) () where
329+ (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
341 fromLists [l] =
330 fromLists [l] =
342 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
331 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
343 in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
332 in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
344 fromLists _ = error "Wrong dimensions"
333 fromLists _ = error "Wrong dimensions"
345
334
346−instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either () rows) where
335+instance (FromLists () rows) => FromLists () (Either () rows) where
347 fromLists ([h] : t) = Fork (One h) (fromLists t)
336 fromLists ([h] : t) = Fork (One h) (fromLists t)
348 fromLists _ = error "Wrong dimensions"
337 fromLists _ = error "Wrong dimensions"
349
338
339+instance {-# OVERLAPPABLE #-}
350−instance {-# OVERLAPPABLE #-} (FromLists e () a, FromLists e () b, Countable a) => FromLists e () (Either a b) where
340+ (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
351− fromLists l@([_] : _) =
341+ fromLists l@([_] : _) =
352 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
342 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
353 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
343 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
354 fromLists _ = error "Wrong dimensions"
344 fromLists _ = error "Wrong dimensions"
355
345
356−instance {-# OVERLAPPABLE #-} (FromLists e (Either a b) c, FromLists e (Either a b) d, Countable c) => FromLists e (Either a b) (Either c d) where
346+instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
347+ => FromLists (Either a b) (Either c d) where
357 fromLists l@(h : t) =
348 fromLists l@(h : t) =
358 let lh = length h
349 let lh = length h
359 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
350 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
@@ -366,8 +357,8 @@ instance {-# OVERLAPPABLE #-} (FromLists e (Either a b) c, FromLists e (Either a
366-- a construction function that operates with indices.
357-- a construction function that operates with indices.
367matrixBuilder' ::
358matrixBuilder' ::
368 forall e cols rows.
359 forall e cols rows.
369− ( FromLists e cols rows,
360+ ( FL cols rows,
370− CountableDimensions cols rows
361+ CountableDims cols rows
371 ) => ((Int, Int) -> e) -> Matrix e cols rows
362 ) => ((Int, Int) -> e) -> Matrix e cols rows
372matrixBuilder' f =
363matrixBuilder' f =
373 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
364 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
@@ -379,17 +370,16 @@ matrixBuilder' f =
379-- a construction function that operates with arbitrary types.
370-- a construction function that operates with arbitrary types.
380matrixBuilder ::
371matrixBuilder ::
381 forall e a b.
372 forall e a b.
382− ( FromListsN e a b,
373+ ( FLN a b,
383− CountableN b,
384 Enum a,
374 Enum a,
385 Enum b,
375 Enum b,
386 Bounded a,
376 Bounded a,
387 Bounded b,
377 Bounded b,
388 Eq a,
378 Eq a,
389− CountableDimensionsN a b
379+ Countable b
390 ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
380 ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
391matrixBuilder f =
381matrixBuilder f =
392− let r = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
382+ let r = fromInteger $ natVal (Proxy :: Proxy (Count b))
393 positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
383 positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
394 in fromLists . map (map f) . transpose . buildList r $ positions
384 in fromLists . map (map f) . transpose . buildList r $ positions
395 where
385 where
@@ -397,11 +387,11 @@ matrixBuilder f =
397 buildList r l = take r l : buildList r (drop r l)
387 buildList r l = take r l : buildList r (drop r l)
398
388
399-- | Constructs a column vector matrix
389-- | Constructs a column vector matrix
400−col :: (FromLists e () rows) => [e] -> Matrix e () rows
390+col :: (FL () rows) => [e] -> Matrix e () rows
401col = fromLists . map (: [])
391col = fromLists . map (: [])
402
392
403-- | Constructs a row vector matrix
393-- | Constructs a row vector matrix
404−row :: (FromLists e cols ()) => [e] -> Matrix e cols ()
394+row :: (FL cols ()) => [e] -> Matrix e cols ()
405row = fromLists . (: [])
395row = fromLists . (: [])
406
396
407-- | Lifts functions to matrices with arbitrary dimensions.
397-- | Lifts functions to matrices with arbitrary dimensions.
@@ -411,8 +401,8 @@ row = fromLists . (: [])
411fromF' ::
401fromF' ::
412 forall a b cols rows e.
402 forall a b cols rows e.
413 ( Liftable e a b,
403 ( Liftable e a b,
414− CountableDimensions cols rows,
404+ CountableDims cols rows,
415− FromLists e rows cols
405+ FL rows cols
416 ) =>
406 ) =>
417 (a -> b) ->
407 (a -> b) ->
418 Matrix e cols rows
408 Matrix e cols rows
@@ -440,35 +430,17 @@ fromF' f =
440fromF ::
430fromF ::
441 forall a b e.
431 forall a b e.
442 ( Liftable e a b,
432 ( Liftable e a b,
443− CountableDimensionsN a b,
433+ CountableDimsN a b,
444− FromListsN e b a
434+ FLN b a
445 ) =>
435 ) =>
446 (a -> b) ->
436 (a -> b) ->
447 Matrix e (Normalize a) (Normalize b)
437 Matrix e (Normalize a) (Normalize b)
448−fromF f =
438+fromF = fromF'
449− let minA = minBound @a
450− maxA = maxBound @a
451− minB = minBound @b
452− maxB = maxBound @b
453− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
454− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
455− elementsA = take ccols [minA .. maxA]
456− elementsB = take rrows [minB .. maxB]
457− combinations = (,) <$> elementsA <*> elementsB
458− combAp = map snd . sort . map (\(a, b) -> if f a == b
459− then ((fromEnum a, fromEnum b), 1)
460− else ((fromEnum a, fromEnum b), 0)) $ combinations
461− mList = buildList combAp rrows
462− in tr $ fromLists mList
463− where
464− buildList [] _ = []
465− buildList l r = take r l : buildList (drop r l) r
466
439
467-- Conversion
440-- Conversion
468
441
469-- | Converts a matrix to a list of lists of elements.
442-- | Converts a matrix to a list of lists of elements.
470toLists :: Matrix e cols rows -> [[e]]
443toLists :: Matrix e cols rows -> [[e]]
471−toLists Empty = []
472toLists (One e) = [[e]]
444toLists (One e) = [[e]]
473toLists (Fork l r) = toLists l ++ toLists r
445toLists (Fork l r) = toLists l ++ toLists r
474toLists (Join l r) = zipWith (++) (toLists l) (toLists r)
446toLists (Join l r) = zipWith (++) (toLists l) (toLists r)
@@ -480,7 +452,7 @@ toList = concat . toLists
480-- Zeros Matrix
452-- Zeros Matrix
481
453
482-- | The zero matrix. A matrix wholly filled with zeros.
454-- | The zero matrix. A matrix wholly filled with zeros.
483−zeros :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
455+zeros :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
484zeros = matrixBuilder' (const 0)
456zeros = matrixBuilder' (const 0)
485
457
486-- Ones Matrix
458-- Ones Matrix
@@ -488,20 +460,20 @@ zeros = matrixBuilder' (const 0)
488-- | The ones matrix. A matrix wholly filled with ones.
460-- | The ones matrix. A matrix wholly filled with ones.
489--
461--
490-- Also known as T (Top) matrix.
462-- Also known as T (Top) matrix.
491−ones :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
463+ones :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
492ones = matrixBuilder' (const 1)
464ones = matrixBuilder' (const 1)
493
465
494-- Const Matrix
466-- Const Matrix
495
467
496-- | The constant matrix constructor. A matrix wholly filled with a given
468-- | The constant matrix constructor. A matrix wholly filled with a given
497-- value.
469-- value.
498−constant :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => e -> Matrix e cols rows
470+constant :: (Num e, FL cols rows, CountableDims cols rows) => e -> Matrix e cols rows
499constant e = matrixBuilder' (const e)
471constant e = matrixBuilder' (const e)
500
472
501-- Bang Matrix
473-- Bang Matrix
502
474
503-- | The T (Top) row vector matrix.
475-- | The T (Top) row vector matrix.
504−bang :: forall e cols. (Num e, Enum e, FromLists e cols (), Countable cols) => Matrix e cols ()
476+bang :: forall e cols. (Num e, Enum e, FL cols (), Countable cols) => Matrix e cols ()
505bang =
477bang =
506 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
478 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
507 in fromLists [take c [1, 1 ..]]
479 in fromLists [take c [1, 1 ..]]
@@ -509,7 +481,7 @@ bang =
509-- iden Matrix
481-- iden Matrix
510
482
511-- | iden matrix.
483-- | iden matrix.
512−iden :: forall e cols . (Num e, FromLists e cols cols, Countable cols) => Matrix e cols cols
484+iden :: forall e cols . (Num e, FL cols cols, Countable cols) => Matrix e cols cols
513iden = matrixBuilder' (bool 0 1 . uncurry (==))
485iden = matrixBuilder' (bool 0 1 . uncurry (==))
514{-# NOINLINE iden #-}
486{-# NOINLINE iden #-}
515
487
@@ -520,13 +492,12 @@ iden = matrixBuilder' (bool 0 1 . uncurry (==))
520-- This definition takes advantage of divide-and-conquer and fusion laws
492-- This definition takes advantage of divide-and-conquer and fusion laws
521-- from LAoP.
493-- from LAoP.
522comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
494comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
523−comp Empty Empty = Empty
524comp (One a) (One b) = One (a * b)
495comp (One a) (One b) = One (a * b)
525comp (Join a b) (Fork c d) = comp a c + comp b d -- Divide-and-conquer law
496comp (Join a b) (Fork c d) = comp a c + comp b d -- Divide-and-conquer law
526comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
497comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
527comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
498comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
528{-# NOINLINE comp #-}
499{-# NOINLINE comp #-}
529−{-# RULES
500+{-# RULES
530 "comp/iden1" forall m. comp m iden = m ;
501 "comp/iden1" forall m. comp m iden = m ;
531 "comp/iden2" forall m. comp iden m = m
502 "comp/iden2" forall m. comp iden m = m
532#-}
503#-}
@@ -536,7 +507,6 @@ comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
536infixl 7 .|
507infixl 7 .|
537-- | Scalar multiplication of matrices.
508-- | Scalar multiplication of matrices.
538(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
509(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
539−(.|) _ Empty = Empty
540(.|) e (One a) = One (e * a)
510(.|) e (One a) = One (e * a)
541(.|) e (Join a b) = Join (e .| a) (e .| b)
511(.|) e (Join a b) = Join (e .| a) (e .| b)
542(.|) e (Fork a b) = Fork (e .| a) (e .| b)
512(.|) e (Fork a b) = Fork (e .| a) (e .| b)
@@ -546,7 +516,6 @@ infixl 7 .|
546infixl 7 ./
516infixl 7 ./
547-- | Scalar multiplication of matrices.
517-- | Scalar multiplication of matrices.
548(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
518(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
549−(./) Empty _ = Empty
550(./) (One a) e = One (a / e)
519(./) (One a) e = One (a / e)
551(./) (Join a b) e = Join (a ./ e) (b ./ e)
520(./) (Join a b) e = Join (a ./ e) (b ./ e)
552(./) (Fork a b) e = Fork (a ./ e) (b ./ e)
521(./) (Fork a b) e = Fork (a ./ e) (b ./ e)
@@ -554,45 +523,59 @@ infixl 7 ./
554-- Projections
523-- Projections
555
524
556-- | Biproduct first component projection
525-- | Biproduct first component projection
557−p1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e (Either m n) m
526+p1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e (Either m n) m
558p1 = join id zeros
527p1 = join id zeros
559
528
560-- | Biproduct second component projection
529-- | Biproduct second component projection
561−p2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e (Either m n) n
530+p2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e (Either m n) n
562p2 = join zeros id
531p2 = join zeros id
563
532
564-- Injections
533-- Injections
565
534
566-- | Biproduct first component injection
535-- | Biproduct first component injection
567−i1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e m (Either m n)
536+i1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e m (Either m n)
568i1 = tr p1
537i1 = tr p1
569
538
570-- | Biproduct second component injection
539-- | Biproduct second component injection
571−i2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e n (Either m n)
540+i2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e n (Either m n)
572i2 = tr p2
541i2 = tr p2
573
542
574-- Dimensions
543-- Dimensions
575
544
576-- | Obtain the number of rows.
545-- | Obtain the number of rows.
577--
546--
578−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
547+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
579−-- dimensions in constant time.
548+-- dimensions in constant time. For a version that doesn't require the
580−--
549+-- constraint see 'rows''.
581−-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
582−-- exchange for performance.
583rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
550rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
584rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
551rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
585
552
553+-- | Obtain the number of rows in an inefficient manner, but without any
554+-- constraints.
555+--
556+-- For a more efficient version see 'rows'.
557+rows' :: Matrix e cols rows -> Int
558+rows' (One _) = 1
559+rows' (Join lhs _) = rows' lhs
560+rows' (Fork top bottom) = rows' top + rows' bottom
561+
586-- | Obtain the number of columns.
562-- | Obtain the number of columns.
587−--
588−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
589−-- dimensions in constant time.
590--
563--
591−-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
564+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
592−-- exchange for performance.
565+-- dimensions in constant time. For a version that doesn't require the
566+-- constraint see 'columns''.
593columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
567columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
594columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
568columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
595
569
570+-- | Obtain the number of columns in an inefficient manner, but without any
571+-- constraints.
572+--
573+-- For a more efficient version see 'columns'.
574+columns' :: Matrix e cols rows -> Int
575+columns' (One _) = 1
576+columns' (Join lhs rhs) = columns' lhs + columns' rhs
577+columns' (Fork top _) = columns' top
578+
596-- Coproduct Bifunctor
579-- Coproduct Bifunctor
597
580
598infixl 5 -|-
581infixl 5 -|-
@@ -601,11 +584,11 @@ infixl 5 -|-
601(-|-) ::
584(-|-) ::
602 forall e n k m j.
585 forall e n k m j.
603 ( Num e,
586 ( Num e,
604− CountableDimensions j k,
587+ CountableDims j k,
605− FromLists e k k,
588+ FL k k,
606− FromLists e j k,
589+ FL j k,
607− FromLists e k j,
590+ FL k j,
608− FromLists e j j
591+ FL j j
609 ) =>
592 ) =>
610 Matrix e n k ->
593 Matrix e n k ->
611 Matrix e m j ->
594 Matrix e m j ->
@@ -618,8 +601,8 @@ infixl 5 -|-
618fstM ::
601fstM ::
619 forall e m k .
602 forall e m k .
620 ( Num e,
603 ( Num e,
621− CountableDimensions k m,
604+ CountableDims k m,
622− FromLists e (Normalize (m, k)) m,
605+ FL (Normalize (m, k)) m,
623 CountableN (m, k)
606 CountableN (m, k)
624 ) => Matrix e (Normalize (m, k)) m
607 ) => Matrix e (Normalize (m, k)) m
625fstM = matrixBuilder' f
608fstM = matrixBuilder' f
@@ -633,8 +616,8 @@ fstM = matrixBuilder' f
633sndM ::
616sndM ::
634 forall e m k .
617 forall e m k .
635 ( Num e,
618 ( Num e,
636− CountableDimensions k m,
619+ CountableDims k m,
637− FromLists e (Normalize (m, k)) k,
620+ FL (Normalize (m, k)) k,
638 CountableN (m, k)
621 CountableN (m, k)
639 ) => Matrix e (Normalize (m, k)) k
622 ) => Matrix e (Normalize (m, k)) k
640sndM = matrixBuilder' f
623sndM = matrixBuilder' f
@@ -647,7 +630,7 @@ sndM = matrixBuilder' f
647-- | Khatri Rao Matrix product also known as matrix pairing.
630-- | Khatri Rao Matrix product also known as matrix pairing.
648--
631--
649-- NOTE: That this is not a true categorical product, see for instance:
632-- NOTE: That this is not a true categorical product, see for instance:
650−--
633+--
651-- @
634-- @
652-- | fstM . kr a b == a
635-- | fstM . kr a b == a
653-- kr a b ==> |
636-- kr a b ==> |
@@ -658,10 +641,10 @@ sndM = matrixBuilder' f
658kr ::
641kr ::
659 forall e cols a b.
642 forall e cols a b.
660 ( Num e,
643 ( Num e,
661− CountableDimensions a b,
644+ CountableDims a b,
662 CountableN (a, b),
645 CountableN (a, b),
663− FromLists e (Normalize (a, b)) a,
646+ FL (Normalize (a, b)) a,
664− FromLists e (Normalize (a, b)) b
647+ FL (Normalize (a, b)) b
665 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
648 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
666kr a b =
649kr a b =
667 let fstM' = fstM @e @a @b
650 let fstM' = fstM @e @a @b
@@ -676,14 +659,14 @@ infixl 4 ><
676(><) ::
659(><) ::
677 forall e m p n q.
660 forall e m p n q.
678 ( Num e,
661 ( Num e,
679− CountableDimensions m n,
662+ CountableDims m n,
680− CountableDimensions p q,
663+ CountableDims p q,
681− CountableDimensionsN (m, n) (p, q),
664+ CountableDimsN (m, n) (p, q),
682− FromLists e (Normalize (m, n)) m,
665+ FL (Normalize (m, n)) m,
683− FromLists e (Normalize (m, n)) n,
666+ FL (Normalize (m, n)) n,
684− FromLists e (Normalize (p, q)) p,
667+ FL (Normalize (p, q)) p,
685− FromLists e (Normalize (p, q)) q
668+ FL (Normalize (p, q)) q
686− )
669+ )
687 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
670 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
688(><) a b =
671(><) a b =
689 let fstM' = fstM @e @m @n
672 let fstM' = fstM @e @m @n
@@ -701,7 +684,6 @@ infixl 4 ><
701-- @
684-- @
702abideJF :: Matrix e cols rows -> Matrix e cols rows
685abideJF :: Matrix e cols rows -> Matrix e cols rows
703abideJF (Join (Fork a c) (Fork b d)) = Fork (Join (abideJF a) (abideJF b)) (Join (abideJF c) (abideJF d)) -- Join-Fork abide law
686abideJF (Join (Fork a c) (Fork b d)) = Fork (Join (abideJF a) (abideJF b)) (Join (abideJF c) (abideJF d)) -- Join-Fork abide law
704−abideJF Empty = Empty
705abideJF (One e) = One e
687abideJF (One e) = One e
706abideJF (Join a b) = Join (abideJF a) (abideJF b)
688abideJF (Join a b) = Join (abideJF a) (abideJF b)
707abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
689abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
@@ -715,7 +697,6 @@ abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
715-- @
697-- @
716abideFJ :: Matrix e cols rows -> Matrix e cols rows
698abideFJ :: Matrix e cols rows -> Matrix e cols rows
717abideFJ (Fork (Join a b) (Join c d)) = Join (Fork (abideFJ a) (abideFJ c)) (Fork (abideFJ b) (abideFJ d)) -- Fork-Join abide law
699abideFJ (Fork (Join a b) (Join c d)) = Join (Fork (abideFJ a) (abideFJ c)) (Fork (abideFJ b) (abideFJ d)) -- Fork-Join abide law
718−abideFJ Empty = Empty
719abideFJ (One e) = One e
700abideFJ (One e) = One e
720abideFJ (Join a b) = Join (abideFJ a) (abideFJ b)
701abideFJ (Join a b) = Join (abideFJ a) (abideFJ b)
721abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
702abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
@@ -724,7 +705,6 @@ abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
724
705
725-- | Matrix transposition.
706-- | Matrix transposition.
726tr :: Matrix e cols rows -> Matrix e rows cols
707tr :: Matrix e cols rows -> Matrix e rows cols
727−tr Empty = Empty
728tr (One e) = One e
708tr (One e) = One e
729tr (Join a b) = Fork (tr a) (tr b)
709tr (Join a b) = Fork (tr a) (tr b)
730tr (Fork a b) = Join (tr a) (tr b)
710tr (Fork a b) = Join (tr a) (tr b)
@@ -733,32 +713,32 @@ tr (Fork a b) = Join (tr a) (tr b)
733
713
734-- | Selective functors 'select' operator equivalent inspired by the
714-- | Selective functors 'select' operator equivalent inspired by the
735-- ArrowMonad solution presented in the paper.
715-- ArrowMonad solution presented in the paper.
736−select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
716+select :: (Num e, FL b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
737select (Fork a b) y = y . a + b -- Divide-and-conquer law
717select (Fork a b) y = y . a + b -- Divide-and-conquer law
738select (Join (Fork a c) (Fork b d)) y = join (y . a + c) (y . b + d) -- Pattern matching + DnC law
718select (Join (Fork a c) (Fork b d)) y = join (y . a + c) (y . b + d) -- Pattern matching + DnC law
739select m y = join y id . m
719select m y = join y id . m
740
720
741branch ::
721branch ::
742 ( Num e,
722 ( Num e,
743− CountableDimensions a b,
723+ CountableDims a b,
744− CountableDimensions c (Either b c),
724+ CountableDims c (Either b c),
745− FromLists e c b,
725+ FL c b,
746− FromLists e a b,
726+ FL a b,
747− FromLists e a a,
727+ FL a a,
748− FromLists e b b,
728+ FL b b,
749− FromLists e c c,
729+ FL c c,
750− FromLists e b a,
730+ FL b a,
751− FromLists e b c,
731+ FL b c,
752− FromLists e (Either b c) b,
732+ FL (Either b c) b,
753− FromLists e (Either b c) c
733+ FL (Either b c) c
754 )
734 )
755 => Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c
735 => Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c
756branch x l r = f x `select` g l `select` r
736branch x l r = f x `select` g l `select` r
757 where
737 where
758− f :: (Num e, Countable a, CountableDimensions b c, FromLists e a b, FromLists e c b, FromLists e b b, FromLists e b a, FromLists e a a)
738+ f :: (Num e, Countable a, CountableDims b c, FL a b, FL c b, FL b b, FL b a, FL a a)
759 => Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
739 => Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
760 f m = fork (tr i1) (i1 . tr i2) . m
740 f m = fork (tr i1) (i1 . tr i2) . m
761− g :: (Num e, CountableDimensions b c, FromLists e b c, FromLists e c c) => Matrix e a c -> Matrix e a (Either b c)
741+ g :: (Num e, CountableDims b c, FL b c, FL c c) => Matrix e a c -> Matrix e a (Either b c)
762 g m = i2 . m
742 g m = i2 . m
763
743
764-- McCarthy's Conditional
744-- McCarthy's Conditional
@@ -767,9 +747,9 @@ branch x l r = f x `select` g l `select` r
767cond ::
747cond ::
768 ( Trivial cols,
748 ( Trivial cols,
769 Countable cols,
749 Countable cols,
770− FromLists e () cols,
750+ FL () cols,
771− FromLists e cols (),
751+ FL cols (),
772− FromLists e cols cols,
752+ FL cols cols,
773 Bounded a,
753 Bounded a,
774 Enum a,
754 Enum a,
775 Num e,
755 Num e,
@@ -782,9 +762,9 @@ cond p f g = join f g . grd p
782grd ::
762grd ::
783 ( Trivial q,
763 ( Trivial q,
784 Countable q,
764 Countable q,
785− FromLists e () q,
765+ FL () q,
786− FromLists e q (),
766+ FL q (),
787− FromLists e q q,
767+ FL q q,
788 Bounded a,
768 Bounded a,
789 Enum a,
769 Enum a,
790 Num e,
770 Num e,
@@ -798,9 +778,9 @@ corr ::
798 forall e a q .
778 forall e a q .
799 ( Trivial q,
779 ( Trivial q,
800 Countable q,
780 Countable q,
801− FromLists e () q,
781+ FL () q,
802− FromLists e q (),
782+ FL q (),
803− FromLists e q q,
783+ FL q q,
804 Liftable e a Bool
784 Liftable e a Bool
805 )
785 )
806 => (a -> Bool) -> Matrix e q q
786 => (a -> Bool) -> Matrix e q q
@@ -829,7 +809,7 @@ prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
829 fill str = replicate (widest - length str - 2) ' ' ++ str
809 fill str = replicate (widest - length str - 2) ' ' ++ str
830
810
831-- | Matrix pretty printer
811-- | Matrix pretty printer
832−pretty :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> String
812+pretty :: (CountableDims cols rows, Show e) => Matrix e cols rows -> String
833pretty m = concat
813pretty m = concat
834 [ "┌ ", unwords (replicate (columns m) blank), " ┐\n"
814 [ "┌ ", unwords (replicate (columns m) blank), " ┐\n"
835 , unlines
815 , unlines
@@ -858,12 +838,11 @@ pretty m = concat
858 (safeGet i j m)
838 (safeGet i j m)
859
839
860-- | Matrix pretty printer
840-- | Matrix pretty printer
861−prettyPrint :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> IO ()
841+prettyPrint :: (CountableDims cols rows, Show e) => Matrix e cols rows -> IO ()
862prettyPrint = putStrLn . pretty
842prettyPrint = putStrLn . pretty
863
843
864-- | Zip two matrices with a given binary function
844-- | Zip two matrices with a given binary function
865zipWithM :: (e -> f -> g) -> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
845zipWithM :: (e -> f -> g) -> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
866−zipWithM _ Empty Empty = Empty
867zipWithM f (One a) (One b) = One (f a b)
846zipWithM f (One a) (One b) = One (f a b)
868zipWithM f (Join a b) (Join c d) = Join (zipWithM f a c) (zipWithM f b d)
847zipWithM f (Join a b) (Join c d) = Join (zipWithM f a c) (zipWithM f b d)
869zipWithM f (Fork a b) (Fork c d) = Fork (zipWithM f a c) (zipWithM f b d)
848zipWithM f (Fork a b) (Fork c d) = Fork (zipWithM f a c) (zipWithM f b d)
@@ -888,14 +867,12 @@ fromBool False = nat 0
888
867
889-- | Relational negation
868-- | Relational negation
890negateM :: Relation cols rows -> Relation cols rows
869negateM :: Relation cols rows -> Relation cols rows
891−negateM Empty = Empty
892negateM (One (Nat p)) = One (Nat (negate p))
870negateM (One (Nat p)) = One (Nat (negate p))
893negateM (Join a b) = Join (negateM a) (negateM b)
871negateM (Join a b) = Join (negateM a) (negateM b)
894negateM (Fork a b) = Fork (negateM a) (negateM b)
872negateM (Fork a b) = Fork (negateM a) (negateM b)
895
873
896-- | Relational addition
874-- | Relational addition
897orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
875orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
898−orM Empty Empty = Empty
899orM (One a) (One b) = One (fromBool (toBool a || toBool b))
876orM (One a) (One b) = One (fromBool (toBool a || toBool b))
900orM (Join a b) (Join c d) = Join (orM a c) (orM b d)
877orM (Join a b) (Join c d) = Join (orM a c) (orM b d)
901orM (Fork a b) (Fork c d) = Fork (orM a c) (orM b d)
878orM (Fork a b) (Fork c d) = Fork (orM a c) (orM b d)
@@ -904,7 +881,6 @@ orM x@(Join _ _) y@(Fork _ _) = orM (abideJF x) y
904
881
905-- | Relational multiplication
882-- | Relational multiplication
906andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
883andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
907−andM Empty Empty = Empty
908andM (One a) (One b) = One (fromBool (toBool a && toBool b))
884andM (One a) (One b) = One (fromBool (toBool a && toBool b))
909andM (Join a b) (Join c d) = Join (andM a c) (andM b d)
885andM (Join a b) (Join c d) = Join (andM a c) (andM b d)
910andM (Fork a b) (Fork c d) = Fork (andM a c) (andM b d)
886andM (Fork a b) (Fork c d) = Fork (andM a c) (andM b d)
@@ -913,7 +889,6 @@ andM x@(Join _ _) y@(Fork _ _) = andM (abideJF x) y
913
889
914-- | Relational subtraction
890-- | Relational subtraction
915subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
891subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
916−subM Empty Empty = Empty
917subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
892subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
918subM (Join a b) (Join c d) = Join (subM a c) (subM b d)
893subM (Join a b) (Join c d) = Join (subM a c) (subM b d)
919subM (Fork a b) (Fork c d) = Fork (subM a c) (subM b d)
894subM (Fork a b) (Fork c d) = Fork (subM a c) (subM b d)
@@ -922,7 +897,6 @@ subM x@(Join _ _) y@(Fork _ _) = subM (abideJF x) y
922
897
923-- | Matrix relational composition.
898-- | Matrix relational composition.
924compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
899compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
925−compRel Empty Empty = Empty
926compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
900compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
927compRel (Join a b) (Fork c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
901compRel (Join a b) (Fork c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
928compRel (Fork a b) c = Fork (compRel a c) (compRel b c) -- Fork fusion law
902compRel (Fork a b) c = Fork (compRel a c) (compRel b c) -- Fork fusion law
@@ -930,7 +904,6 @@ compRel c (Join a b) = Join (compRel c a) (compRel c b) -- Join fusion
930
904
931-- | Matrix relational right division
905-- | Matrix relational right division
932divR :: Relation b c -> Relation b a -> Relation a c
906divR :: Relation b c -> Relation b a -> Relation a c
933−divR Empty Empty = Empty
934divR (One a) (One b) = One (fromBool (not (toBool b) || toBool a)) -- b implies a
907divR (One a) (One b) = One (fromBool (not (toBool b) || toBool a)) -- b implies a
935divR (Join a b) (Join c d) = andM (divR a c) (divR b d)
908divR (Join a b) (Join c d) = andM (divR a c) (divR b d)
936divR (Fork a b) c = Fork (divR a c) (divR b c)
909divR (Fork a b) c = Fork (divR a c) (divR b c)
@@ -953,58 +926,24 @@ divS s r = divL r s `intersection` divR (tr r) (tr s)
953fromFRel' ::
926fromFRel' ::
954 forall a b cols rows.
927 forall a b cols rows.
955 ( Liftable Boolean a b,
928 ( Liftable Boolean a b,
956− CountableDimensions cols rows,
929+ CountableDims cols rows,
957− FromLists Boolean rows cols
930+ FL rows cols
958 ) =>
931 ) =>
959 (a -> b) ->
932 (a -> b) ->
960 Relation cols rows
933 Relation cols rows
961−fromFRel' f =
934+fromFRel' = fromF'
962− let minA = minBound @a
963− maxA = maxBound @a
964− minB = minBound @b
965− maxB = maxBound @b
966− ccols = fromInteger $ natVal (Proxy :: Proxy (Count cols))
967− rrows = fromInteger $ natVal (Proxy :: Proxy (Count rows))
968− elementsA = take ccols [minA .. maxA]
969− elementsB = take rrows [minB .. maxB]
970− combinations = (,) <$> elementsA <*> elementsB
971− combAp = map snd . sort . map (\(a, b) -> if f a == b
972− then ((fromEnum a, fromEnum b), nat 1)
973− else ((fromEnum a, fromEnum b), nat 0)) $ combinations
974− mList = buildList combAp rrows
975− in tr $ fromLists mList
976− where
977− buildList [] _ = []
978− buildList l r = take r l : buildList (drop r l) r
979
935
980-- | Lifts functions to relations with dimensions matching @a@ and @b@
936-- | Lifts functions to relations with dimensions matching @a@ and @b@
981-- cardinality's.
937-- cardinality's.
982fromFRel ::
938fromFRel ::
983 forall a b.
939 forall a b.
984 ( Liftable Boolean a b,
940 ( Liftable Boolean a b,
985− CountableDimensionsN a b,
941+ CountableDimsN a b,
986− FromLists Boolean (Normalize b) (Normalize a)
942+ FLN b a
987 ) =>
943 ) =>
988 (a -> b) ->
944 (a -> b) ->
989 Relation (Normalize a) (Normalize b)
945 Relation (Normalize a) (Normalize b)
990−fromFRel f =
946+fromFRel = fromFRel'
991− let minA = minBound @a
992− maxA = maxBound @a
993− minB = minBound @b
994− maxB = maxBound @b
995− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
996− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
997− elementsA = take ccols [minA .. maxA]
998− elementsB = take rrows [minB .. maxB]
999− combinations = (,) <$> elementsA <*> elementsB
1000− combAp = map snd . sort . map (\(a, b) -> if f a == b
1001− then ((fromEnum a, fromEnum b), nat 1)
1002− else ((fromEnum a, fromEnum b), nat 0)) $ combinations
1003− mList = buildList combAp rrows
1004− in tr $ fromLists mList
1005− where
1006− buildList [] _ = []
1007− buildList l r = take r l : buildList (drop r l) r
1008
947
1009-- | Lifts a relation function to a Boolean Matrix
948-- | Lifts a relation function to a Boolean Matrix
1010toRel ::
949toRel ::
@@ -1014,17 +953,17 @@ toRel ::
1014 Enum a,
953 Enum a,
1015 Enum b,
954 Enum b,
1016 Eq b,
955 Eq b,
1017− CountableDimensionsN a b,
956+ CountableDims a b,
1018− FromListsN Boolean b a
957+ FLN b a
1019− )
958+ )
1020 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
959 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
1021toRel f =
960toRel f =
1022 let minA = minBound @a
961 let minA = minBound @a
1023 maxA = maxBound @a
962 maxA = maxBound @a
1024 minB = minBound @b
963 minB = minBound @b
1025 maxB = maxBound @b
964 maxB = maxBound @b
1026− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
965+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
1027− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
966+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
1028 elementsA = take ccols [minA .. maxA]
967 elementsA = take ccols [minA .. maxA]
1029 elementsB = take rrows [minB .. maxB]
968 elementsB = take rrows [minB .. maxB]
1030 combinations = (,) <$> elementsA <*> elementsB
969 combinations = (,) <$> elementsA <*> elementsB
src/LAoP/Matrix/Nat.hs+63−67
@@ -56,18 +56,17 @@ module LAoP.Matrix.Nat
56
56
57 -- * Constraint type synonyms
57 -- * Constraint type synonyms
58 Countable,
58 Countable,
59− CountableDimensions,
59+ CountableDims,
60 CountableN,
60 CountableN,
61 CountableNz,
61 CountableNz,
62− CountableDimensionsN,
62+ CountableDimsN,
63− FromListsN,
63+ FLN,
64− FromListsNz,
64+ FLNz,
65 Liftable,
65 Liftable,
66 TrivialE,
66 TrivialE,
67 TrivialP,
67 TrivialP,
68
68
69 -- * Primitives
69 -- * Primitives
70− empty,
71 one,
70 one,
72 join,
71 join,
73 fork,
72 fork,
@@ -78,7 +77,7 @@ module LAoP.Matrix.Nat
78 I.Normalize,
77 I.Normalize,
79
78
80 -- * Matrix construction and conversion
79 -- * Matrix construction and conversion
81− I.FromLists,
80+ I.FL,
82 fromLists,
81 fromLists,
83 toLists,
82 toLists,
84 toList,
83 toList,
@@ -167,22 +166,19 @@ newtype Matrix e (cols :: Nat) (rows :: Nat) = M (I.Matrix e (I.FromNat cols) (I
167 deriving (Show, Num, Eq, Ord, NFData) via (I.Matrix e (I.FromNat cols) (I.FromNat rows))
166 deriving (Show, Num, Eq, Ord, NFData) via (I.Matrix e (I.FromNat cols) (I.FromNat rows))
168
167
169-- | Constraint type synonyms to keep the type signatures less convoluted
168-- | Constraint type synonyms to keep the type signatures less convoluted
170−type Countable a = KnownNat (I.Count a)
169+type Countable a = KnownNat (I.Count a)
171−type CountableDimensions a b = (Countable a, Countable b)
170+type CountableDims a b = (Countable a, Countable b)
172−type CountableN a = KnownNat (I.Count (I.FromNat a))
171+type CountableN a = KnownNat (I.Count (I.FromNat a))
173−type CountableNz a = KnownNat (I.Count (I.Normalize a))
172+type CountableNz a = KnownNat (I.Count (I.Normalize a))
174−type CountableDimensionsN a b = (CountableN a, CountableN b)
173+type CountableDimsN a b = (CountableN a, CountableN b)
175−type FromListsN e a b = I.FromLists e (I.FromNat a) (I.FromNat b)
174+type FLN e a b = I.FL (I.FromNat a) (I.FromNat b)
176−type FromListsNz e a b = I.FromLists e (I.Normalize a) (I.Normalize b)
175+type FLNz e a b = I.FL (I.Normalize a) (I.Normalize b)
177−type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
176+type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
178−type TrivialE a b = I.FromNat (a + b) ~ Either (I.FromNat a) (I.FromNat b)
177+type TrivialE a b = I.FromNat (a + b) ~ Either (I.FromNat a) (I.FromNat b)
179−type TrivialP a b = I.FromNat (a * b) ~ I.FromNat (I.Count (I.FromNat a) * I.Count (I.FromNat b))
178+type TrivialP a b = I.FromNat (a * b) ~ I.FromNat (I.Count (I.FromNat a) * I.Count (I.FromNat b))
180
179
181-- Primitives
180-- Primitives
182
181
183−empty :: Matrix e 0 0
184−empty = M I.Empty
185−
186one :: e -> Matrix e 1 1
182one :: e -> Matrix e 1 1
187one = M . I.One
183one = M . I.One
188
184
@@ -218,27 +214,27 @@ infixl 2 ===
218
214
219-- Construction
215-- Construction
220
216
221−fromLists :: (FromListsN e cols rows) => [[e]] -> Matrix e cols rows
217+fromLists :: (FLN e cols rows) => [[e]] -> Matrix e cols rows
222fromLists = M . I.fromLists
218fromLists = M . I.fromLists
223
219
224-- | Matrix builder function. Constructs a matrix provided with
220-- | Matrix builder function. Constructs a matrix provided with
225-- a construction function that operates with indices.
221-- a construction function that operates with indices.
226matrixBuilder' ::
222matrixBuilder' ::
227− (FromListsN e cols rows, CountableN cols, CountableN rows)
223+ (FLN e cols rows, CountableN cols, CountableN rows)
228 => ((Int, Int) -> e) -> Matrix e cols rows
224 => ((Int, Int) -> e) -> Matrix e cols rows
229matrixBuilder' = M . I.matrixBuilder'
225matrixBuilder' = M . I.matrixBuilder'
230
226
231−col :: (I.FromLists e () (I.FromNat rows)) => [e] -> Matrix e 1 rows
227+col :: (I.FL () (I.FromNat rows)) => [e] -> Matrix e 1 rows
232col = M . I.col
228col = M . I.col
233
229
234−row :: (I.FromLists e (I.FromNat cols) ()) => [e] -> Matrix e cols 1
230+row :: (I.FL (I.FromNat cols) ()) => [e] -> Matrix e cols 1
235row = M . I.row
231row = M . I.row
236
232
237fromF' ::
233fromF' ::
238 ( Liftable e a b,
234 ( Liftable e a b,
239 CountableN cols,
235 CountableN cols,
240 CountableN rows,
236 CountableN rows,
241− FromListsN e rows cols
237+ FLN e rows cols
242 ) =>
238 ) =>
243 (a -> b) ->
239 (a -> b) ->
244 Matrix e cols rows
240 Matrix e cols rows
@@ -248,7 +244,7 @@ fromF ::
248 ( Liftable e a b,
244 ( Liftable e a b,
249 CountableNz a,
245 CountableNz a,
250 CountableNz b,
246 CountableNz b,
251− FromListsNz e b a
247+ FLNz e b a
252 ) =>
248 ) =>
253 (a -> b) ->
249 (a -> b) ->
254 Matrix e (I.Count a) (I.Count b)
250 Matrix e (I.Count a) (I.Count b)
@@ -265,21 +261,21 @@ toList (M m) = I.toList m
265-- Zeros Matrix
261-- Zeros Matrix
266
262
267zeros ::
263zeros ::
268− (Num e, FromListsN e cols rows, CountableN cols, CountableN rows) =>
264+ (Num e, FLN e cols rows, CountableN cols, CountableN rows) =>
269 Matrix e cols rows
265 Matrix e cols rows
270zeros = M I.zeros
266zeros = M I.zeros
271
267
272-- Ones Matrix
268-- Ones Matrix
273
269
274ones ::
270ones ::
275− (Num e, FromListsN e cols rows, CountableN cols, CountableN rows) =>
271+ (Num e, FLN e cols rows, CountableN cols, CountableN rows) =>
276 Matrix e cols rows
272 Matrix e cols rows
277ones = M I.ones
273ones = M I.ones
278
274
279-- Const Matrix
275-- Const Matrix
280
276
281constant ::
277constant ::
282− (Num e, FromListsN e cols rows, CountableN cols, CountableN rows) =>
278+ (Num e, FLN e cols rows, CountableN cols, CountableN rows) =>
283 e ->
279 e ->
284 Matrix e cols rows
280 Matrix e cols rows
285constant = M . I.constant
281constant = M . I.constant
@@ -288,14 +284,14 @@ constant = M . I.constant
288
284
289bang ::
285bang ::
290 forall e cols.
286 forall e cols.
291− (Num e, Enum e, I.FromLists e (I.FromNat cols) (), CountableN cols) =>
287+ (Num e, Enum e, I.FL (I.FromNat cols) (), CountableN cols) =>
292 Matrix e cols 1
288 Matrix e cols 1
293bang = M I.bang
289bang = M I.bang
294
290
295-- iden Matrix
291-- iden Matrix
296
292
297iden ::
293iden ::
298− (Num e, FromListsN e cols cols, CountableN cols) =>
294+ (Num e, FLN e cols cols, CountableN cols) =>
299 Matrix e cols cols
295 Matrix e cols cols
300iden = M I.iden
296iden = M I.iden
301
297
@@ -320,9 +316,9 @@ infixl 7 ./
320
316
321p1 ::
317p1 ::
322 ( Num e,
318 ( Num e,
323− CountableDimensionsN n m,
319+ CountableDimsN n m,
324− FromListsN e n m,
320+ FLN e n m,
325− FromListsN e m m,
321+ FLN e m m,
326 TrivialE m n
322 TrivialE m n
327 ) =>
323 ) =>
328 Matrix e (m + n) m
324 Matrix e (m + n) m
@@ -330,9 +326,9 @@ p1 = M I.p1
330
326
331p2 ::
327p2 ::
332 ( Num e,
328 ( Num e,
333− CountableDimensionsN n m,
329+ CountableDimsN n m,
334− FromListsN e m n,
330+ FLN e m n,
335− FromListsN e n n,
331+ FLN e n n,
336 TrivialE m n
332 TrivialE m n
337 ) =>
333 ) =>
338 Matrix e (m + n) n
334 Matrix e (m + n) n
@@ -342,9 +338,9 @@ p2 = M I.p2
342
338
343i1 ::
339i1 ::
344 ( Num e,
340 ( Num e,
345− CountableDimensionsN n rows,
341+ CountableDimsN n rows,
346− FromListsN e n rows,
342+ FLN e n rows,
347− FromListsN e rows rows,
343+ FLN e rows rows,
348 TrivialE rows n
344 TrivialE rows n
349 ) =>
345 ) =>
350 Matrix e rows (rows + n)
346 Matrix e rows (rows + n)
@@ -352,9 +348,9 @@ i1 = tr p1
352
348
353i2 ::
349i2 ::
354 ( Num e,
350 ( Num e,
355− CountableDimensionsN rows m,
351+ CountableDimsN rows m,
356− FromListsN e m rows,
352+ FLN e m rows,
357− FromListsN e rows rows,
353+ FLN e rows rows,
358 TrivialE m rows
354 TrivialE m rows
359 ) =>
355 ) =>
360 Matrix e rows (m + rows)
356 Matrix e rows (m + rows)
@@ -373,11 +369,11 @@ infixl 5 -|-
373-- | Coproduct Bifunctor (Direct sum)
369-- | Coproduct Bifunctor (Direct sum)
374(-|-) ::
370(-|-) ::
375 ( Num e,
371 ( Num e,
376− CountableDimensionsN j k,
372+ CountableDimsN j k,
377− FromListsN e k k,
373+ FLN e k k,
378− FromListsN e j k,
374+ FLN e j k,
379− FromListsN e k j,
375+ FLN e k j,
380− FromListsN e j j,
376+ FLN e j j,
381 TrivialE n m,
377 TrivialE n m,
382 TrivialE k j
378 TrivialE k j
383 ) =>
379 ) =>
@@ -390,9 +386,9 @@ infixl 5 -|-
390fstM ::
386fstM ::
391 forall e m k .
387 forall e m k .
392 ( Num e,
388 ( Num e,
393− CountableDimensionsN m k,
389+ CountableDimsN m k,
394 CountableN (m * k),
390 CountableN (m * k),
395− FromListsN e (m * k) m,
391+ FLN e (m * k) m,
396 TrivialP m k
392 TrivialP m k
397 ) => Matrix e (m * k) m
393 ) => Matrix e (m * k) m
398fstM = M (I.fstM @e @(I.FromNat m) @(I.FromNat k))
394fstM = M (I.fstM @e @(I.FromNat m) @(I.FromNat k))
@@ -401,8 +397,8 @@ fstM = M (I.fstM @e @(I.FromNat m) @(I.FromNat k))
401sndM ::
397sndM ::
402 forall e m k.
398 forall e m k.
403 ( Num e,
399 ( Num e,
404− CountableDimensionsN k m,
400+ CountableDimsN k m,
405− FromListsN e (m * k) k,
401+ FLN e (m * k) k,
406 CountableN (m * k),
402 CountableN (m * k),
407 TrivialP m k
403 TrivialP m k
408 ) => Matrix e (m * k) k
404 ) => Matrix e (m * k) k
@@ -412,10 +408,10 @@ sndM = M (I.sndM @e @(I.FromNat m) @(I.FromNat k))
412kr ::
408kr ::
413 forall e cols a b.
409 forall e cols a b.
414 ( Num e,
410 ( Num e,
415− CountableDimensionsN a b,
411+ CountableDimsN a b,
416 CountableN (a * b),
412 CountableN (a * b),
417− FromListsN e (a * b) a,
413+ FLN e (a * b) a,
418− FromListsN e (a * b) b,
414+ FLN e (a * b) b,
419 TrivialP a b
415 TrivialP a b
420 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a * b)
416 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a * b)
421kr a b =
417kr a b =
@@ -429,13 +425,13 @@ infixl 4 ><
429(><) ::
425(><) ::
430 forall e m p n q.
426 forall e m p n q.
431 ( Num e,
427 ( Num e,
432− CountableDimensionsN m n,
428+ CountableDimsN m n,
433− CountableDimensionsN p q,
429+ CountableDimsN p q,
434− CountableDimensionsN (m * n) (p * q),
430+ CountableDimsN (m * n) (p * q),
435− FromListsN e (m * n) m,
431+ FLN e (m * n) m,
436− FromListsN e (m * n) n,
432+ FLN e (m * n) n,
437− FromListsN e (p * q) p,
433+ FLN e (p * q) p,
438− FromListsN e (p * q) q,
434+ FLN e (p * q) q,
439 TrivialP m n,
435 TrivialP m n,
440 TrivialP p q
436 TrivialP p q
441 ) => Matrix e m p -> Matrix e n q -> Matrix e (m * n) (p * q)
437 ) => Matrix e m p -> Matrix e n q -> Matrix e (m * n) (p * q)
@@ -459,7 +455,7 @@ tr (M m) = M (I.tr m)
459-- Selective 'select' operator
455-- Selective 'select' operator
460select ::
456select ::
461 ( Num e,
457 ( Num e,
462− FromListsN e rows1 rows1,
458+ FLN e rows1 rows1,
463 CountableN rows1,
459 CountableN rows1,
464 I.FromNat rows2 ~ I.FromNat rows1,
460 I.FromNat rows2 ~ I.FromNat rows1,
465 I.FromNat cols1 ~ I.FromNat cols2,
461 I.FromNat cols1 ~ I.FromNat cols2,
@@ -472,9 +468,9 @@ select (M m) (M y) = M (I.select m y)
472cond ::
468cond ::
473 ( I.FromNat (I.Count (I.FromNat cols)) ~ I.FromNat cols,
469 ( I.FromNat (I.Count (I.FromNat cols)) ~ I.FromNat cols,
474 CountableN cols,
470 CountableN cols,
475− I.FromLists e () (I.FromNat cols),
471+ I.FL () (I.FromNat cols),
476− I.FromLists e (I.FromNat cols) (),
472+ I.FL (I.FromNat cols) (),
477− FromListsN e cols cols,
473+ FLN e cols cols,
478 Liftable e a Bool
474 Liftable e a Bool
479 )
475 )
480 =>
476 =>
@@ -483,10 +479,10 @@ cond p (M a) (M b) = M (I.cond p a b)
483
479
484-- Pretty print
480-- Pretty print
485
481
486−pretty :: (CountableDimensionsN cols rows, Show e) => Matrix e cols rows -> String
482+pretty :: (CountableDimsN cols rows, Show e) => Matrix e cols rows -> String
487pretty (M m) = I.pretty m
483pretty (M m) = I.pretty m
488
484
489−prettyPrint :: (CountableDimensionsN cols rows, Show e) => Matrix e cols rows -> IO ()
485+prettyPrint :: (CountableDimsN cols rows, Show e) => Matrix e cols rows -> IO ()
490prettyPrint (M m) = I.prettyPrint m
486prettyPrint (M m) = I.prettyPrint m
491
487
492-- | Zip two matrices with a given binary function
488-- | Zip two matrices with a given binary function
src/LAoP/Matrix/Type.hs+117−111
@@ -6,7 +6,6 @@
6{-# LANGUAGE FlexibleContexts #-}
6{-# LANGUAGE FlexibleContexts #-}
7{-# LANGUAGE FlexibleInstances #-}
7{-# LANGUAGE FlexibleInstances #-}
8{-# LANGUAGE GADTs #-}
8{-# LANGUAGE GADTs #-}
9−{-# LANGUAGE KindSignatures #-}
10{-# LANGUAGE MultiParamTypeClasses #-}
9{-# LANGUAGE MultiParamTypeClasses #-}
11{-# LANGUAGE NoStarIsType #-}
10{-# LANGUAGE NoStarIsType #-}
12{-# LANGUAGE RankNTypes #-}
11{-# LANGUAGE RankNTypes #-}
@@ -59,10 +58,10 @@ module LAoP.Matrix.Type
59
58
60 -- * Constraint type aliases
59 -- * Constraint type aliases
61 Countable,
60 Countable,
62− CountableDimensions,
61+ CountableDims,
63 CountableN,
62 CountableN,
64− CountableDimensionsN,
63+ CountableDimsN,
65− FromListsN,
64+ FLN,
66 Liftable,
65 Liftable,
67 Trivial,
66 Trivial,
68 TrivialP,
67 TrivialP,
@@ -72,7 +71,6 @@ module LAoP.Matrix.Type
72 One,
71 One,
73
72
74 -- * Primitives
73 -- * Primitives
75− empty,
76 one,
74 one,
77 join,
75 join,
78 fork,
76 fork,
@@ -106,8 +104,8 @@ module LAoP.Matrix.Type
106 multM,
104 multM,
107
105
108 -- * Selective equivalent instance function
106 -- * Selective equivalent instance function
109− selectM,
107+ selectM,
110−
108+
111 -- * Monad equivalent instance function
109 -- * Monad equivalent instance function
112 returnM,
110 returnM,
113 bindM,
111 bindM,
@@ -115,7 +113,9 @@ module LAoP.Matrix.Type
115 -- * Misc
113 -- * Misc
116 -- ** Get dimensions
114 -- ** Get dimensions
117 columns,
115 columns,
116+ columns',
118 rows,
117 rows,
118+ rows',
119
119
120 -- ** Matrix Transposition
120 -- ** Matrix Transposition
121 tr,
121 tr,
@@ -183,7 +183,7 @@ where
183import Data.Void
183import Data.Void
184import Data.Proxy
184import Data.Proxy
185import Data.Kind
185import Data.Kind
186−import GHC.TypeLits
186+import GHC.TypeLits
187import Control.DeepSeq
187import Control.DeepSeq
188import LAoP.Utils
188import LAoP.Utils
189import qualified LAoP.Matrix.Internal as I
189import qualified LAoP.Matrix.Internal as I
@@ -193,21 +193,21 @@ newtype Matrix e (cols :: Type) (rows :: Type) = M (I.Matrix e (I.Normalize cols
193 deriving (Show, Num, Eq, Ord, NFData) via (I.Matrix e (I.Normalize cols) (I.Normalize rows))
193 deriving (Show, Num, Eq, Ord, NFData) via (I.Matrix e (I.Normalize cols) (I.Normalize rows))
194
194
195-- | Constraint type synonyms to keep the type signatures less convoluted
195-- | Constraint type synonyms to keep the type signatures less convoluted
196−type Countable a = KnownNat (I.Count a)
196+type Countable a = KnownNat (I.Count a)
197−type CountableDimensions a b = (Countable a, Countable b)
197+type CountableDims a b = (Countable a, Countable b)
198−type CountableN a = KnownNat (I.Count (I.Normalize a))
198+type CountableN a = KnownNat (I.Count (I.Normalize a))
199−type CountableDimensionsN a b = (CountableN a, CountableN b)
199+type CountableDimsN a b = (CountableN a, CountableN b)
200−type FromListsN e a b = I.FromLists e (I.Normalize a) (I.Normalize b)
200+type FLN a b = I.FromLists (I.Normalize a) (I.Normalize b)
201−type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
201+type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
202−type Trivial a = I.Normalize (I.Normalize a) ~ I.Normalize (I.Normalize (I.Normalize a))
202+type Trivial a = I.Normalize (I.Normalize a) ~ I.Normalize (I.Normalize (I.Normalize a))
203−type Trivial2 a = I.Normalize a ~ I.Normalize (I.Normalize a)
203+type Trivial2 a = I.Normalize a ~ I.Normalize (I.Normalize a)
204−type Trivial3 a = I.FromNat (I.Count (I.Normalize (I.Normalize a))) ~ I.Normalize (I.Normalize a)
204+type Trivial3 a = I.FromNat (I.Count (I.Normalize (I.Normalize a))) ~ I.Normalize (I.Normalize a)
205−type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
205+type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
206
206
207-- | It is possible to implement a constrained version of the category type
207-- | It is possible to implement a constrained version of the category type
208-- class.
208-- class.
209instance (Num e) => Category (Matrix e) where
209instance (Num e) => Category (Matrix e) where
210− type Object (Matrix e) a = (FromListsN e a a, CountableN a)
210+ type Object (Matrix e) a = (FLN a a, CountableN a)
211 id = iden
211 id = iden
212 (.) = comp
212 (.) = comp
213
213
@@ -215,10 +215,10 @@ instance (Num e) => Category (Matrix e) where
215bimapM ::
215bimapM ::
216 ( Liftable e a b,
216 ( Liftable e a b,
217 Liftable e c d,
217 Liftable e c d,
218− CountableDimensionsN a c,
218+ CountableDimsN a c,
219− CountableDimensionsN b d,
219+ CountableDimsN b d,
220− FromListsN e d c,
220+ FLN d c,
221− FromListsN e b a
221+ FLN b a
222 ) => (a -> b) -> (c -> d) -> Matrix e a c -> Matrix e b d
222 ) => (a -> b) -> (c -> d) -> Matrix e a c -> Matrix e b d
223bimapM f g m = fromF g . m . tr (fromF f)
223bimapM f g m = fromF g . m . tr (fromF f)
224
224
@@ -230,10 +230,6 @@ type One = ()
230
230
231-- Primitives
231-- Primitives
232
232
233−-- | Empty matrix constructor
234−empty :: Matrix e Zero Zero
235−empty = M I.Empty
236−
237-- | Unit matrix constructor
233-- | Unit matrix constructor
238one :: e -> Matrix e One One
234one :: e -> Matrix e One One
239one = M . I.One
235one = M . I.One
@@ -271,10 +267,10 @@ infixl 2 ===
271-- Functor hierarchy
267-- Functor hierarchy
272
268
273-- | Functor instance equivalent function
269-- | Functor instance equivalent function
274−fmapM ::
270+fmapM ::
275 ( Liftable e a b,
271 ( Liftable e a b,
276− CountableDimensionsN a b,
272+ CountableDimsN a b,
277− FromListsN e b a
273+ FLN b a
278 )
274 )
279 =>
275 =>
280 (a -> b) -> Matrix e c a -> Matrix e c b
276 (a -> b) -> Matrix e c a -> Matrix e c b
@@ -285,23 +281,23 @@ unitM :: (Num e) => Matrix e () ()
285unitM = one 1
281unitM = one 1
286
282
287-- | Applicative instance equivalent 'unit' function,
283-- | Applicative instance equivalent 'unit' function,
288−multM ::
284+multM ::
289− ( CountableDimensionsN a b,
285+ ( CountableDimsN a b,
290 CountableN (a, b),
286 CountableN (a, b),
291 Num e,
287 Num e,
292− FromListsN e (a, b) a,
288+ FLN (a, b) a,
293− FromListsN e (a, b) b,
289+ FLN (a, b) b,
294 TrivialP a b
290 TrivialP a b
295 ) => Matrix e c a -> Matrix e c b -> Matrix e c (a, b)
291 ) => Matrix e c a -> Matrix e c b -> Matrix e c (a, b)
296multM = kr
292multM = kr
297
293
298-- | Monad instance equivalent 'return' function,
294-- | Monad instance equivalent 'return' function,
299−returnM ::
295+returnM ::
300− forall e a .
296+ forall e a .
301 ( Num e,
297 ( Num e,
302 Enum e,
298 Enum e,
303 Enum a,
299 Enum a,
304− FromListsN e () a,
300+ FLN () a,
305 Countable a
301 Countable a
306 ) => a -> Matrix e One a
302 ) => a -> Matrix e One a
307returnM a = col l
303returnM a = col l
@@ -318,35 +314,35 @@ bindM = flip comp
318
314
319-- | Build a matrix out of a list of list of elements. Throws a runtime
315-- | Build a matrix out of a list of list of elements. Throws a runtime
320-- error if the dimensions do not match.
316-- error if the dimensions do not match.
321−fromLists :: (FromListsN e cols rows) => [[e]] -> Matrix e cols rows
317+fromLists :: (FLN cols rows) => [[e]] -> Matrix e cols rows
322fromLists = M . I.fromLists
318fromLists = M . I.fromLists
323
319
324-- | Matrix builder function. Constructs a matrix provided with
320-- | Matrix builder function. Constructs a matrix provided with
325-- a construction function that operates with indices.
321-- a construction function that operates with indices.
326matrixBuilder' ::
322matrixBuilder' ::
327− (FromListsN e cols rows, CountableDimensionsN cols rows )
323+ (FLN cols rows, CountableDimsN cols rows )
328 => ((Int, Int) -> e) -> Matrix e cols rows
324 => ((Int, Int) -> e) -> Matrix e cols rows
329matrixBuilder' = M . I.matrixBuilder'
325matrixBuilder' = M . I.matrixBuilder'
330
326
331-- | Matrix builder function. Constructs a matrix provided with
327-- | Matrix builder function. Constructs a matrix provided with
332-- a construction function that operates with arbitrary types.
328-- a construction function that operates with arbitrary types.
333matrixBuilder ::
329matrixBuilder ::
334− ( FromListsN e a b,
330+ ( FLN a b,
335 Enum a,
331 Enum a,
336 Enum b,
332 Enum b,
337 Bounded a,
333 Bounded a,
338 Bounded b,
334 Bounded b,
339 Eq a,
335 Eq a,
340− CountableDimensionsN a b
336+ Countable b
341 ) => ((a, b) -> e) -> Matrix e a b
337 ) => ((a, b) -> e) -> Matrix e a b
342matrixBuilder f = M (I.matrixBuilder f)
338matrixBuilder f = M (I.matrixBuilder f)
343
339
344-- | Constructs a column vector matrix
340-- | Constructs a column vector matrix
345−col :: (FromListsN e () rows) => [e] -> Matrix e One rows
341+col :: (FLN () rows) => [e] -> Matrix e One rows
346col = M . I.col
342col = M . I.col
347
343
348-- | Constructs a row vector matrix
344-- | Constructs a row vector matrix
349−row :: (FromListsN e cols ()) => [e] -> Matrix e cols One
345+row :: (FLN cols ()) => [e] -> Matrix e cols One
350row = M . I.row
346row = M . I.row
351
347
352-- | Lifts functions to matrices with arbitrary dimensions.
348-- | Lifts functions to matrices with arbitrary dimensions.
@@ -355,8 +351,8 @@ row = M . I.row
355-- types @a@ or @b@ allows.
351-- types @a@ or @b@ allows.
356fromF' ::
352fromF' ::
357 ( Liftable e a b,
353 ( Liftable e a b,
358− CountableDimensionsN cols rows,
354+ CountableDimsN cols rows,
359− FromListsN e rows cols
355+ FLN rows cols
360 ) =>
356 ) =>
361 (a -> b) -> Matrix e cols rows
357 (a -> b) -> Matrix e cols rows
362fromF' = M . I.fromF'
358fromF' = M . I.fromF'
@@ -365,8 +361,8 @@ fromF' = M . I.fromF'
365-- cardinality's.
361-- cardinality's.
366fromF ::
362fromF ::
367 ( Liftable e a b,
363 ( Liftable e a b,
368− CountableDimensionsN a b,
364+ CountableDimsN a b,
369− FromListsN e b a
365+ FLN b a
370 ) =>
366 ) =>
371 (a -> b) -> Matrix e a b
367 (a -> b) -> Matrix e a b
372fromF = M . I.fromF
368fromF = M . I.fromF
@@ -374,8 +370,8 @@ fromF = M . I.fromF
374-- | Lifts relation functions to Boolean Matrix
370-- | Lifts relation functions to Boolean Matrix
375toRel ::
371toRel ::
376 ( Liftable (Natural 0 1) a b,
372 ( Liftable (Natural 0 1) a b,
377− CountableDimensionsN a b,
373+ CountableDims a b,
378− FromListsN (Natural 0 1) b a
374+ FLN b a
379 ) => (a -> b -> Bool) -> Matrix (Natural 0 1) a b
375 ) => (a -> b -> Bool) -> Matrix (Natural 0 1) a b
380toRel = M . I.toRel
376toRel = M . I.toRel
381
377
@@ -393,7 +389,7 @@ toList (M m) = I.toList m
393
389
394-- | The zero matrix. A matrix wholly filled with zeros.
390-- | The zero matrix. A matrix wholly filled with zeros.
395zeros ::
391zeros ::
396− (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
392+ (Num e, FLN cols rows, CountableDimsN cols rows)
397 => Matrix e cols rows
393 => Matrix e cols rows
398zeros = M I.zeros
394zeros = M I.zeros
399
395
@@ -403,7 +399,7 @@ zeros = M I.zeros
403--
399--
404-- Also known as T (Top) matrix.
400-- Also known as T (Top) matrix.
405ones ::
401ones ::
406− (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
402+ (Num e, FLN cols rows, CountableDimsN cols rows)
407 => Matrix e cols rows
403 => Matrix e cols rows
408ones = M I.ones
404ones = M I.ones
409
405
@@ -412,7 +408,7 @@ ones = M I.ones
412-- | The constant matrix constructor. A matrix wholly filled with a given
408-- | The constant matrix constructor. A matrix wholly filled with a given
413-- value.
409-- value.
414constant ::
410constant ::
415− (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
411+ (Num e, FLN cols rows, CountableDimsN cols rows)
416 => e -> Matrix e cols rows
412 => e -> Matrix e cols rows
417constant = M . I.constant
413constant = M . I.constant
418
414
@@ -421,19 +417,19 @@ constant = M . I.constant
421-- | The T (Top) row vector matrix.
417-- | The T (Top) row vector matrix.
422bang ::
418bang ::
423 forall e cols.
419 forall e cols.
424− (Num e, Enum e, FromListsN e cols (), CountableN cols) =>
420+ (Num e, Enum e, FLN cols (), CountableN cols) =>
425 Matrix e cols One
421 Matrix e cols One
426bang = M I.bang
422bang = M I.bang
427
423
428-- | Point constant relation
424-- | Point constant relation
429−point ::
425+point ::
430 ( Bounded a,
426 ( Bounded a,
431 Enum a,
427 Enum a,
432 Eq a,
428 Eq a,
433 Num e,
429 Num e,
434 Ord e,
430 Ord e,
435 CountableN a,
431 CountableN a,
436− FromListsN e a One
432+ FLN a One
437 ) => a -> Matrix e One a
433 ) => a -> Matrix e One a
438point = fromF . const
434point = fromF . const
439
435
@@ -441,7 +437,7 @@ point = fromF . const
441
437
442-- | iden matrix
438-- | iden matrix
443iden ::
439iden ::
444− (Num e, FromListsN e a a, CountableN a) =>
440+ (Num e, FLN a a, CountableN a) =>
445 Matrix e a a
441 Matrix e a a
446iden = M I.iden
442iden = M I.iden
447{-# NOINLINE iden #-}
443{-# NOINLINE iden #-}
@@ -455,7 +451,7 @@ iden = M I.iden
455comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
451comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
456comp (M a) (M b) = M (I.comp a b)
452comp (M a) (M b) = M (I.comp a b)
457{-# NOINLINE comp #-}
453{-# NOINLINE comp #-}
458−{-# RULES
454+{-# RULES
459 "comp/iden1" forall m. comp m iden = m ;
455 "comp/iden1" forall m. comp m iden = m ;
460 "comp/iden2" forall m. comp iden m = m
456 "comp/iden2" forall m. comp iden m = m
461#-}
457#-}
@@ -477,9 +473,9 @@ infixl 7 ./
477-- | Biproduct first component projection
473-- | Biproduct first component projection
478p1 ::
474p1 ::
479 ( Num e,
475 ( Num e,
480− CountableDimensionsN n m,
476+ CountableDimsN n m,
481− FromListsN e n m,
477+ FLN n m,
482− FromListsN e m m
478+ FLN m m
483 ) =>
479 ) =>
484 Matrix e (Either m n) m
480 Matrix e (Either m n) m
485p1 = M I.p1
481p1 = M I.p1
@@ -487,9 +483,9 @@ p1 = M I.p1
487-- | Biproduct second component projection
483-- | Biproduct second component projection
488p2 ::
484p2 ::
489 ( Num e,
485 ( Num e,
490− CountableDimensionsN n m,
486+ CountableDimsN n m,
491− FromListsN e m n,
487+ FLN m n,
492− FromListsN e n n
488+ FLN n n
493 ) =>
489 ) =>
494 Matrix e (Either m n) n
490 Matrix e (Either m n) n
495p2 = M I.p2
491p2 = M I.p2
@@ -499,9 +495,9 @@ p2 = M I.p2
499-- | Biproduct first component injection
495-- | Biproduct first component injection
500i1 ::
496i1 ::
501 ( Num e,
497 ( Num e,
502− CountableDimensionsN n m,
498+ CountableDimsN n m,
503− FromListsN e n m,
499+ FLN n m,
504− FromListsN e m m
500+ FLN m m
505 ) =>
501 ) =>
506 Matrix e m (Either m n)
502 Matrix e m (Either m n)
507i1 = tr p1
503i1 = tr p1
@@ -509,9 +505,9 @@ i1 = tr p1
509-- | Biproduct second component injection
505-- | Biproduct second component injection
510i2 ::
506i2 ::
511 ( Num e,
507 ( Num e,
512− CountableDimensionsN n m,
508+ CountableDimsN n m,
513− FromListsN e m n,
509+ FLN m n,
514− FromListsN e n n
510+ FLN n n
515 ) =>
511 ) =>
516 Matrix e n (Either m n)
512 Matrix e n (Either m n)
517i2 = tr p2
513i2 = tr p2
@@ -520,24 +516,34 @@ i2 = tr p2
520
516
521-- | Obtain the number of rows.
517-- | Obtain the number of rows.
522--
518--
523−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
519+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
524−-- dimensions in constant time.
520+-- dimensions in constant time. For a version that doesn't require the
525−--
521+-- constraint see 'rows''.
526−-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
527−-- exchange for performance.
528rows :: (CountableN rows) => Matrix e cols rows -> Int
522rows :: (CountableN rows) => Matrix e cols rows -> Int
529rows (M m) = I.rows m
523rows (M m) = I.rows m
530
524
525+-- | Obtain the number of rows in an inefficient manner, but without any
526+-- constraints.
527+--
528+-- For a more efficient version see 'rows'.
529+rows' :: Matrix e cols rows -> Int
530+rows' (M m) = I.rows' m
531+
531-- | Obtain the number of columns.
532-- | Obtain the number of columns.
532−--
533−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
534−-- dimensions in constant time.
535--
533--
536−-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
534+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
537−-- exchange for performance.
535+-- dimensions in constant time. For a version that doesn't require the
536+-- constraint see 'columns''.
538columns :: (CountableN cols) => Matrix e cols rows -> Int
537columns :: (CountableN cols) => Matrix e cols rows -> Int
539columns (M m) = I.columns m
538columns (M m) = I.columns m
540
539
540+-- | Obtain the number of columns in an inefficient manner, but without any
541+-- constraints.
542+--
543+-- For a more efficient version see 'columns'.
544+columns' :: Matrix e cols rows -> Int
545+columns' (M m) = I.columns' m
546+
541-- Coproduct Bifunctor
547-- Coproduct Bifunctor
542
548
543infixl 5 -|-
549infixl 5 -|-
@@ -545,11 +551,11 @@ infixl 5 -|-
545-- | Matrix coproduct functor also known as matrix direct sum.
551-- | Matrix coproduct functor also known as matrix direct sum.
546(-|-) ::
552(-|-) ::
547 ( Num e,
553 ( Num e,
548− CountableDimensionsN j k,
554+ CountableDimsN j k,
549− FromListsN e k k,
555+ FLN k k,
550− FromListsN e j k,
556+ FLN j k,
551− FromListsN e k j,
557+ FLN k j,
552− FromListsN e j j
558+ FLN j j
553 ) =>
559 ) =>
554 Matrix e n k ->
560 Matrix e n k ->
555 Matrix e m j ->
561 Matrix e m j ->
@@ -559,23 +565,23 @@ infixl 5 -|-
559-- Khatri Rao Product and projections
565-- Khatri Rao Product and projections
560
566
561-- | Khatri Rao product first component projection matrix.
567-- | Khatri Rao product first component projection matrix.
562−fstM ::
568+fstM ::
563 forall e m k .
569 forall e m k .
564 ( Num e,
570 ( Num e,
565− CountableDimensionsN m k,
571+ CountableDimsN m k,
566 CountableN (m, k),
572 CountableN (m, k),
567− FromListsN e (m, k) m,
573+ FLN (m, k) m,
568 TrivialP m k
574 TrivialP m k
569 ) => Matrix e (m, k) m
575 ) => Matrix e (m, k) m
570fstM = M (I.fstM @e @(I.Normalize m) @(I.Normalize k))
576fstM = M (I.fstM @e @(I.Normalize m) @(I.Normalize k))
571
577
572-- | Khatri Rao product second component projection matrix.
578-- | Khatri Rao product second component projection matrix.
573−sndM ::
579+sndM ::
574 forall e m k.
580 forall e m k.
575 ( Num e,
581 ( Num e,
576− CountableDimensionsN k m,
582+ CountableDimsN k m,
577 CountableN (m, k),
583 CountableN (m, k),
578− FromListsN e (m, k) k,
584+ FLN (m, k) k,
579 TrivialP m k
585 TrivialP m k
580 ) => Matrix e (m, k) k
586 ) => Matrix e (m, k) k
581sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
587sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
@@ -583,9 +589,9 @@ sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
583-- | Khatri Rao Matrix product also known as matrix pairing.
589-- | Khatri Rao Matrix product also known as matrix pairing.
584--
590--
585-- NOTE: That this is not a true categorical product, see for instance:
591-- NOTE: That this is not a true categorical product, see for instance:
586−--
592+--
587-- @
593-- @
588−-- | fstM . kr a b == a
594+-- | fstM . kr a b == a
589-- kr a b ==> |
595-- kr a b ==> |
590-- | sndM . kr a b == b
596-- | sndM . kr a b == b
591-- @
597-- @
@@ -594,10 +600,10 @@ sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
594kr ::
600kr ::
595 forall e cols a b.
601 forall e cols a b.
596 ( Num e,
602 ( Num e,
597− CountableDimensionsN a b,
603+ CountableDimsN a b,
598 CountableN (a, b),
604 CountableN (a, b),
599− FromListsN e (a, b) a,
605+ FLN (a, b) a,
600− FromListsN e (a, b) b,
606+ FLN (a, b) b,
601 TrivialP a b
607 TrivialP a b
602 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a, b)
608 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a, b)
603kr a b =
609kr a b =
@@ -613,13 +619,13 @@ infixl 4 ><
613(><) ::
619(><) ::
614 forall e m p n q.
620 forall e m p n q.
615 ( Num e,
621 ( Num e,
616− CountableDimensionsN m n,
622+ CountableDimsN m n,
617− CountableDimensionsN p q,
623+ CountableDimsN p q,
618− CountableDimensionsN (m, n) (p, q),
624+ CountableDimsN (m, n) (p, q),
619− FromListsN e (m, n) m,
625+ FLN (m, n) m,
620− FromListsN e (m, n) n,
626+ FLN (m, n) n,
621− FromListsN e (p, q) p,
627+ FLN (p, q) p,
622− FromListsN e (p, q) q,
628+ FLN (p, q) q,
623 TrivialP m n,
629 TrivialP m n,
624 TrivialP p q
630 TrivialP p q
625 ) => Matrix e m p -> Matrix e n q -> Matrix e (m, n) (p, q)
631 ) => Matrix e m p -> Matrix e n q -> Matrix e (m, n) (p, q)
@@ -631,7 +637,7 @@ infixl 4 ><
631-- Matrix abide Join Fork
637-- Matrix abide Join Fork
632
638
633-- | Matrix "abiding" followin the 'Join'-'Fork' abide law.
639-- | Matrix "abiding" followin the 'Join'-'Fork' abide law.
634−--
640+--
635-- Law:
641-- Law:
636--
642--
637-- @
643-- @
@@ -643,7 +649,7 @@ abideJF (M m) = M (I.abideJF m)
643-- Matrix abide Fork Join
649-- Matrix abide Fork Join
644
650
645-- | Matrix "abiding" followin the 'Fork'-'Join' abide law.
651-- | Matrix "abiding" followin the 'Fork'-'Join' abide law.
646−--
652+--
647-- Law:
653-- Law:
648--
654--
649-- @
655-- @
@@ -662,9 +668,9 @@ tr (M m) = M (I.tr m)
662
668
663-- | Selective functors 'select' operator equivalent inspired by the
669-- | Selective functors 'select' operator equivalent inspired by the
664-- ArrowMonad solution presented in the paper.
670-- ArrowMonad solution presented in the paper.
665−selectM ::
671+selectM ::
666 ( Num e,
672 ( Num e,
667− FromListsN e b b,
673+ FLN b b,
668 CountableN b
674 CountableN b
669 ) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
675 ) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
670selectM (M m) (M y) = M (I.select m y)
676selectM (M m) (M y) = M (I.select m y)
@@ -677,9 +683,9 @@ cond ::
677 Trivial2 a,
683 Trivial2 a,
678 Trivial3 a,
684 Trivial3 a,
679 CountableN a,
685 CountableN a,
680− FromListsN e () a,
686+ FLN () a,
681− FromListsN e a (),
687+ FLN a (),
682− FromListsN e a a,
688+ FLN a a,
683 Liftable e a Bool
689 Liftable e a Bool
684 )
690 )
685 =>
691 =>
@@ -689,11 +695,11 @@ cond p (M a) (M b) = M (I.cond p a b)
689-- Pretty print
695-- Pretty print
690
696
691-- | Matrix pretty printer
697-- | Matrix pretty printer
692−pretty :: (CountableDimensionsN cols rows, Show e) => Matrix e cols rows -> String
698+pretty :: (CountableDimsN cols rows, Show e) => Matrix e cols rows -> String
693pretty (M m) = I.pretty m
699pretty (M m) = I.pretty m
694
700
695-- | Matrix pretty printer
701-- | Matrix pretty printer
696−prettyPrint :: (CountableDimensionsN cols rows, Show e) => Matrix e cols rows -> IO ()
702+prettyPrint :: (CountableDimsN cols rows, Show e) => Matrix e cols rows -> IO ()
697prettyPrint (M m) = I.prettyPrint m
703prettyPrint (M m) = I.prettyPrint m
698
704
699-- | Zip two matrices with a given binary function
705-- | Zip two matrices with a given binary function
src/LAoP/Relation.hs+3−4
@@ -32,16 +32,15 @@ module LAoP.Relation
32
32
33 -- * Constraint type synonyms
33 -- * Constraint type synonyms
34 Countable,
34 Countable,
35− CountableDimensions,
35+ CountableDims,
36 CountableN,
36 CountableN,
37− CountableDimensionsN,
37+ CountableDimsN,
38− FromListsN,
38+ FLN,
39 Liftable,
39 Liftable,
40 Trivial,
40 Trivial,
41 TrivialP,
41 TrivialP,
42
42
43 -- * Primitives
43 -- * Primitives
44− empty,
45 one,
44 one,
46 join,
45 join,
47 (|||),
46 (|||),
src/LAoP/Relation/Internal.hs+128−130
@@ -43,16 +43,15 @@ module LAoP.Relation.Internal
43
43
44 -- * Constraint type synonyms
44 -- * Constraint type synonyms
45 Countable,
45 Countable,
46− CountableDimensions,
46+ CountableDims,
47 CountableN,
47 CountableN,
48− CountableDimensionsN,
48+ CountableDimsN,
49− FromListsN,
49+ FLN,
50 Liftable,
50 Liftable,
51 Trivial,
51 Trivial,
52 TrivialP,
52 TrivialP,
53
53
54 -- * Primitives
54 -- * Primitives
55− empty,
56 one,
55 one,
57 join,
56 join,
58 (|||),
57 (|||),
@@ -194,19 +193,19 @@ newtype Relation a b = R (I.Matrix Boolean (I.Normalize a) (I.Normalize b))
194deriving instance (Read (I.Matrix Boolean (I.Normalize a) (I.Normalize b))) => Read (Relation a b)
193deriving instance (Read (I.Matrix Boolean (I.Normalize a) (I.Normalize b))) => Read (Relation a b)
195
194
196-- | Constraint type synonyms to keep the type signatures less convoluted
195-- | Constraint type synonyms to keep the type signatures less convoluted
197−type Countable a = KnownNat (I.Count a)
196+type Countable a = KnownNat (I.Count a)
198−type CountableDimensions a b = (Countable a, Countable b)
197+type CountableDims a b = (Countable a, Countable b)
199−type CountableN a = KnownNat (I.Count (I.Normalize a))
198+type CountableN a = KnownNat (I.Count (I.Normalize a))
200−type CountableDimensionsN a b = (CountableN a, CountableN b)
199+type CountableDimsN a b = (CountableN a, CountableN b)
201−type FromListsN a b = I.FromLists Boolean (I.Normalize a) (I.Normalize b)
200+type FLN a b = I.FromLists (I.Normalize a) (I.Normalize b)
202−type Liftable a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num Boolean, Ord Boolean)
201+type Liftable a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num Boolean, Ord Boolean)
203−type Trivial a = I.Normalize a ~ I.Normalize (I.Normalize a)
202+type Trivial a = I.Normalize a ~ I.Normalize (I.Normalize a)
204−type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
203+type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
205
204
206-- | It is possible to implement a constrained version of the category type
205-- | It is possible to implement a constrained version of the category type
207-- class.
206-- class.
208instance Category Relation where
207instance Category Relation where
209− type Object Relation a = (FromListsN a a, CountableN a)
208+ type Object Relation a = (FLN a a, CountableN a)
210 id = iden
209 id = iden
211 (.) = comp
210 (.) = comp
212
211
@@ -229,10 +228,6 @@ type One = ()
229
228
230-- Primitives
229-- Primitives
231
230
232−-- | Empty matrix constructor
233−empty :: Relation Zero Zero
234−empty = R I.Empty
235−
236-- | Unit matrix constructor
231-- | Unit matrix constructor
237one :: Boolean -> Relation One One
232one :: Boolean -> Relation One One
238one = R . I.One
233one = R . I.One
@@ -269,26 +264,26 @@ infixl 2 ===
269
264
270-- | Build a matrix out of a list of list of elements. Throws a runtime
265-- | Build a matrix out of a list of list of elements. Throws a runtime
271-- error if the dimensions do not match.
266-- error if the dimensions do not match.
272−fromLists :: (FromListsN a b) => [[Boolean]] -> Relation a b
267+fromLists :: (FLN a b) => [[Boolean]] -> Relation a b
273fromLists = R . I.fromLists
268fromLists = R . I.fromLists
274
269
275-- | Relation builder function. Constructs a relation provided with
270-- | Relation builder function. Constructs a relation provided with
276-- a construction function that operates with indices.
271-- a construction function that operates with indices.
277relationBuilder' ::
272relationBuilder' ::
278− (FromListsN a b, CountableDimensionsN a b) =>
273+ (FLN a b, CountableDimsN a b) =>
279 ((Int, Int) -> Boolean) -> Relation a b
274 ((Int, Int) -> Boolean) -> Relation a b
280relationBuilder' = R . I.matrixBuilder'
275relationBuilder' = R . I.matrixBuilder'
281
276
282-- | Relation builder function. Constructs a relation provided with
277-- | Relation builder function. Constructs a relation provided with
283-- a construction function that operates with arbitrary types.
278-- a construction function that operates with arbitrary types.
284relationBuilder ::
279relationBuilder ::
285− ( FromListsN a b,
280+ ( FLN a b,
286 Enum a,
281 Enum a,
287 Enum b,
282 Enum b,
288 Bounded a,
283 Bounded a,
289 Bounded b,
284 Bounded b,
290 Eq a,
285 Eq a,
291− CountableDimensionsN a b
286+ CountableDims a b
292 ) => ((a, b) -> Boolean) -> Relation a b
287 ) => ((a, b) -> Boolean) -> Relation a b
293relationBuilder = R . I.matrixBuilder
288relationBuilder = R . I.matrixBuilder
294
289
@@ -298,18 +293,18 @@ relationBuilder = R . I.matrixBuilder
298-- types @a@ or @b@ allows.
293-- types @a@ or @b@ allows.
299fromF' ::
294fromF' ::
300 ( Liftable a b,
295 ( Liftable a b,
301− CountableDimensionsN c d,
296+ CountableDimsN c d,
302− FromListsN d c
297+ FLN d c
303 )
298 )
304 => (a -> b) -> Relation c d
299 => (a -> b) -> Relation c d
305fromF' f = R (I.fromFRel' f)
300fromF' f = R (I.fromFRel' f)
306
301
307-- | Lifts functions to matrices with dimensions matching @a@ and @b@
302-- | Lifts functions to matrices with dimensions matching @a@ and @b@
308-- cardinality's.
303-- cardinality's.
309−fromF ::
304+fromF ::
310 ( Liftable a b,
305 ( Liftable a b,
311− CountableDimensionsN a b,
306+ CountableDimsN a b,
312− FromListsN b a
307+ FLN b a
313 )
308 )
314 => (a -> b) -> Relation a b
309 => (a -> b) -> Relation a b
315fromF f = R (I.fromFRel f)
310fromF f = R (I.fromFRel f)
@@ -317,8 +312,8 @@ fromF f = R (I.fromFRel f)
317-- | Lifts relation functions to 'Relation'
312-- | Lifts relation functions to 'Relation'
318toRel ::
313toRel ::
319 ( Liftable a b,
314 ( Liftable a b,
320− CountableDimensionsN a b,
315+ CountableDims a b,
321− FromListsN b a
316+ FLN b a
322 )
317 )
323 => (a -> b -> Bool) -> Relation a b
318 => (a -> b -> Bool) -> Relation a b
324toRel = R . I.toRel
319toRel = R . I.toRel
@@ -327,9 +322,9 @@ toRel = R . I.toRel
327fromRel ::
322fromRel ::
328 ( Liftable a b,
323 ( Liftable a b,
329 Eq a,
324 Eq a,
330− CountableDimensionsN a b,
325+ CountableDimsN a b,
331− FromListsN a One,
326+ FLN a One,
332− FromListsN b One
327+ FLN b One
333 )
328 )
334 => Relation a b -> (a -> b -> Bool)
329 => Relation a b -> (a -> b -> Bool)
335fromRel r a b = pointApBool a b r
330fromRel r a b = pointApBool a b r
@@ -353,12 +348,12 @@ toBool r = case toList r of
353-- | Power transpose.
348-- | Power transpose.
354--
349--
355-- Maps a relation to a set valued function.
350-- Maps a relation to a set valued function.
356−pt ::
351+pt ::
357 ( Liftable a b,
352 ( Liftable a b,
358 Eq a,
353 Eq a,
359− CountableDimensionsN a b,
354+ CountableDimsN a b,
360− FromListsN a One,
355+ FLN a One,
361− FromListsN b One
356+ FLN b One
362 )
357 )
363 => Relation a b -> (a -> List b)
358 => Relation a b -> (a -> List b)
364pt r a =
359pt r a =
@@ -370,8 +365,8 @@ belongs ::
370 ( Bounded a,
365 ( Bounded a,
371 Enum a,
366 Enum a,
372 Eq a,
367 Eq a,
373− CountableDimensionsN (List a) a,
368+ CountableDims (List a) a,
374− FromListsN a (List a)
369+ FLN a (List a)
375 )
370 )
376 => Relation (List a) a
371 => Relation (List a) a
377belongs = toRel elemR
372belongs = toRel elemR
@@ -383,14 +378,14 @@ belongs = toRel elemR
383-- | The zero relation. A relation where no element of type @a@ relates
378-- | The zero relation. A relation where no element of type @a@ relates
384-- with elements of type @b@.
379-- with elements of type @b@.
385--
380--
386−-- Also known as ⊥ (Bottom) Relation or empty Relation.
381+-- Also known as ⊥ (Bottom) Relation.
387--
382--
388-- @
383-- @
389-- r `.` ⊥ == ⊥ `.` r == ⊥
384-- r `.` ⊥ == ⊥ `.` r == ⊥
390-- ⊥ ``sse`` R && R ``sse`` T == True
385-- ⊥ ``sse`` R && R ``sse`` T == True
391-- @
386-- @
392zeros ::
387zeros ::
393− (FromListsN a b, CountableDimensionsN a b) =>
388+ (FLN a b, CountableDimsN a b) =>
394 Relation a b
389 Relation a b
395zeros = relationBuilder' (const (nat 0))
390zeros = relationBuilder' (const (nat 0))
396
391
@@ -405,7 +400,7 @@ zeros = relationBuilder' (const (nat 0))
405-- ⊥ ``sse`` R && R ``sse`` T == True
400-- ⊥ ``sse`` R && R ``sse`` T == True
406-- @
401-- @
407ones ::
402ones ::
408− (FromListsN a b, CountableDimensionsN a b) =>
403+ (FLN a b, CountableDimsN a b) =>
409 Relation a b
404 Relation a b
410ones = relationBuilder' (const (nat 1))
405ones = relationBuilder' (const (nat 1))
411
406
@@ -413,17 +408,17 @@ ones = relationBuilder' (const (nat 1))
413
408
414-- | The T (Top) row vector relation.
409-- | The T (Top) row vector relation.
415bang ::
410bang ::
416− (FromListsN a One, CountableN a) =>
411+ (FLN a One, CountableN a) =>
417 Relation a One
412 Relation a One
418bang = ones
413bang = ones
419
414
420-- | Point constant relation
415-- | Point constant relation
421−point ::
416+point ::
422 ( Bounded a,
417 ( Bounded a,
423 Enum a,
418 Enum a,
424 Eq a,
419 Eq a,
425 CountableN a,
420 CountableN a,
426− FromListsN a One
421+ FLN a One
427 ) => a -> Relation One a
422 ) => a -> Relation One a
428point = fromF . const
423point = fromF . const
429
424
@@ -435,7 +430,7 @@ point = fromF . const
435-- 'iden' `.` r == r == r `.` 'iden'
430-- 'iden' `.` r == r == r `.` 'iden'
436-- @
431-- @
437iden ::
432iden ::
438− (FromListsN a a, CountableN a) => Relation a a
433+ (FLN a a, CountableN a) => Relation a a
439iden = relationBuilder' (bool (nat 0) (nat 1) . uncurry (==))
434iden = relationBuilder' (bool (nat 0) (nat 1) . uncurry (==))
440
435
441-- | Relational composition
436-- | Relational composition
@@ -489,7 +484,7 @@ shrunkBy r s = r `intersection` divR s (conv r)
489-- r ``overriddenBy`` r == r
484-- r ``overriddenBy`` r == r
490-- @
485-- @
491overriddenBy ::
486overriddenBy ::
492− ( FromListsN b b,
487+ ( FLN b b,
493 CountableN b
488 CountableN b
494 ) => Relation a b -> Relation a b -> Relation a b
489 ) => Relation a b -> Relation a b -> Relation a b
495overriddenBy r s = s `union` r `intersection` divR zeros (conv s)
490overriddenBy r s = s `union` r `intersection` divR zeros (conv s)
@@ -501,9 +496,9 @@ overriddenBy r s = s `union` r `intersection` divR zeros (conv s)
501pointAp ::
496pointAp ::
502 ( Liftable a b,
497 ( Liftable a b,
503 Eq a,
498 Eq a,
504− CountableDimensionsN a b,
499+ CountableDimsN a b,
505− FromListsN a One,
500+ FLN a One,
506− FromListsN b One
501+ FLN b One
507 ) => a -> b -> Relation a b -> Relation One One
502 ) => a -> b -> Relation a b -> Relation One One
508pointAp a b r = conv (point b) . r . point a
503pointAp a b r = conv (point b) . r . point a
509
504
@@ -513,9 +508,9 @@ pointAp a b r = conv (point b) . r . point a
513pointApBool ::
508pointApBool ::
514 ( Liftable a b,
509 ( Liftable a b,
515 Eq a,
510 Eq a,
516− CountableDimensionsN a b,
511+ CountableDimsN a b,
517− FromListsN a One,
512+ FLN a One,
518− FromListsN b One
513+ FLN b One
519 ) => a -> b -> Relation a b -> Bool
514 ) => a -> b -> Relation a b -> Bool
520pointApBool a b r = toBool $ conv (point b) . r . point a
515pointApBool a b r = toBool $ conv (point b) . r . point a
521
516
@@ -595,19 +590,19 @@ divisionF f g = conv g . f
595-- Taxonomy of binary relations
590-- Taxonomy of binary relations
596
591
597-- | A 'Relation' @r@ is 'simple' 'iff' @'coreflexive' ('img' r)@
592-- | A 'Relation' @r@ is 'simple' 'iff' @'coreflexive' ('img' r)@
598−simple :: (CountableN b, FromListsN b b) => Relation a b -> Bool
593+simple :: (CountableN b, FLN b b) => Relation a b -> Bool
599simple = coreflexive . img
594simple = coreflexive . img
600
595
601-- | A 'Relation' @r@ is 'injective' 'iff' @'coreflexive' ('ker' r)@
596-- | A 'Relation' @r@ is 'injective' 'iff' @'coreflexive' ('ker' r)@
602−injective :: (CountableN a, FromListsN a a) => Relation a b -> Bool
597+injective :: (CountableN a, FLN a a) => Relation a b -> Bool
603injective = coreflexive . ker
598injective = coreflexive . ker
604
599
605-- | A 'Relation' @r@ is 'entire' 'iff' @'reflexive' ('ker' r)@
600-- | A 'Relation' @r@ is 'entire' 'iff' @'reflexive' ('ker' r)@
606−entire :: (CountableN a, FromListsN a a) => Relation a b -> Bool
601+entire :: (CountableN a, FLN a a) => Relation a b -> Bool
607entire = reflexive . ker
602entire = reflexive . ker
608
603
609-- | A 'Relation' @r@ is 'surjective' 'iff' @'reflexive' ('img' r)@
604-- | A 'Relation' @r@ is 'surjective' 'iff' @'reflexive' ('img' r)@
610−surjective :: (CountableN b, FromListsN b b) => Relation a b -> Bool
605+surjective :: (CountableN b, FLN b b) => Relation a b -> Bool
611surjective = reflexive . img
606surjective = reflexive . img
612
607
613-- | A 'Relation' @r@ is a 'function' 'iff' @'simple' r && 'entire' r@
608-- | A 'Relation' @r@ is a 'function' 'iff' @'simple' r && 'entire' r@
@@ -620,9 +615,9 @@ surjective = reflexive . img
620-- r `.` f ``sse`` s == r ``sse`` s `.` f
615-- r `.` f ``sse`` s == r ``sse`` s `.` f
621-- @
616-- @
622function ::
617function ::
623− ( CountableDimensionsN a b,
618+ ( CountableDimsN a b,
624− FromListsN a a,
619+ FLN a a,
625− FromListsN b b
620+ FLN b b
626 )
621 )
627 => Relation a b -> Bool
622 => Relation a b -> Bool
628function r = simple r && entire r
623function r = simple r && entire r
@@ -630,7 +625,7 @@ function r = simple r && entire r
630-- | A 'Relation' @r@ is a 'representation' 'iff' @'injective' r && 'entire' r@
625-- | A 'Relation' @r@ is a 'representation' 'iff' @'injective' r && 'entire' r@
631representation ::
626representation ::
632 ( CountableN a,
627 ( CountableN a,
633− FromListsN a a
628+ FLN a a
634 )
629 )
635 => Relation a b -> Bool
630 => Relation a b -> Bool
636representation r = injective r && entire r
631representation r = injective r && entire r
@@ -638,45 +633,45 @@ representation r = injective r && entire r
638-- | A 'Relation' @r@ is an 'abstraction' 'iff' @'surjective' r && 'simple' r@
633-- | A 'Relation' @r@ is an 'abstraction' 'iff' @'surjective' r && 'simple' r@
639abstraction ::
634abstraction ::
640 ( CountableN b,
635 ( CountableN b,
641− FromListsN b b
636+ FLN b b
642 )
637 )
643 => Relation a b -> Bool
638 => Relation a b -> Bool
644abstraction r = surjective r && simple r
639abstraction r = surjective r && simple r
645
640
646-- | A 'Relation' @r@ is a 'surjection' 'iff' @'function' r && 'abstraction' r@
641-- | A 'Relation' @r@ is a 'surjection' 'iff' @'function' r && 'abstraction' r@
647surjection ::
642surjection ::
648− ( CountableDimensionsN a b,
643+ ( CountableDimsN a b,
649− FromListsN a a,
644+ FLN a a,
650− FromListsN b b
645+ FLN b b
651 )
646 )
652 => Relation a b -> Bool
647 => Relation a b -> Bool
653surjection r = function r && abstraction r
648surjection r = function r && abstraction r
654
649
655-- | A 'Relation' @r@ is a 'injection' 'iff' @'function' r && 'representation' r@
650-- | A 'Relation' @r@ is a 'injection' 'iff' @'function' r && 'representation' r@
656injection ::
651injection ::
657− ( CountableDimensionsN a b,
652+ ( CountableDimsN a b,
658− FromListsN a a,
653+ FLN a a,
659− FromListsN b b
654+ FLN b b
660 )
655 )
661 => Relation a b -> Bool
656 => Relation a b -> Bool
662injection r = function r && representation r
657injection r = function r && representation r
663
658
664-- | A 'Relation' @r@ is an 'bijection' 'iff' @'injection' r && 'surjection' r@
659-- | A 'Relation' @r@ is an 'bijection' 'iff' @'injection' r && 'surjection' r@
665bijection ::
660bijection ::
666− ( CountableDimensionsN a b,
661+ ( CountableDimsN a b,
667− FromListsN b b,
662+ FLN b b,
668− FromListsN a a
663+ FLN a a
669 ) => Relation a b -> Bool
664 ) => Relation a b -> Bool
670bijection r = injection r && surjection r
665bijection r = injection r && surjection r
671
666
672-- Properties of relations
667-- Properties of relations
673
668
674-- | A 'Relation' @r@ is 'reflexive' 'iff' @'id' ``sse`` r@
669-- | A 'Relation' @r@ is 'reflexive' 'iff' @'id' ``sse`` r@
675−reflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
670+reflexive :: (CountableN a, FLN a a) => Relation a a -> Bool
676reflexive r = id <= r
671reflexive r = id <= r
677
672
678-- | A 'Relation' @r@ is 'coreflexive' 'iff' @r ``sse`` 'id'@
673-- | A 'Relation' @r@ is 'coreflexive' 'iff' @r ``sse`` 'id'@
679−coreflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
674+coreflexive :: (CountableN a, FLN a a) => Relation a a -> Bool
680coreflexive r = r <= id
675coreflexive r = r <= id
681
676
682-- | A 'Relation' @r@ is 'transitive' 'iff' @(r `.` r) ``sse`` r@
677-- | A 'Relation' @r@ is 'transitive' 'iff' @(r `.` r) ``sse`` r@
@@ -688,35 +683,35 @@ symmetric :: Relation a a -> Bool
688symmetric r = r == conv r
683symmetric r = r == conv r
689
684
690-- | A 'Relation' @r@ is anti-symmetric 'iff' @(r ``intersection`` 'conv' r) ``sse`` 'id'@
685-- | A 'Relation' @r@ is anti-symmetric 'iff' @(r ``intersection`` 'conv' r) ``sse`` 'id'@
691−antiSymmetric :: (CountableN a, FromListsN a a) => Relation a a -> Bool
686+antiSymmetric :: (CountableN a, FLN a a) => Relation a a -> Bool
692antiSymmetric r = (r `intersection` conv r) `sse` id
687antiSymmetric r = (r `intersection` conv r) `sse` id
693
688
694-- | A 'Relation' @r@ is 'irreflexive' 'iff' @(r ``intersection`` 'id') == 'zeros'@
689-- | A 'Relation' @r@ is 'irreflexive' 'iff' @(r ``intersection`` 'id') == 'zeros'@
695−irreflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
690+irreflexive :: (CountableN a, FLN a a) => Relation a a -> Bool
696irreflexive r = (r `intersection` id) == zeros
691irreflexive r = (r `intersection` id) == zeros
697
692
698-- | A 'Relation' @r@ is 'connected' 'iff' @(r ``union`` 'conv' r) == 'ones'@
693-- | A 'Relation' @r@ is 'connected' 'iff' @(r ``union`` 'conv' r) == 'ones'@
699−connected :: (CountableN a, FromListsN a a) => Relation a a -> Bool
694+connected :: (CountableN a, FLN a a) => Relation a a -> Bool
700connected r = (r `union` conv r) == ones
695connected r = (r `union` conv r) == ones
701
696
702-- | A 'Relation' @r@ is a 'preorder' 'iff' @'reflexive' r && 'transitive' r@
697-- | A 'Relation' @r@ is a 'preorder' 'iff' @'reflexive' r && 'transitive' r@
703−preorder :: (CountableN a, FromListsN a a) => Relation a a -> Bool
698+preorder :: (CountableN a, FLN a a) => Relation a a -> Bool
704preorder r = reflexive r && transitive r
699preorder r = reflexive r && transitive r
705
700
706-- | A 'Relation' @r@ is a partial-order 'iff' @'antiSymmetric' r && 'preorder' r@
701-- | A 'Relation' @r@ is a partial-order 'iff' @'antiSymmetric' r && 'preorder' r@
707−partialOrder :: (CountableN a, FromListsN a a) => Relation a a -> Bool
702+partialOrder :: (CountableN a, FLN a a) => Relation a a -> Bool
708partialOrder r = antiSymmetric r && preorder r
703partialOrder r = antiSymmetric r && preorder r
709
704
710-- | A 'Relation' @r@ is a linear-order 'iff' @'connected' r && 'partialOrder' r@
705-- | A 'Relation' @r@ is a linear-order 'iff' @'connected' r && 'partialOrder' r@
711−linearOrder :: (CountableN a, FromListsN a a) => Relation a a -> Bool
706+linearOrder :: (CountableN a, FLN a a) => Relation a a -> Bool
712linearOrder r = connected r && partialOrder r
707linearOrder r = connected r && partialOrder r
713
708
714-- | A 'Relation' @r@ is an 'equivalence' 'iff' @'symmetric' r && 'preorder' r@
709-- | A 'Relation' @r@ is an 'equivalence' 'iff' @'symmetric' r && 'preorder' r@
715−equivalence :: (CountableN a, FromListsN a a) => Relation a a -> Bool
710+equivalence :: (CountableN a, FLN a a) => Relation a a -> Bool
716equivalence r = symmetric r && preorder r
711equivalence r = symmetric r && preorder r
717
712
718-- | A 'Relation' @r@ is a partial-equivalence 'iff' @'partialOrder' r && 'equivalence' r@
713-- | A 'Relation' @r@ is a partial-equivalence 'iff' @'partialOrder' r && 'equivalence' r@
719−partialEquivalence :: (CountableN a, FromListsN a a) => Relation a a -> Bool
714+partialEquivalence :: (CountableN a, FLN a a) => Relation a a -> Bool
720partialEquivalence r = partialOrder r && equivalence r
715partialEquivalence r = partialOrder r && equivalence r
721
716
722-- | A 'Relation' @r@ is 'difunctional' or regular wherever
717-- | A 'Relation' @r@ is 'difunctional' or regular wherever
@@ -748,10 +743,10 @@ difunctional r = r . conv r . r == r
748-- 'eitherR' ('splitR' r s) ('splitR' t v) == 'splitR' ('eitherR' r t) ('eitherR' s v)
743-- 'eitherR' ('splitR' r s) ('splitR' t v) == 'splitR' ('eitherR' r t) ('eitherR' s v)
749-- @
744-- @
750splitR ::
745splitR ::
751− ( CountableDimensionsN a b,
746+ ( CountableDimsN a b,
752 CountableN (a, b),
747 CountableN (a, b),
753− FromListsN (a, b) a,
748+ FLN (a, b) a,
754− FromListsN (a, b) b,
749+ FLN (a, b) b,
755 TrivialP a b
750 TrivialP a b
756 )
751 )
757 => Relation c a -> Relation c b -> Relation c (a, b)
752 => Relation c a -> Relation c b -> Relation c (a, b)
@@ -764,9 +759,9 @@ splitR (R f) (R g) = R (I.kr f g)
764-- @
759-- @
765fstR ::
760fstR ::
766 forall a b .
761 forall a b .
767− ( CountableDimensionsN a b,
762+ ( CountableDimsN a b,
768 CountableN (a, b),
763 CountableN (a, b),
769− FromListsN (a, b) a,
764+ FLN (a, b) a,
770 TrivialP a b
765 TrivialP a b
771 )
766 )
772 => Relation (a, b) a
767 => Relation (a, b) a
@@ -779,9 +774,9 @@ fstR = R (I.fstM @Boolean @(I.Normalize a) @(I.Normalize b))
779-- @
774-- @
780sndR ::
775sndR ::
781 forall a b .
776 forall a b .
782− ( CountableDimensionsN a b,
777+ ( CountableDimsN a b,
783 CountableN (a, b),
778 CountableN (a, b),
784− FromListsN (a, b) b,
779+ FLN (a, b) b,
785 TrivialP a b
780 TrivialP a b
786 )
781 )
787 => Relation (a, b) b
782 => Relation (a, b) b
@@ -797,13 +792,13 @@ infixl 4 ><
797-- (r '><' s) `.` (p '><' q) == (r `.` p) '><' (s `.` q)
792-- (r '><' s) `.` (p '><' q) == (r `.` p) '><' (s `.` q)
798-- @
793-- @
799(><) ::
794(><) ::
800− ( CountableDimensionsN a b,
795+ ( CountableDimsN a b,
801− CountableDimensionsN c d,
796+ CountableDimsN c d,
802− CountableDimensionsN (a, c) (b, d),
797+ CountableDimsN (a, c) (b, d),
803− FromListsN (a, c) a,
798+ FLN (a, c) a,
804− FromListsN (a, c) c,
799+ FLN (a, c) c,
805− FromListsN (b, d) b,
800+ FLN (b, d) b,
806− FromListsN (b, d) d,
801+ FLN (b, d) d,
807 TrivialP a c,
802 TrivialP a c,
808 TrivialP b d
803 TrivialP b d
809 )
804 )
@@ -837,9 +832,9 @@ eitherR = join
837-- 'i1' `.` 'i2' = 'zeros'
832-- 'i1' `.` 'i2' = 'zeros'
838-- @
833-- @
839i1 ::
834i1 ::
840− ( CountableDimensionsN a b,
835+ ( CountableDimsN a b,
841− FromListsN b a,
836+ FLN b a,
842− FromListsN a a
837+ FLN a a
843 )
838 )
844 => Relation a (Either a b)
839 => Relation a (Either a b)
845i1 = R I.i1
840i1 = R I.i1
@@ -851,9 +846,9 @@ i1 = R I.i1
851-- 'i1' `.` 'i2' = 'zeros'
846-- 'i1' `.` 'i2' = 'zeros'
852-- @
847-- @
853i2 ::
848i2 ::
854− ( CountableDimensionsN a b,
849+ ( CountableDimsN a b,
855− FromListsN a b,
850+ FLN a b,
856− FromListsN b b
851+ FLN b b
857 )
852 )
858 => Relation b (Either a b)
853 => Relation b (Either a b)
859i2 = R I.i2
854i2 = R I.i2
@@ -866,11 +861,11 @@ infixl 5 -|-
866-- r '-|-' s == 'eitherR' ('i1' `.` r) ('i2' `.` s)
861-- r '-|-' s == 'eitherR' ('i1' `.` r) ('i2' `.` s)
867-- @
862-- @
868(-|-) ::
863(-|-) ::
869− ( CountableDimensionsN b d,
864+ ( CountableDimsN b d,
870− FromListsN b b,
865+ FLN b b,
871− FromListsN d b,
866+ FLN d b,
872− FromListsN b d,
867+ FLN b d,
873− FromListsN d d
868+ FLN d d
874 )
869 )
875 => Relation a b -> Relation c d -> Relation (Either a c) (Either b d)
870 => Relation a b -> Relation c d -> Relation (Either a c) (Either b d)
876(-|-) (R a) (R b) = R ((I.-|-) a b)
871(-|-) (R a) (R b) = R ((I.-|-) a b)
@@ -883,13 +878,13 @@ infixl 5 -|-
883-- 'trans'/'untrans';
878-- 'trans'/'untrans';
884-- more-over, where each particular attribute is placed (input/output) is irrelevant.
879-- more-over, where each particular attribute is placed (input/output) is irrelevant.
885trans ::
880trans ::
886− ( CountableDimensionsN a b,
881+ ( CountableDimsN a b,
887 CountableN c,
882 CountableN c,
888− CountableDimensionsN (a, b) (c, b),
883+ CountableDimsN (a, b) (c, b),
889− FromListsN (c, b) c,
884+ FLN (c, b) c,
890− FromListsN (c, b) b,
885+ FLN (c, b) b,
891− FromListsN (a, b) a,
886+ FLN (a, b) a,
892− FromListsN (a, b) b,
887+ FLN (a, b) b,
893 Trivial (a, b),
888 Trivial (a, b),
894 Trivial (c, b),
889 Trivial (c, b),
895 TrivialP a b,
890 TrivialP a b,
@@ -904,13 +899,13 @@ trans r = splitR r sndR . conv fstR
904-- 'trans'/'untrans';
899-- 'trans'/'untrans';
905-- more-over, where each particular attribute is placed (input/output) is irrelevant.
900-- more-over, where each particular attribute is placed (input/output) is irrelevant.
906untrans ::
901untrans ::
907− ( CountableDimensionsN a b,
902+ ( CountableDimsN a b,
908 CountableN c,
903 CountableN c,
909− CountableDimensionsN (a, b) (c, b),
904+ CountableDimsN (a, b) (c, b),
910− FromListsN (c, b) c,
905+ FLN (c, b) c,
911− FromListsN (c, b) b,
906+ FLN (c, b) b,
912− FromListsN (a, b) b,
907+ FLN (a, b) b,
913− FromListsN (a, b) a,
908+ FLN (a, b) a,
914 Trivial (a, b),
909 Trivial (a, b),
915 Trivial (c, b),
910 Trivial (c, b),
916 TrivialP a b,
911 TrivialP a b,
@@ -932,9 +927,10 @@ untrans s = fstR . conv (splitR (conv s) sndR)
932predR ::
927predR ::
933 ( Bounded a,
928 ( Bounded a,
934 Enum a,
929 Enum a,
930+ Countable a,
935 CountableN a,
931 CountableN a,
936− FromListsN a a,
932+ FLN a a,
937− FromListsN Bool a
933+ FLN Bool a
938 )
934 )
939 => Relation a Bool -> Relation a a
935 => Relation a Bool -> Relation a a
940predR p = id `intersection` divisionF (fromF (const True)) p
936predR p = id `intersection` divisionF (fromF (const True)) p
@@ -949,7 +945,7 @@ predR p = id `intersection` divisionF (fromF (const True)) p
949-- @
945-- @
950equalizer ::
946equalizer ::
951 ( CountableN a,
947 ( CountableN a,
952− FromListsN a a
948+ FLN a a
953 )
949 )
954 => Relation a b -> Relation a b -> Relation a a
950 => Relation a b -> Relation a b -> Relation a a
955equalizer f g = id `intersection` divisionF f g
951equalizer f g = id `intersection` divisionF f g
@@ -959,23 +955,25 @@ equalizer f g = id `intersection` divisionF f g
959-- @
955-- @
960-- 'guard' p = 'i2' ``overriddenBy`` 'i1' `.` 'predR' p
956-- 'guard' p = 'i2' ``overriddenBy`` 'i1' `.` 'predR' p
961-- @
957-- @
962−guard ::
958+guard ::
963 ( Bounded b,
959 ( Bounded b,
964 Enum b,
960 Enum b,
965 CountableN b,
961 CountableN b,
966− FromListsN b b,
962+ Countable b,
963+ FLN b b,
967− FromListsN Bool b
964+ FLN Bool b
968 ) => Relation b Bool -> Relation b (Either b b)
965 ) => Relation b Bool -> Relation b (Either b b)
969guard p = conv (eitherR (predR p) (predR (negate p)))
966guard p = conv (eitherR (predR p) (predR (negate p)))
970
967
971-- | Relational McCarthy's conditional.
968-- | Relational McCarthy's conditional.
972−cond ::
969+cond ::
973 ( Bounded b,
970 ( Bounded b,
974 Enum b,
971 Enum b,
972+ Countable b,
975 CountableN b,
973 CountableN b,
976− FromListsN b b,
974+ FLN b b,
977− FromListsN Bool b
975+ FLN Bool b
978− )
976+ )
979 => Relation b Bool -> Relation b c -> Relation b c -> Relation b c
977 => Relation b Bool -> Relation b c -> Relation b c -> Relation b c
980cond p r s = eitherR r s . guard p
978cond p r s = eitherR r s . guard p
981
979
@@ -985,7 +983,7 @@ cond p r s = eitherR r s . guard p
985-- since @'ker' r ``sse`` 'id'@ in such situations.
983-- since @'ker' r ``sse`` 'id'@ in such situations.
986domain ::
984domain ::
987 ( CountableN a,
985 ( CountableN a,
988− FromListsN a a
986+ FLN a a
989 ) => Relation a b -> Relation a a
987 ) => Relation a b -> Relation a a
990domain r = ker r `intersection` id
988domain r = ker r `intersection` id
991
989
@@ -995,16 +993,16 @@ domain r = ker r `intersection` id
995-- since @'img' f ``sse`` id@ for any @f@.
993-- since @'img' f ``sse`` id@ for any @f@.
996range ::
994range ::
997 ( CountableN b,
995 ( CountableN b,
998− FromListsN b b
996+ FLN b b
999 ) => Relation a b -> Relation b b
997 ) => Relation a b -> Relation b b
1000range r = img r `intersection` id
998range r = img r `intersection` id
1001
999
1002-- Relation pretty print
1000-- Relation pretty print
1003
1001
1004-- | Relation pretty printing
1002-- | Relation pretty printing
1005−pretty :: (CountableDimensionsN a b) => Relation a b -> String
1003+pretty :: (CountableDimsN a b) => Relation a b -> String
1006pretty (R a) = I.pretty a
1004pretty (R a) = I.pretty a
1007
1005
1008-- | Relation pretty printing
1006-- | Relation pretty printing
1009−prettyPrint :: (CountableDimensionsN a b) => Relation a b -> IO ()
1007+prettyPrint :: (CountableDimsN a b) => Relation a b -> IO ()
1010prettyPrint (R a) = I.prettyPrint a
1008prettyPrint (R a) = I.prettyPrint a
stack-8.8.3.yamlnew file+1
@@ -0,0 +1 @@
1+resolver: lts-15.5
stack.yaml+1−66
@@ -1,66 +1 @@
1−# This file was automatically generated by 'stack init'
2−#
3−# Some commonly used options have been documented as comments in this file.
4−# For advanced use and comprehensive documentation of the format, please see:
5−# https://docs.haskellstack.org/en/stable/yaml_configuration/
6−
7−# Resolver to choose a 'specific' stackage snapshot or a compiler version.
8−# A snapshot resolver dictates the compiler version and the set of packages
9−# to be used for project dependencies. For example:
10−#
11−# resolver: lts-3.5
12−# resolver: nightly-2015-09-21
13−# resolver: ghc-7.10.2
14−#
15−# The location of a snapshot can be provided as a file or url. Stack assumes
16−# a snapshot provided as a file might change, whereas a url resource does not.
17−#
18−# resolver: ./custom-snapshot.yaml
19−# resolver: https://example.com/snapshots/2018-01-01.yaml
20−resolver: lts-14.18
1+resolver: lts-14.3
21−
22−# User packages to be built.
23−# Various formats can be used as shown in the example below.
24−#
25−# packages:
26−# - some-directory
27−# - https://example.com/foo/bar/baz-0.0.2.tar.gz
28−# subdirs:
29−# - auto-update
30−# - wai
31−packages:
32−- .
33−# Dependency packages to be pulled from upstream that are not in the resolver.
34−# These entries can reference officially published versions as well as
35−# forks / in-progress versions pinned to a git hash. For example:
36−#
37−# extra-deps:
38−# - acme-missiles-0.3
39−# - git: https://github.com/commercialhaskell/stack.git
40−# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
41−#
42−# extra-deps: []
43−
44−# Override default flag values for local packages and extra-deps
45−# flags: {}
46−
47−# Extra package databases containing global packages
48−# extra-package-dbs: []
49−
50−# Control whether we use the GHC we find on the path
51−# system-ghc: true
52−#
53−# Require a specific version of stack, using version ranges
54−# require-stack-version: -any # Default
55−# require-stack-version: ">=2.1"
56−#
57−# Override the architecture used by stack, especially useful on Windows
58−# arch: i386
59−# arch: x86_64
60−#
61−# Extra directories used by stack for building
62−# extra-include-dirs: [/path/to/dir]
63−# extra-lib-dirs: [/path/to/dir]
64−#
65−# Allow a newer minor version of GHC than the snapshot specifies
66−# compiler-check: newer-minor
stack.yaml.lock+4−4
@@ -6,7 +6,7 @@
6packages: []
6packages: []
7snapshots:
7snapshots:
8- completed:
8- completed:
9− size: 524789
9+ size: 523878
10− url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/14/18.yaml
10+ url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/14/3.yaml
11− sha256: 646be71223e08234131c6989912e6011e01b9767bc447b6d466a35e14360bdf2
11+ sha256: 470c46c27746a48c7c50f829efc0cf00112787a7804ee4ac7a27754658f6d92c
12− original: lts-14.18
12+ original: lts-14.3
app/Quantum.hs → test/Examples/Quantum.hsrenamed+2−2
@@ -1,5 +1,5 @@
1{-# LANGUAGE StandaloneDeriving #-}
1{-# LANGUAGE StandaloneDeriving #-}
2−module Quantum where
2+module Examples.Quantum where
3
3
4import LAoP.Utils
4import LAoP.Utils
5import LAoP.Matrix.Type
5import LAoP.Matrix.Type
@@ -21,7 +21,7 @@ cnot = kr fstM (fromF xor)
21ccnot :: (Num e, Ord e) => Matrix e ((Bool, Bool), Bool) ((Bool, Bool), Bool)
21ccnot :: (Num e, Ord e) => Matrix e ((Bool, Bool), Bool) ((Bool, Bool), Bool)
22ccnot = kr fstM (fromF f)
22ccnot = kr fstM (fromF f)
23 where
23 where
24− f = xor . (tp (uncurry (&&)) id)
24+ f = xor . tp (uncurry (&&)) id
25 tp f g (a,b) = (f a, g b)
25 tp f g (a,b) = (f a, g b)
26
26
27had :: Matrix (Complex Double) Bool Bool
27had :: Matrix (Complex Double) Bool Bool
app/Examples.hs → test/Examples/Readme.hsrenamed+1−2
@@ -1,11 +1,10 @@
1{-# LANGUAGE GADTs #-}
1{-# LANGUAGE GADTs #-}
2{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE DeriveGeneric #-}
3{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE FlexibleContexts #-}
4−{-# LANGUAGE TypeOperators #-}
5{-# LANGUAGE TypeApplications #-}
4{-# LANGUAGE TypeApplications #-}
6{-# LANGUAGE DataKinds #-}
5{-# LANGUAGE DataKinds #-}
7
6
8−module Examples
7+module Examples.Readme
9 ( exec
8 ( exec
10 )
9 )
11 where
10 where
app/Spreadsheets.hs → test/Examples/Spreadsheets.hsrenamed+1−2
@@ -1,7 +1,6 @@
1{-# LANGUAGE DeriveGeneric #-}
1{-# LANGUAGE DeriveGeneric #-}
2
2
3−module Spreadsheets
3+module Examples.Spreadsheets
4− where
5
4
6import LAoP.Utils
5import LAoP.Utils
7import LAoP.Matrix.Type
6import LAoP.Matrix.Type
test/Spec.hs+7−1
@@ -1,2 +1,8 @@
1+module Main (main) where
2+
3+import qualified Examples.Readme as RD
4+
1main :: IO ()
5main :: IO ()
2−main = putStrLn "Test suite not yet implemented"
6+main = do
7+ putStrLn ("Testing if README works" :: String)
8+ RD.exec