forked from DCMLab/protovoices-haskell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parse.hs
986 lines (910 loc) · 36.4 KB
/
Parse.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE TypeFamilies #-}
{- | This module contains code that is specific to parsing the protovoice grammar.
It implements a number of evaluators ('Eval') that can be used with the various parsers.
-}
module PVGrammar.Parse
( -- * Generic Parsing
-- | Evaluators that directly return protovoice operations.
-- They can be embedded into a semiring using 'mapEvalScore'.
IsNote
, protoVoiceEvaluator
, protoVoiceEvaluatorImpure
, protoVoiceEvaluatorNoRepSplit
, protoVoiceEvaluatorLimitedSize
-- * Parsing Derivations
, pvDerivUnrestricted
, pvDerivRightBranch
-- * Counting Parses
, pvCountUnrestricted
, pvCountNoRepSplit
, pvCountNoRepSplitRightBranch
, pvCountNoRepSplitRightBranchSplitFirst
) where
import Common
import PVGrammar
import Musicology.Pitch
( Diatonic
, Interval (..)
, Notation
, pc
, pto
)
import Probability
import Numeric.Probability.Distribution qualified as P
import Control.Monad
import Control.DeepSeq (NFData)
-- import Control.Monad (foldM)
import Data.Foldable
( foldl'
, toList
)
import Data.HashMap.Strict qualified as HM
import Data.HashSet qualified as S
import Data.Hashable (Hashable)
import Data.Kind (Constraint, Type)
import Data.List qualified as L
import Data.Map.Strict qualified as M
import Data.Maybe
( catMaybes
, mapMaybe
, maybeToList
)
import GHC.Generics (Generic)
import Internal.MultiSet qualified as MS
import Musicology.Core
( HasPitch (..)
, Pitch
, Pitched (..)
, isStep
)
import System.Random.Stateful (initStdGen, newIOGenM, StatefulGen, IOGenM, StdGen)
import Control.Monad.Trans.Maybe
import Control.Monad.Trans (lift)
-- helper type: Either for terminal and non-terminal edges
-- -------------------------------------------------------
{- | A tag that distinguishes between objects related to terminal and non-terminal edges.
Like 'Either', but with semantic constructor names to avoid confusion.
-}
data EdgeEither a b
= -- | marks an terminal edge (or some related object)
Reg !a
| -- | marks a non-terminal edge (or some related object)
Pass !b
deriving (Eq, Ord, Show, Generic, Hashable, NFData)
-- helper type: enum for possible operations
-- -----------------------------------------
{- | A tag that distinguishes four different types of operations:
regular split, passing split, left ornament, and right ornament
-}
data Elaboration a b c d
= -- | marks a terminal split
EReg !a
| -- | marks a non-terminal split
EPass !b
| -- | marks a right ornament
ER !c
| -- | marks a left ornament
EL !d
deriving (Eq, Ord, Show, Generic, Hashable, NFData)
{- | Takes a collection of 'Elaboration'
and splits it into lists for each elaboration type.
-}
partitionElaborations
:: Foldable t => t (Elaboration a b c d) -> ([a], [b], [c], [d])
partitionElaborations = foldl' select ([], [], [], [])
where
select (a, b, c, d) (EReg t) = (t : a, b, c, d)
select (a, b, c, d) (EPass n) = (a, n : b, c, d)
select (a, b, c, d) (ER l) = (a, b, l : c, d)
select (a, b, c, d) (EL r) = (a, b, c, r : d)
-- parsing Ornamentations
-- ======================
-- | A constraint alias for note types.
type IsNote :: Type -> Constraint
type IsNote n =
(HasPitch n, Diatonic (ICOf (IntervalOf n)), Eq (ICOf (IntervalOf n)))
-- | Checks if the middle pitch is between the left and the right pitch.
between
:: (Eq i, Interval i)
=> Pitch i
-- ^ left pitch
-> Pitch i
-- ^ middle pitch
-> Pitch i
-- ^ right pitch
-> Bool
between pl pm pr =
pl /= pm && pm /= pr && pl /= pr && dir1 == odir && dir2 == odir
where
odir = direction $ pl `pto` pr
dir1 = direction $ pl `pto` pm
dir2 = direction $ pm `pto` pr
{- | Attempts to reduce three nodes using an ornamentation operation.
If succesfull, returns the ornament type and the parent edge,
which is either a non-terminal edge for passing notes,
or a terminal edge for all other operations.
-}
findOrnament
:: (IsNote n)
=> StartStop n
-> StartStop n
-> StartStop n
-> Bool
-> Bool
-> Maybe
( EdgeEither
(DoubleOrnament, Edge n)
(PassingOrnament, InnerEdge n)
)
findOrnament (Inner l) (Inner m) (Inner r) True True
| pl == pm && pm == pr = Just $ Reg (FullRepeat, (Inner l, Inner r))
| pl == pm && so = Just $ Reg (RightRepeatOfLeft, (Inner l, Inner r))
| pm == pr && so = Just $ Reg (LeftRepeatOfRight, (Inner l, Inner r))
where
pl = pc $ pitch l
pm = pc $ pitch m
pr = pc $ pitch r
so = isStep $ pl `pto` pr
findOrnament (Inner l) (Inner m) (Inner r) _ _
| pl == pr && s1 = Just $ Reg (FullNeighbor, (Inner l, Inner r))
| s1 && s2 && between pl pm pr = Just $ Pass (PassingMid, (l, r))
where
pl = pc $ pitch l
pm = pc $ pitch m
pr = pc $ pitch r
s1 = isStep $ pl `pto` pm
s2 = isStep $ pm `pto` pr
findOrnament Start (Inner _) Stop _ _ = Just $ Reg (RootNote, (Start, Stop))
findOrnament _ _ _ _ _ = Nothing
{- | Attempts to reduce three notes as a passing motion
where one of the child edges is a non-terminal edge.
Since one of the edges is a terminal edge,
the corresponding outer note could be start/stop symbol, in which case the reduction fails.
The side with the terminal edge is thus a @StartStop Pitch i@ within a 'Reg',
while the non-terminal side is a @Pitch i@ within an 'Pass'.
Exactly one side must be a 'Reg' and the other an 'Pass', otherwise the reduction fails.
-}
findPassing
:: (IsNote n)
=> EdgeEither (StartStop n) n
-> n
-> EdgeEither (StartStop n) n
-> Maybe (InnerEdge n, PassingOrnament)
findPassing (Reg (Inner l)) m (Pass r)
| isStep (pl `pto` pm) && between pl pm pr =
Just ((l, r), PassingLeft)
where
pl = pc $ pitch l
pm = pc $ pitch m
pr = pc $ pitch r
findPassing (Pass l) m (Reg (Inner r))
| isStep (pm `pto` pr) && between pl pm pr =
Just ((l, r), PassingRight)
where
pl = pc $ pitch l
pm = pc $ pitch m
pr = pc $ pitch r
findPassing _ _ _ = Nothing
findRightOrnament :: (IsNote n) => n -> n -> Maybe RightOrnament
findRightOrnament l m
| pl == pm = Just RightRepeat
| isStep (pl `pto` pm) = Just RightNeighbor
| otherwise = Nothing
where
pl = pc $ pitch l
pm = pc $ pitch m
findLeftOrnament :: (IsNote n) => n -> n -> Maybe LeftOrnament
findLeftOrnament m r
| pm == pr = Just LeftRepeat
| isStep (pm `pto` pr) = Just LeftNeighbor
| otherwise = Nothing
where
pm = pc $ pitch m
pr = pc $ pitch r
-- evaluator interface
-- ===================
{- | The evaluator that represents the proto-voice grammar.
As scores it returns a representation of each operation.
These scores do not form a semiring,
but can be embedded into different semirings using 'mapEvalScore'.
-}
protoVoiceEvaluator
:: (Foldable t, Foldable t2, Eq n, Ord n, IsNote n, Notation n, Hashable n)
=> Eval (Edges n) (t (Edge n)) (Notes n) (t2 n) (PVLeftmost n)
protoVoiceEvaluator =
mkLeftmostEval
pvUnspreadMiddle
pvUnspreadLeft
pvUnspreadRight
pvUnsplit
pvThaw
pvSlice
protoVoiceEvaluatorImpure
:: (Foldable t, Foldable t2, Eq n, Ord n, IsNote n, Notation n, Hashable n)
=> EvalImpure (Edges n) (t (Edge n)) (Notes n) (t2 n) (PVLeftmost n)
protoVoiceEvaluatorImpure =
mkLeftmostEvalImpure
pvUnspreadMiddle
pvUnspreadLeft
pvUnspreadRight
pvUnsplit
pvUnsplit'
pvThaw
pvSlice
{- | Computes the verticalization (unspread) of a middle transition.
If the verticalization is admitted, returns the corresponding operation.
-}
pvUnspreadMiddle
:: (Eq n, Ord n, Hashable n, IsNote n)
=> UnspreadMiddle (Edges n) (Notes n) (Spread n)
pvUnspreadMiddle (Notes nl, edges, Notes nr)
| any notARepetition (edgesReg edges) = Nothing
| otherwise = Just (Notes top, op)
where
notARepetition (p1, p2) = fmap (pc . pitch) p1 /= fmap (pc . pitch) p2
top = MS.maxUnion nl nr
leftMS = nl MS.\\ nr
left = HM.fromList $ fmap ToLeft <$> MS.toOccurList leftMS
rightMS = nr MS.\\ nl
right = HM.fromList $ fmap ToRight <$> MS.toOccurList rightMS
bothSet =
S.intersection (MS.toSet nl) (MS.toSet nr)
`S.difference` (MS.toSet leftMS `S.union` MS.toSet rightMS)
both = S.foldl' (\m k -> HM.insert k ToBoth m) HM.empty bothSet
op = SpreadOp (left <> right <> both) edges
{- | Computes all left parent transitions for a verticalization and a left child transition.
Here, this operation is always admitted and unique,
so the edges from the child transition are just passed through.
-}
pvUnspreadLeft :: UnspreadLeft (Edges n) (Notes n)
pvUnspreadLeft (el, _) _ = [el]
{- | Computes all right parent transition for a verticalization and a right child transition.
Here, this operation is always admitted and unique,
so the edges from the child transition are just passed through.
-}
pvUnspreadRight :: UnspreadRight (Edges n) (Notes n)
pvUnspreadRight (_, er) _ = [er]
{- | Computes all possible unsplits of two child transitions.
Since transitions here only represent the certain edges,
'pvUnsplit' must also take into account unelaborated edges,
which are not present in the child transitions.
-}
pvUnsplit'
:: (IsNote n, Notation n, Ord n, Hashable n)
=> StartStop (Notes n)
-> Edges n
-> Notes n
-> Edges n
-> StartStop (Notes n)
-> IO [(Edges n, Split n)]
pvUnsplit' notesl (Edges leftRegs leftPass) (Notes notesm) (Edges rightRegs rightPass) notesr =
do
gen <- initStdGen
mgen <- newIOGenM gen
let options = getOptions mgen notesm
combinations <- collectRandomChoice mgen $ getCombinations mgen options
-- collectRandomChoice mgen $ enumerateAll mgen
-- -- pure $ map mkTop combinations
pure $ map mkTop combinations
where
-- preprocessing of the notes left and right of the unsplit
!innerL = Reg <$> innerNotes notesl
!innerR = Reg <$> innerNotes notesr
-- find all reduction options for every pitch
getOptions mgen notesm = map (noteOptions mgen) $ MS.toOccurList notesm
-- find all reduction options for every pitch
-- noteOptions
-- :: IOGenM StdGen
-- -> (n, Int)
-- -> [([(Edge n, (n, DoubleOrnament))],
-- [(InnerEdge n, (n, PassingOrnament))], [(n, (n, RightOrnament))],
-- [(n, (n, LeftOrnament))])]
noteOptions mgen (note, nocc)
| nocc < MS.size mandatoryLeft || nocc < MS.size mandatoryRight =
[]
| otherwise = do
partitionElaborations <$> enumerateOptions mgen mandatoryLeft mandatoryRight nocc
-- | nocc < MS.size mandatoryLeft || nocc < MS.size mandatoryRight =
-- []
-- | otherwise =
-- partitionElaborations
-- <$> enumerateOptions mandatoryLeft mandatoryRight nocc
where
-- compute the mandatory edges for the current pitch:
mleftRegs = S.map (Reg . fst) $ S.filter ((== Inner note) . snd) leftRegs
mleftPass = MS.map (Pass . fst) $ MS.filter ((== note) . snd) leftPass
mrightRegs = S.map (Reg . snd) $ S.filter ((== Inner note) . fst) rightRegs
mrightPass = MS.map (Pass . snd) $ MS.filter ((== note) . fst) rightPass
mandatoryLeft = MS.fromSet mleftRegs <> mleftPass
mandatoryRight = MS.fromSet mrightRegs <> mrightPass
-- the possible reductions of a (multiple) pitch are enumerated in three stages:
-- stage 1: consume all mandatory edges on the left
enumerateOptions mgen ml mr n = do
(mr', n', acc) <- MS.foldM goL (mr, n, []) ml
(n'', acc') <- MS.foldM goR (n', acc) mr'
goFree freeOptions n'' acc'
goL (_, 0, _) _ = []
goL (mr, n, acc) l = do
(new, mr') <- pickLeft n l mr
pure (mr', n - 1, new : acc)
-- combine a mandatory left with a mandatory right or free right edge
pickLeft n l mr
| n > MS.size mr = mand <> opt <> single
| otherwise = mand
where
mand = do
r <- MS.distinctElems mr
red <- maybeToList $ tryReduction True True l note r
pure (red, MS.delete r mr)
-- TODO: remove mr options here?
tryOpt r = tryReduction True (r `S.member` mrightRegs) l note r
opt = (,mr) <$> mapMaybe tryOpt innerR
single = fmap (,mr) $ maybeToList $ tryLeftReduction note l
-- stage 2: consume all remaining mandatory edges on the right
goR (0, _) _ = []
goR (n, acc) r = do
new <- pickRight r
pure (n - 1, new : acc)
-- combine mandatory right with free left edge
pickRight r = opt <> single
where
tryOpt l = tryReduction (l `S.member` mleftRegs) True l note r
opt = mapMaybe tryOpt innerL
single = maybeToList $ tryRightReduction note r
-- stage 3: explain all remaining notes through a combination of unknown edges
goFree _ 0 acc = pure acc
goFree [] _ _ = []
goFree [lastOpt] n acc = pure $ L.replicate n lastOpt <> acc
goFree (opt : opts) n acc = do
nopt <- [0 .. n]
goFree opts (n - nopt) (L.replicate nopt opt <> acc)
-- list all options for free reduction
freeOptions = pickFreeBoth <> pickFreeLeft <> pickFreeRight
-- combine two free edges
pickFreeBoth = do
l <- innerL
r <- innerR
maybeToList $
tryReduction (l `S.member` mleftRegs) (r `S.member` mrightRegs) l note r
-- reduce to left using free edge
pickFreeLeft = mapMaybe (tryLeftReduction note) innerL
-- reduce to right using free edge
pickFreeRight = mapMaybe (tryRightReduction note) innerR
-- at all stages: try out potential reductions:
-- two terminal edges: any ornament
tryReduction lIsUsed rIsUsed (Reg notel) notem (Reg noter) = do
reduction <- findOrnament notel (Inner notem) noter lIsUsed rIsUsed
pure $ case reduction of
(Reg (orn, parent)) -> EReg (parent, (notem, orn))
(Pass (pass, parent)) -> EPass (parent, (notem, pass))
-- a non-terminal edge left and a terminal edge right: passing note
tryReduction _ _ notel@(Pass _) notem noter@(Reg _) = do
(parent, pass) <- findPassing notel notem noter
pure $ EPass (parent, (notem, pass))
-- a terminal edge left and a non-terminal edge right: passing note
tryReduction _ _ notel@(Reg _) notem noter@(Pass _) = do
(parent, pass) <- findPassing notel notem noter
pure $ EPass (parent, (notem, pass))
-- all other combinations are forbidden
tryReduction _ _ _ _ _ = Nothing
-- single reduction to a left parent
tryLeftReduction notem (Reg (Inner notel)) = do
orn <- findRightOrnament notel notem
pure $ ER (notel, (notem, orn))
tryLeftReduction _ _ = Nothing
-- single reduction to a right parent
tryRightReduction notem (Reg (Inner noter)) = do
orn <- findLeftOrnament notem noter
pure $ EL (noter, (notem, orn))
tryRightReduction _ _ = Nothing
-- compute all possible combinations of reduction options
getCombinations mgen options = do
guard $ any L.null options
pickRandomChoice mgen $ foldM (pickOption mgen) ([], [], [], []) options -- otherwise, compute all combinations
-- picks all different options for a single note in the list monad
pickOption mgen (accReg, accPass, accL, accR) opts = do
(regs, pass, ls, rs) <- opts
pure (regs <> accReg, pass <> accPass, ls <> accL, rs <> accR)
-- convert a combination into a derivation operation:
-- turn the accumulated information into the format expected from the evaluator
mkTop (regs, pass, rs, ls) =
if True -- validate
then (top, SplitOp tmap ntmap rmap lmap leftRegs rightRegs passL passR)
else
error $
"invalid unsplit:\n notesl="
<> show notesl
<> "\n notesr="
<> show notesr
<> "\n notesm="
<> show (Notes notesm)
<> "\n left="
<> show (Edges leftRegs leftPass)
<> "\n right="
<> show (Edges rightRegs rightPass)
<> "\n top="
<> show top
where
-- validate =
-- all ((`L.elem` innerNotes notesl) . fst . fst) regs
-- && all ((`L.elem` innerNotes notesr) . snd . fst) regs
-- && all ((`L.elem` innerNotes notesl) . Inner . fst) rs
-- && all ((`L.elem` innerNotes notesr) . Inner . fst) ls
-- collect all operations
mapify xs = M.fromListWith (<>) $ fmap (: []) <$> xs
tmap = mapify regs
ntmap = mapify pass
lmap = mapify ls
rmap = mapify rs
top = Edges (S.fromList (fst <$> regs)) (MS.fromList (fst <$> pass))
passL = foldr MS.delete leftPass $ mapMaybe leftPassingChild pass
passR = foldr MS.delete rightPass $ mapMaybe rightPassingChild pass
leftPassingChild ((l, _r), (m, orn)) =
if orn == PassingRight then Just (l, m) else Nothing
rightPassingChild ((_l, r), (m, orn)) =
if orn == PassingLeft then Just (m, r) else Nothing
-- undefined
-- do
-- gen <- initStdGen
-- mgen <- newIOGenM gen
-- collectRandomChoice mgen $ enumerateAll mgen
-- -- pure $ map mkTop combinations
-- where
-- enumerateAll mgen = do
-- options <- evalOptions mgen
-- combinations <- evalCombinations mgen options
-- let res = map mkTop combinations
-- pickRandomChoice mgen res
-- -- pickRandomChoice mgen $
--
-- -- preprocessing of the notes left and right of the unsplit
-- !innerL = Reg <$> innerNotes notesl
-- !innerR = Reg <$> innerNotes notesr
--
-- -- find all reduction options for every pitch
-- -- evalOptions
-- -- :: StatefulGen g IO
-- -- => g
-- -- -> MaybeT IO
-- -- [([(Edge n, (n, DoubleOrnament))],
-- -- [(InnerEdge n, (n, PassingOrnament))], [(n, (n, RightOrnament))],
-- -- [(n, (n, LeftOrnament))])]
-- evalOptions mgen = do
-- pickRandomChoice mgen $ mapM (evalNoteOptions mgen) $ MS.toOccurList notesm
--
-- -- evalNoteOptions
-- -- :: StatefulGen g IO => g
-- -- -> (n, Int)
-- -- -> MaybeT IO ([(Edge n, (n, DoubleOrnament))],
-- -- [(InnerEdge n, (n, PassingOrnament))], [(n, (n, RightOrnament))],
-- -- [(n, (n, LeftOrnament))])
-- evalNoteOptions mgen (note, nocc) = do
-- guard (nocc < MS.size mandatoryLeft || nocc < MS.size mandatoryRight )
-- allOps <- enumerateOptions mgen mandatoryLeft mandatoryRight nocc
-- pickRandomChoice mgen $ partitionElaborations <$> [allOps]
-- -- pure $ partitionElaborations <$> allOps
-- where
-- -- compute the mandatory edges for the current pitch:
-- mleftRegs = S.map (Reg . fst) $ S.filter ((== Inner note) . snd) leftRegs
-- mleftPass = MS.map (Pass . fst) $ MS.filter ((== note) . snd) leftPass
-- mrightRegs = S.map (Reg . snd) $ S.filter ((== Inner note) . fst) rightRegs
-- mrightPass = MS.map (Pass . snd) $ MS.filter ((== note) . fst) rightPass
-- mandatoryLeft = MS.fromSet mleftRegs <> mleftPass
-- mandatoryRight = MS.fromSet mrightRegs <> mrightPass
--
-- -- the possible reductions of a (multiple) pitch are enumerated in three stages:
--
-- -- stage 1: consume all mandatory edges on the left
-- enumerateOptions mgen ml mr n = do
-- (mr', n', acc) <- pickRandomChoice mgen $ MS.foldM goL (mr, n, []) ml
-- (n'', acc') <- pickRandomChoice mgen $ MS.foldM goR (n', acc) mr'
-- freeOpts <- freeOptions mgen
-- goFree mgen [freeOpts] n'' acc'
-- goL (_, 0, _) _ = []
-- goL (mr, n, acc) l = do
-- (new, mr') <- pickLeft n l mr
-- pure (mr', n - 1, new : acc)
-- -- combine a mandatory left with a mandatory right or free right edge
-- pickLeft n l mr
-- | n > MS.size mr = mand <> opt <> single
-- | otherwise = mand
-- where
-- mand = do
-- r <- MS.distinctElems mr
-- red <- maybeToList $ tryReduction True True l note r
-- pure (red, MS.delete r mr)
-- -- TODO: remove mr options here?
-- tryOpt r = tryReduction True (r `S.member` mrightRegs) l note r
-- opt = (,mr) <$> mapMaybe tryOpt innerR
-- single = fmap (,mr) $ maybeToList $ tryLeftReduction note l
--
-- -- stage 2: consume all remaining mandatory edges on the right
-- goR (0, _) _ = []
-- goR (n, acc) r = do
-- new <- pickRight r
-- pure (n - 1, new : acc)
-- -- combine mandatory right with free left edge
-- pickRight r = opt <> single
-- where
-- tryOpt l = tryReduction (l `S.member` mleftRegs) True l note r
-- opt = mapMaybe tryOpt innerL
-- single = maybeToList $ tryRightReduction note r
--
-- -- stage 3: explain all remaining notes through a combination of unknown edges
-- goFree mgen _ 0 acc = pickRandomChoice mgen acc
-- goFree mgen [] _ _ = pickRandomChoice mgen []
-- goFree mgen [lastOpt] n acc = pickRandomChoice mgen $ L.replicate n lastOpt <> acc
-- goFree mgen (opt : opts) n acc = do
-- nopt <- pickRandomChoice mgen [0 .. n]
-- goFree mgen opts (n - nopt) (L.replicate nopt opt <> acc)
--
-- -- list all options for free reduction
-- freeOptions mgen = do
-- freeBoth <- pickFreeBoth mgen
-- freeLeft <- pickFreeLeft mgen
-- freeRight <- pickFreeRight mgen
-- pickRandomChoice mgen [freeBoth <> freeLeft <> freeRight]
-- -- combine two free edges
-- pickFreeBoth mgen = do
-- l <- pickRandomChoice mgen innerL
-- r <- pickRandomChoice mgen innerR
-- pickRandomChoice mgen $ maybeToList $
-- tryReduction (l `S.member` mleftRegs) (r `S.member` mrightRegs) l note r
-- -- reduce to left using free edge
-- pickFreeLeft mgen = do
-- pickRandomChoice mgen $ mapMaybe (tryLeftReduction note) innerL
-- -- reduce to right using free edge
-- pickFreeRight mgen = do
-- pickRandomChoice mgen $ mapMaybe (tryRightReduction note) innerR
--
-- -- at all stages: try out potential reductions:
--
-- -- two terminal edges: any ornament
-- tryReduction lIsUsed rIsUsed (Reg notel) notem (Reg noter) = do
-- reduction <- findOrnament notel (Inner notem) noter lIsUsed rIsUsed
-- pure $ case reduction of
-- (Reg (orn, parent)) -> EReg (parent, (notem, orn))
-- (Pass (pass, parent)) -> EPass (parent, (notem, pass))
-- -- a non-terminal edge left and a terminal edge right: passing note
-- tryReduction _ _ notel@(Pass _) notem noter@(Reg _) = do
-- (parent, pass) <- findPassing notel notem noter
-- pure $ EPass (parent, (notem, pass))
-- -- a terminal edge left and a non-terminal edge right: passing note
-- tryReduction _ _ notel@(Reg _) notem noter@(Pass _) = do
-- (parent, pass) <- findPassing notel notem noter
-- pure $ EPass (parent, (notem, pass))
-- -- all other combinations are forbidden
-- tryReduction _ _ _ _ _ = Nothing
--
-- -- single reduction to a left parent
-- tryLeftReduction notem (Reg (Inner notel)) = do
-- orn <- findRightOrnament notel notem
-- pure $ ER (notel, (notem, orn))
-- tryLeftReduction _ _ = Nothing
--
-- -- single reduction to a right parent
-- tryRightReduction notem (Reg (Inner noter)) = do
-- orn <- findLeftOrnament notem noter
-- pure $ EL (noter, (notem, orn))
-- tryRightReduction _ _ = Nothing
--
-- -- compute all possible combinations of reduction options
-- evalCombinations mgen options = do
-- guard $ any L.null options
-- pickRandomChoice mgen $ foldM (pickOption mgen) ([], [], [], []) options -- otherwise, compute all combinations
-- -- picks all different options for a single note in the list monad
-- pickOption mgen (accReg, accPass, accL, accR) opts = do
-- (regs, pass, ls, rs) <- opts
-- pure (regs <> accReg, pass <> accPass, ls <> accL, rs <> accR)
--
-- -- convert a combination into a derivation operation:
-- -- turn the accumulated information into the format expected from the evaluator
-- mkTop (regs, pass, rs, ls) =
-- if True -- validate
-- then (top, SplitOp tmap ntmap rmap lmap leftRegs rightRegs passL passR)
-- else
-- error $
-- "invalid unsplit:\n notesl="
-- <> show notesl
-- <> "\n notesr="
-- <> show notesr
-- <> "\n notesm="
-- <> show (Notes notesm)
-- <> "\n left="
-- <> show (Edges leftRegs leftPass)
-- <> "\n right="
-- <> show (Edges rightRegs rightPass)
-- <> "\n top="
-- <> show top
-- where
-- -- validate =
-- -- all ((`L.elem` innerNotes notesl) . fst . fst) regs
-- -- && all ((`L.elem` innerNotes notesr) . snd . fst) regs
-- -- && all ((`L.elem` innerNotes notesl) . Inner . fst) rs
-- -- && all ((`L.elem` innerNotes notesr) . Inner . fst) ls
--
-- -- collect all operations
-- mapify xs = M.fromListWith (<>) $ fmap (: []) <$> xs
-- tmap = mapify regs
-- ntmap = mapify pass
-- lmap = mapify ls
-- rmap = mapify rs
-- top = Edges (S.fromList (fst <$> regs)) (MS.fromList (fst <$> pass))
-- passL = foldr MS.delete leftPass $ mapMaybe leftPassingChild pass
-- passR = foldr MS.delete rightPass $ mapMaybe rightPassingChild pass
-- leftPassingChild ((l, _r), (m, orn)) =
-- if orn == PassingRight then Just (l, m) else Nothing
-- rightPassingChild ((_l, r), (m, orn)) =
-- if orn == PassingLeft then Just (m, r) else Nothing
{- | Computes all possible unsplits of two child transitions.
Since transitions here only represent the certain edges,
'pvUnsplit' must also take into account unelaborated edges,
which are not present in the child transitions.
-}
pvUnsplit
:: (IsNote n, Notation n, Ord n, Hashable n)
=> StartStop (Notes n)
-> Edges n
-> Notes n
-> Edges n
-> StartStop (Notes n)
-> [(Edges n, Split n)]
pvUnsplit notesl (Edges leftRegs leftPass) (Notes notesm) (Edges rightRegs rightPass) notesr =
map mkTop combinations
where
-- preprocessing of the notes left and right of the unsplit
!innerL = Reg <$> innerNotes notesl
!innerR = Reg <$> innerNotes notesr
-- find all reduction options for every pitch
!options = noteOptions <$> MS.toOccurList notesm
noteOptions (note, nocc)
| nocc < MS.size mandatoryLeft || nocc < MS.size mandatoryRight =
[]
| otherwise =
partitionElaborations
<$> enumerateOptions mandatoryLeft mandatoryRight nocc
where
-- compute the mandatory edges for the current pitch:
mleftRegs = S.map (Reg . fst) $ S.filter ((== Inner note) . snd) leftRegs
mleftPass = MS.map (Pass . fst) $ MS.filter ((== note) . snd) leftPass
mrightRegs = S.map (Reg . snd) $ S.filter ((== Inner note) . fst) rightRegs
mrightPass = MS.map (Pass . snd) $ MS.filter ((== note) . fst) rightPass
mandatoryLeft = MS.fromSet mleftRegs <> mleftPass
mandatoryRight = MS.fromSet mrightRegs <> mrightPass
-- the possible reductions of a (multiple) pitch are enumerated in three stages:
-- stage 1: consume all mandatory edges on the left
enumerateOptions ml mr n = do
(mr', n', acc) <- MS.foldM goL (mr, n, []) ml
(n'', acc') <- MS.foldM goR (n', acc) mr'
goFree freeOptions n'' acc'
goL (_, 0, _) _ = []
goL (mr, n, acc) l = do
(new, mr') <- pickLeft n l mr
pure (mr', n - 1, new : acc)
-- combine a mandatory left with a mandatory right or free right edge
pickLeft n l mr
| n > MS.size mr = mand <> opt <> single
| otherwise = mand
where
mand = do
r <- MS.distinctElems mr
red <- maybeToList $ tryReduction True True l note r
pure (red, MS.delete r mr)
-- TODO: remove mr options here?
tryOpt r = tryReduction True (r `S.member` mrightRegs) l note r
opt = (,mr) <$> mapMaybe tryOpt innerR
single = fmap (,mr) $ maybeToList $ tryLeftReduction note l
-- stage 2: consume all remaining mandatory edges on the right
goR (0, _) _ = []
goR (n, acc) r = do
new <- pickRight r
pure (n - 1, new : acc)
-- combine mandatory right with free left edge
pickRight r = opt <> single
where
tryOpt l = tryReduction (l `S.member` mleftRegs) True l note r
opt = mapMaybe tryOpt innerL
single = maybeToList $ tryRightReduction note r
-- stage 3: explain all remaining notes through a combination of unknown edges
goFree _ 0 acc = pure acc
goFree [] _ _ = []
goFree [lastOpt] n acc = pure $ L.replicate n lastOpt <> acc
goFree (opt : opts) n acc = do
nopt <- [0 .. n]
goFree opts (n - nopt) (L.replicate nopt opt <> acc)
-- list all options for free reduction
freeOptions = pickFreeBoth <> pickFreeLeft <> pickFreeRight
-- combine two free edges
pickFreeBoth = do
l <- innerL
r <- innerR
maybeToList $
tryReduction (l `S.member` mleftRegs) (r `S.member` mrightRegs) l note r
-- reduce to left using free edge
pickFreeLeft = mapMaybe (tryLeftReduction note) innerL
-- reduce to right using free edge
pickFreeRight = mapMaybe (tryRightReduction note) innerR
-- at all stages: try out potential reductions:
-- two terminal edges: any ornament
tryReduction lIsUsed rIsUsed (Reg notel) notem (Reg noter) = do
reduction <- findOrnament notel (Inner notem) noter lIsUsed rIsUsed
pure $ case reduction of
(Reg (orn, parent)) -> EReg (parent, (notem, orn))
(Pass (pass, parent)) -> EPass (parent, (notem, pass))
-- a non-terminal edge left and a terminal edge right: passing note
tryReduction _ _ notel@(Pass _) notem noter@(Reg _) = do
(parent, pass) <- findPassing notel notem noter
pure $ EPass (parent, (notem, pass))
-- a terminal edge left and a non-terminal edge right: passing note
tryReduction _ _ notel@(Reg _) notem noter@(Pass _) = do
(parent, pass) <- findPassing notel notem noter
pure $ EPass (parent, (notem, pass))
-- all other combinations are forbidden
tryReduction _ _ _ _ _ = Nothing
-- single reduction to a left parent
tryLeftReduction notem (Reg (Inner notel)) = do
orn <- findRightOrnament notel notem
pure $ ER (notel, (notem, orn))
tryLeftReduction _ _ = Nothing
-- single reduction to a right parent
tryRightReduction notem (Reg (Inner noter)) = do
orn <- findLeftOrnament notem noter
pure $ EL (noter, (notem, orn))
tryRightReduction _ _ = Nothing
-- compute all possible combinations of reduction options
!combinations =
if any L.null options -- check if any note has no options
then [] -- if yes, then no reduction is possible at all
else foldM pickOption ([], [], [], []) options -- otherwise, compute all combinations
-- picks all different options for a single note in the list monad
pickOption (accReg, accPass, accL, accR) opts = do
(regs, pass, ls, rs) <- opts
pure (regs <> accReg, pass <> accPass, ls <> accL, rs <> accR)
-- convert a combination into a derivation operation:
-- turn the accumulated information into the format expected from the evaluator
mkTop (regs, pass, rs, ls) =
if True -- validate
then (top, SplitOp tmap ntmap rmap lmap leftRegs rightRegs passL passR)
else
error $
"invalid unsplit:\n notesl="
<> show notesl
<> "\n notesr="
<> show notesr
<> "\n notesm="
<> show (Notes notesm)
<> "\n left="
<> show (Edges leftRegs leftPass)
<> "\n right="
<> show (Edges rightRegs rightPass)
<> "\n top="
<> show top
where
-- validate =
-- all ((`L.elem` innerNotes notesl) . fst . fst) regs
-- && all ((`L.elem` innerNotes notesr) . snd . fst) regs
-- && all ((`L.elem` innerNotes notesl) . Inner . fst) rs
-- && all ((`L.elem` innerNotes notesr) . Inner . fst) ls
-- collect all operations
mapify xs = M.fromListWith (<>) $ fmap (: []) <$> xs
tmap = mapify regs
ntmap = mapify pass
lmap = mapify ls
rmap = mapify rs
top = Edges (S.fromList (fst <$> regs)) (MS.fromList (fst <$> pass))
passL = foldr MS.delete leftPass $ mapMaybe leftPassingChild pass
passR = foldr MS.delete rightPass $ mapMaybe rightPassingChild pass
leftPassingChild ((l, _r), (m, orn)) =
if orn == PassingRight then Just (l, m) else Nothing
rightPassingChild ((_l, r), (m, orn)) =
if orn == PassingLeft then Just (m, r) else Nothing
{- | Computes all potential ways a surface transition could have been frozen.
In this grammar, this operation is unique and just turns ties into edges.
-}
pvThaw
:: (Foldable t, Ord n, Hashable n)
=> StartStop (Notes n)
-> Maybe (t (Edge n))
-> StartStop (Notes n)
-> [(Edges n, Freeze)]
pvThaw _ e _ = [(Edges (S.fromList $ maybe [] toList e) MS.empty, FreezeOp)]
pvSlice :: (Foldable t, Eq n, Hashable n) => t n -> Notes n
pvSlice = Notes . MS.fromList . toList
-- evaluators in specific semirings
-- ================================
{- | A restricted version of the PV evaluator
that prohibits split operations in which one of the parent slices is repeated entirely.
-}
protoVoiceEvaluatorNoRepSplit
:: (Foldable t, Foldable t2, Eq n, Ord n, IsNote n, Notation n, Hashable n)
=> Eval (Edges n) (t (Edge n)) (Notes n) (t2 n) (PVLeftmost n)
protoVoiceEvaluatorNoRepSplit = Eval vm vl vr filterSplit t s
where
(Eval vm vl vr mg t s) = protoVoiceEvaluator
filterSplit l lt mid rt r typ = filter ok $ mg l lt mid rt r typ
ok (_, LMSplitLeft op) = not $ onlyRepeats op
ok (_, LMSplitOnly op) = not $ onlyRepeats op
ok (_, LMSplitRight op) = not $ onlyRepeats op
ok _ = False
onlyRepeats (SplitOp regs pass rs ls _ _ _ _) =
M.null pass && (allRepetitionsLeft || allRepetitionsRight)
where
allSinglesRepeat =
all (check (== RightRepeat)) (M.toList rs)
&& all (check (== LeftRepeat)) (M.toList ls)
allRepetitionsLeft =
all (check isRepetitionOnLeft) (M.toList regs) && allSinglesRepeat
allRepetitionsRight =
all (check isRepetitionOnRight) (M.toList regs) && allSinglesRepeat
check fpred (_, os) = all (fpred . snd) os
protoVoiceEvaluatorLimitedSize
:: Int
-> Eval (Edges n) (t (Edge n)) (Notes n) (t2 n) (PVLeftmost n)
-> Eval (Edges n) (t (Edge n)) (Notes n) (t2 n) (PVLeftmost n)
protoVoiceEvaluatorLimitedSize n e = Eval filterUnspreadM vl vr mg t s
where
(Eval vm vl vr mg t s) = e
filterUnspreadM (sl, tm, sr) = do
v <- vm (sl, tm, sr)
case v of
(Notes ns, v')
| MS.size ns < n -> Just (Notes ns, v')
| otherwise -> Nothing
-- | An evaluator for protovoices that produces values in the 'Derivations' semiring.
pvDerivUnrestricted
:: (Foldable t, Foldable t2, Eq n, Ord n, IsNote n, Notation n, Hashable n)
=> Eval
(Edges n)
(t (Edge n))
(Notes n)
(t2 n)
(Derivations (PVLeftmost n))
pvDerivUnrestricted = mapEvalScore Do protoVoiceEvaluator
{- | An evaluator for protovoices that produces values in the 'Derivations' semiring.
- Enforces right-branching spreads (see 'rightBranchSpread').
-}
pvDerivRightBranch
:: (Foldable t, Foldable t2, Eq n, Ord n, IsNote n, Notation n, Hashable n)
=> Eval
(Merged, (RightBranchSpread, Edges n))
(t (Edge n))
((), ((), Notes n))
(t2 n)
(Derivations (PVLeftmost n))
pvDerivRightBranch =
splitFirst $ rightBranchSpread $ mapEvalScore Do protoVoiceEvaluatorNoRepSplit
-- | An evaluator for protovoices that produces values in the counting semiring.
pvCountUnrestricted
:: (Foldable t, Foldable t2, Eq n, Ord n, IsNote n, Notation n, Hashable n)
=> Eval (Edges n) (t (Edge n)) (Notes n) (t2 n) Int
pvCountUnrestricted = mapEvalScore (const 1) protoVoiceEvaluator
{- | An evaluator for protovoices that produces values in the counting semiring.
- Prohibits split operations in which one of the parent slices is repeated entirely (see 'protoVoiceEvaluatorNoRepSplit').
-}
pvCountNoRepSplit
:: (Foldable t, Foldable t2, Eq n, Ord n, IsNote n, Notation n, Hashable n)
=> Eval (Edges n) (t (Edge n)) (Notes n) (t2 n) Int
pvCountNoRepSplit = mapEvalScore (const 1) protoVoiceEvaluatorNoRepSplit
{- | An evaluator for protovoices that produces values in the counting semiring.
- Prohibits split operations in which one of the parent slices is repeated entirely (see 'protoVoiceEvaluatorNoRepSplit').
- Enforces right-branching spreads (see 'rightBranchSpread').
-}
pvCountNoRepSplitRightBranch
:: (Foldable t, Foldable t2, Eq n, Ord n, IsNote n, Notation n, Hashable n)
=> Eval (RightBranchSpread, Edges n) (t (Edge n)) ((), Notes n) (t2 n) Int
pvCountNoRepSplitRightBranch = rightBranchSpread pvCountNoRepSplit
{- | An evaluator for protovoices that produces values in the counting semiring.
- Prohibits split operations in which one of the parent slices is repeated entirely (see 'protoVoiceEvaluatorNoRepSplit').
- Enforces right-branching spreads (see 'rightBranchSpread').
- Normalizes the order of adjacent split and spread operations to split-before-spread (see 'splitFirst').
-}
pvCountNoRepSplitRightBranchSplitFirst
:: (Foldable t, Foldable t2, Eq n, Ord n, IsNote n, Notation n, Hashable n)
=> Eval
(Merged, (RightBranchSpread, Edges n))
(t (Edge n))
((), ((), Notes n))
(t2 n)
Int
pvCountNoRepSplitRightBranchSplitFirst = splitFirst pvCountNoRepSplitRightBranch