Canonical sequence alignments
The following grammar is a variant of Gotoh's, restricted to canonical alignments. (Canonical alignments avoid redundant re-arrangements of gaps, thereby reducing the search space and making the consideration of near-optimal solutions more meaningful.). Read the full description in Systematic Dynamic Programming in Bioinformatics.
```> module Canon where

> import Array
> import List
> import TTCombinators
```
The signature:
```> data Alignment = Nil                    |
>                  D  Char Alignment      |
>                  I       Alignment Char |
>                  R  Char Alignment Char |
>                  Dx Char Alignment      |
>                  Ix      Alignment Char
>                                            deriving (Eq, Show)
```
The yield grammar:
```> canon_alignments alg inpX inpY = axiom alignment where
>   (nil, d, i, r, dx, ix, h) = alg
>
>   alignment = tabulated (
>                match                          |||
>                d <<< xbase -~~ xDel           |||
>                i <<<           xIns ~~- ybase ... h )
>
>   xDel      = tabulated (
>                match                           |||
>                dx <<< xbase -~~ xDel           |||
>                i  <<<           xIns ~~- ybase ... h )
>
>   xIns      = tabulated (
>                match                 |||
>                ix <<< xIns ~~- ybase ... h )
>
>   match     = tabulated (
>                nil ><< empty                         |||
>                r   <<< xbase -~~ alignment ~~- ybase ... h)
```
Bind input:
```>   infixl 7  -~~, ~~-
>   (_, _, xbase, ybase, empty, _, _, (-~~), (~~-), tabulated)
>     = bindParserCombinators inpX inpY
```
Enumeration algebra:
```> enum :: Canon_Algebra Char Alignment
> enum = (nil, d, i, r, dx, ix, h) where
>    nil = Nil
>    d   = D
>    i   = I
>    r   = R
>    dx  = Dx
>    ix  = Ix
>    h   = id
```
Pretty printing algebra:
```> prettyprint :: Canon_Algebra Char (String, String)
> prettyprint = (nil, d, i, r, dx, ix, h) where
>   nil          = ("","")
>   d  x (l,r)   = (x:l, open:r)
>   i    (l,r) y = (open:l, y:r)
>   r  x (l,r) y = (x:l,y:r)
>   dx x (l,r) 	 = (x:l, extend:r)
>   ix   (l,r) y = (extend:l, y:r)
>   h            = id
>   open         = '='
>   extend	 = '-'
```
Counting Algebra:
```> count :: Canon_Algebra Char Int
> count = (nil, d, i, r, dx, ix, h) where
>    nil     = 1
>    d x s   = s
>    i s y   = s
>    r a s b = s
>    dx x s  = s
>    ix s y  = s
>    h []    = []
>    h l     = [sum l]
```
Affine gap score algebra:
```> affine :: Canon_Algebra Char Int
> affine = (nil, d, i, r, dx, ix, h) where
>    nil     = 0
>    d x s   = s + open + extend
>    i s y   = s + open + extend
>    r a s b = s + w a b
>    dx x s  = s + extend
>    ix s y  = s + extend
>    h []    = []
>    h l     = [maximum l]

>  -- simple definitions for open, extend and w:
>    open   = (-15)
>    extend = (-1)
>    w a b  = if a==b then 4 else -3
```
Algebra type:
```> type Canon_Algebra alphabet answer = (
>   answer,                                   -- nil
>   alphabet -> answer ->             answer, -- d
>               answer -> alphabet -> answer, -- i
>   alphabet -> answer -> alphabet -> answer, -- r
>   alphabet -> answer ->             answer, -- dx
>               answer -> alphabet -> answer, -- ix
>   )
```
Algebra cross product:
```> infix ***
> (***) :: Eq answer1 =>
>          Canon_Algebra alphabet answer1 ->
>          Canon_Algebra alphabet answer2 ->
> alg1 *** alg2 = (nil, d, i, r, dx, ix, h) where
>    (nil1, d1, i1, r1, dx1, ix1, h1) = alg1
>    (nil2, d2, i2, r2, dx2, ix2, h2) = alg2
>
>    nil            = (nil1, nil2)
>    d  x (s1,s2)   = (d1 x s1, d2 x s2)
>    i    (s1,s2) y = (i1 s1 y, i2 s2 y)
>    r  x (s1,s2) y = (r1 x s1 y, r2 x s2 y)
>    dx x (s1,s2)   = (dx1 x s1, dx2 x s2)
>    ix   (s1,s2) y = (ix1 s1 y, ix2 s2 y)
>
>    h xs = [(x1,x2)| x1 <- nub \$ h1 [ y1 | (y1,y2) <- xs],
>                     x2 <-       h2 [ y2 | (y1,y2) <- xs, y1 == x1]]
```
For usage on your local machine: