ãã®ã¿ã¹ã¯ã¯ãŒãããã¯çºçããŸããã§ããããRã§èšè¿°ãããã³ãŒããšHaskellã§èšè¿°ãããã¢ãã«ãäžç·ã«äœ¿çšããããã«1ã€ã®äŒæ¥ã«å¿ èŠã§ããã åæã«ãRããã°ã©ãã³ã°ã®å°é家ãHaskellãåŠã¶å¿ èŠããªãããã«ãããã䜿çšããããšãæãŸããŸããã
ãããã£ãŠã課é¡ã¯Haskellã®ä»ã®èšèªã®ã©ã€ãã©ãªãšã³ãŒããå¹ççãªæ¹æ³ã§æå¹ã«ããããšã§ããã ãã®å Žåã®å¹çãšã¯ãç°ãªãèšèªã®é¢æ°éã§å¯èœãªéãããŒã¿å€æã®ãªãŒããŒãããã³ã¹ãããªããèšèªéã§é¢æ°ãåŒã³åºãã®ãå¯èœãªéãå®ãããšã§ãã çŸæç¹ã§ã¯ãäŸã䜿çšããŠ2ã€ã®é¡äŒŒããã©ã€ãã©ãªãinline-rãšinline-cã§èšè¿°ãããŠããŸãããã®äŸã䜿çšããŠããã®ææ³ã®ç¹å®ã®æŠå¿µã説æããŸãã
- inline-cã¯ãCã³ãŒããhaskellã³ãŒãã«æ¿å ¥ã§ããããã«ããããªãåçŽãªã©ã€ãã©ãªã§ãã å®éã«ã¯ããã®ã©ã€ãã©ãªã¯ãç¬èªã®è£å©é¢æ°ãèšè¿°ãããããã®FFIã©ãããŒãäœæããããã®ããªãåçŽãªæé ã®èªååã§ãã ãã®ã©ã€ãã©ãªã¯ãHaskellã³ãŒãã§èšèªã®ã€ã³ã¯ã«ãŒãžã§ã³ãç°¡åã«ããïŒã©ã³ã¿ã€ã ããªããïŒæ¹æ³ã瀺ããŠããŸãã
- inline-r-Haskellã³ãŒãã«Rã³ãŒããå«ããããšãå¯èœã«ããã©ã€ãã©ãªãç°ãªãã©ã³ã¿ã€ã ã·ã¹ãã ãçµã¿åãããæ¹æ³ã瀺ããŠããã®ã§ããã§ã«éåžžã«èå³æ·±ããã®ã§ãã åçã«åæå®ãããRã³ãŒããHaskellã³ãŒãã«çµã¿èŸŒã¿ã2ã€ã®ã¬ããŒãžã³ã¬ã¯ã¿ãŒãããã·ã¹ãã äžã®ããŒã¿ãå¹ççã«ç®¡çããŸãã
ãã®ãããªãœãªã¥ãŒã·ã§ã³ãå®è£ ããã«ã¯ã次ã®åé¡ã解決ããå¿ èŠããããŸããã
- ä»ã®èšèªã®ã³ãŒããæ¥ç¶ããæ¹æ³ã
- å¥ã®èšèªã§ã®æ¿å ¥ã®æ§æã¯ã©ãããã¹ããã ã³ãŒãå ¥åæ¹æ³ã
- äž¡æ¹ã®ã©ã€ãã©ãªãåäœããããã«ããŒã¿ãå€æããæ¹æ³
ããŸããŸãªRTSïŒã©ã³ã¿ã€ã ã·ã¹ãã ïŒå®è¡ã·ã¹ãã ïŒè€æ°ã®ã¬ããŒãžã³ã¬ã¯ã¿ãŒãªã©ïŒã§çºçãããã®ä»ã®åé¡ãåçåä»ãã®äœ¿çš
ã©ã³ã¿ã€ã çµ±å
RTSãæããªãåçŽãªèšèªïŒCãRustïŒãæå¹ã«ããã«ã¯ããã¹ãŠãéåžžã«ç°¡åã§ãããããã®ã©ã³ã¿ã€ã ã¯Haskellã®ããã°ã©ã ã§ãã ãã®å ŽåãFFIå€éšé¢æ°ãåŒã³åºãããã®ã€ã³ã¿ãŒãã§ã€ã¹ã®ã¿ããµããŒããããCã€ã³ã¿ãŒãã§ã€ã¹ã¯ã»ãšãã©ã®å Žåã«é©ããŠããŸãã
ããããRã®ãããªããã©ãŒãã³ã¹ã·ã¹ãã ã®å Žåããããèµ·åããŠéä¿¡ããæ¹æ³ã®åé¡ãçããŸãã ãã®åé¡ã解決ããããã®æšæºçãªã¢ãããŒãã®1ã€ã¯ããã€ã³ã¿ãŒããªã¿ãŒãïŒããã°ã©ã ã¿ãŒã²ããèšèªïŒãå¥ã®ããã»ã¹ãšããŠèµ·åããRPCããŒã«ã®1ã€ã䜿çšããŠãœãŒã¹ããã°ã©ã ãšã¡ãã»ãŒãžã亀æããããšã§ãã ãã ãããã®æ¹æ³ã«ã¯ããã€ãã®æ¬ ç¹ããããŸãã
ã©ã³ã¿ã€ã ã«
eval
ã¡ãœããã®é¡äŒŒç©ããªãå Žåã¯ãèªåã§äœæããå¿ èŠããããŸãã ç¹å®ã®ã¿ã¹ã¯ã«ç¹åãå¿ èŠã«ãªãå Žåãããããœãªã¥ãŒã·ã§ã³ã®äžè¬æ§ãäœäžããŸãã ãããã«ããããã®ã¡ãœããã¯ãå®è¡ã®å®å šæ§ã«é¢ããçåãæèµ·ããŸãã
ãããã€ããŒã«ããŒã¿ã転éããã«ã¯ããããã¯ãŒã¯ãä»ããŠããŒã¿ãéä¿¡ããå¿ èŠãããïŒãŒãã³ããŒã䜿çšãããšè€éãã軜æžã§ããïŒãã·ãªã¢ã«åãšéã·ãªã¢ã«åãå¿ èŠã§ãã 代æ¿æ段ãšããŠãå ±æã¡ã¢ãªã®äœ¿çšã«åºã¥ã転éã¡ã«ããºã ã®äœæããããŸãããã³ããŒã¬ããŒãžã³ã¬ã¯ã¿ãŒã®äœ¿çšãªã©ãã¡ã¢ãªãžã®çŽæ¥ã¢ã¯ã»ã¹ãå¶éãããããè¿œå ã®åé¡ãçºçãããããèšèªã§ã¯éåžžã«å°é£ãªäœæ¥ã«ãªããŸãã
ãã®æ¹æ³ã®å©ç¹ã®1ã€ã¯ããœãªã¥ãŒã·ã§ã³ã®åçŽãã§ãããã€ãã³ãã«ãŒããçµ±åããå¿ èŠããªãããšã§ããããã¯ãããšãã°ã°ã©ãã£ãã¯ã¹ã®ã¬ã³ããªã³ã°ã«å¿ èŠã§ãã
ãããã®å¶éã¯ãéåžžãæå°ãªãŒããŒãããèŠä»¶ãšäºææ§ããããŸããã ãããã£ãŠãå€ãã®å ŽåãHaskellã«ã©ã³ã¿ã€ã ç°å¢ãçµã¿èŸŒãæ¹ãé©åã§ããã ãã®ãœãªã¥ãŒã·ã§ã³ã¯ãããã€ãã®è¿œå æ©èœãåæã«éããŸãã
- å ±æã¡ã¢ãªã䜿çšãããããããŒã¿ã®ã·ãªã¢ã«åãšéä¿¡ãšããäœåãªäœæ¥ã¯ãªããªããŸãã
- C APIïŒCã€ã³ã¿ãŒãã§ãŒã¹ã䜿çšããŠã©ã€ãã©ãªãããšã¯ã¹ããŒããããé¢æ°ã«ãã£ãŠæäŸãããAPIïŒã䜿çšããããšãå¯èœã«ãªããå¯èœãªå Žåã¯ãã€ã³ã¿ãŒããªã¿ãŒ/ããã°ã©ã ãšã©ãŒã®å¶åŸ¡ã匷åãããŸãã
- ãã®ãããªããã°ã©ã ã¯ãããããå®è¡ã§ããããŒã¿ãšæ©èœã«ã€ããŠãããç¥ãããŠããããããå®è¡ãããã«æé©åã§ããŸãã
ãã®ãœãªã¥ãŒã·ã§ã³ã§ã¯ãããšãã°èšèªã©ã³ã¿ã€ã ç°å¢ãè¿œå ã®å¶éã課ãå Žåãããšãã°TLSïŒã¹ã¬ããããŒã«ã«ã¹ãã¬ãŒãžïŒã䜿çšããå ŽåããŸãã¯æ¬è³ªçã«ã·ã³ã°ã«ã¹ã¬ããã§ããå Žåãæè¡çãªåé¡ãçºçããå¯èœæ§ããããŸãã æåã®ã±ãŒã¹ã§ã¯ã Control.Concurrent.forkOSã䜿çšããŠOSãªã³ã¯ã¹ã¬ãããäœæããïŒãŸãã¯ã¡ã€ã³ã¹ã¬ããã®ã¿ã䜿çšããïŒå¿ èŠããããŸãã 次ã«ãèšèªã®åŽã§ã¯ãšãªãã·ãªã¢ã«åããå¿ èŠããããŸããããšãã°ãããã¯ããªãåçŽãªããã¿ãŒã³ãã䜿çšããŠå®è¡ã§ããŸãã
{-# LANGUAGE ExistentialQuantification #-} import Control.Concurrent data Task = forall a . Task (IO a) (MVar (Either SomeException a)) sendTask :: Chan -> IO a -> IO a sendTask chan f = do result <- newEmptyMVar writeChan chan (Task f result) takeMVar result runTasks :: Chan -> IO () runTasks chan = forever $ do Task f result <- readChan chan mask_ $ do r <- try f putMVar result r
ãã®ã¢ãããŒãã«ã¯ãããšãã°ãããã°ã©ã ã¬ãã«ã§ãã®ãã£ãã«ã®äžææ§ãä¿èšŒããå¿ èŠãããå Žåã
ReaderT Chan
ãŸãã¯ã°ããŒãã«å€æ°ã®ç°å¢ã«ãã£ãã«ãä¿åãããªã©ãããã€ãã®ããªãšãŒã·ã§ã³ããããŸãïŒãã®ãœãªã¥ãŒã·ã§ã³ãéžæããåã«3åèããŠãã ããããæ£åŒã«æ£åœåã§ããŸãïŒã ãŸããå Žåã«ãã£ãŠã¯ãçºä¿¡è ãçæããã¢ã¯ã·ã§ã³ããã£ãã«ã«è»¢éããããã§çµæãMVarã«å ¥ããŠå¿çããæ¹ãè¯ãå ŽåããããŸãã ããããã»ãã³ãã£ãã¯äžïŒæ£ç¢ºã«ïŒããã³ïŒäŸå€ãåŠçããŠãã®ã¹ã¬ããã®å®è¡ãå®äºããåã«ïŒãä»ã®ãœãªã¥ãŒã·ã§ã³ã¯ãããšåçã«ãªããŸãã
ããŒã¿è»¢é
ããŒã¿ã転éãããšãã解決ããå¿ èŠãããæåã®è³ªåã¯ãHaskellã§å€éšèšèªããããŒã¿ã衚瀺ããæ¹æ³ã§ãããããå¯èœã«ããæãåçŽãªã¿ã€ãã¯ãCã¿ã€ããCCharãCIntãCStringãCStringLenãPtrãªã©ã§ãã ããã¯ãCã¿ã€ãã®Haskellã®ã¿ã€ããžã®ãããã³ã°ã§ãããäœã¬ãã«èšèªã®ããŒã¿ãè¡šãã®ã«ååã§ãã ãããã®åå®å šãªäœæ¥ã®ããã«ããã®ãããªããŒã¿ã¯ã©ãããŒåïŒnewtypesïŒã§ã©ããã§ããŸããããã«ãããäžæ¹ã§ã¯åã·ã¹ãã ãèš±å¯ããä»æ¹ã§ã¯å®è¡æã«ãŸã£ããè² è·ããããŸããã
ç¹ã«ãã©ã°ã€ã³èšèªã¯åçã§ããå¯èœæ§ããããããRã¯èšèªã§ãã®æå³ãã©ã®ããã«è¡šçŸããããšããåé¡ãæèµ·ããŸãã åæã«ãå¯èœã§ããã°ãå¯èœã§ããã°éçã«æ¢ç¥ã®åæ å ±ãæã¡ãããšæããŸãã ãã®å Žåãã³ã³ãã€ã©ãŒã¯ã³ãŒãã®æ£ç¢ºæ§ãããã«ä¿èšŒããã¿ã€ãããšã«ã³ãŒããçæããããšãã§ããŸãïŒHaskellã®ã¿ã€ãã¯ã©ã¹ãåç §ïŒã ããã§ãåçåã·ã¹ãã ãæã€èšèªã¯ãŠãã¿ã€ãèšèªã§ãããã€ãŸã éçã§ã¯ãããã«è¡šãããåã®å šäœã®å®å®ã¯ãåäžã®åã«ãã£ãŠèšè¿°ãããŸãã ãã®ã¢ãããŒãã䜿çšãããšãåŒã®ã¿ã€ãã瀺ããã¡ã³ãã ã¿ã€ãã§ã¿ã°ä»ãããããã€ã³ã¿ãŒãšããŠèšèªã®å€ãæ³åã§ããŸãïŒãã€ã³ã¿ãŒã®ä»£ããã«ãå«ãŸããèšèªã§ã®è¡šç€ºæ¹æ³ã«å¿ããŠãããŒãã«å ã®ã€ã³ããã¯ã¹ãŸãã¯å€ãäžæã«èå¥ããå¥ã®èå¥åãååšããå ŽåããããŸãïŒ
data SEXPTYPE -- generated by hsc2hs newtype SEXP a = SEXP (Ptr SEXPTYPE)
SEXPTYPE
ã¯ãCèšèªã€ã³ã¿ãŒãã§ã€ã¹ã«ãšã¯ã¹ããŒããããã¿ã€ãã§ãã åçèšèªã§äºæ³ãããããã«ãåŒã®åãäžæã§ããããšã瀺ãããã«ã次ã®ã¢ãããŒãã䜿çšããŸãã
åã«é¢ããæ å ±ã転éããã«ã¯ã
DataKinds
æ¡åŒµæ©èœã䜿çšã§ããŸããããã«ãããåçŽãªããŒã¿åã®ã³ã³ã¹ãã©ã¯ã¿ãŒãåã®ã¬ãã«ãã€ãŸã `SomeThing 'False`ã®ãããªã¿ã€ããäœæããŸãã 次ã«ããã©ã°å¯èœãªèšèªã§å¯èœãªãã¹ãŠã®ããªããã£ãåããªã¹ãããŸããããšãã°ãRã®å Žåã
data SEXPTYPE = NIL | Symbol | List | Closure | Int | Real ... deriving (Eq,Ord,Show)
åŒã«ç¹å®ã®åãããããšãããã£ãŠããå ŽåïŒããšãã°ãHaskellã§äœæãããå ŽåïŒããã®åãæ瀺çã«ãSEXP 'List`ãŸã㯠`SEXP' Realããšæžãããšãã§ããŸãã ãã ããåçèšèªã§äœæ¥ããå Žåãããã¯ååã§ã¯ãããŸããã ãã¹ãŠã®æ»ãå€ã®åã¯ããã§ç¥ãããŠããªããããçã«åçãªåãæž¡ãããšãã§ããããã«ããããšæããŸãã ãããè¡ãã«ã¯ãRank2Typesæ¡åŒµæ©èœã䜿çšããŠæ¬¡ã®ã¿ã€ããèšè¿°ããŸãã
data SomeSEXP = SomeSEXP (forall a . SEXP a)
ãã®ã³ãŒãã¯ããã¹ãŠ
a
SomeSEXP a
åŒã®
SomeSEXP a
ãæ£ããããšãæå³ããŸãã åçãªäŸ¡å€ã®äºå®ãæ£ç¢ºã«åæ ããŠããŸãã å€éšèšèªã®ã¡ãœããã
SomeSEXP
ãå®å šã«è¿ãããšãã§ããããã«ãªã
SomeSEXP
ããåãæ¢ç¥ã®åã«å€æããã«ã¯ãé¢æ°ãäœæã§ããŸã
cast :: SomeSEXP -> Maybe (SEXP a)
åŒã®ã¿ã€ãããã§ãã¯ããåŒãŸãã¯ãšã©ãŒãè¿ããŸãã
ãŸããããã€ãã®é¢æ°ã§ã¯ããŸããŸãªã¿ã€ãã®å€æ°ãå ¥åã«é©çšã§ãããããåèšã¿ã€ãã䜿çšã§ããããã«ããããšèããŠããŸãã
ãã®ãããªã¿ã€ããäœæããã«ã¯ãTypeFamiliesæ¡åŒµæ©èœã䜿çšãããã®ã¿ã€ããèš±å¯ãããã¿ã€ãã®ãªã¹ãã«å«ãŸããŠãããã©ããããã§ãã¯ããæ°ããã¿ã€ã `In`ãäœæã§ããŸãã
infix 1 :â -- | The predicate @a :â as@ states that @a@ is a member type of the set @as@. type family (a :: SEXPTYPE) :â (as :: [SEXPTYPE]) :: Constraint where 'Any :â as = () a :â (a ': as) = () a :â (b ': as) = a :â as type In ab = a :â b
ãã®ã³ãŒãã¯ãã¿ã€ãã¬ãã«ã§ã®éåžžã®ãã¿ãŒã³ãããã³ã°ãšåãããã«æ©èœããŸãã
()
ã¯æåã瀺ããã¹ããŒãã¡ã³ããæ€èšŒã§ããªãããšã¯ãšã©ãŒã§ãã
In 'Int (Env ': Int ': '[])
äŸãèããŠã¿ãŸãããã æåã«ã
Int
ã¯
'Env
ãšäžèŽãããåã¯
In 'Int ('Int ': '[])
ãšçããããã3çªç®ã®æ¡ä»¶ã«ãªããŸãã ãã®ã³ã³ãã€ã©ãŒã¯ããã«åçŽåããã2çªç®ã®åŒã«åé¡ãããŸãã
'Real
ã«ãã§ãã¯ããŒã¯ãä»ãããšã
In 'Real '[]
é²ã¿ããã¿ãŒã³ããªãããåãšã©ãŒãçºçããŸãã
ãã®ã¢ãããŒãã®åé¡ã¯ãåãåå°ã§ã¯ãªãããšã§ãã åŒ
foo :: In a ['Int,'Real] => a -> SEXP 'Int
ããå Žåãå
a
æšæž¬ããããšã¯ã§ããŸãããããã¯äŸ¿å©ã§ã¯ãªããããããŸããã
ãã®ãã¥ãŒã§ã¯ãC APIã䜿çšããŠæ¢ã«é¢æ°ãæäœããã³ãŒããæžãéã«ããçšåºŠã®å©äŸ¿æ§ãå®çŸã§ããŸãã æåã¯ããã®ã¢ãããŒãã¯ããµã³ãã«ã代æ°çããŒã¿æ§é ãšã®æ¯èŒãªã©ãææ°ã®ããã°ã©ãã³ã°èšèªã䜿çšããèœåã劚ããããã«æããããããããŸããããããã§ã¯ãããŸããã
ãã®ãããªæ§é ã䜿çšããã«ã¯ã次ã®ææ³ã䜿çšã§ããŸãã æ§é ïŒãã¥ãŒïŒã®ã€ã¡ãŒãžãäœæããæ§é ã®æµ ãè¡šçŸã®ã¿ãå®çŸ©ããŸããããã«ãããã¡ã¢ãªå ã«æ§é å šäœã®è¡šç€ºãäœæããå¿ èŠããªããªããŸããããã«ãäžè¬çãªå Žåãã³ã³ãã€ã©ã¯ãã¹ãŠã®äžéæ§é ãå®å šã«åé€ãããªãŒããŒããããªãã§å€éšããŒã¿æ§é ãæäœããã®ã«äŸ¿å©ã§ãã
data HExp :: * -> SEXPTYPE -> * where -- Primitive types. The field names match those of <RInternals.h>. Nil :: HExp R.Nil -- Fields: pname, value, internal. Symbol :: SEXP R.Char -> SEXP a -> SEXP b -> HExp R.Symbol -- Fields: carval, cdrval, tagval. List :: (R.IsPairList b, c :â [R.Symbol, R.Nil]) => SEXP a -> SEXP b -> SEXP c -> HExp R.List ...
ãããŠããã®ãããªç»åãäœæããé¢æ° `hexp`ã玹ä»ããŸãïŒ
hexp :: SEXP sa -> HExp sa
次ã«ã
ViewPatterns
æ¡åŒµæ©èœã䜿çšããŠããã¿ãŒã³ãããã³ã°ã䜿çšã§ããŸãã
foo (hexp -> Symbol s) = ...
å®å šãæãã«ã¯ãæ©èœãå¿ èŠã§ã
unhexp :: HExp sa -> ms (SEXP a)
ããã§ã¯ãäºæ³ããã
unhexp . hexp = id
ã«ãŒã«ãæºãã
unhexp . hexp = id
ããªãããšã«æ³šæããããšãéèŠ
unhexp . hexp = id
unhexpã¯åžžã«æ°ããæ§é ãäœæããããã
unhexp . hexp = id
ã
ãã®ææ³ã«ãããå€éšã®è€éãªæ§é ãç°¡åã«æäœã§ããŸãã ãŸããä»»æã®å€éšããã°ã©ãã³ã°èšèªã«ç§»æ€ã§ããŸãã ããã§ã®æåŸã®ã¹ãããã¯ãããŒã·ã£ãªã³ã°HaskellããŒã¿æ§é ãå€éšèšèªæ§é ã«å€æããããšã§ããããã®ããã«ãåã¯ã©ã¹ãå ¥åã§ããŸã
class Literal a ty | a -> ty where -- | Internal function for converting a literal to a 'SEXP' value. You -- probably want to be using 'mkSEXP' instead. mkSEXP :: a -> IO (SEXP V ty) fromSEXP :: SEXP s ty -> a
`| a-> ty`ã¯ãã©ã¡ãŒã¿ãŒéã®æ©èœçãªé¢ä¿ã§ãããã¿ã€ã 'a`ãtyã®ã¿ã€ããäžæã«æ±ºå®ããããšã瀺ããŸãã ãã®æ å ±ã¯ãã³ã³ãã€ã©ãåãå°åºããã®ã«éåžžã«åœ¹ç«ã¡ã誀ã£ãã€ã³ã¹ã¿ã³ã¹ãäœæããããšãèš±å¯ããŸããã 次ã«ãHaskellã®ãã€ãã£ãåã«å¯ŸããŠãå€æé¢æ°ãå®çŸ©ããŸãã
å€åœèªã§ã³ãŒããæžã
ã³ãŒããèšè¿°ããããã«ãå éšåã蟌ã¿èšèªïŒe-dslïŒïŒé«ã¬ãã«èšèªã§ã¯ãã©ã€ãã©ãªãšåã蟌ã¿å°çšèšèªã®å¢çã¯éåžžã«ãŒãããŠããŸãïŒãŸãã¯æ¬æ Œçãªdslãäœæã§ããŸãã ãDSLããã€ãŸã å®éã«ã¯ãããã«å€æŽããããã©ã°ã€ã³ã ãã®æ¹æ³ã¯ãHaskellãç¥ããªããåã蟌ã¿èšèªãç¥ã£ãŠãã人ãã³ãŒããäœæã§ããããããã䟿å©ã«èŠããŸãã ã©ã®ããã«æŽçã§ããã©ã®ããã«èŠãããã
Haskellã®ãœãŒã¹ã³ãŒããçæããããã«ãASTãæ§ç¯ã§ããTemplate Haskellæ¡åŒµããããŸãããããã«ã¯äžé£ã®å¶éããããããã«åãã§ãã¯ã¯éåžžã«å¶éãããŠããŸãã çæããããã¹ãŠã®åŒã¯ããããåãã¿ã€ãã®
Q Exp
æã¡ãã³ã³ãã€ã©ãŒã¯ã³ã³ãã©ã¯ãã®äžéšããã§ãã¯ã§ããŸããããã®çµæãç¡å¹ãªã³ãŒããçæãããå¯èœæ§ããããã³ãŒãçææã«ã³ã³ãã€ã©ãŒã«ãã£ãŠæåŠãããŸãã
ç䌌åŒçšç¬Šãäœæããããšãã§ããŸããQuasiQuotes
[generator|some-code |]
ããã§
generator
ã¯ã
some-code
ã解æããŠçµæãååŸããé¢æ°ã§ãã çµæã¯ãTemplateHaskellã䜿çšããŠçæãããHaskellã³ãŒãããã¡ã€ã«ïŒããšãã°ãå€éšCãã¡ã€ã«ïŒã®äœæã
find / -delete
æºåŒçšã¯ã³ãŒããå«ããããã®åªããåè£ã§ããæ°ããããŒãµãŒãäœæã§ããŸãã ããã«ããããžã§ã¯ããã³ã³ãã€ã«ãããšãã«ãRãåŒã³åºããŠãœãŒã¹ã³ãŒããæž¡ããASTãååŸããŸãã ãã®åŸããã®ASTãåŠçããHaskellããã®å€æ°çœ®æãèæ ®ããŠãå®è¡æã«åãASTãæ§ç¯ããã³ãŒããçæããŸãã
ããšãã°ãç°¡åãªã³ãŒãïŒ
let x = [1,2,3] in [r| x_hs + x_hs|]
ã«ãªããŸãïŒ
eval $ unhexpIO =<< Lang (installIO â+â) (unHexp $ List (mkSEXP x) (List (mkSEXP x) Nil)
å®éãçæãããã³ãŒãã¯ãªãœãŒã¹ã®ä¿è·ãGCã«é¢ããèšäºã®è©³çŽ°ãæ£ç¢ºã«ç£èŠãããããã³ãŒããç解ããã®ã¯å°ãé£ãããªããŸãã
ã¡ã¢ãªã·ã¹ãã ã®é åãšè¡šç€º
ãã®èšèªã®çµã¿åããã®æ倧ã®é£ç¹ã¯ãã¬ããŒãžã³ã¬ã¯ã¿ãŒãšã®ããåãã§ãã ç¹ã«ãã¬ããŒãžã³ã¬ã¯ã¿ãã¢ã»ã³ããªäžã«ãªããžã§ã¯ãã移åã§ããå Žåãåé¡ã¯è€éã«ãªããŸãã 幞ããªããšã«ãCïŒã¬ããŒãžã³ã¬ã¯ã¿ãŒãååšããªãïŒãRãžã®ãªããžã§ã¯ãã移åã§ããªãããããããã®æäœãå€§å¹ ã«ç°¡çŽ åãããŸãã
å®å šãªæäœã®ããã®æãç°¡åãªã¡ã«ããºã ã¯ãä¿è·ããããªããžã§ã¯ããåé€ãããã®ãé²ãç¹å¥ãªãªããžã§ã¯ãã®äœæã§ããããã®æå¹æéã¯å¥ã®èšèªããå¶åŸ¡ã§ããŸããHaskellã®å ŽåïŒ
Foreign.StablePtr.newStablePtr
ãä¿è·ããªããžã§ã¯ãã®äœæ
Foreign.StablePtr.freeStablePtr
ãä¿è·ããªããžã§ã¯ãã®åé€
extern void hs_free_stable_ptr (HsStablePtr sp);
-å€éšèšèªããã®ãä¿è·ããªããžã§ã¯ãã®åé€
Rã§ã¯ã次ã®ãšããã§ãã
preserveObject
ãªããžã§ã¯ãã䜿çšæžã¿ãšããŠããŒã¯ãã
releaseObject
ãªããžã§ã¯ããæªäœ¿çšãšããŠããŒã¯ãã
次ã«ãRããã®ãªããžã§ã¯ãã䜿çšããŠhaskellãå®è¡ãããšã次ã®ããšãã§ããŸãã
newtype Protected a = Prected (ForeignPtr SEXPPTR) protect (SEXP p) = do preserveObject p newForeignPtr p releaseObject
ãã®æ¹æ³ã¯ååã§ããããã¹ãŠã®èšèªã«é©çšã§ããããã§ã¯ãããŸããã å¿ èŠãªAPIããªãå Žåãããããã¹ã¿ãã¯ãäžã®ãªããžã§ã¯ãã«å¯ŸããŠã¯æ©èœãããæåŸã«ãR
O(N)
ã§ãªããžã§ã¯ããäœæããã³åé€ããã³ã¹ããªã©ãéåžžã«é«ãã³ã¹ãããããå¯èœæ§ããããŸã
N
ãã®æ¹æ³ã§ä¿è·ããããªããžã§ã¯ã
N
æ°ã ãããã£ãŠããã®ãããªæ¹æ³ãäž»ãªæ¹æ³ãšããŠäœ¿çšããããšã¯ãå§ãã§ããŸããã
ãã ããããã¯Rã®ãªããžã§ã¯ããä¿è·ããå¯äžã®æ¹æ³ã§ã¯ãããŸããããã®ä¿è·æ¹æ³ã«å ããŠããä¿è·ã¹ã¿ãã¯ãã«åºã¥ãå¥ã®æ¹æ³ããããŸããä»»æã®ãªããžã§ã¯ããã¹ã¿ãã¯ã«é 眮ããNåã®ãªããžã§ã¯ããã¹ã¿ãã¯ããåé€ã§ããŸãããããã®æäœã®è€éãã¯OïŒ1ïŒã§ãã ãªããžã§ã¯ãä¿è·ã«å¯Ÿãããã®ã¢ãããŒãã¯ãRèªäœãšCã§èšè¿°ãããé¢æ°ã®äž¡æ¹ã§äœ¿çšãããŸãããããŠããã®æ¹æ³ãHaskellã§ã衚瀺ããããšæããŸãã ãã®ãããéçé åã䜿çšããŠå®è£ ããããšãã§ããŸãããã®ææ³ã¯Oleg Kiselevã®èšäºã§èª¬æãããŠããŸã ã
Rã®ã¬ããŒãžã³ã¬ã¯ã·ã§ã³ã·ã¹ãã ã®æ©èœã«ããããã®ã¢ãããŒãã¯å ã®ãœãªã¥ãŒã·ã§ã³ãšã¯ç°ãªããŸãããè¡šçŸåã¯åçã§ã
newtype R sma = R {runR :: ReaderTïŒIORef IntïŒma}
掟çïŒãã¡ã³ã¯ã¿ãŒãApplicativeãMonadïŒ
IORef
ããã¹ã¿ãã¯ãã®ãã®ã»ã°ã¡ã³ãã«å²ãåœãŠããããªããžã§ã¯ãã®æ°ãä¿åããçµäºæã«ãããããã¹ãŠè§£æŸã§ããããã«ããŸãã 次ã«ãè£å©æ©èœãå°å ¥ãããŸãã
runRegion :: (forall s . R s IO a) -> IO a runRegion f = do ref <- newIORef 0 runReaderT (runR f) ref
ããã³äž»ãªæ©èœïŒ
region :: (forall s . R (sm) ma) -> ma region f = âŠ
ãã
SEXP
ãè¿œå ã®ãã¡ã³ãã å€æ°
s
ã§
SEXP
ã¿ã€ã
SEXP
å±éããå¿ èŠããããŸããããã¯ããã®å€æ°ãå²ãåœãŠãããããŸãã¯ä¿è·ãããé åã瀺ããŸãã
ããã§sã¯å°åã®åœ¹å²ãæãããŸã;ããã«ã2ã€ã®å°åã玹ä»ããŸãïŒ
data V
空ã®é åãå€æ°ãã¹ã¿ãã¯ã®ã©ã®ã»ã°ã¡ã³ãã«ãå±ããŠããããä¿è·ãããŠããªãããšãæå³ããŸãïŒæ¬¡åã¡ã¢ãªãå²ãåœãŠããããšãã«åé€ã§ããŸãïŒ
data G
ã¯ã°ããŒãã«é åã§ãããå€æ°ãä¿åã«ãã£ãŠä¿è·ãããŠããããã¹ãŠã®é åã§äœ¿çšã§ããããšã瀺ããŸãã
ãããŠããªãŒãžã§ã³æ©èœã®å©ããåããŠã
forall s1 s2 s3 . s1 (s2 s3 V))
æ§é ã®éå±€ãæ§ç¯ã§ããŸã
forall s1 s2 s3 . s1 (s2 s3 V))
forall s1 s2 s3 . s1 (s2 s3 V))
ã
ããã§æãèå³æ·±ãã®ã¯ãé åã®åã蟌ã¿ïŒé åïŒã®æäœã«é¢ããŠéããŠããæ Œåãå°å ¥ã§ããããšã§ãã ãã®å Žåã次ã®ããã«ãªããŸãã
V < .. < s2 (s1) < s1 < G
ãã®å Žåãå°ããé åã®ã倧ãããé åã®èŠçŽ ãå®å šã«äœ¿çšã§ããããšãããã³æäœãå°å ¥ã§ããããšãç¥ã£ãŠããŸãã
loosen :: (s1 < s2) => SEXP s2 a -> SEXP s1 a <source> , : <source language="haskell"> someOperation :: (s < s1, s < s2) => SEXP s1 Int -> SEXP s2 Int -> R s (SEXP s Int)
äžéé åã§ãªã¬ãŒã·ã§ã³ãäœæããããã«ã®ã¿æ®ããŸãïŒ
type Ancestor = (<) class Ancestor s s1 instance Ancestor (R sm) (R sm) instance Ancestor parent region => Ancestor parent (R s region)
ãããã«
äžèšã¯ãHaskellã«ä»ã®èšèªãå«ããããã«é©çšã§ãããã¯ããã¯ã®åºæ¬èŠçŽ ã説æããŠããŸãã ãã®ææ³ã¯ã
C
ã
Rust
ãªã©ã®äœã¬ãã«èšèªã®ã³ãŒããå«ããããã«é©çšã§ããã³ãŒãã®å¹çãåäžãããŸãïŒå¿ èŠã«å¿ããŠãå®éã«åäžãããŸãïŒã ãŸãã¯ãå€æ°ã®ã©ã€ãã©ãªãæã€èšèªãããã«ãããæ¢ã«è¡ãããäœæ¥ãããçŽãããšãªãã䟿å©ãªããã°ã©ãã³ã°èšèªã§äœæ¥ããããšãã§ããŸããããšãã°ã
python
ããŸãã¯
maxima
ã
reduce
ãŸãã¯ç¬èªã®é¡äŒŒç©ãªã©ã®ç¹æ®èšèªã§ãã
ãŸããå€éšã³ãŒããšã®çžäºäœçšãé åã®äœæãã³ãŒãçæã¯éåžžã«è¡šé¢çã§ãããšèããããŠããããããããã詳现ã«æ€èšããå¿ èŠãããå Žåã¯ã次ã®èšäºã§ãããè¡ãããšãã§ããŸãã