on a3aa4a2 help sign in
28 files changed+498−457
since splitsnapshots
unifiedsplit
.ghc.environment.x86_64-linux-8.6.5+17 .ghc.environment.x86_64-linux-8.8.3+17 .github/workflows/ci.yml−43 .gitignore+2−54 .travis.yml−47 CHANGELOG.md−17 ChangeLog.md+3 LICENSE+26−17 README.md+30−34 Setup.hs+2 benchmark/Main.hs → app/Benchmarks.hs+6−3 test/Examples/Readme.hs → app/Examples.hs+2−1 app/Main.hs+7 test/Examples/Quantum.hs → app/Quantum.hs+2−2 test/Examples/Spreadsheets.hs → app/Spreadsheets.hs+2−1 appveyor.yml−34 hackage.sh+10 laop.cabal+72−82 package.yaml+57 src/LAoP/Matrix/Internal.hs+117−54 src/LAoP/Matrix/Nat.hs+4 src/LAoP/Matrix/Type.hs+35−42 src/LAoP/Relation.hs+1 src/LAoP/Relation/Internal.hs+15−13 stack-8.8.3.yaml−1 stack.yaml+66−1 stack.yaml.lock+4−4 test/Spec.hs+1−7
.ghc.environment.x86_64-linux-8.6.5new file+17
@@ -0,0 +1,17 @@
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
.ghc.environment.x86_64-linux-8.8.3new file+17
@@ -0,0 +1,17 @@
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.8.3/package.db
10+package-db dist-docs.Q1TJPL/packagedb/ghc-8.8.3
11+package-id laop-0.1.0.7-inplace
12+package-id base-4.13.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.4.0
.github/workflows/ci.ymldeleted−43
@@ -1,43 +0,0 @@
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+2−54
@@ -1,55 +1,3 @@
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
251.stack-work/
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]
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
2+laop.cabal
3+*~
.travis.ymldeleted−47
@@ -1,47 +0,0 @@
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.mddeleted−17
@@ -1,17 +0,0 @@
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.mdnew file+3
@@ -0,0 +1,3 @@
1+# Changelog for laop
2+
3+## Unreleased changes
LICENSE+26−17
@@ -1,21 +1,30 @@
1−MIT License
1+Copyright Armando Santos (c) 2019
22
3−Copyright (c) 2020 Armando Santos
3+All rights reserved.
44
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
10−furnished to do so, subject to the following conditions:
5+Redistribution and use in source and binary forms, with or without
6+modification, are permitted provided that the following conditions are met:
117
12−The above copyright notice and this permission notice shall be included in all
13−copies or substantial portions of the Software.
8+ * Redistributions of source code must retain the above copyright
9+ notice, this list of conditions and the following disclaimer.
1410
15−THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16−IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17−FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18−AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19−LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20−OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21−SOFTWARE.
11+ * Redistributions in binary form must reproduce the above
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
23+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
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
29+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
README.md+30−34
@@ -1,11 +1,5 @@
11# laop - Linear Algebra of Programming library
22
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−
93The LAoP discipline generalises relations and functions treating them as
104Boolean matrices and in turn consider these as arrows.
115
@@ -16,38 +10,40 @@ motivation behind the library, the underlying theory, and implementation details
1610This module offers many of the combinators mentioned in the work of
1711[Macedo (2012)](https://repositorium.sdum.uminho.pt/handle/1822/22894) and [Oliveira (2012)](https://pdfs.semanticscholar.org/ccf5/27fa9179081223bffe8067edd81948644fc0.pdf).
1812
19−See the package in hackage [here](https://hackage.haskell.org/package/laop-0.1.1.0)
20−
21−A Functional Pearl has been written and can be regarded as the [reference document](https://github.com/bolt12/tymfgg-pearl) for this library.
22−
23−## Features
24−
25−This library offers 3 main matrix programming modules:
13+See the package in hackage [here](https://hackage.haskell.org/package/laop-0.1.0.5)
2614
27−- One in which matrices are typed with type level natural numbers;
28−- One in which matrices are typed with arbitrary generic data types;
29−- One in which matrices are regarded as relations, i.e. boolean matrices;
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).
3016
31−There's also an experimental module that uses matrices to represent probability
32−distributions.
17+A Functional Pearl is also on its way, hopefully will be the reference document for this library.
3318
34−The most interesting feature is that matrices are represented as an inductive data type.
35−Given this formulation, matrix algorithms can be expressed in a much more elegant and
36−calculational style than the traditional vector of vectors representation. This data type
37−guarantees that a matrix will always have valid dimensions. It can also express block matrix
38−computations naturally, which leads to total, efficient and statically typed manipulation
39−and transformation functions.
40−
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.
42−
43−Given this, this new matrix formulation compared to other libraries:
44−
45−- Is more compositional and polymorphic and does not have partial matrix manipulation functions (hence less chances for usage errors);
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.
47−
48−## Known issues
19+## Features
4920
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).
21+This library offers:
22+
23+- An internal module representation that uses normal datatypes (`Void`, `()`, `Either`) for matrix dimensions and two
24+ type families `FromNat` and `Count` to make it easier to work with these type of matrices.
25+- An interfacing module that uses generalised types for the dimensions and is just a newtype wrapper
26+ around the internal matrix data type.
27+- An interfacing module that uses type level naturals for the dimensions and is just a newtype wrapper
28+ around the internal matrix data type.
29+- An interfacing module that restricts the matrix contents to being only 0's or 1's (True
30+ and False). This module is called 'Relation' and offers many of the combinators seen in
31+ AoP and Relational Calculus/Algebra.
32+- A very simple Probabilistic Programming module and several functions and data types to
33+ make it easier to deal with sample space.
34+
35+Given this, this new matrix formulation compared to other libraries one has much more advantages, such as:
36+
37+- Has an inductive definition enabling writing matrix manipulation functions in a much more elegant and calculational way;
38+- Statically typed dimensions;
39+- Polymorphic data type dimensions;
40+- Polymorphic matrix content;
41+- Awesome dimension type inference;
42+- Fast type natural conversion via `FromNat` type family;
43+- Matrix `Junc` and `Split`-ing in O(1);
44+- Matrix composition takes advantage of divide-and-conquer and fusion laws.
45+
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.
5147
5248## Notes
5349
Setup.hsnew file+2
@@ -0,0 +1,2 @@
1+import Distribution.Simple
2+main = defaultMain
benchmark/Main.hs → app/Benchmarks.hsrenamed+6−3
@@ -8,7 +8,10 @@
88{-# LANGUAGE AllowAmbiguousTypes #-}
99{-# LANGUAGE ScopedTypeVariables #-}
1010
11−module Main (main) where
11+module Benchmarks
12+ ( benchmark
13+ )
14+ where
1215
1316import LAoP.Dist.Internal
1417import LAoP.Utils
@@ -144,8 +147,8 @@ setupEnv2 = do
144147 distList2 <- generate (resize 1 (randomDistF @(Natural 0 100) @(Natural 0 100)))
145148 return (m21, m40, dist, dist2, distList1, distList2)
146149
147−main :: IO ()
148−main = defaultMain [
150+benchmark :: IO ()
151+benchmark = defaultMain [
149152 env setupEnv $ \ ~(m11, m12, m21, m22, m31, m32) -> bgroup "Matrix Composition" [
150153 bgroup "10x10" [
151154 bench "WHNF - 10x10" $ whnf (comp m11) m12
test/Examples/Readme.hs → app/Examples.hsrenamed+2−1
@@ -1,10 +1,11 @@
11{-# LANGUAGE GADTs #-}
22{-# LANGUAGE DeriveGeneric #-}
33{-# LANGUAGE FlexibleContexts #-}
4+{-# LANGUAGE TypeOperators #-}
45{-# LANGUAGE TypeApplications #-}
56{-# LANGUAGE DataKinds #-}
67
7−module Examples.Readme
8+module Examples
89 ( exec
910 )
1011 where
app/Main.hsnew file+7
@@ -0,0 +1,7 @@
1+module Main where
2+
3+import Examples
4+import Benchmarks
5+
6+main :: IO ()
7+main = benchmark
test/Examples/Quantum.hs → app/Quantum.hsrenamed+2−2
@@ -1,5 +1,5 @@
11{-# LANGUAGE StandaloneDeriving #-}
2−module Examples.Quantum where
2+module Quantum where
33
44import LAoP.Utils
55import LAoP.Matrix.Type
@@ -21,7 +21,7 @@ cnot = kr fstM (fromF xor)
2121ccnot :: (Num e, Ord e) => Matrix e ((Bool, Bool), Bool) ((Bool, Bool), Bool)
2222ccnot = kr fstM (fromF f)
2323 where
24− f = xor . tp (uncurry (&&)) id
24+ f = xor . (tp (uncurry (&&)) id)
2525 tp f g (a,b) = (f a, g b)
2626
2727had :: Matrix (Complex Double) Bool Bool
test/Examples/Spreadsheets.hs → app/Spreadsheets.hsrenamed+2−1
@@ -1,6 +1,7 @@
11{-# LANGUAGE DeriveGeneric #-}
22
3−module Examples.Spreadsheets
3+module Spreadsheets
4+ where
45
56import LAoP.Utils
67import LAoP.Matrix.Type
appveyor.ymldeleted−34
@@ -1,34 +0,0 @@
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
hackage.shnew file+10
@@ -0,0 +1,10 @@
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.cabal+72−82
@@ -1,89 +1,79 @@
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
1+cabal-version: 1.12
202
21−source-repository head
22− type: git
23− location: https://github.com/bolt12/laop.git
3+-- This file has been generated from package.yaml by hpack version 0.31.2.
4+--
5+-- see: https://github.com/sol/hpack
6+--
7+-- hash: c53e5ac8b0734f570fcea8d217d84e401323172c60a00eee9d60ffbaef505191
248
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
9+name: laop
10+version: 0.1.0.7
11+description: Please see the README on GitHub at <https://github.com/xxxx/laop#readme>
12+category: Data
13+homepage: https://github.com/xxxx/laop#readme
14+bug-reports: https://github.com/xxxx/laop/issues
15+author: Armando Santos
16+maintainer: armandoifsantos@gmail.com
17+copyright: MIT
18+license: BSD3
19+license-file: LICENSE
20+build-type: Simple
21+extra-source-files:
22+ README.md
23+ ChangeLog.md
4324
44− default-language: Haskell2010
25+source-repository head
26+ type: git
27+ location: https://github.com/xxxx/laop
4528
4629library
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
30+ exposed-modules:
31+ LAoP.Dist
32+ LAoP.Dist.Internal
33+ LAoP.Matrix.Internal
34+ LAoP.Matrix.Nat
35+ LAoP.Matrix.Type
36+ LAoP.Relation
37+ LAoP.Relation.Internal
38+ LAoP.Utils
39+ LAoP.Utils.Internal
40+ other-modules:
41+ Paths_laop
42+ hs-source-dirs:
43+ src
44+ build-depends:
45+ base >=4.7 && <5
46+ , deepseq
47+ default-language: Haskell2010
5848
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
49+executable laop-exe
50+ main-is: Main.hs
51+ other-modules:
52+ Benchmarks
53+ Examples
54+ Spreadsheets
55+ Paths_laop
56+ hs-source-dirs:
57+ app
58+ ghc-options: -threaded -rtsopts -with-rtsopts=-N2 -funfolding-use-threshold=16 -fexcess-precision -optc-O3 -optc-ffast-math
59+ build-depends:
60+ QuickCheck
61+ , base >=4.7 && <5
62+ , criterion
63+ , deepseq
64+ , laop
65+ default-language: Haskell2010
7366
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
67+test-suite laop-test
68+ type: exitcode-stdio-1.0
69+ main-is: Spec.hs
70+ other-modules:
71+ Paths_laop
72+ hs-source-dirs:
73+ test
74+ ghc-options: -threaded -rtsopts -with-rtsopts=-N
75+ build-depends:
76+ base >=4.7 && <5
77+ , deepseq
78+ , laop
79+ default-language: Haskell2010
package.yamlnew file+57
@@ -0,0 +1,57 @@
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/Matrix/Internal.hs+117−54
@@ -30,7 +30,7 @@
3030-- motivation behind the library, the underlying theory, and implementation details.
3131--
3232-- This module offers many of the combinators mentioned in the work of
33−-- Macedo (2012) and Oliveira (2012).
33+-- Macedo (2012) and Oliveira (2012).
3434--
3535-- This is an Internal module and it is no supposed to be imported.
3636--
@@ -43,8 +43,8 @@ module LAoP.Matrix.Internal
4343 --
4444 -- There exists two type families that make it easier to write
4545 -- matrix dimensions: 'FromNat' and 'Count'. This approach
46− -- leads to a very straightforward implementation
47− -- of LAoP combinators.
46+ -- leads to a very straightforward implementation
47+ -- of LAoP combinators.
4848
4949 -- * Type safe matrix representation
5050 Matrix (..),
@@ -60,6 +60,7 @@ module LAoP.Matrix.Internal
6060 Trivial,
6161
6262 -- * Primitives
63+ empty,
6364 one,
6465 join,
6566 fork,
@@ -86,9 +87,7 @@ module LAoP.Matrix.Internal
8687 -- * Misc
8788 -- ** Get dimensions
8889 columns,
89− columns',
9090 rows,
91− rows',
9291
9392 -- ** Matrix Transposition
9493 tr,
@@ -176,7 +175,6 @@ import Data.Kind
176175import Data.List
177176import Data.Maybe
178177import Data.Proxy
179−import Data.Typeable
180178import Data.Void
181179import GHC.TypeLits
182180import Data.Type.Equality
@@ -187,6 +185,7 @@ import qualified Prelude (id, (.))
187185
188186-- | LAoP (Linear Algebra of Programming) Inductive Matrix definition.
189187data Matrix e cols rows where
188+ Empty :: Matrix e Void Void
190189 One :: e -> Matrix e () ()
191190 Join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
192191 Fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
@@ -216,6 +215,7 @@ type family Count (d :: Type) :: Nat where
216215--
217216-- Thanks to Li-Yao Xia this type family is super fast.
218217type family FromNat (n :: Nat) :: Type where
218+ FromNat 0 = Void
219219 FromNat 1 = ()
220220 FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2))
221221
@@ -243,15 +243,17 @@ type Trivial a = FromNat (Count a) ~ a
243243-- class.
244244instance (Num e) => Category (Matrix e) where
245245 type Object (Matrix e) a = (FL a a, Countable a)
246− id = iden
246+ id = iden
247247 (.) = comp
248248
249249instance NFData e => NFData (Matrix e cols rows) where
250+ rnf Empty = ()
250251 rnf (One e) = rnf e
251252 rnf (Join a b) = rnf a `seq` rnf b
252253 rnf (Fork a b) = rnf a `seq` rnf b
253254
254255instance Eq e => Eq (Matrix e cols rows) where
256+ Empty == Empty = True
255257 (One a) == (One b) = a == b
256258 (Join a b) == (Join c d) = a == c && b == d
257259 (Fork a b) == (Fork c d) = a == c && b == d
@@ -259,21 +261,25 @@ instance Eq e => Eq (Matrix e cols rows) where
259261 x@(Join _ _) == y@(Fork _ _) = abideJF x == y
260262
261263instance Num e => Num (Matrix e cols rows) where
264+
262265 a + b = zipWithM (+) a b
263266
264267 a - b = zipWithM (-) a b
265268
266269 a * b = zipWithM (*) a b
267270
271+ abs Empty = Empty
268272 abs (One a) = One (abs a)
269273 abs (Join a b) = Join (abs a) (abs b)
270274 abs (Fork a b) = Fork (abs a) (abs b)
271275
276+ signum Empty = Empty
272277 signum (One a) = One (signum a)
273278 signum (Join a b) = Join (signum a) (signum b)
274279 signum (Fork a b) = Fork (signum a) (signum b)
275280
276281instance Ord e => Ord (Matrix e cols rows) where
282+ Empty <= Empty = True
277283 (One a) <= (One b) = a <= b
278284 (Join a b) <= (Join c d) = (a <= c) && (b <= d)
279285 (Fork a b) <= (Fork c d) = (a <= c) && (b <= d)
@@ -282,6 +288,10 @@ instance Ord e => Ord (Matrix e cols rows) where
282288
283289-- Primitives
284290
291+-- | Empty matrix constructor
292+empty :: Matrix e Void Void
293+empty = Empty
294+
285295-- | Unit matrix constructor
286296one :: e -> Matrix e () ()
287297one = One
@@ -317,34 +327,35 @@ class FromLists cols rows where
317327 -- error if the dimensions do not match.
318328 fromLists :: [[e]] -> Matrix e cols rows
319329
320−instance FromLists () () where
330+instance FromLists Void Void where
331+ fromLists [] = Empty
332+ fromLists _ = error "Wrong dimensions"
333+
334+instance {-# OVERLAPPING #-} FromLists () () where
321335 fromLists [[e]] = One e
322336 fromLists _ = error "Wrong dimensions"
323337
324−instance (FromLists cols ()) => FromLists (Either () cols) () where
338+instance {-# OVERLAPPING #-} (FromLists cols ()) => FromLists (Either () cols) () where
325339 fromLists [h : t] = Join (One h) (fromLists [t])
326340 fromLists _ = error "Wrong dimensions"
327341
328−instance {-# OVERLAPPABLE #-}
329− (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
342+instance {-# OVERLAPPABLE #-} (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
330343 fromLists [l] =
331344 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
332345 in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
333346 fromLists _ = error "Wrong dimensions"
334347
335−instance (FromLists () rows) => FromLists () (Either () rows) where
348+instance {-# OVERLAPPING #-} (FromLists () rows) => FromLists () (Either () rows) where
336349 fromLists ([h] : t) = Fork (One h) (fromLists t)
337350 fromLists _ = error "Wrong dimensions"
338351
339−instance {-# OVERLAPPABLE #-}
340− (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
341− fromLists l@([_] : _) =
352+instance {-# OVERLAPPABLE #-} (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
353+ fromLists l@([_] : _) =
342354 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
343355 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
344356 fromLists _ = error "Wrong dimensions"
345357
346−instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
347− => FromLists (Either a b) (Either c d) where
358+instance {-# OVERLAPPABLE #-} (FromLists (Either a b) c, FromLists (Either a b) d, Countable c) => FromLists (Either a b) (Either c d) where
348359 fromLists l@(h : t) =
349360 let lh = length h
350361 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
@@ -371,15 +382,16 @@ matrixBuilder' f =
371382matrixBuilder ::
372383 forall e a b.
373384 ( FLN a b,
385+ CountableN b,
374386 Enum a,
375387 Enum b,
376388 Bounded a,
377389 Bounded b,
378390 Eq a,
379− Countable b
391+ CountableDimsN a b
380392 ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
381393matrixBuilder f =
382− let r = fromInteger $ natVal (Proxy :: Proxy (Count b))
394+ let r = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
383395 positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
384396 in fromLists . map (map f) . transpose . buildList r $ positions
385397 where
@@ -435,12 +447,30 @@ fromF ::
435447 ) =>
436448 (a -> b) ->
437449 Matrix e (Normalize a) (Normalize b)
438−fromF = fromF'
450+fromF f =
451+ let minA = minBound @a
452+ maxA = maxBound @a
453+ minB = minBound @b
454+ maxB = maxBound @b
455+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
456+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
457+ elementsA = take ccols [minA .. maxA]
458+ elementsB = take rrows [minB .. maxB]
459+ combinations = (,) <$> elementsA <*> elementsB
460+ combAp = map snd . sort . map (\(a, b) -> if f a == b
461+ then ((fromEnum a, fromEnum b), 1)
462+ else ((fromEnum a, fromEnum b), 0)) $ combinations
463+ mList = buildList combAp rrows
464+ in tr $ fromLists mList
465+ where
466+ buildList [] _ = []
467+ buildList l r = take r l : buildList (drop r l) r
439468
440469-- Conversion
441470
442471-- | Converts a matrix to a list of lists of elements.
443472toLists :: Matrix e cols rows -> [[e]]
473+toLists Empty = []
444474toLists (One e) = [[e]]
445475toLists (Fork l r) = toLists l ++ toLists r
446476toLists (Join l r) = zipWith (++) (toLists l) (toLists r)
@@ -492,12 +522,13 @@ iden = matrixBuilder' (bool 0 1 . uncurry (==))
492522-- This definition takes advantage of divide-and-conquer and fusion laws
493523-- from LAoP.
494524comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
525+comp Empty Empty = Empty
495526comp (One a) (One b) = One (a * b)
496527comp (Join a b) (Fork c d) = comp a c + comp b d -- Divide-and-conquer law
497528comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
498529comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
499530{-# NOINLINE comp #-}
500−{-# RULES
531+{-# RULES
501532 "comp/iden1" forall m. comp m iden = m ;
502533 "comp/iden2" forall m. comp iden m = m
503534#-}
@@ -507,6 +538,7 @@ comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
507538infixl 7 .|
508539-- | Scalar multiplication of matrices.
509540(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
541+(.|) _ Empty = Empty
510542(.|) e (One a) = One (e * a)
511543(.|) e (Join a b) = Join (e .| a) (e .| b)
512544(.|) e (Fork a b) = Fork (e .| a) (e .| b)
@@ -516,6 +548,7 @@ infixl 7 .|
516548infixl 7 ./
517549-- | Scalar multiplication of matrices.
518550(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
551+(./) Empty _ = Empty
519552(./) (One a) e = One (a / e)
520553(./) (Join a b) e = Join (a ./ e) (b ./ e)
521554(./) (Fork a b) e = Fork (a ./ e) (b ./ e)
@@ -544,38 +577,24 @@ i2 = tr p2
544577
545578-- | Obtain the number of rows.
546579--
547−-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
548−-- dimensions in constant time. For a version that doesn't require the
549−-- constraint see 'rows''.
580+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
581+-- dimensions in constant time.
582+--
583+-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
584+-- exchange for performance.
550585rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
551586rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
552587
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−
562588-- | Obtain the number of columns.
589+--
590+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
591+-- dimensions in constant time.
563592--
564−-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
565−-- dimensions in constant time. For a version that doesn't require the
566−-- constraint see 'columns''.
593+-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
594+-- exchange for performance.
567595columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
568596columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
569597
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−
579598-- Coproduct Bifunctor
580599
581600infixl 5 -|-
@@ -630,7 +649,7 @@ sndM = matrixBuilder' f
630649-- | Khatri Rao Matrix product also known as matrix pairing.
631650--
632651-- NOTE: That this is not a true categorical product, see for instance:
633−--
652+--
634653-- @
635654-- | fstM . kr a b == a
636655-- kr a b ==> |
@@ -666,7 +685,7 @@ infixl 4 ><
666685 FL (Normalize (m, n)) n,
667686 FL (Normalize (p, q)) p,
668687 FL (Normalize (p, q)) q
669− )
688+ )
670689 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
671690(><) a b =
672691 let fstM' = fstM @e @m @n
@@ -684,6 +703,7 @@ infixl 4 ><
684703-- @
685704abideJF :: Matrix e cols rows -> Matrix e cols rows
686705abideJF (Join (Fork a c) (Fork b d)) = Fork (Join (abideJF a) (abideJF b)) (Join (abideJF c) (abideJF d)) -- Join-Fork abide law
706+abideJF Empty = Empty
687707abideJF (One e) = One e
688708abideJF (Join a b) = Join (abideJF a) (abideJF b)
689709abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
@@ -697,6 +717,7 @@ abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
697717-- @
698718abideFJ :: Matrix e cols rows -> Matrix e cols rows
699719abideFJ (Fork (Join a b) (Join c d)) = Join (Fork (abideFJ a) (abideFJ c)) (Fork (abideFJ b) (abideFJ d)) -- Fork-Join abide law
720+abideFJ Empty = Empty
700721abideFJ (One e) = One e
701722abideFJ (Join a b) = Join (abideFJ a) (abideFJ b)
702723abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
@@ -705,6 +726,7 @@ abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
705726
706727-- | Matrix transposition.
707728tr :: Matrix e cols rows -> Matrix e rows cols
729+tr Empty = Empty
708730tr (One e) = One e
709731tr (Join a b) = Fork (tr a) (tr b)
710732tr (Fork a b) = Join (tr a) (tr b)
@@ -843,6 +865,7 @@ prettyPrint = putStrLn . pretty
843865
844866-- | Zip two matrices with a given binary function
845867zipWithM :: (e -> f -> g) -> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
868+zipWithM _ Empty Empty = Empty
846869zipWithM f (One a) (One b) = One (f a b)
847870zipWithM f (Join a b) (Join c d) = Join (zipWithM f a c) (zipWithM f b d)
848871zipWithM f (Fork a b) (Fork c d) = Fork (zipWithM f a c) (zipWithM f b d)
@@ -867,12 +890,14 @@ fromBool False = nat 0
867890
868891-- | Relational negation
869892negateM :: Relation cols rows -> Relation cols rows
893+negateM Empty = Empty
870894negateM (One (Nat p)) = One (Nat (negate p))
871895negateM (Join a b) = Join (negateM a) (negateM b)
872896negateM (Fork a b) = Fork (negateM a) (negateM b)
873897
874898-- | Relational addition
875899orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
900+orM Empty Empty = Empty
876901orM (One a) (One b) = One (fromBool (toBool a || toBool b))
877902orM (Join a b) (Join c d) = Join (orM a c) (orM b d)
878903orM (Fork a b) (Fork c d) = Fork (orM a c) (orM b d)
@@ -881,6 +906,7 @@ orM x@(Join _ _) y@(Fork _ _) = orM (abideJF x) y
881906
882907-- | Relational multiplication
883908andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
909+andM Empty Empty = Empty
884910andM (One a) (One b) = One (fromBool (toBool a && toBool b))
885911andM (Join a b) (Join c d) = Join (andM a c) (andM b d)
886912andM (Fork a b) (Fork c d) = Fork (andM a c) (andM b d)
@@ -889,6 +915,7 @@ andM x@(Join _ _) y@(Fork _ _) = andM (abideJF x) y
889915
890916-- | Relational subtraction
891917subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
918+subM Empty Empty = Empty
892919subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
893920subM (Join a b) (Join c d) = Join (subM a c) (subM b d)
894921subM (Fork a b) (Fork c d) = Fork (subM a c) (subM b d)
@@ -897,6 +924,7 @@ subM x@(Join _ _) y@(Fork _ _) = subM (abideJF x) y
897924
898925-- | Matrix relational composition.
899926compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
927+compRel Empty Empty = Empty
900928compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
901929compRel (Join a b) (Fork c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
902930compRel (Fork a b) c = Fork (compRel a c) (compRel b c) -- Fork fusion law
@@ -904,6 +932,7 @@ compRel c (Join a b) = Join (compRel c a) (compRel c b) -- Join fusion
904932
905933-- | Matrix relational right division
906934divR :: Relation b c -> Relation b a -> Relation a c
935+divR Empty Empty = Empty
907936divR (One a) (One b) = One (fromBool (not (toBool b) || toBool a)) -- b implies a
908937divR (Join a b) (Join c d) = andM (divR a c) (divR b d)
909938divR (Fork a b) c = Fork (divR a c) (divR b c)
@@ -931,7 +960,24 @@ fromFRel' ::
931960 ) =>
932961 (a -> b) ->
933962 Relation cols rows
934−fromFRel' = fromF'
963+fromFRel' f =
964+ let minA = minBound @a
965+ maxA = maxBound @a
966+ minB = minBound @b
967+ maxB = maxBound @b
968+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count cols))
969+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count rows))
970+ elementsA = take ccols [minA .. maxA]
971+ elementsB = take rrows [minB .. maxB]
972+ combinations = (,) <$> elementsA <*> elementsB
973+ combAp = map snd . sort . map (\(a, b) -> if f a == b
974+ then ((fromEnum a, fromEnum b), nat 1)
975+ else ((fromEnum a, fromEnum b), nat 0)) $ combinations
976+ mList = buildList combAp rrows
977+ in tr $ fromLists mList
978+ where
979+ buildList [] _ = []
980+ buildList l r = take r l : buildList (drop r l) r
935981
936982-- | Lifts functions to relations with dimensions matching @a@ and @b@
937983-- cardinality's.
@@ -939,11 +985,28 @@ fromFRel ::
939985 forall a b.
940986 ( Liftable Boolean a b,
941987 CountableDimsN a b,
942− FLN b a
988+ FL (Normalize b) (Normalize a)
943989 ) =>
944990 (a -> b) ->
945991 Relation (Normalize a) (Normalize b)
946−fromFRel = fromFRel'
992+fromFRel f =
993+ let minA = minBound @a
994+ maxA = maxBound @a
995+ minB = minBound @b
996+ maxB = maxBound @b
997+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
998+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
999+ elementsA = take ccols [minA .. maxA]
1000+ elementsB = take rrows [minB .. maxB]
1001+ combinations = (,) <$> elementsA <*> elementsB
1002+ combAp = map snd . sort . map (\(a, b) -> if f a == b
1003+ then ((fromEnum a, fromEnum b), nat 1)
1004+ else ((fromEnum a, fromEnum b), nat 0)) $ combinations
1005+ mList = buildList combAp rrows
1006+ in tr $ fromLists mList
1007+ where
1008+ buildList [] _ = []
1009+ buildList l r = take r l : buildList (drop r l) r
9471010
9481011-- | Lifts a relation function to a Boolean Matrix
9491012toRel ::
@@ -953,17 +1016,17 @@ toRel ::
9531016 Enum a,
9541017 Enum b,
9551018 Eq b,
956− CountableDims a b,
1019+ CountableDimsN a b,
9571020 FLN b a
958− )
1021+ )
9591022 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
9601023toRel f =
9611024 let minA = minBound @a
9621025 maxA = maxBound @a
9631026 minB = minBound @b
9641027 maxB = maxBound @b
965− ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
966− rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
1028+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
1029+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
9671030 elementsA = take ccols [minA .. maxA]
9681031 elementsB = take rrows [minB .. maxB]
9691032 combinations = (,) <$> elementsA <*> elementsB
src/LAoP/Matrix/Nat.hs+4
@@ -67,6 +67,7 @@ module LAoP.Matrix.Nat
6767 TrivialP,
6868
6969 -- * Primitives
70+ empty,
7071 one,
7172 join,
7273 fork,
@@ -179,6 +180,9 @@ type TrivialP a b = I.FromNat (a * b) ~ I.FromNat (I.Count (I.FromNat a) *
179180
180181-- Primitives
181182
183+empty :: Matrix e 0 0
184+empty = M I.Empty
185+
182186one :: e -> Matrix e 1 1
183187one = M . I.One
184188
src/LAoP/Matrix/Type.hs+35−42
@@ -71,6 +71,7 @@ module LAoP.Matrix.Type
7171 One,
7272
7373 -- * Primitives
74+ empty,
7475 one,
7576 join,
7677 fork,
@@ -104,8 +105,8 @@ module LAoP.Matrix.Type
104105 multM,
105106
106107 -- * Selective equivalent instance function
107− selectM,
108−
108+ selectM,
109+
109110 -- * Monad equivalent instance function
110111 returnM,
111112 bindM,
@@ -113,9 +114,7 @@ module LAoP.Matrix.Type
113114 -- * Misc
114115 -- ** Get dimensions
115116 columns,
116− columns',
117117 rows,
118− rows',
119118
120119 -- ** Matrix Transposition
121120 tr,
@@ -230,6 +229,10 @@ type One = ()
230229
231230-- Primitives
232231
232+-- | Empty matrix constructor
233+empty :: Matrix e Zero Zero
234+empty = M I.Empty
235+
233236-- | Unit matrix constructor
234237one :: e -> Matrix e One One
235238one = M . I.One
@@ -267,7 +270,7 @@ infixl 2 ===
267270-- Functor hierarchy
268271
269272-- | Functor instance equivalent function
270−fmapM ::
273+fmapM ::
271274 ( Liftable e a b,
272275 CountableDimsN a b,
273276 FLN b a
@@ -281,7 +284,7 @@ unitM :: (Num e) => Matrix e () ()
281284unitM = one 1
282285
283286-- | Applicative instance equivalent 'unit' function,
284−multM ::
287+multM ::
285288 ( CountableDimsN a b,
286289 CountableN (a, b),
287290 Num e,
@@ -292,8 +295,8 @@ multM ::
292295multM = kr
293296
294297-- | Monad instance equivalent 'return' function,
295−returnM ::
296− forall e a .
298+returnM ::
299+ forall e a .
297300 ( Num e,
298301 Enum e,
299302 Enum a,
@@ -333,7 +336,7 @@ matrixBuilder ::
333336 Bounded a,
334337 Bounded b,
335338 Eq a,
336− Countable b
339+ CountableDimsN a b
337340 ) => ((a, b) -> e) -> Matrix e a b
338341matrixBuilder f = M (I.matrixBuilder f)
339342
@@ -370,7 +373,7 @@ fromF = M . I.fromF
370373-- | Lifts relation functions to Boolean Matrix
371374toRel ::
372375 ( Liftable (Natural 0 1) a b,
373− CountableDims a b,
376+ CountableDimsN a b,
374377 FLN b a
375378 ) => (a -> b -> Bool) -> Matrix (Natural 0 1) a b
376379toRel = M . I.toRel
@@ -389,7 +392,7 @@ toList (M m) = I.toList m
389392
390393-- | The zero matrix. A matrix wholly filled with zeros.
391394zeros ::
392− (Num e, FLN cols rows, CountableDimsN cols rows)
395+ (Num e, FLN cols rows, CountableDimsN cols rows)
393396 => Matrix e cols rows
394397zeros = M I.zeros
395398
@@ -399,7 +402,7 @@ zeros = M I.zeros
399402--
400403-- Also known as T (Top) matrix.
401404ones ::
402− (Num e, FLN cols rows, CountableDimsN cols rows)
405+ (Num e, FLN cols rows, CountableDimsN cols rows)
403406 => Matrix e cols rows
404407ones = M I.ones
405408
@@ -408,7 +411,7 @@ ones = M I.ones
408411-- | The constant matrix constructor. A matrix wholly filled with a given
409412-- value.
410413constant ::
411− (Num e, FLN cols rows, CountableDimsN cols rows)
414+ (Num e, FLN cols rows, CountableDimsN cols rows)
412415 => e -> Matrix e cols rows
413416constant = M . I.constant
414417
@@ -422,7 +425,7 @@ bang ::
422425bang = M I.bang
423426
424427-- | Point constant relation
425−point ::
428+point ::
426429 ( Bounded a,
427430 Enum a,
428431 Eq a,
@@ -451,7 +454,7 @@ iden = M I.iden
451454comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
452455comp (M a) (M b) = M (I.comp a b)
453456{-# NOINLINE comp #-}
454−{-# RULES
457+{-# RULES
455458 "comp/iden1" forall m. comp m iden = m ;
456459 "comp/iden2" forall m. comp iden m = m
457460#-}
@@ -516,34 +519,24 @@ i2 = tr p2
516519
517520-- | Obtain the number of rows.
518521--
519−-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
520−-- dimensions in constant time. For a version that doesn't require the
521−-- constraint see 'rows''.
522+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
523+-- dimensions in constant time.
524+--
525+-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
526+-- exchange for performance.
522527rows :: (CountableN rows) => Matrix e cols rows -> Int
523528rows (M m) = I.rows m
524529
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−
532530-- | Obtain the number of columns.
531+--
532+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
533+-- dimensions in constant time.
533534--
534−-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
535−-- dimensions in constant time. For a version that doesn't require the
536−-- constraint see 'columns''.
535+-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
536+-- exchange for performance.
537537columns :: (CountableN cols) => Matrix e cols rows -> Int
538538columns (M m) = I.columns m
539539
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−
547540-- Coproduct Bifunctor
548541
549542infixl 5 -|-
@@ -565,7 +558,7 @@ infixl 5 -|-
565558-- Khatri Rao Product and projections
566559
567560-- | Khatri Rao product first component projection matrix.
568−fstM ::
561+fstM ::
569562 forall e m k .
570563 ( Num e,
571564 CountableDimsN m k,
@@ -576,7 +569,7 @@ fstM ::
576569fstM = M (I.fstM @e @(I.Normalize m) @(I.Normalize k))
577570
578571-- | Khatri Rao product second component projection matrix.
579−sndM ::
572+sndM ::
580573 forall e m k.
581574 ( Num e,
582575 CountableDimsN k m,
@@ -589,9 +582,9 @@ sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
589582-- | Khatri Rao Matrix product also known as matrix pairing.
590583--
591584-- NOTE: That this is not a true categorical product, see for instance:
592−--
585+--
593586-- @
594−-- | fstM . kr a b == a
587+-- | fstM . kr a b == a
595588-- kr a b ==> |
596589-- | sndM . kr a b == b
597590-- @
@@ -637,7 +630,7 @@ infixl 4 ><
637630-- Matrix abide Join Fork
638631
639632-- | Matrix "abiding" followin the 'Join'-'Fork' abide law.
640−--
633+--
641634-- Law:
642635--
643636-- @
@@ -649,7 +642,7 @@ abideJF (M m) = M (I.abideJF m)
649642-- Matrix abide Fork Join
650643
651644-- | Matrix "abiding" followin the 'Fork'-'Join' abide law.
652−--
645+--
653646-- Law:
654647--
655648-- @
@@ -668,7 +661,7 @@ tr (M m) = M (I.tr m)
668661
669662-- | Selective functors 'select' operator equivalent inspired by the
670663-- ArrowMonad solution presented in the paper.
671−selectM ::
664+selectM ::
672665 ( Num e,
673666 FLN b b,
674667 CountableN b
src/LAoP/Relation.hs+1
@@ -41,6 +41,7 @@ module LAoP.Relation
4141 TrivialP,
4242
4343 -- * Primitives
44+ empty,
4445 one,
4546 join,
4647 (|||),
src/LAoP/Relation/Internal.hs+15−13
@@ -52,6 +52,7 @@ module LAoP.Relation.Internal
5252 TrivialP,
5353
5454 -- * Primitives
55+ empty,
5556 one,
5657 join,
5758 (|||),
@@ -228,6 +229,10 @@ type One = ()
228229
229230-- Primitives
230231
232+-- | Empty matrix constructor
233+empty :: Relation Zero Zero
234+empty = R I.Empty
235+
231236-- | Unit matrix constructor
232237one :: Boolean -> Relation One One
233238one = R . I.One
@@ -283,7 +288,7 @@ relationBuilder ::
283288 Bounded a,
284289 Bounded b,
285290 Eq a,
286− CountableDims a b
291+ CountableDimsN a b
287292 ) => ((a, b) -> Boolean) -> Relation a b
288293relationBuilder = R . I.matrixBuilder
289294
@@ -301,7 +306,7 @@ fromF' f = R (I.fromFRel' f)
301306
302307-- | Lifts functions to matrices with dimensions matching @a@ and @b@
303308-- cardinality's.
304−fromF ::
309+fromF ::
305310 ( Liftable a b,
306311 CountableDimsN a b,
307312 FLN b a
@@ -312,7 +317,7 @@ fromF f = R (I.fromFRel f)
312317-- | Lifts relation functions to 'Relation'
313318toRel ::
314319 ( Liftable a b,
315− CountableDims a b,
320+ CountableDimsN a b,
316321 FLN b a
317322 )
318323 => (a -> b -> Bool) -> Relation a b
@@ -348,7 +353,7 @@ toBool r = case toList r of
348353-- | Power transpose.
349354--
350355-- Maps a relation to a set valued function.
351−pt ::
356+pt ::
352357 ( Liftable a b,
353358 Eq a,
354359 CountableDimsN a b,
@@ -365,7 +370,7 @@ belongs ::
365370 ( Bounded a,
366371 Enum a,
367372 Eq a,
368− CountableDims (List a) a,
373+ CountableDimsN (List a) a,
369374 FLN a (List a)
370375 )
371376 => Relation (List a) a
@@ -378,7 +383,7 @@ belongs = toRel elemR
378383-- | The zero relation. A relation where no element of type @a@ relates
379384-- with elements of type @b@.
380385--
381−-- Also known as ⊥ (Bottom) Relation.
386+-- Also known as ⊥ (Bottom) Relation or empty Relation.
382387--
383388-- @
384389-- r `.` ⊥ == ⊥ `.` r == ⊥
@@ -413,7 +418,7 @@ bang ::
413418bang = ones
414419
415420-- | Point constant relation
416−point ::
421+point ::
417422 ( Bounded a,
418423 Enum a,
419424 Eq a,
@@ -927,7 +932,6 @@ untrans s = fstR . conv (splitR (conv s) sndR)
927932predR ::
928933 ( Bounded a,
929934 Enum a,
930− Countable a,
931935 CountableN a,
932936 FLN a a,
933937 FLN Bool a
@@ -955,25 +959,23 @@ equalizer f g = id `intersection` divisionF f g
955959-- @
956960-- 'guard' p = 'i2' ``overriddenBy`` 'i1' `.` 'predR' p
957961-- @
958−guard ::
962+guard ::
959963 ( Bounded b,
960964 Enum b,
961965 CountableN b,
962− Countable b,
963966 FLN b b,
964967 FLN Bool b
965968 ) => Relation b Bool -> Relation b (Either b b)
966969guard p = conv (eitherR (predR p) (predR (negate p)))
967970
968971-- | Relational McCarthy's conditional.
969−cond ::
972+cond ::
970973 ( Bounded b,
971974 Enum b,
972− Countable b,
973975 CountableN b,
974976 FLN b b,
975977 FLN Bool b
976− )
978+ )
977979 => Relation b Bool -> Relation b c -> Relation b c -> Relation b c
978980cond p r s = eitherR r s . guard p
979981
stack-8.8.3.yamldeleted−1
@@ -1 +0,0 @@
1−resolver: lts-15.5
stack.yaml+66−1
@@ -1 +1,66 @@
1−resolver: lts-14.3
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
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 @@
66packages: []
77snapshots:
88- completed:
9− size: 523878
10− url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/14/3.yaml
11− sha256: 470c46c27746a48c7c50f829efc0cf00112787a7804ee4ac7a27754658f6d92c
12− original: lts-14.3
9+ size: 524789
10+ url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/14/18.yaml
11+ sha256: 646be71223e08234131c6989912e6011e01b9767bc447b6d466a35e14360bdf2
12+ original: lts-14.18
test/Spec.hs+1−7
@@ -1,8 +1,2 @@
1−module Main (main) where
2−
3−import qualified Examples.Readme as RD
4−
51main :: IO ()
6−main = do
7− putStrLn ("Testing if README works" :: String)
8− RD.exec
2+main = putStrLn "Test suite not yet implemented"