Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I suppose it's also possible to implement it using type classes. To avoid circular functional dependencies, we declare that the types of vectors and matrices depend on the scalar type. While it requires to have a <code>newtype</code> for scalars as well as for their corresponding vectors and matrices, it has also some advantages. In particular, we don't need to keep the constraint <code>Num a</code> at the declaration of <code>mmul</code> and <code>vmul</code>. We can leave it up to instance implementations what constraints they impose on their scalar values.</p> <pre><code>{-# LANGUAGE TypeFamilies #-} class MatrixNxN a where data Matrix a :: * data Vector a :: * -- | Multiplication of two N-by-N-element matrices mmul :: Matrix a -&gt; Matrix a -&gt; Matrix a -- | Multiplication of an N-by-N-element matrix -- and an N-element column vector vmul :: Matrix a -&gt; Vector a -&gt; Vector a -- List matrices on any kind of numbers: newtype ListScalar a = ListScalar a instance Num a =&gt; MatrixNxN (ListScalar a) where newtype Matrix (ListScalar a) = ListMatrix [[a]] newtype Vector (ListScalar a) = ListVector [a] vmul (ListMatrix mss) (ListVector vs) = ... mmul (ListMatrix m1ss) (ListMatrix m2ss) = ... -- We can have matrices that have no `Num` instance for -- their scalars, like Z2 implemented as `Bool`: newtype ListBool = ListBool Bool instance MatrixNxN ListBool where newtype Matrix ListBool = ListBoolMatrix [[Bool]] newtype Vector ListBool = ListBoolVector [Bool] vmul (ListBoolMatrix mss) (ListBoolVector vs) = ... mmul (ListBoolMatrix m1ss) (ListBoolMatrix m2ss) = ... </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload