-- |Int-ranked min leftist heap yodataLeftHeap'prioa=E|TInta(LeftHeap'prioa)(LeftHeap'prioa)deriving(Show,Eq,Functor,Foldable)typeLeftHeapa=LeftHeap'MinPolicyadataMinPolicydataMaxPolicyclassMergeOrdpwherecompareByPriority::Orda=>a->a->BoolinstanceMergeOrdMinPolicywhere...instanceMergeOrdMaxPolicywhere...instance(MergeOrdprio)=>Heap(LeftHeap'prio)wheremerge::(Orda)=>LeftHeap'prioa->LeftHeap'prioa->LeftHeap'prioamergeh1h2|comparePriority@prioh1h2=...|otherwise=......-- |Leftist Heap w/ minimum values at headnewtypeMinHeapa=MinHeap{unMinHeap::LeftHeap'MinPolicya}derivingstock(Show,Eq)derivingHeapvia(LeftHeap'MinPolicy)-- |Leftist Heap w/ maximum values at headnewtypeMaxHeapa=MaxHeap{unMaxHeap::LeftHeap'MaxPolicya}derivingstock(Show,Eq)derivingHeapvia(LeftHeap'MaxPolicy)
dataF_lte_R-- ^ Rebalance when size of front list is equal to size of reardataTwoF_lte_R-- ^ Rebalance when twice the size of front list is equal to size of rearclassBalanceConditioncwhereneedsRebalance::Int->Int->BoolinstanceBalanceConditionF_lte_Rwhere...instanceBalanceConditionTwoF_lte_Rwhere...instance(BalanceConditioninvariant)=>Queue(BankersQueue'invariant)where...-- some code branching on `needsRebalance @invariant`typeBankersQueuea=BankersQueue'F_lte_RanewtypeLessMovementBQa=LessMovementBQ{unLessMovementBQ::(BankersQueue'TwoF_lte_Ra)}derivingstock(Show,Eq,Functor,Foldable)derivingQueuevia(BankersQueue'TwoF_lte_R)
I’m working through Okasaki’s PFDS, and I've found myself using
DerivingVia
as a low hassle way of getting different flavors of the same datatypeI can write a "primary" instance that's parameterized by some ad-hoc property(set) for cases where I want derived instances to be different
To derive Min and Max Heaps off one instance, I just pulled the comparison out of
Heap.merge
And again with Banker’s Queues to change the condition triggering rebalances
I’m sure I could have done some clever type-level stuffs by feeding the list sizes to
BalanceCondition
Or not used
Bool
s :lol:But I’m content for now with how much boilerplate I saved and how simple the resulting code was
I bet there’s more fun to be had stacking properties encoding via style, and using GDP-style naming of properties
Anyways,
DerivingVia
is very cool!