1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE ImplicitPrelude #-}
5 {-# LANGUAGE LambdaCase #-}
6 {-# LANGUAGE MultiParamTypeClasses #-}
7 {-# LANGUAGE OverloadedStrings #-}
8 {-# LANGUAGE PatternSynonyms #-}
9 {-# LANGUAGE ScopedTypeVariables #-}
10 {-# LANGUAGE TypeApplications #-}
11 {-# LANGUAGE TypeFamilies #-}
12 {-# LANGUAGE UndecidableInstances #-} -- For Show (SomeData a)
13 module Symantic.Typed.View where
18 import qualified Data.Function as Fun
19 import qualified Prelude
21 import Symantic.Parser.Grammar.Fixity
22 import Symantic.Typed.Lang
23 import Symantic.Typed.Data
24 import Symantic.Typed.Trans
27 View :: (ViewEnv -> ShowS) -> View a
28 ViewUnifix :: Unifix -> String -> String -> View (a -> b)
29 ViewInfix :: Infix -> String -> String -> View (a -> b -> c)
30 ViewApp :: View (b -> a) -> View b -> View a
32 runView :: View a -> ViewEnv -> ShowS
33 runView (View v) env = v env
34 runView (ViewInfix _op name _infixName) _env = showString name
35 runView (ViewUnifix _op name _unifixName) _env = showString name
36 runView (ViewApp f x) env =
38 runView f env{viewEnv_op = (op, SideL) } Fun..
40 runView x env{viewEnv_op = (op, SideR) }
43 type instance Output View = View
44 instance Trans View View where
47 instance IsString (View a) where
48 fromString s = View Fun.$ \_env -> showString s
49 instance Show (View a) where
50 showsPrec p = (`runView` ViewEnv
51 { viewEnv_op = (infixN p, SideL)
52 , viewEnv_pair = pairParen
53 , viewEnv_lamDepth = 1
55 instance Show (SomeData View a) where
56 showsPrec p (SomeData x) = showsPrec p (trans @_ @View x)
60 { viewEnv_op :: (Infix, Side)
61 , viewEnv_pair :: Pair
62 , viewEnv_lamDepth :: Int
65 pairView :: ViewEnv -> Infix -> ShowS -> ShowS
67 if isPairNeeded (viewEnv_op env) op
68 then showString o Fun.. s Fun.. showString c
70 where (o,c) = viewEnv_pair env
72 instance Abstractable View where
75 lam1 f = viewLam "u" f
76 ViewInfix op _name infixName .@ ViewApp x y = View Fun.$ \env ->
78 runView x env{viewEnv_op=(op, SideL)} Fun..
79 showString " " Fun.. showString infixName Fun.. showString " " Fun..
80 runView y env{viewEnv_op=(op, SideR)}
81 ViewInfix op name _infixName .@ x = View Fun.$ \env ->
82 showParen Prelude.True Fun.$
83 runView x env{viewEnv_op=(op, SideL)} Fun..
84 showString " " Fun.. showString name
86 viewLam :: String -> (View a -> View b) -> View (a -> b)
87 viewLam varPrefix f = View Fun.$ \env ->
89 let x = showString varPrefix Fun..
90 showsPrec 0 (viewEnv_lamDepth env) in
91 -- showString "Lam1 (" .
92 showString "\\" Fun.. x Fun.. showString " -> " Fun..
93 runView (f (View (\_env -> x))) env
94 { viewEnv_op = (op, SideL)
95 , viewEnv_lamDepth = Prelude.succ (viewEnv_lamDepth env)
100 instance Anythingable View
101 instance Bottomable View where
103 instance Show c => Constantable c View where
104 constant c = View Fun.$ \_env -> shows c
105 instance Eitherable View where
108 instance Equalable View where
109 equal = ViewInfix (infixN 4) "(==)" "=="
110 instance Listable View where
111 cons = ViewInfix (infixR 5) "(:)" ":"
113 instance Maybeable View where