1 {-# LANGUAGE UndecidableInstances #-}
3 module Symantic.Syntaxes.Extras where
5 import Data.Function qualified as Fun
7 import Debug.Trace (trace)
8 import Symantic.Semantics.Identity
9 import Symantic.Semantics.LetInserter
10 import Symantic.Semantics.Viewer
11 import Symantic.Semantics.Viewer.Fixity
12 import Symantic.Syntaxes.Classes (Abstractable (..), Instantiable (..))
13 import Symantic.Syntaxes.Derive
14 import Text.Show (show, showString)
15 import Prelude qualified
18 class Addable sem where
19 add :: sem (Int -> Int -> Int)
21 FromDerived Addable sem =>
22 sem (Int -> Int -> Int)
31 (+) x y = add .@ x .@ y
32 instance Addable sem => Addable (OpenCode sem)
33 instance Addable sem => Addable (AnnotatedCode sem)
34 instance Addable sem => Addable (LetInserter sem)
35 instance Addable Viewer where
36 add = ViewerInfix (infixB SideL 6) "(+)" "+"
37 instance Addable Identity where
38 add = lam (\x -> lam (x Prelude.+))
41 class Logable sem where
42 log :: Prelude.String -> sem a -> sem a
44 FromDerived1 Logable sem =>
48 log msg = liftDerived1 (log msg)
49 instance Logable sem => Logable (OpenCode sem)
50 instance Logable sem => Logable (AnnotatedCode sem)
51 instance Logable sem => Logable (LetInserter sem)
52 instance Logable Viewer where
53 log msg a = Viewer (\_env -> showString "log " Fun.. showString (show msg)) .@ a
54 instance Logable Identity where
55 log msg = Identity Fun.. trace msg Fun.. runIdentity
57 -- * Type 'Substractable'
58 class Substractable sem where
59 substract :: sem (Int -> Int -> Int)
61 FromDerived Substractable sem =>
62 sem (Int -> Int -> Int)
63 substract = liftDerived substract
64 infixl 6 `substract`, -
71 (-) x y = substract .@ x .@ y
72 instance Substractable sem => Substractable (OpenCode sem)
73 instance Substractable sem => Substractable (AnnotatedCode sem)
74 instance Substractable sem => Substractable (LetInserter sem)
75 instance Substractable Viewer where
76 substract = ViewerInfix (infixB SideL 6) "(-)" "-"
77 instance Substractable Identity where
78 substract = lam (\x -> lam (x Prelude.-))
80 -- * Type Multiplicable
81 class Multiplicable sem where
82 multiply :: sem (Int -> Int -> Int)
84 FromDerived Multiplicable sem =>
85 sem (Int -> Int -> Int)
86 multiply = liftDerived multiply
87 infixl 7 `multiply`, *
94 (*) x y = multiply .@ x .@ y
95 instance Multiplicable sem => Multiplicable (OpenCode sem)
96 instance Multiplicable sem => Multiplicable (AnnotatedCode sem)
97 instance Multiplicable sem => Multiplicable (LetInserter sem)
98 instance Multiplicable Viewer where
99 multiply = ViewerInfix (infixB SideL 7) "(*)" "*"
100 instance Multiplicable Identity where
101 multiply = lam (\x -> lam (x Prelude.*))
104 class Divisible i sem where
105 divide :: sem (i -> i -> i)
107 FromDerived (Divisible i) sem =>
109 divide = liftDerived divide
117 (/) x y = divide .@ x .@ y
118 instance Divisible i sem => Divisible i (OpenCode sem)
119 instance Divisible i sem => Divisible i (AnnotatedCode sem)
120 instance Divisible i sem => Divisible i (LetInserter sem)
121 instance Divisible i Viewer where
122 divide = ViewerInfix (infixB SideL 7) "(/)" "/"
123 instance Prelude.Fractional i => Divisible i Identity where
124 divide = lam (\x -> lam (x Prelude./))