[21] | 1 | {-# LANGUAGE FlexibleInstances #-} |
---|
| 2 | |
---|
[20] | 3 | module Ops where |
---|
| 4 | |
---|
[21] | 5 | import qualified Data.Supply as S |
---|
| 6 | import Prelude (String, Int, ($), (.)) |
---|
| 7 | import qualified Prelude |
---|
[20] | 8 | import Lambda |
---|
[21] | 9 | |
---|
| 10 | class (Lambda l) => LOps l where |
---|
| 11 | infixop :: String -> Int -> (a -> b -> c) -> l a -> l b -> l c |
---|
[23] | 12 | infixopr :: String -> Int -> (a -> b -> c) -> l a -> l b -> l c |
---|
| 13 | infixopl :: String -> Int -> (a -> b -> c) -> l a -> l b -> l c |
---|
[21] | 14 | fun :: String -> a -> l a |
---|
| 15 | |
---|
| 16 | (+) :: l Int -> l Int -> l Int |
---|
[23] | 17 | (+) = infixopl "+" 6 (Prelude.+) |
---|
[21] | 18 | |
---|
| 19 | (-) :: l Int -> l Int -> l Int |
---|
[23] | 20 | (-) = infixopl "-" 6 (Prelude.-) |
---|
[21] | 21 | |
---|
| 22 | (*) :: l Int -> l Int -> l Int |
---|
[23] | 23 | (*) = infixopl "*" 7 (Prelude.*) |
---|
[21] | 24 | |
---|
| 25 | |
---|
| 26 | instance LOps Q where |
---|
[23] | 27 | infixopl _ _ f lhs rhs = Q (eval lhs `f` eval rhs) |
---|
| 28 | infixop _ _ f lhs rhs = Q (eval lhs `f` eval rhs) |
---|
| 29 | infixopr _ _ f lhs rhs = Q (eval lhs `f` eval rhs) |
---|
[21] | 30 | fun _ = Q |
---|
| 31 | |
---|
| 32 | instance LOps S where |
---|
| 33 | fun name _ = S (\_ p -> Prelude.showsPrec p name) |
---|
| 34 | |
---|
[23] | 35 | infixopl name prec _ lhs rhs = S(\s p -> |
---|
[21] | 36 | let (s1, s2) = S.split2 s |
---|
| 37 | in Prelude.showParen (p Prelude.> prec) $ |
---|
| 38 | unS lhs s1 prec . |
---|
| 39 | Prelude.showString name . |
---|
| 40 | unS rhs s2 (Prelude.succ prec) |
---|
| 41 | ) |
---|
[23] | 42 | infixop name prec _ lhs rhs = S(\s p -> |
---|
| 43 | let (s1, s2) = S.split2 s |
---|
| 44 | in Prelude.showParen (p Prelude.> prec) $ |
---|
| 45 | unS lhs s1 (Prelude.succ prec) . |
---|
| 46 | Prelude.showString name . |
---|
| 47 | unS rhs s2 (Prelude.succ prec) |
---|
| 48 | ) |
---|
| 49 | infixopr name prec _ lhs rhs = S(\s p -> |
---|
| 50 | let (s1, s2) = S.split2 s |
---|
| 51 | in Prelude.showParen (p Prelude.> prec) $ |
---|
| 52 | unS lhs s1 (Prelude.succ prec) . |
---|
| 53 | Prelude.showString name . |
---|
| 54 | unS rhs s2 prec |
---|
| 55 | ) |
---|