1 {-# OPTIONS_GHC -XModalTypes -ddump-types -XNoMonoPatBinds -XFlexibleContexts #-}
4 import GHC.HetMet.CodeTypes hiding ((-))
5 import Prelude hiding ( id, (.) )
7 --------------------------------------------------------------------------------
10 -- This shows how to build a two-level program one step at a time by
11 -- slowly rearranging it until the brackets can be inserted.
14 -- a one-level function to compute the dot product of two vectors
15 dotproduct :: [Int] -> [Int] -> Int
22 (a*b)+(dotproduct ax bx)
24 -- A slightly modified version of the dot product: note that we
25 -- check for zeroes and ones to avoid multiplying. In a one-level
26 -- program this yields no advantage, however!
27 dotproduct' :: [Int] -> [Int] -> Int
33 (b:bx) -> (dotproduct' ax bx)
36 (b:bx) -> b+(dotproduct' ax bx)
40 (a*b)+(dotproduct' ax bx)
42 -- A two-level version of the dot product. Note how we ask for the first
43 -- vector, then produce a program which is optimized for multiplying
44 -- by that particular vector. If there are zeroes or ones in the
45 -- original vector, we will emit code which is faster than a one-level
48 dotproduct'' :: forall g.
49 GuestLanguageAdd g Integer =>
50 GuestLanguageMult g Integer =>
51 GuestIntegerLiteral g =>
52 [Integer] -> <[ [Integer] -> Integer ]>@g
56 (0:ax) -> <[ \v2 -> case v2 of
58 (b:bx) -> ~~(dotproduct'' ax) bx ]>
59 (1:ax) -> <[ \v2 -> case v2 of
61 (b:bx) -> b + ~~(dotproduct'' ax) bx ]>
63 (a:ax) -> <[ \v2 -> case v2 of
65 (b:bx) -> ~~(guestIntegerLiteral a) * b + ~~(dotproduct'' ax) bx ]>