Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit b1a2b85

Browse files
author
Patrick Thomson
committed
Merge remote-tracking branch 'origin/master' into machines-to-streaming
2 parents 9f20899 + 6c31189 commit b1a2b85

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

semantic.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ library
272272
-- Serialization
273273
, Serializing.Format
274274
, Serializing.SExpression
275+
, Serializing.SExpression.Precise
275276
, Tags.Taggable
276277
, Tags.Tagging
277278
-- Custom Prelude
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{-# LANGUAGE DataKinds, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, OverloadedStrings, ScopedTypeVariables, TypeApplications, TypeFamilies, TypeOperators, UndecidableInstances #-}
2+
module Serializing.SExpression.Precise
3+
( serializeSExpression
4+
) where
5+
6+
import Data.ByteString.Builder
7+
import Data.Foldable (fold)
8+
import Data.List (intersperse)
9+
import Data.Text (Text)
10+
import GHC.Generics
11+
12+
serializeSExpression :: ToSExpression t => t -> Builder
13+
serializeSExpression t = toSExpression t 0 <> "\n"
14+
15+
16+
nl :: Int -> Builder
17+
nl n | n <= 0 = ""
18+
| otherwise = "\n"
19+
20+
pad :: Int -> Builder
21+
pad n = stringUtf8 (replicate (2 * n) ' ')
22+
23+
24+
class ToSExpression t where
25+
toSExpression :: t -> Int -> Builder
26+
27+
instance (ToSExpressionWithStrategy strategy t, strategy ~ ToSExpressionStrategy t) => ToSExpression t where
28+
toSExpression = toSExpressionWithStrategy @strategy undefined
29+
30+
31+
data Strategy = Generic | Show
32+
33+
type family ToSExpressionStrategy t :: Strategy where
34+
ToSExpressionStrategy Text = 'Show
35+
ToSExpressionStrategy _ = 'Generic
36+
37+
class ToSExpressionWithStrategy (strategy :: Strategy) t where
38+
toSExpressionWithStrategy :: proxy strategy -> t -> Int -> Builder
39+
40+
instance Show t => ToSExpressionWithStrategy 'Show t where
41+
toSExpressionWithStrategy _ t _ = stringUtf8 (show t)
42+
43+
instance (Generic t, GToSExpression (Rep t)) => ToSExpressionWithStrategy 'Generic t where
44+
toSExpressionWithStrategy _ t n = nl n <> pad n <> "(" <> fold (intersperse " " (gtoSExpression (from t) n)) <> ")"
45+
46+
47+
class GToSExpression f where
48+
gtoSExpression :: f (Int -> Builder) -> (Int -> [Builder])
49+
50+
instance GToSExpression f => GToSExpression (M1 D d f) where
51+
gtoSExpression = gtoSExpression . unM1
52+
53+
instance (GToSExpression f, GToSExpression g) => GToSExpression (f :+: g) where
54+
gtoSExpression (L1 l) = gtoSExpression l
55+
gtoSExpression (R1 r) = gtoSExpression r
56+
57+
instance (Constructor c, GToSExpression f) => GToSExpression (M1 C c f) where
58+
gtoSExpression m n = stringUtf8 (conName m) : gtoSExpression (unM1 m) (n + 1)
59+
60+
instance (GToSExpression f, GToSExpression g) => GToSExpression (f :*: g) where
61+
gtoSExpression (l :*: r) = gtoSExpression l <> gtoSExpression r
62+
63+
instance GToSExpression U1 where
64+
gtoSExpression _ _ = []
65+
66+
instance GToSExpression f => GToSExpression (M1 S s f) where
67+
gtoSExpression = gtoSExpression . unM1 -- FIXME: show the selector name, if any
68+
69+
instance ToSExpression k => GToSExpression (K1 R k) where
70+
gtoSExpression k = pure . toSExpression (unK1 k)

test/Data/Functor/Listable.hs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,6 @@ liftCons4 :: [Tier a] -> [Tier b] -> [Tier c] -> [Tier d] -> (a -> b -> c -> d -
104104
liftCons4 tiers1 tiers2 tiers3 tiers4 f = mapT (uncurry4 f) (tiers1 >< tiers2 >< tiers3 >< tiers4) `addWeight` 1
105105
where uncurry4 f (a, (b, (c, d))) = f a b c d
106106

107-
-- | Lifts a quinary constructor to a list of tiers, given lists of tiers for its arguments.
108-
--
109-
-- Commonly used in the definition of 'Listable1' and 'Listable2' instances.
110-
_liftCons5 :: [Tier a] -> [Tier b] -> [Tier c] -> [Tier d] -> [Tier e] -> (a -> b -> c -> d -> e -> f) -> [Tier f]
111-
_liftCons5 tiers1 tiers2 tiers3 tiers4 tiers5 f = mapT (uncurry5 f) (tiers1 >< tiers2 >< tiers3 >< tiers4 >< tiers5) `addWeight` 1
112-
where uncurry5 f (a, (b, (c, (d, e)))) = f a b c d e
113-
114107
-- | Lifts a senary constructor to a list of tiers, given lists of tiers for its arguments.
115108
--
116109
-- Commonly used in the definition of 'Listable1' and 'Listable2' instances.

0 commit comments

Comments
 (0)