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

Commit c14a100

Browse files
committed
workaround for polymorphic record update not supported by MicroHs
turns missing field update into a runtime undefined error instead of a compile-time type error; so be sure to update both the `query` and `pureValue` of the `Pattern` when using `polymorphic`. see also: <augustss/MicroHs#190>
1 parent 5bb3d0f commit c14a100

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

tidal-core/src/Sound/Tidal/Pattern.hs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ instance Functor Pattern where
6969

7070
instance (NFData a) => NFData (Pattern a)
7171

72+
polymorphic :: Pattern a -> Pattern b
73+
polymorphic = fmap (const undefined)
74+
75+
polymorphicEvent :: Event a -> Event b
76+
polymorphicEvent = fmap (const undefined)
77+
7278
pattern_ :: (State -> [Event a]) -> Pattern a
7379
pattern_ f = Pattern f Nothing Nothing
7480

@@ -233,7 +239,7 @@ instance Monad Pattern where
233239
--
234240
-- TODO - what if a continuous pattern contains a discrete one, or vice-versa?
235241
unwrap :: Pattern (Pattern a) -> Pattern a
236-
unwrap pp = pp {query = q, pureValue = Nothing}
242+
unwrap pp = (polymorphic pp) {query = q, pureValue = Nothing}
237243
where
238244
q st =
239245
concatMap
@@ -250,7 +256,7 @@ unwrap pp = pp {query = q, pureValue = Nothing}
250256
-- | Turns a pattern of patterns into a single pattern. Like @unwrap@,
251257
-- but structure only comes from the inner pattern.
252258
innerJoin :: Pattern (Pattern b) -> Pattern b
253-
innerJoin pp' = pp' {query = q, pureValue = Nothing}
259+
innerJoin pp' = (polymorphic pp') {query = q, pureValue = Nothing}
254260
where
255261
q st =
256262
concatMap
@@ -266,7 +272,7 @@ innerJoin pp' = pp' {query = q, pureValue = Nothing}
266272
-- | Turns a pattern of patterns into a single pattern. Like @unwrap@,
267273
-- but structure only comes from the outer pattern.
268274
outerJoin :: Pattern (Pattern a) -> Pattern a
269-
outerJoin pp = pp {query = q, pureValue = Nothing}
275+
outerJoin pp = (polymorphic pp) {query = q, pureValue = Nothing}
270276
where
271277
q st =
272278
concatMap
@@ -285,7 +291,7 @@ outerJoin pp = pp {query = q, pureValue = Nothing}
285291
-- TODO - what if a continuous pattern contains a discrete one, or vice-versa?
286292
-- TODO - steps
287293
squeezeJoin :: Pattern (Pattern a) -> Pattern a
288-
squeezeJoin pp = pp {query = q, pureValue = Nothing}
294+
squeezeJoin pp = (polymorphic pp) {query = q, pureValue = Nothing}
289295
where
290296
q st =
291297
concatMap
@@ -619,7 +625,7 @@ withQueryControls f pat = pat {query = query pat . (\(State a m) -> State a (f m
619625
-- | @withEvent f p@ returns a new @Pattern@ with each event mapped over
620626
-- function @f@.
621627
withEvent :: (Event a -> Event b) -> Pattern a -> Pattern b
622-
withEvent f p = p {query = map f . query p, pureValue = Nothing}
628+
withEvent f p = (polymorphic p) {query = map f . query p, pureValue = Nothing}
623629

624630
-- | @withEvent f p@ returns a new @Pattern@ with each value mapped over
625631
-- function @f@.
@@ -629,7 +635,7 @@ withValue f pat = withEvent (fmap f) pat
629635
-- | @withEvent f p@ returns a new @Pattern@ with f applied to the resulting list of events for each query
630636
-- function @f@.
631637
withEvents :: ([Event a] -> [Event b]) -> Pattern a -> Pattern b
632-
withEvents f p = p {query = f . query p, pureValue = Nothing}
638+
withEvents f p = (polymorphic p) {query = f . query p, pureValue = Nothing}
633639

634640
-- | @withPart f p@ returns a new @Pattern@ with function @f@ applied
635641
-- to the part.
@@ -848,7 +854,7 @@ rev p =
848854
-- | Mark values in the first pattern which match with at least one
849855
-- value in the second pattern.
850856
matchManyToOne :: (b -> a -> Bool) -> Pattern a -> Pattern b -> Pattern (Bool, b)
851-
matchManyToOne f pa pb = pa {query = q, pureValue = Nothing}
857+
matchManyToOne f pa pb = (polymorphic pa) {query = q, pureValue = Nothing}
852858
where
853859
q st = map match $ query pb st
854860
where
@@ -1299,7 +1305,7 @@ groupEventsBy f (e : es) = eqs : groupEventsBy f (es \\ eqs)
12991305
-- assumes that all events in the list have same whole/part
13001306
collectEvent :: [Event a] -> Maybe (Event [a])
13011307
collectEvent [] = Nothing
1302-
collectEvent l@(e : _) = Just $ e {context = con, value = vs}
1308+
collectEvent l@(e : _) = Just $ (polymorphicEvent e) {context = con, value = vs}
13031309
where
13041310
con = unionC $ map context l
13051311
vs = map value l
@@ -1324,7 +1330,7 @@ collect :: (Eq a) => Pattern a -> Pattern [a]
13241330
collect = collectBy sameDur
13251331

13261332
uncollectEvent :: Event [a] -> [Event a]
1327-
uncollectEvent e = [e {value = value e !! i, context = resolveContext i (context e)} | i <- [0 .. length (value e) - 1]]
1333+
uncollectEvent e = [(polymorphicEvent e) {value = value e !! i, context = resolveContext i (context e)} | i <- [0 .. length (value e) - 1]]
13281334
where
13291335
resolveContext i (Context xs) = if length xs <= i then Context [] else Context [xs !! i]
13301336

tidal-core/src/Sound/Tidal/UI.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,7 +1949,7 @@ spaceOut xs p = _slow (toRational $ sum xs) $ stack $ map (`compressArc` p) spac
19491949
--
19501950
-- > d1 $ n ("[0,4,7] [-12,-8,-5]") # s "superpiano" # sustain 2
19511951
flatpat :: Pattern [a] -> Pattern a
1952-
flatpat p = p {query = concatMap (\(Event c b b' xs) -> map (Event c b b') xs) . query p, pureValue = Nothing}
1952+
flatpat p = (polymorphic p) {query = concatMap (\(Event c b b' xs) -> map (Event c b b') xs) . query p, pureValue = Nothing}
19531953

19541954
-- | @layer@ takes a list of 'Pattern'-returning functions and a seed element,
19551955
-- stacking the result of applying the seed element to each function in the list.
@@ -2878,7 +2878,7 @@ squeeze _ [] = silence
28782878
squeeze ipat pats = squeezeJoin $ (pats !!!) <$> ipat
28792879

28802880
squeezeJoinUp :: Pattern ControlPattern -> ControlPattern
2881-
squeezeJoinUp pp = pp {query = q, pureValue = Nothing}
2881+
squeezeJoinUp pp = (polymorphic pp) {query = q, pureValue = Nothing}
28822882
where
28832883
q st = concatMap (f st) (query (filterDigital pp) st)
28842884
f st (Event c (Just w) p v) =

0 commit comments

Comments
 (0)