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