1 {-# LANGUAGE DerivingStrategies #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE StandaloneDeriving #-}
4 {-# OPTIONS_GHC -fno-warn-orphans #-}
5 module Symantic.Parser.Haskell.View where
8 import Data.Function (($), (.))
10 import Data.Semigroup (Semigroup(..))
11 import Data.String (IsString(..), String)
13 import Text.Show (Show(..), ShowS, shows, showParen, showString)
14 import qualified Data.Function as Fun
16 import Symantic.Parser.Grammar.Fixity
17 import qualified Symantic.Parser.Haskell.Optimize as H
20 newtype ViewTerm a = ViewTerm { unViewTerm :: ViewTermInh -> ShowS }
22 instance IsString (ViewTerm a) where
23 fromString s = ViewTerm $ \_inh -> showString s
25 -- ** Type 'ViewTermInh'
28 { viewTermInh_op :: (Infix, Side)
29 , viewTermInh_pair :: Pair
30 , viewTermInh_lamDepth :: Int
33 pairViewTerm :: ViewTermInh -> Infix -> ShowS -> ShowS
34 pairViewTerm inh op s =
35 if isPairNeeded (viewTermInh_op inh) op
36 then showString o . s . showString c
38 where (o,c) = viewTermInh_pair inh
40 instance Show (ViewTerm a) where
41 showsPrec p v = unViewTerm v ViewTermInh
42 { viewTermInh_op = (infixN p, SideL)
43 , viewTermInh_pair = pairParen
44 , viewTermInh_lamDepth = 1
46 instance Show (H.Term repr a) where
47 showsPrec p = showsPrec p . go
49 go :: forall b. H.Term repr b -> ViewTerm b
53 (H.:.) H.:@ f H.:@ g -> ViewTerm $ \inh ->
54 pairViewTerm inh op Fun.$
55 unViewTerm (go f) inh{viewTermInh_op=op} Fun..
56 showString " . " Fun..
57 unViewTerm (go g) inh{viewTermInh_op=op}
62 H.Char t -> ViewTerm $ \_inh ->
67 H.Char t -> ViewTerm $ \_inh -> shows t
68 H.Cons H.:@ x H.:@ xs -> ViewTerm $ \inh ->
69 pairViewTerm inh op Fun.$
70 unViewTerm (go x) inh{viewTermInh_op=(op, SideL)} Fun..
71 showString " : " Fun..
72 unViewTerm (go xs) inh{viewTermInh_op=(op, SideR)}
75 H.Eq H.:@ x H.:@ y -> ViewTerm $ \inh ->
76 pairViewTerm inh op Fun.$
77 unViewTerm (go x) inh{viewTermInh_op=(op, SideL)} Fun..
78 showString " == " Fun..
79 unViewTerm (go y) inh{viewTermInh_op=(op, SideR)}
81 H.Eq H.:@ x -> ViewTerm $ \inh ->
83 unViewTerm (go x) inh{viewTermInh_op=(op, SideL)} Fun..
87 H.Var v -> fromString v
88 H.Lam1 f -> viewLam "u" f
89 H.Lam f -> viewLam "x" f
90 f H.:@ x -> ViewTerm $ \inh ->
92 unViewTerm (go f) inh{viewTermInh_op = (op, SideL) } .
93 -- showString " :@ " .
95 unViewTerm (go x) inh{viewTermInh_op = (op, SideR) }
103 viewLam :: forall b c. String -> (H.Term repr b -> H.Term repr c) -> ViewTerm (b -> c)
104 viewLam v f = ViewTerm $ \inh ->
105 pairViewTerm inh op $
106 let x = v<>show (viewTermInh_lamDepth inh) in
107 -- showString "Lam1 (" .
108 showString "\\" . showString x . showString " -> " .
109 (unViewTerm (go (f (H.Var x))) inh
110 { viewTermInh_op = (op, SideL)
111 , viewTermInh_lamDepth = viewTermInh_lamDepth inh + 1