Local similarity sequence alignment: The Smith-Waterman algorithm
> module SmithWaterman 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:
> sw_alignments alg inpX inpY = axiom loc_align where

>   (nil, d, i, r, dx, ix, h) = alg
>
>   skip_right a b    = a
>   skip_left  a b    = b
>   empty     (i,j)   = [()]
>
>   loc_align = tabulated (
>                alignment                                    |||
>                skip_right <<<           loc_align ~~- ybase |||
>                skip_left  <<< xbase -~~ loc_align           ... h)
>
>   alignment = tabulated (
>                nil ><< empty                         |||
>                r   <<< xbase -~~ alignment ~~- ybase |||
>                d   <<< xbase -~~ xDel                |||
>                i   <<<           xIns      ~~- ybase ... h)
>
>   xDel      = tabulated (
>                alignment             |||
>                dx <<< xbase -~~ xDel ... h )
>
>   xIns      = tabulated (
>                alignment             |||
>                ix <<< xIns ~~- ybase ... h )
Bind input:
>   infixl 7  -~~, ~~-
>   (_, _, xbase, ybase, _, _, _, (-~~), (~~-), tabulated)
>     = bindParserCombinators inpX inpY
Enumeration algebra:
> enum :: SmithWaterman_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 :: SmithWaterman_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 :: SmithWaterman_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 :: SmithWaterman_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 SmithWaterman_Algebra alphabet answer = (
>   )
Algebra cross product:
> infix ***
> (***) :: Eq answer1 =>
> 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: