1 {-# LANGUAGE FlexibleInstances #-}
2 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE TypeFamilies #-}
5 module TFHOE.Repr.String where
7 -- import Data.Functor.Identity (Identity)
8 import Data.Monoid ((<>))
9 import Data.String (IsString(..))
13 -- * Type 'Repr_String'
15 -- | 'String' interpreter.
16 newtype Repr_String (fun:: * -> *) h
19 -- Inherited attributes:
21 -> Repr_String_Lambda_Depth
22 -- Synthetised attributes:
25 type Repr_String_Lambda_Depth = Int
27 string_repr :: Repr_String fun h -> String
28 string_repr r = unRepr_String r precedence_Toplevel 0
30 string_repr_any :: ty h -> Repr_String fun h -> Either err String
31 string_repr_any _ty = return . string_repr
33 instance Show (Repr_String fun a) where
36 instance Expr_Fun fun (Repr_String fun) where
37 type Fun_from_Repr (Repr_String fun) = fun
38 app (Repr_String f) (Repr_String x) = Repr_String $ \p v ->
39 let p' = precedence_App in
41 f p' v <> " " <> x p' v
42 inline = repr_string_fun "!"
43 val = repr_string_fun ""
44 lazy = repr_string_fun "~"
46 let_inline = repr_string_let "!"
47 let_val = repr_string_let ""
48 let_lazy = repr_string_let "~"
50 -- ** Instance 'Fun' helpers
51 repr_string_fun :: String -> (Repr_String fun a2 -> Repr_String fun a1) -> Repr_String fun a
52 repr_string_fun mode e =
54 let p' = precedence_Fun in
55 let x = "x" <> show v in
57 "\\" <> mode <> x <> " -> " <>
58 unRepr_String (e (Repr_String $ \_p _v -> x)) p' (succ v)
62 -> (Repr_String fun a3 -> Repr_String fun a2)
64 repr_string_let mode e in_ =
66 let p' = precedence_Let in
67 let x = "x" <> show v in
69 "let" <> mode <> " " <> x <> " = " <> unRepr_String e p (succ v) <> " in " <>
70 unRepr_String (in_ (Repr_String $ \_p _v -> x)) p (succ v)
72 instance Expr_Bool (Repr_String fun) where
73 bool a = Repr_String $ \_p _v -> show a
76 let p' = precedence_Neg in
77 paren p p' $ "!" <> x (precedence_succ p') v
78 and (Repr_String x) (Repr_String y) =
80 let p' = precedence_And in
81 paren p p' $ x p' v <> " & " <> y p' v
82 or (Repr_String x) (Repr_String y) =
84 let p' = precedence_Or in
85 paren p p' $ x p' v <> " | " <> y p' v
86 {-xor (Repr_String x) (Repr_String y) =
88 let p' = precedence_Xor in
89 paren p p' $ x p' v <> " * " <> y p' v
91 instance Expr_Int (Repr_String fun) where
92 int a = Repr_String $ \_p _v -> show a
93 add (Repr_String x) (Repr_String y) =
95 let p' = precedence_Add in
96 paren p p' $ x p' v <> " + " <> y p' v
98 instance Expr_If Repr_String where
103 Repr_String $ \p v ->
104 let p' = precedence_If in
106 "if " <> cond p' v <>
107 " then " <> ok p' v <>
109 when_ (Repr_String cond) (Repr_String ok) =
110 Repr_String $ \p v ->
111 let p' = precedence_If in
113 "when " <> cond p' v <>
117 -- ** Type 'Precedence'
119 newtype Precedence = Precedence Int
120 deriving (Eq, Ord, Show)
121 precedence_pred :: Precedence -> Precedence
122 precedence_pred (Precedence p) = Precedence (pred p)
123 precedence_succ :: Precedence -> Precedence
124 precedence_succ (Precedence p) = Precedence (succ p)
125 paren :: (Monoid s, IsString s) => Precedence -> Precedence -> s -> s
128 then fromString "(" <> x <> fromString ")"
131 precedence_Toplevel :: Precedence
132 precedence_Toplevel = Precedence 0
133 precedence_Fun :: Precedence
134 precedence_Fun = Precedence 1
135 precedence_Let :: Precedence
136 precedence_Let = Precedence 2
137 precedence_If :: Precedence
138 precedence_If = Precedence 3
139 precedence_Or :: Precedence
140 precedence_Or = Precedence 4
141 precedence_Add :: Precedence
142 precedence_Add = precedence_Or
143 precedence_Xor :: Precedence
144 precedence_Xor = Precedence 5
145 precedence_And :: Precedence
146 precedence_And = Precedence 6
147 precedence_App :: Precedence
148 precedence_App = Precedence 7
149 precedence_Neg :: Precedence
150 precedence_Neg = Precedence 8
151 precedence_Atomic :: Precedence
152 precedence_Atomic = Precedence 9