大規模数値計算を支える Haskell ── Pragmatic Haskell in Large-Scale Numerical Computation──

717e56bac925ce2db1e6f1cf7708056d?s=47 Hiromi Ishii
November 09, 2019

大規模数値計算を支える Haskell ── Pragmatic Haskell in Large-Scale Numerical Computation──

Haskell Day 2019 での講演抄録です。

717e56bac925ce2db1e6f1cf7708056d?s=128

Hiromi Ishii

November 09, 2019
Tweet

Transcript

  1. 3.
  2. 7.

    w 'PVOEFEJO+VMZ *KPJOFEUIJTTQSJOH w 0VS(PBM(FOFSBM1VSQPTF 6MUSBIJHITQFFE -BSHFTDBMF /VNFSJDBM4PMWFS w (FOFSBM1VSQPTF$BOTJNVMBUF"/:QIZTJDBM

    QIFOPNFOBFYQSFTTFEBT%J⒎FSFOUJBM&RVBUJPOT w 4QFFE4DBMF#JMMJPODFMMTºWBMTº.4UFQT
 #GSFFEPNº.JMMJPO4UFQT
  3. 8.

    w 'PVOEFEJO+VMZ *KPJOFEUIJTTQSJOH w 0VS(PBM(FOFSBM1VSQPTF 6MUSBIJHITQFFE -BSHFTDBMF /VNFSJDBM4PMWFS w (FOFSBM1VSQPTF$BOTJNVMBUF"/:QIZTJDBM

    QIFOPNFOBFYQSFTTFEBT%J⒎FSFOUJBM&RVBUJPOT w 4QFFE4DBMF#JMMJPODFMMTºWBMTº.4UFQT
 #GSFFEPNº.JMMJPO4UFQT w 0VSTPMWFSJTDVSSFOUMZXSJUUFOJO -JOFTPG)BTLFMM  BOEDPOTJTUTPGQBDLBHFT
  4. 11.

    "HHSFTTJWF6TFPG()$ TypeApplications FlexibleContexts MPTC DataKinds TypeFamilies ScopedTypeVariables FlexibleInstances TypeOperators UndecidableInstances

    DerivingStrategies OverloadedStrings PolyKinds TemplateHaskell RecordWildCards GND 0 50 100 150 200 5PUBM.PEVMFT &YUFOTJPOT 0O()$
  5. 12.

    "HHSFTTJWF6TFPG()$ TypeApplications FlexibleContexts MPTC DataKinds TypeFamilies ScopedTypeVariables FlexibleInstances TypeOperators UndecidableInstances

    DerivingStrategies OverloadedStrings PolyKinds TemplateHaskell RecordWildCards GND 0 50 100 150 200 5PUBM.PEVMFT &YUFOTJPOT 0O()$ 8FBSFVTJOHGFBUVSFTJOUSPEVDFEJO
 QuantifiedConstraints, DerivingVia, Source Plugins
  6. 14.

    0VS(PBMTBOE)BTLFMM w (FOFSBM1VSQPTF4PMWFS w %FQFOEFOU)BTLFMM w 5BHMFTT'JOBMBOE*OJUJBM4UZMF w 4QFFEBOE4DBMF w

    1SJNJUJWF.POBET 3FXSJUJOH3VMFT 4QFDJBMJTBUJPO *OUFSQSPDFTT DPNNVOJDBUJPO w .POBEJD4FRVFODF"CTUSBDUJPOUPNBLFVTFPGUIFTF w 1MVHJOTVTJOH&YUFOTJCMF3FDPSET w "OENBOZPUIFSUFDIOJRVFTOPUNFOUJPOFEUIJTUBML &YQSFTTJWJUZ4BGFUZ .PEVMBSJUZ &YUFOTJCJMJUZ
  7. 15.

    0VS(PBMTBOE)BTLFMM w (FOFSBM1VSQPTF4PMWFS w %FQFOEFOU)BTLFMM w 5BHMFTT'JOBMBOE*OJUJBM4UZMF w 4QFFEBOE4DBMF w

    1SJNJUJWF.POBET 3FXSJUJOH3VMFT 4QFDJBMJTBUJPO *OUFSQSPDFTT DPNNVOJDBUJPO w .POBEJD4FRVFODF"CTUSBDUJPOUPNBLFVTFPGUIFTF w 1MVHJOTVTJOH&YUFOTJCMF3FDPSET w "OENBOZPUIFSUFDIOJRVFTOPUNFOUJPOFEUIJTUBML &YQSFTTJWJUZ4BGFUZ
  8. 21.

    &YBNQMFT#PVOEBSZ4BOJUZ BOE2VBOUJUZ/BNF w 8FUSFBUTPNBOZEJ⒎FSFOULJOETPGRVBOUJUJFT w WFMPDJUZ UFNQFSBUVSF FMFDUSJDDIBSHF w 4PNFPGUIFNTIBSFTUIFTBNFSFQSFTFOUBUJPO

    CVUUIFZ BSFDPNQMFUFMZEJ⒎FSFOURVOBUJUJFT w "OEUIFZSFRVJSFTEJ⒎FSFOUQSFQSPDFTTJ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TTJPO w 4JODFXFXBOUUPBMMPXPQFSBUJPOTPOMZPOYetRVBOUJUJFT XFQSPWJEFNumsPOMZ GPSYets BOEIJEFDPOTUSVDUPSTGSPNPUIFSNPEVMFT w /VNJOTUBODFJTJOEFQPGLBOEOBNF XFDBOKVTUXSJUFBQPMZNPSQIJDJOTUT data Sanity = Yet | Done newtype TagQty name (s  Sanity) = TQty { unTagQty  PhysQty } unproc  TagQty k t 'Done  TagQty t 'Yet class Processable env k name where process  env  PhysQty  TagQty name 'Done instance s ~ 'Yet  Num (TagQty k name s)
  12. 25.

    5ZQFDIFDLFS1MVHJO w 8FBMTPVTFUZQFMFWFMOBUVSBMTUPNBJOUBJOJOHFHEJNFOTJPOT w 1SPCMFN()$DBOOPUTFF  GPSOPODPODSFUF WBSJBCMFL  w

    8FDBOVTFFYJTUJOH5ZQF$IFDLFS1MVHJOTUPUFMM()$UIBUTVDI DPOTUSBJOUTIPME w 8FVTFghc-typelits-natnormalise[4]. w &WFOUIPVHIJUDBOTPMWFFRVBUJPOBMDPOTUSBJOUTPOMZ CVUJUTV⒏DFT BOEJUTFFNTFYUFOTJPOUPOPOFRVBUJPOBMPOFJTOPUTPIBSE . {-# OPTIONS_GHC -fplugin GHC.TypeLits.Normalise #-}
 {-# OPTIONS_GHC -fplugin-opt ( ):allow-negated-numbers #-} (k + 1) − 1 = k
  13. 29.
  14. 30.

    4VNNBSZ-FU5ZQFT 3FNFNCFS w )VNBOTGPSHFUJOWBSJBOUT-FUUZQFTSFNFNCFS w 8JUI%BUB,JOET POFDBOFOGPSDFJOWBSJBOUTTUBUJDBMMZ BUUZQF MFWFM BMMPXTPQFSBUJPOTPOMZPOUIFTBNFEFHT

    POMZPO VOQSPDFTTFERUZT BOETPPO  w 4VDIQBSBNFUFSTXIJDIEPFTOUPDDVSJOBDUVBMWBMVFJTDBMMFE QIBOUPNUZQFT w ()$JTOPUTPDMFWFSUPTPMWFUSJWJBMDPOTUSBJOUT CVUPOFDBOBVHNFOU UIFN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.PEVMBSJUZ
  18. 43.

    .POBEEFMJNJUTUBTLT w "GUFSBMNPTUIBMGBOZFBS UIFTUSVDUVSFIBTDIBOHFEUJNFCZUJNF w #VUUIFTUBUFNFOUDBOTUJMMIPMEUSVFJOBTFOTF w 5ZQF$MBTTFT GPMMPXJOHUBHMFTTpOBMBQQSPBDI BCTUSBDUT

    FBDITFQBSBUFUBTLT w %J⒎FSFOUJBMPQFSBUJPOT DBMDVMBUJPOTDIFNBUB *0  w &RVBUJPOXSJUFSTIBTUPDBSFBCPVUPOMZBCTUSBDUEJ⒎FSFOUJBM PQFSBUJPOTPOMZ
  19. 44.

    .POBEEFMJNJUTUBTLT w "GUFSBMNPTUIBMGBOZFBS UIFTUSVDUVSFIBTDIBOHFEUJNFCZUJNF w #VUUIFTUBUFNFOUDBOTUJMMIPMEUSVFJOBTFOTF w 5ZQF$MBTTFT GPMMPXJOHUBHMFTTpOBMBQQSPBDI BCTUSBDUT

    FBDITFQBSBUFUBTLT w %J⒎FSFOUJBMPQFSBUJPOT DBMDVMBUJPOTDIFNBUB *0  w &RVBUJPOXSJUFSTIBTUPDBSFBCPVUPOMZBCTUSBDUEJ⒎FSFOUJBM PQFSBUJPOTPOMZ w #BDLFOE 4PMWFS XSJUFSTDBOJNQMFNFOUUIFNDPODSFUFMZ  MFBWJOHFRVBUJPOTUIFNTFMWFTVOUPVDIFE
  20. 45.

    5BHMFTT'JOBM4UZMF<> data family PhysQty m class Monad m  MonadDiff

    m where grad  PhysQty m  m (PhysQty m) div  PhysQty m  m (PhysQty m) w "CTUSBDUDPNNPOPQFSBUJPOBTBNPOBEJDGVODUJPO w "DUVBMMZ diffOFFETTPNFHFPNFUSJDJOGPBTBOFYUSBBSHVNFOU  CVUJUJTIJEEFOJOMonadic value m w 5IJTIJEFTUIFJNQMEFUBJMPGEJ⒎PQTBOEFOBCMFTVTUPTFQBSBUFJNQM GSPNBCTUSBDUFRVBUJPOT5IJTFODPVSBHFTNPEVMBSJUZBOEHFOFSBMJUZ
  21. 51.

    0VUDPNFNVMUJQMF CBDLFOET w 0OFDBOJNQMFNFOUBOETXJUDINVMUJQMFTPMWFSTFBTJMZ w 8JUIPVUSFXSJUJOHUIFFYJTUJOHEJ⒎FSFOUJBMFRVBUJPOT w $VSSFOUMZXFIBWF4PMWFSTJTPNPSQIJDUPQVSFReader envBOEReaderT env

    IO w 5IFMBUUFSJTDBMMFE3*0QBUUFSOJO)BTLFMMDPNNVOJUZ w (JWFTVTVOJGPSNUSFBUNFOUGPSCPUIJNNVUBCMF TUBUFTZFUSFUBJOJOHUISFBEBOEFYDFQUJPOTBGFUZ
  22. 52.

    0VUDPNFNVMUJQMF CBDLFOET w 0OFDBOJNQMFNFOUBOETXJUDINVMUJQMFTPMWFSTFBTJMZ w 8JUIPVUSFXSJUJOHUIFFYJTUJOHEJ⒎FSFOUJBMFRVBUJPOT w $VSSFOUMZXFIBWF4PMWFSTJTPNPSQIJDUPQVSFReader envBOEReaderT env

    IO w 5IFMBUUFSJTDBMMFE3*0QBUUFSOJO)BTLFMMDPNNVOJUZ w (JWFTVTVOJGPSNUSFBUNFOUGPSCPUIJNNVUBCMF TUBUFTZFUSFUBJOJOHUISFBEBOEFYDFQUJPOTBGFUZ w 8FVTFDerivingViaUPSFEVDFCPJMFSQMBUFDPEFTGPS TVDIJOTUBODFT
  23. 55.

    1SPCMFN5PP.VDI#JOET ν △ u − 1 ρ grad P (-)

     ((nu *)  laplacian u)  ((/rho)  grad u) 1SPCMFN)BWFUPXSJUF( )UPDPNQPTFGVODUJPOT
  24. 56.

    1SPCMFN5PP.VDI#JOET w 8FVTF("%5T JOJUJBMSFQS UPQSPWJEFTNBSU DPOTUSVDUPST ν △ u −

    1 ρ grad P (-)  ((nu *)  laplacian u)  ((/rho)  grad u) 1SPCMFN)BWFUPXSJUF( )UPDPNQPTFGVODUJPOT
  25. 57.

    1SPCMFN5PP.VDI#JOET w 8FVTF("%5T JOJUJBMSFQS UPQSPWJEFTNBSU DPOTUSVDUPST w 5IFZBMTPUBLFTDBSFPGBEEJUJPOBMQSFQPTU QSPDFTTFTGPSFBDIEJ⒎FSFOUJBMPQFSBUPST ν

    △ u − 1 ρ grad P (-)  ((nu *)  laplacian u)  ((/rho)  grad u) 1SPCMFN)BWFUPXSJUF( )UPDPNQPTFGVODUJPOT
  26. 58.

    *OJUJBM3FQSFTFOUBUJPO w 'SFF 0QFSBUJPOBM .POBEBOETNBSUDPOT VTJOH("%5T w %J⒎FSFOUJBMPQFSBUPSTDBOBDDFQUBOZUFSNXIJDIDBOCF DBTUFEUPUIFJSJOUFOEFEBSHVNFOUT data

    Expr m a where Join  Expr m (Expr m a)  Expr m a Grad  (ToPhysQty m qty)  Expr m qty  Expr m (PhysQty m) grad'  (ToPhysQty m a)  a  Expr m (PhysQty m) grad' = Grad . toExpr -- pre-process for conversion
  27. 59.

    &YBNQMF stokesEqn u ρ p = eval $ (1 /:

    ρ) .*: (eta .*: laplace' u -: grad' p) ∂u ∂t = 1 ρ (η ▵ u − grad P) "MNPTU-JUFSBM5SBOTMBUJPOJOUPUIF$PEF 㾎0OFEPFTOUIBWFUPLOPXBCPVUBOZ JNQMFNFOUBUJPOEFUBJMUPXSJUFFRVBUJPOT
  28. 64.

    5BHMFTTpOBM4VNNBSZ w 4FQBSBUFTBSFBPGUBTLTCZNPOBEJDPQFSBUJPOT  FOBCMJOHUPDPODFOUSBUFPOUIFJSPXOCVTJOFTT w &RVBUJPOXSJUFSTEPOUIBWFUPDBSFBCPVUJNQM EFUBJM w 8FFYQMPJUTJNJMBSUFDITUPEFMJNJUNBOZPUIFS

    UBTLTFRVBUJPO PQFSBUJPOJNQM DBDIJOH JOUFS QSPDFTTDPNNVOJDBUJPO w 8FDBOVTFJOJUJBMSFQSFTFOUBUJPOUPFMJNJOBUF CJOEIFMMJOUBHMFTTpOBMTUZMF
  29. 65.

    0VS(PBMTBOE)BTLFMM w (FOFSBM1VSQPTF4PMWFS w %FQFOEFOU)BTLFMM w 5BHMFTT'JOBMBOE*OJUJBM3FQSFTFOUBUJPO w 4QFFEBOE4DBMF w

    1SJNJUJWF.POBET 3FXSJUJOH3VMFT 4QFDJBMJTBUJPO *OUFSQSPDFTT DPNNVOJDBUJPO w .POBEJD4FRVFODF"CTUSBDUJPOUPNBLFVTFPGUIFTF w 1MVHJOTVTJOH&YUFOTJCMF3FDPSET w "OENBOZPUIFSUFDIOJRVFTOPUNFOUJPOFEUIJTUBML .PEVMBSJUZ
  30. 66.

    0VS(PBMTBOE)BTLFMM w (FOFSBM1VSQPTF4PMWFS w %FQFOEFOU)BTLFMM w 5BHMFTT'JOBMBOE*OJUJBM3FQSFTFOUBUJPO w 4QFFEBOE4DBMF w

    1SJNJUJWF.POBET 3FXSJUJOH3VMFT 4QFDJBMJTBUJPO *OUFSQSPDFTT DPNNVOJDBUJPO w .POBEJD4FRVFODF"CTUSBDUJPOUPNBLFVTFPGUIFTF w 1MVHJOTVTJOH&YUFOTJCMF3FDPSET w "OENBOZPUIFSUFDIOJRVFTOPUNFOUJPOFEUIJTUBML
  31. 68.

    4FRVFOUJBMUZQFTJO)BTLFMM w 1IZTJDBMWBMVFTBSFFYQSFTTFEBTTFRVFODFTPGSFBMT w 8IJDI4FRVFODFUZQFNVTUXFVTF  w -JTUTHJWFOJO1SFMVEF TJNQMFBOEEFDMBSBUJWF*'CVU4-08 JOEFYJOH

    BENJUT'VTJPO w 7FDUPST'BTU 0  JOEFYJOHXJUIEFDMBSBUJWF*' F⒏DJFOU TUPSBHFGPS6OCPYFEUZQFT BENJUT'VTJPO 5FNQFSBUVSFT Cell #0 #1 #2 #3 ʜʜ 273K 273K 300K 450K
  32. 75.

    4USFBN'VTJPO< > w 'VODUJPOTPOTFRVFODFTBUJTpFTTPNFBMHFCSBJDMBXT XIJDIGVTFT NVMUJQMFMPPQJOUPPOFQBTT w VectorsFYQMPJUTVDISFMBUJPOTUPPQUJNJTFGVODUJPOT w 5IFZPODFDPOWFSUWFDUPSTUPBCTUSBDUStreamTBOEBundleT

    w 4USFBNBNPOBEJDHFOFSBUJOHGVODUJPO w #VOEMF4USFBN PQUJPOBMDPODSFUFWFDUPSWBMVFBOEMFOHUI JOGPSNBUJPO w TUSFBNVOTUSFBNGVODUJPOTDPOWFSUT7FDUPSTUPGSPN#VOEMFT f z ∘ g ≡ (f ∘ g) z
  33. 76.

    1SPCMFN.BOZLJOETPG WFDUPS w #PYFEDBOTUPSFBOZUZQFT w 6OCPYFEDBOTUPSF TVNPGQSPEVDUTPG VOCPYFE UZQFTFH*OU %PVCMF

    #PPM w 4UPSBCMFGPSDPNNVOJDBUJPOTXJUI$MJCSBSZ w BOENPOPNPSQIJDOFXUZQFXSBQQFSTBSPVOEUIFN w 1SPCMFN8FXBOUUPUSFBUUIFNVOJGPSNMZ w $BOXFEPUIJTSFUBJOJOHF⒏DJFODZPGWFDUPST
  34. 77.

    'JSTU"UUFNQU type family Elem seq type family Key seq class

    Monad m  MSeq m seq where imap  (Key seq  Elem seq  Elem seq)  seq m seq index  Index seq  seq  m (Elem seq) ifoldr  (Key seq  Elem seq  Elem seq)   accum   instance Monad m  MSeq m (Vector a) instance Monad m  MSeq m (VecMap k a) .POBEJDSFTVMU w 1SPCMFN.POBEJDSFTVMUNJHIUTBDSJpDFTUSFBNGVTJPO w 1SPCMFN)BWFUPJNQMFNFOUWFDUPSPQFSBUJPOTBMMCZIBOET .BOZPUIFSPQT
  35. 82.

    "OTXFSTUSFBNVOTUSFBN w "MNPTUBMMWFDUPSPQFSBUJPOTBSFJOEFFEJNQMEBT DPNQPTJUJPOTPGTUSFBNVOTUSFBNXJUI4USFBN #VOEMFPQFSBUJPOT w 4JODF4USFBN#VOEMFTBSFNPOBEJDCZEFGBVMUBOE UIFZGVTFTXFMM XFDBOHFUF⒏DJFODZGPSGSFF w

    5IFSVMFstream ◦ unstream ≡ returnSFEVDFT JOUFSOBMUFSNTFWFOJGUIFSFTVMUJTNPOBEJD w 0OFDBOBMTPQSPWJEFBO*'UPGVTFEJ⒎FSFOULJOEPG WFDUPSTJOUPPOF
  36. 83.

    3FTVMU type family VecRep seq  Type  Type class

    (Monad m)  MSequence m seq where unfoldrM  (s  m (Maybe (Elem seq, s)))  s  m seq indexM'  seq  Int  m (Elem seq) stream'  seq  B.Bundle m (VecRep seq) (Elem seq) unstream'  B.Bundle m (VecRep seq) (Elem seq)  m seq
  37. 84.

    3FTVMU type family VecRep seq  Type  Type class

    (Monad m)  MSequence m seq where unfoldrM  (s  m (Maybe (Elem seq, s)))  s  m seq indexM'  seq  Int  m (Elem seq) stream'  seq  B.Bundle m (VecRep seq) (Elem seq) unstream'  B.Bundle m (VecRep seq) (Elem seq)  m seq w "MMPUIFSPQFSBUJPOTBSFEFSJWFEGSPNUIFTFQSJNJUJWFTXJUI Data.Vector.Fusion.Bundle.MonadicNPEVMF
  38. 85.

    3FTVMU type family VecRep seq  Type  Type class

    (Monad m)  MSequence m seq where unfoldrM  (s  m (Maybe (Elem seq, s)))  s  m seq indexM'  seq  Int  m (Elem seq) stream'  seq  B.Bundle m (VecRep seq) (Elem seq) unstream'  B.Bundle m (VecRep seq) (Elem seq)  m seq w "MMPUIFSPQFSBUJPOTBSFEFSJWFEGSPNUIFTFQSJNJUJWFTXJUI Data.Vector.Fusion.Bundle.MonadicNPEVMF w 0OFDBOFYQMPJUGVTJPOSVMFTQSPWJEFECZWFDUPSQBDLBHFGPSGSFF
  39. 86.

    3FTVMU type family VecRep seq  Type  Type class

    (Monad m)  MSequence m seq where unfoldrM  (s  m (Maybe (Elem seq, s)))  s  m seq indexM'  seq  Int  m (Elem seq) stream'  seq  B.Bundle m (VecRep seq) (Elem seq) unstream'  B.Bundle m (VecRep seq) (Elem seq)  m seq w "MMPUIFSPQFSBUJPOTBSFEFSJWFEGSPNUIFTFQSJNJUJWFTXJUI Data.Vector.Fusion.Bundle.MonadicNPEVMF w 0OFDBOFYQMPJUGVTJPOSVMFTQSPWJEFECZWFDUPSQBDLBHFGPSGSFF w #POVT)FUFSPHFOFPVT$PNCJOBUPSTBOE1SJN.POBEPQUJNJTBUJPO
  40. 87.

    #POVT)FUFSPHFOFPVT $PNCJOBUPST w 0OFDBONBQ[JQEJ⒎FSFOULJOEPGTUSFBNTJOPOFQBTT w 8FQSPWJEF3FXSJUF3VMFTUPFMJNJOBUFDPOWFSTJPOPWFSIFBET XIFOTPNFPGBSHVNFOUTIBWFUIFTBNFWFDUPSSFQSFTFOUBUJPO hzipWith  (MSeq

    m s, MSeq m t, MSeq m u, Key s ~ Key t, Key s ~ Key u)  (Elem s  Elem t  Elem u)  s  t  m u hzipWithM f = \as bs  unstream $ B.zipWithM f (B.reVector $ stream as) (B.reVector $ stream bs)
  41. 92.

    #POVT%FTUSVDUJWF "TTJHONFOUTBSF:PVS'SJFOET w MSeqBTTVNFTUIFJSSFUVSOUZQFUPCFNPOBEJD w 5IFSFJTBDMBTTPG.POBETBENJUUJOHEFTUSVDUJWF BTTJHONFOUT PrimMonad GSPNprimitiveQBDLBHF w

    1SPWJEFTBDPNNPO*'GPSSTBOEIONPOBETBOE NPOBEUSBOTGPSNFSTBSPVOEUIFN w *GVOEFSMZJOHNPOBEmJTPrimMonad POFDBOVTFUIFJS BCJMJUZGPSNVUBCMFSFGFSFODFTUPHBJOQFSGPSNBODF
  42. 93.

    VOTUSFBNXJUI1SJN.POBE w *GNJTPrimMonad JOQBSUJDVMBSJGIOXJUIFYUSBUSBOTGPSNFS MBZFSTPOUPQ XFDBOVTFNVUBCMFPQFSBUJPOTPOUIFNUPHFU WFDUPSTJOTUBOUMZ w 5IJTHBWFVTYTQFFEVQTJOQSBDUJDF &YBDUSFDPSETBSF

    MPTUTPSSZ  ‎ $PNQPTFTXFMMXJUIUBHMFTTpOBMBQQSPBDI unstreamPrimM  (PrimMonad m, Vector (VRep s) (Elem s), VRep s (Elem seq) ~R# seq)  B.Bundle m u (Element s)  m s unstreamPrimM s = coerce  (G.unsafeFreeze  MG.munstream s) {-# RULES "unstreamM[IO]" unstreamM = unstreamPrimM #-}
  43. 96.

    .POBEJD4FR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UFDIOJRVFTOPUNFOUJPOFEUIJTUBML &YUFOTJCJMJUZ
  46. 103.

    .PUJWBUJPO1MVHJO .FDIBOJTN w 5BHMFTTpOBMTUZMFBMMPXFEVTUPXSJUFNVMUJQMF 4PMWFSTOJDFMZ w CVUUIFPGTPMWFSTFWFOUVBMMZJODSFBTFE BOE JUIBECFDPNFIBSEUPNBJOUBJOBMMPGUIFN w

    *UJTEFTJSBCMFGPSVTUPIBWFPOMZBMJNJUFEOVNCFS PG4PMWFSTBOEFYUFOEUIFNCZ1MVHJOTGPS NPEVMBSJUZBOENBJOUBJOBCJMJUZ
  47. 104.

    .PUJWBUJPO1MVHJO .FDIBOJTN w 5BHMFTTpOBMTUZMFBMMPXFEVTUPXSJUFNVMUJQMF 4PMWFSTOJDFMZ w CVUUIFPGTPMWFSTFWFOUVBMMZJODSFBTFE BOE JUIBECFDPNFIBSEUPNBJOUBJOBMMPGUIFN w

    *UJTEFTJSBCMFGPSVTUPIBWFPOMZBMJNJUFEOVNCFS PG4PMWFSTBOEFYUFOEUIFNCZ1MVHJOTGPS NPEVMBSJUZBOENBJOUBJOBCJMJUZ w 8IBUTOFFEFEGPSUIFN
  48. 105.

    1MVHJO4QFD class Plugin p where data PState p  Type

    data PConfig p  Type type POutput p  Type type PlugM p (m  *  *)  Constraint pluginOptionP  OptParser (PConfig p) setupState  MonadIO m  PConfig p  m (PState p) initialise  (p ∈ ps, MonadPlugins ps m, PlugM p m)  proxy p  Info  m Info finalise  (p ∈ ps, MonadPlugins ps m, PlugM p m)  proxy p  Info  m Info genOutput  (p ∈ ps, MonadPlugins ps m, PlugM p m)  PConfig p  m (POutput p)
  49. 106.

    #VOEMJOHNVMUJQMFQMVHJO EBUBUPPOF w 8FSFQSFTFOUUIF4PMWFSNPOBETXJUIQMVHJOTQTBTB4PMWFS QT XIFSFQTJTBUZQFMFWFMMJTUPGQMVHJOT data PStateBundle ps =

    PSB { p  PState p | p ∈ ps } w 0OFDBOTJNVMBUFTVDIEBUBUZQFTXJUI )JHIFS,JOEFE  &YUFOTJCMF3FDPSET JOUIJTDBTF ),)-JTUTV⒏DFT UIPVHI w "OFOUJSF4PMWFSXJUIQMVHJOTpsNVTULFFQJOGP BTTPDJBUFEUPFBDIQMVHJOBTBCVOEMF TPNFUIJOHMJLF Solver '[] -- Solver without any Plugin 
 Solver '[A, B, C] -- Solver with Plugins A, B and C
  50. 107.

    &YUFOTJCMF3FDPSET w )BTLFMMT3FDPSE-BCFMTBOE7BMVFTBSFpYFE w &YUFOTJCMF3FDPSETPOFDBOEZOBNJDBMMZBEEEFMFUF  DPOTUSBJOpFMETBOEUSFBUQPMZNPSQIJDBMMZ SFUBJOJOHUZQFTBGFUZ w 'PSFBDIp

    ∈ ps  PState p WBMVFJOstates  PStateB psDBOCF SFUSJFWFECZstates ^. hfieldL @p w 5IFSFBSFQMFOUZPGFYJTUJOHMJCT extensible, vinyl, superrecords,FUD  GPSFYUSFDTCVUXFJNQMECZPVSTFMWFTGPSTPNFSFBTPOT w ),%PQT TVDIBTCGPMES.CGPMEM. .FNCFSDPOTUS DBOCFVTFEUP QSPDFTTCVOEMFTCBDLXBSEGPSXBSE type PBundle h ps = Record h (IdentFields ps) type PStateB ps = PBundle PState ps
  51. 113.

    8IZDVTUPNMJCSBSZ w *UNVTUTBUJTGZUIFGPMMPXJOHTBUUIFTBNFUJNF w 'BTU"DDFTT*OUFSOBMSFQSNVTUCFBSSBZWFDUPS OPUMJOLFE MJTU w -JHIU1BDLBHF%FQFOEFODZXFOFFEPOMZXIBUXFOFFE w

    1PMZNPSQIJD-BCFMXFXBOUUPMBCFMpFMETXJUIBSCJUSBSZ UZQFT OPUOFDFTTBSJMZDPOWFSUJCMFUP4ZNCPMTPS/BUT w ),%MJLFPQFSBUJPOTOFFEFEGPSWBSJPVTGPMEJOHNBQQJOH PQFSBUJPOTFHTFSJBMJTBUJPO JOJUJBMJTBUJPO
  52. 114.

    8IZDVTUPNMJCSBSZ w *UNVTUTBUJTGZUIFGPMMPXJOHTBUUIFTBNFUJNF w 'BTU"DDFTT*OUFSOBMSFQSNVTUCFBSSBZWFDUPS OPUMJOLFE MJTU w -JHIU1BDLBHF%FQFOEFODZXFOFFEPOMZXIBUXFOFFE w

    1PMZNPSQIJD-BCFMXFXBOUUPMBCFMpFMETXJUIBSCJUSBSZ UZQFT OPUOFDFTTBSJMZDPOWFSUJCMFUP4ZNCPMTPS/BUT w ),%MJLFPQFSBUJPOTOFFEFEGPSWBSJPVTGPMEJOHNBQQJOH PQFSBUJPOTFHTFSJBMJTBUJPO JOJUJBMJTBUJPO w 0VSMJCBMTPQSPWJEFT()$1MVHJOT BMMPXJOHUPXSJUF3FDPSE DPOTUSVDUJPOOBUVSBMMZBOESFEVDFTUIFBNPVOUPGIBOE XSJUUFOQSPPGT
  53. 115.

    #FGPSF5D1MVHJO (&%~)  forall l xs h a. Member l

    (Labels xs)  Record h xs  l := (h (ElemAt (Idx l (Labels xs)) (Vals xs))  h a)  Record h ((l  a) |< xs) Record hl &%~ (_ := f) = Record $ gcastWith (takeMapApply @Entry' @(Index l (Labels xs)) @xs) $ gcastWith (dropMapApply @Entry' @(Index l (Labels xs) + 1) @xs) $ gcastWith (mapApplyAppend @Entry' @(Take (Index l (Labels xs)) xs) @((l  a) ': Drop (Index l (Labels xs) + 1) xs) ) $ hmodify' (Proxy @(Index l (Labels xs))) f hl 5SJWJBMIBOEXSJUUFOQSPPGT .BQI YT ZT .BQIYT .BQIZT 
  54. 116.

    #FGPSF5D1MVHJO (&%~)  forall l xs h a. Member l

    (Labels xs)  Record h xs  l := (h (ElemAt (Idx l (Labels xs)) (Vals xs))  h a)  Record h ((l  a) |< xs) Record hl &%~ (_ := f) = Record $ gcastWith (takeMapApply @Entry' @(Index l (Labels xs)) @xs) $ gcastWith (dropMapApply @Entry' @(Index l (Labels xs) + 1) @xs) $ gcastWith (mapApplyAppend @Entry' @(Take (Index l (Labels xs)) xs) @((l  a) ': Drop (Index l (Labels xs) + 1) xs) ) $ hmodify' (Proxy @(Index l (Labels xs))) f hl 5SJWJBMIBOEXSJUUFOQSPPGT .BQI YT ZT .BQIYT .BQIZT 
  55. 117.

    "GUFS5D1MVHJO {-# OPTIONS_GHC -fplugin Data.TypeLevel.List.Solver #-} (&%~)  forall l

    xs h a. Member l (Labels xs)  Record h xs  l := (h (ElemAt (Idx l (Labels xs)) (Vals xs))  h a)  Record h ((l  a) |< xs) Record hl &%~ (_ := f) = Record $ hmodify' (Proxy @(Index l (Labels xs))) f hl
  56. 118.

    4PVSDF1MVHJO {-# LANGUAGE OverloadedLabels #-} record = emptyRec &+ #a

    := 42 &+ #b := False &+ #c := () {-# OPTIONS_GHC -fplugin Data.Record.Plugin #-} {-# LANGUAGE OverloadedLabels #-} record = MkRecord { a = 42  Int, b = False, c = () } 8JUIPVU4PVSDF1MVHJO 8JUI4PVSDF1MVHJO w 0WFSMPBEJOHGPS3FDPSEVQEBUFBOEBDDFTTPSTZOUBYDBOOPUCF JNQMFNFOUFEXJUIUIFDVSSFOU()$4PVSDF1MVHJO.FDIBOJTN
  57. 123.

    1MVHJOT4VNNBSZ w &YUFOTJCMF3FDPSETBSFQBSUJDVMBSMZVTFGVMGPS1MVHJOT w ),%PQFSBUJPOT TVDIBTIFUFSPGPMETBOENBQT DBOCFVTFEUP JNQMJOJUJBMJTBUJPOTFSJBMJTBUJPO w /PUPOMZ),%T

    CVUTJNQMFWBMVFMFWFMGPMEJOHPGUZQFMFWFMMJTUT JTBMTPVTFGVM w "LJOEPGSPXQPMZNPSQIJTNDBOBMTPCFVTFEUPUSFBUBCVOEMFPG QIZTJDBMRVBOUJUJFTQPMZNPSQIJDBMMZ
  58. 124.

    1MVHJOT4VNNBSZ w &YUFOTJCMF3FDPSETBSFQBSUJDVMBSMZVTFGVMGPS1MVHJOT w ),%PQFSBUJPOT TVDIBTIFUFSPGPMETBOENBQT DBOCFVTFEUP JNQMJOJUJBMJTBUJPOTFSJBMJTBUJPO w /PUPOMZ),%T

    CVUTJNQMFWBMVFMFWFMGPMEJOHPGUZQFMFWFMMJTUT JTBMTPVTFGVM w "LJOEPGSPXQPMZNPSQIJTNDBOBMTPCFVTFEUPUSFBUBCVOEMFPG QIZTJDBMRVBOUJUJFTQPMZNPSQIJDBMMZ w 8FEFWJTFEDVTUPN&YU3FDMJCSBSZXJUI
  59. 125.

    1MVHJOT4VNNBSZ w &YUFOTJCMF3FDPSETBSFQBSUJDVMBSMZVTFGVMGPS1MVHJOT w ),%PQFSBUJPOT TVDIBTIFUFSPGPMETBOENBQT DBOCFVTFEUP JNQMJOJUJBMJTBUJPOTFSJBMJTBUJPO w /PUPOMZ),%T

    CVUTJNQMFWBMVFMFWFMGPMEJOHPGUZQFMFWFMMJTUT JTBMTPVTFGVM w "LJOEPGSPXQPMZNPSQIJTNDBOBMTPCFVTFEUPUSFBUBCVOEMFPG QIZTJDBMRVBOUJUJFTQPMZNPSQIJDBMMZ w 8FEFWJTFEDVTUPN&YU3FDMJCSBSZXJUI w 1PMZNPSQIJDMBCFMT GBTUJOEFYJOH BVUPNBUJDDPOTUSBJOUTPMWJOH BTTJTUFECZ5ZQFDIFDLFSQMVHJOT
  60. 127.

    $PODMVTJPOT w )BTLFMMDBOCFVTFEBDIJFWFTJNVMUBOFPVTMZ w )JHIQFSGPNBODFBOE  w "CTUSBDU IJHIMZFYUFOTJCMF HFOFSBMQVSQPTFTPGUXBSF

    w ()$T%FQFOEFOU5ZQFTIFMQTFYUFOTJCJMJUZBOELFFQJOHJOWBSJBOUT w )VNBOT'PSHFU-FU5ZQFT3FNFNCFS w %FQFOEFOU5ZQFTBOE%FTUSVDUJWF"TTJHONFOUTBSFZPVSGSJFET w 8FBSFQMBOOJOHUPQVCMJTIJOUFSOBMMJCSBSJFT JOOPUTPGBSGVUVSF  IPQFGVMMZ  w 4UJMM UIFSFBSFNBOZUIJOHTUPCFJNQSPWFE
  61. 128.

    #JCMJPHSBQIZ  Oleg Kiselyov, Tagless-final style. http:// okmij.org/ftp/tagless-final/index.html 2. D.

    Coutts, R. Leshchinskiy and D. Stewart, Stream Fusion: From Lists to Streams to Nothing at All. 3. G. Mainland, R. Leshchinskiy and SPJ, Exploiting Vector Instructions with Generalized Stream Fusion. 4. ghc-typelits-natnormalise package
  62. 129.
  63. 130.

    "OZ2VFTUJPOT w )BTLFMMDBOCFVTFEBDIJFWFTJNVMUBOFPVTMZ w )JHIQFSGPNBODFBOE  w "CTUSBDU IJHIMZFYUFOTJCMF HFOFSBMQVSQPTFTPGUXBSF

    w ()$T%FQFOEFOU5ZQFTIFMQTFYUFOTJCJMJUZBOELFFQJOHJOWBSJBOUT w )VNBOT'PSHFU-FU5ZQFT3FNFNCFS w %FQFOEFOU5ZQFTBOE%FTUSVDUJWF"TTJHONFOUTBSFZPVSGSJFET w 8FBSFQMBOOJOHUPQVCMJTIJOUFSOBMMJCSBSJFT JOOPUTPGBSGVUVSF  IPQFGVMMZ  w 4UJMM UIFSFBSFNBOZUIJOHTUPCFJNQSPWFE