Mojo ð¥
å æ¥ããã°ã©ãã³ã°èšèª Mojo ãšåŒã°ãããã®ã®ã¢ããŠã³ã¹ã¡ã³ãããã£ãããã®èšèªã®ãã¶ã€ã³ãç§ã®ã¹ã€ãŒãã¹ãããã«åºããæãã ã£ãã®ã§ãä»ã®ãã¡ãã泚ç®ããŠããã䜿ããããªãšããããå°æ¥äœ¿ãããšã«ãªããããªèšèªãªã®ã§ç°¡åã«äœãã§ããããã調æ»ããŠãŸãšããŠããããã
ãŠãªãšããŠã¯ãC 䞊ã®ããã©ãŒãã³ã¹ãåºã Pythonããšãã£ããšããã ãããã
k0kubun ããããã³ã¡ã³ããè£ã§ããã£ãŠãããã£ãŠèŠããã«äžŠååãšã SIMD åãšãå
¥ããã35,000åã®ããã©ãŒãã³ã¹ãåºãããã ãã©ããã㯠Python ã®éšåãšã¯åŒã¹ãªããŠãçŽ ã® Python åãããŠæ¬åœã«ããããããã¯æªãããªãïŒãšã®ããšã§ãå€æä¿çããŸã ðð»ââïž k0kubun ããããããšã
èšèªã®ãã¶ã€ã³ãšããŠã¯ãAI éçºã«åããããã°ã©ãã³ã°ãæäŸã§ããããèšèšãããŠãããšæãããè¡šåŽã¯ Python ã ãã现ããã·ã¹ãã ããã°ã©ãã³ã°èšèªå¯ãã®æäœãã§ããããã«èšèšãããŠããããããèšèªä»æ§ãšããŠäžæ°é貫ããŠããããšã«ãã£ãŠãåŸæ¥ã® Python ã«ãã AI ããã°ã©ãã³ã°ãæ±ããŠããåé¡ã解æ¶ã§ããããšãçã£ãã®ã ãšæããéåžžã® Python ãšã¯ç°ãªãåä»ãããã£ããéçã«ãªãããããã§ãããããŸã Python ãå©çšããäžã§ã®æ倧ã®ããã«ããã¯ã§ãã GIL ããªããªããPython ãšã®ç§»æ€æ§ãããªãæèãããŠãããã¡ã¢ãªç®¡çã«ã€ããŠã¯ Rust ãšåãã§ãããŒãã§ãã«ãŒãæ¡çšããŠãããšæãããã[1]ãå人çã«ã¯ Rust ãæžããŠããéã«æããŠãããã ãŒãã»ãã³ãã£ã¯ã¹åšãã§äžæçã ãšæããã€ã³ããã ãŒãããã³ãã£ã¯ã¹äžå¿ã®èšèªèšèšããããèŸããäžæã«åé¿ã§ãããã¶ã€ã³ã«ãªã£ãŠããã®ã§ã¯ãªãããšæããŠããã
çŸåšã¯ Playground ãžã®å ¥å Žã waitlist æ¹åŒã§ç®¡çãããŠããããã§ãå šå¡ã觊ããç¶æ ã§ã¯ãªããGitHub ãªããžããªã«ãäœãã³ãŒããå ¬éãããŠããããã§ã¯ãªãããã§ãä»ã®ãšãã Doc 以äžã®æ å ±ããªããç§ã waitlist ã«ã¯ç³ã蟌ãã ãã®ã®ãŸã å ¥ããŠããªãããšããããã§ã觊ããŠãããªãèšèªã«ã€ããŠæžããŠããã®ã§ããããã£ãŠããªãããšãå€ãã§ãã
æ£çŽ Python ã«å¯Ÿããäžæºã¯ææ³ãšããããã¯ããŒã«åšããåä»ããã¹ããŒããªã©ã«èµ·å ããããšãå€ããšæããç§ãçŸå Žã§ Python ãæžããŠããŠãŠããšãªãã®ã¯ææ³ãã®ãã®ãããããããéšåã [2]ãææ³ã®ããããããã¯ãããã ãäžã®äžã«åºãŸã£ãŠãœãããŠã§ã¢ãšã³ãžãã¢ã§ãªã人ã§ãã£ãŠãå©çšãããšãããããããããããããã®åžæ°æš©ãåŸãŠãããšèšã£ãŠããããšæããMojo ã¯ãPython ã®ãããšãããæ®ããŠäœ¿ããããã«ãã€ã€ã課é¡ç¹ãæ¹è¯ãããã©ã³ã¹ã®è¯ããé¢çœããã€ã³ããããªãšæã£ãã
ãã®ã¹ã¯ã©ãããæ®ãå§ããã®ã¯åã«ããã¥ã¡ã³ãã« Owned ãšã Borrowed ãšãã®åé¢ãèŠããŠãããããæææš©ã·ã¹ãã ãå¥ã®èšèªã§å®çŸããæ°ããªäŸãªã®ããªãšæã£ãã®ããã£ããã§ãã
-
調ã¹ãŠã¿ãŠåŸã ã«ãããå§ããããã©ãããã ãŒããšã³ããŒãšã¯å¥ã®ã¡ã¢ãªç®¡çãããŠããããªæãããããSwift ã¿ããã«å€åãšåç §åãåããŠç®¡çããŠããå¯èœæ§ããããããæ£çŽãªãšãããPlayground ã«å ¥ã£ãŠäœãèµ·ããŠãããã確ãããŠã¿ãªããšããããããŸããã â©ïž
-
æã¯ã€ã³ãã³ãããŒã¹ã®ææ³ã«ãŠããšãªã£ãããšããã£ãããæè¿ã¯ VSCode ã䜿ãããã«ãªã£ãŠèªåã§ãã©ãŒãããããŠãããããã«ãªã£ãããŸã£ãã®ã§ãããæ £ããŠããŸã£ããããããã¯ããã±ãŒãžç®¡çãšãé åžæ¹æ³ã«ãŠããšãªãããšãå€ãã â©ïž
Mojo ã¯åºæ¬çãªè·¯ç·ã§ã¯ Python ãå šé¢èžè¥²ãã圢ããšã£ãŠããããPython ãéžãã§ããã®ã«ã¯äžèšã®ãããªçç±ããããããã
- Python 㯠ML ããã®ã»ãã®é åã«ãããŠæ¯é çãªããžã·ã§ã³ããã€ããã«ãªã£ãŠããããšããã®ããèšèªã®ææ³èªäœãåŠç¿ãããããã³ãã¥ããã£ããšã³ã·ã¹ãã ã®å å®åºŠåããªã©ãããããã
- åçåä»ãã®æ©æµãåãããã API ãæäŸããã®ã«ãŽã£ãããªèšèªã ããã
äžæ¹ã§ Python 㯠Python ã§æŽå²ã®é·ãèšèªãšããããšãããã次ã®ãããªåé¡ãæ±ããŠãããšææããã
- two-world åé¡: Python ã¯ã·ã¹ãã ããã°ã©ãã³ã°ã«ã¯äžåããªããããã¡ãã䜿çšããŠããã©ãŒãã³ã¹ãåŸãããšããå Žåã«ã¯ C ã C++ çã®ã·ã¹ãã ããã°ã©ãã³ã°ã«ç¹åããèšèªã®åãåããå¿ èŠãããããã ããã¯ãããšãã°å®è£ æã« C ã C++ ãcpython çã®å éšãç¥ã£ãŠããå¿ èŠããããªã©ã®åŠç¿ã³ã¹ãã®é¢ã§ã®åé¡ããããPython ã®äžçãšã·ã¹ãã ããã°ã©ãã³ã°ã®äžçãåé¢ãããŠããããšããããšã³ã·ã¹ãã ãè€éåããŠããã
- three-world ã N-world åé¡: æ©æ¢°åŠç¿ã«ãããŠã¯ããã« CUDA ãªã©ãå¿ èŠã«ãªããããããŸã§å«ããŠäžè²«ããŠå©çšã§ãããããã¬ããããã¡ã€ã©ã®ãããªããŒã«ããªããéçºã«ã¯ããŸããŸãªå¶çŽããããšãããã
- ã¢ãã€ã«ããµãŒããŒåšãã®ãããã€ã®åé¡: ããã¯èŠããã« Python ã®ãã«ãã·ã¹ãã ãé åžæ¹æ³ã«ã€ããŠèšã£ãŠããïŒ
fn
ãš def
ã©ããã Mojo ã«ã¯é¢æ°å®çŸ©ã2ãã¿ãŒã³ãããããã
-
def
: Python ãšå®å šäºæãã§ãããåçæ§ããã€é¢æ°å®çŸ©ã -
fn
:def
ã® "strict mode" ã«ãããããã pedantic 㧠strict ãªïŒèŠããã«å®å šåŽã«åããïŒdef
ãfn
ã«ãªããããã
ããšãã°äžèšã®ãããªéãããããéã«ãããš def
ã«ã¯ããããæ±ããããªã:
- é¢æ°ã®ããã£éšåã¯ããã©ã«ãã§ã€ãã¥ãŒã¿ãã«ã«ãªãããããå éšã®å€æ°å®£èšãšãæäœãšããå šéšã€ãã¥ãŒã¿ãã«ã«ãªãã£ãŠããããšããªïŒã§ãå¯å€ãªæäœãé¢æ°å éšã«ããå Žåãã³ããŒäžå¯èœãªåãåŒæ°ãšããŠäœ¿çšã§ããããã«ãªããããã
- åŒæ°ã«ã¯åã®æå®ãå¿ èŠã«ãªããæ»ãå€ã®åæå®ãå³å¯å¯ãã«ãªããæå®ããªããš None ãè¿ããŠããšèŠãªãããã«ãªãã
- ããŒã«ã«å€æ°ãã¡ãããš
let
ãšvar
ãçšããŠå®£èšããŠäœ¿çšããå¿ èŠãåºãŠããã - def ã fn ãäŸå€ããµããŒãããŠããããfn åŽã§ã¯ raises ã䜿çšããŠæ瀺çã«å®£èšããå¿ èŠãããã
èŠããã« def
ïŒPython åŽïŒã§ç·©ãèšå®ãããŠãããã®ãããå°ãæ瀺çãã€å³å¯ãªèšå®ãããªããšãããªãããšãä¿èšŒããã®ã fn
ãšããããšãªãã ãšæããããMojo åŽã®äžçç·ã§æžãŸããå Žåã¯åºæ¬ã¯ fn
ã§å®çŸ©ãã¡ãããã¿ãããªäœ¿ãæ¹ããããã ããããäžèŠãããšéãããããã«ãããšããã°ãããã«ãããã移æ€æ§ãä¿ã€ããã«ã¯ãããããèšèšã ãšæããã
let
ãš var
äž¡è
å
±ã«å€æ°å®£èšããããã€ãã¥ãŒã¿ãã« = let
ããã¥ãŒã¿ãã« = var
ãã©ã£ã¡ãã¬ãã·ã«ã«ã¹ã³ãŒããªã®ãšãã·ã£ããŒãŠã£ã³ã°ããµããŒãããŠããã
struct
ããŒã¿åã宣èšã§ãããPython ã® class ãšã¯éã£ãŠéçã
struct MyPair:
var first: Int
var second: Int
def __init__(self&, first: Int, second: Int):
self.first = first
self.second = second
def __lt__(self, rhs: MyPair) -> Bool:
return self.first < rhs.first or
(self.first == rhs.first and
self.second < rhs.second)
æææš©åšã
æææš©åšãã«ã€ããŠç¥ãåã«ããŸãããããããã€ãåã®åæåã®æ¹æ³ãããããããŠããããæŽçããŠããå¿ èŠãããããã
ãã®ã»ã¯ã·ã§ã³ã«è©³ãã話ãèŒã£ãŠããã以äžã¯ç§ãç解ããå 容ãç°¡åã«ã¡ã¢ãããã®ã
ãŸã Mojo ã®åã«ã¯ copyable
ãš movable
ããã®ã©ã¡ãã§ããªãæŠå¿µãååšãããããã
- Non-copyable and non-movable: ã¡ã¢ãªã¢ãã¬ã¹ãéèŠãªåãå®çŸ©ããéã«äœ¿çšãããããšãã°ã¢ãããã¯æäœãå¿
èŠãªãã®ãªã©ãåŸè¿°ãã
__moveinit__
ãš__copyinit__
ã®ã©ã¡ããå®çŸ©ãããŠããªãå Žåã«ãã®åãšããŠå€å¥ããããããã - movable: ããããã ãŒãããåã
__moveinit__
ãå®çŸ©ãããšããã«ãªãã - copyable: æåéãã³ããŒã§ããåã ãšæããã©ãè£ã§ã©ãããåŠçã«ãªã£ãŠããã®ãã¯ããããããªããPython ã ãšãªããžã§ã¯ãã¯å
šéšåç
§ã§ã³ããŒå¯èœã ããã©ãè£ã§åç
§ã«ãŠã³ãã§ç®¡çãããŠããããããšåããããªããšãè£ã§è¡ãå¯èœæ§ã¯ããã
__copyinit__
ãå®çŸ©ãããšããã«ãªãã
ããŒã¹ã®ã¢ã€ãã£ã¢ã¯ Swift ã®äœè ãªã ããã£ãŠãSwift ã® value semantics ãš ARC ãããããã¡ãã«ãæã£ãŠãããšããªã®ããªïŒãã®èŸºãã¯æ£çŽåŠçç³»ãå®éã«äœ¿ã£ããèŠããããªããšãŸã 詳现ã¯ããããªãã
ä»åãããããã£ãã®ããããšãã° move ãš copy ãäž¡æ¹å¯èœã«ããåãäžèšã®ããã«å®çŸ©ããå Žå
struct MyString:
var data: Pointer[Int8]
# StringRef is a pointer + length and works with StringLiteral.
def __init__(self&, input: StringRef):
self.data = ...
# Copy the string by deep copying the underlying malloc'd data.
def __copyinit__(self&, existing: Self):
self.data = strdup(existing.data)
# This isn't required, but optimizes unneeded copies.
def __moveinit__(self&, owned existing: Self):
self.data = existing.data
def __del__(owned self):
free(self.data.address)
def __add__(self, rhs: MyString) -> MyString: ...
ã ãŒããšã³ããŒãæ瀺çã«äžèšã®ããã«éžæã§ããç¹ã ãšæãã
fn test_my_string():
var s1 = MyString("hello ")
var s2 = s1 # s2.__copyinit__(s1) runs here
print(s1)
var s3 = s1^ # s3.__moveinit__(s1) runs here
print(s2)
# s2.__del__() runs here
print(s3)
# s3.__del__() runs here
ç§ã®æã Rust ã®äžæºã®ã²ãšã€ã«ãã³ããŒãšã ãŒãã®æå®æ¹æ³ããŸã£ããåãã§ãæåã®ãã¡ã¯ã³ããŒã®åŽã®ååšã«æ°ã¥ãã«ãããšæããç¹ã«ãã£ã[1]ããã®ããã«åºå¥ãã€ããŠãããšã©ãã§ã ãŒããããã®ããäžç®çç¶ã§ããããããããã«æãããMojo 㯠Python ãšã®äºææ§ã®åé¡ããã ãŒãã»ãã³ãã£ã¯ã¹æåãªèšèªã§ã¯ãªãããïŒãšãããã§ããªããïŒã ãããã³ããŒãããã©ã«ãã§ã ãŒãã ^
ã䜿ã£ãç¹å¥æäœãèŠæ±ããã®ã¯çŽåŸã
-
ãã¡ãããRust ã¯ã ãŒãã»ãã³ãã£ã¯ã¹ãäžå¿ã«èšèšãããããã°ã©ãã³ã°èšèªã§ãããããã ãŒããããã©ã«ãåäœã§ããã®ã¯åé¡ã§ã¯ãªããšæãããããã®èŠ³ç¹ããèŠããšã³ããŒã¯ç¹å¥æäœã«ããããããããå°ãããããããåºå¥ããŠãããã£ãããããªãããšããæ°ãããã â©ïž
ãã¹ãã©ã¯ã¿
Rust ã®ã ãŒãã«æ £ããããŠããŠæåäžèšã®ã³ãŒããèŠãŠããïŒããšæã£ãŠããŸã£ãã
fn use_strings():
var a = MyString("hello a")
var b = MyString("hello b")
print(a)
# a.__del__() runs here
print(b)
# b.__del__() runs here
a = MyString("temporary a")
# a.__del__() runs here
other_stuff()
a = MyString("final a")
print(a)
# a.__del__() runs here
other_stuff()
ãåŒã³åºãçŽå㧠a
ã®ãã¹ãã©ã¯ã¿ãåŒã³åºãããã®ãããããããªãã£ããã ãŒãã®å Žåãother_stuff()
ã®çŽåã§æ¶ããããšã¯ãªããŠãa
ãå代å
¥ããç¬éã«æ¶ããããªæããããã
ãããããã Mojo ã®è£åŽã§è¡ãããã®ã¯ã ãŒããããªãã£ãœããèšããªãã°ããã£ã¡ãã¢ã¯ãã£ããªã¬ããŒãžã³ã¬ã¯ã¿ãŒ (hyper-active garbage collector)ããèµ°ã£ãŠããã¿ãããããã¥ã¡ã³ãã®äžã§ã¯ASAP ããªã·ãŒãšãåŒãã§ãããã©ãããã³ã³ãã€ã©ãããããã¯åäœãé¢æ°åŒã³åºããªã©ã®ã¿ã€ãã³ã°ã§ããªãå³å¯ã«ããã®ã¡ã¢ãªé åãäžèŠã«ãªã£ããããšããã§ãã¯ããŠã解æŸããããã«ãªã£ãŠãããããã匷ãã®ãããŒè§£æããããããã«äœãããŠããã®ãããããªãã
print(a) ã®çŽåŸã§ã a.del() ãåŒã°ããŠããã®ã§ãé¢æ°å ããããŒè§£æããŠåããŒã«ã«å€æ°ã䜿ãããªããªã£ãæçäœçœ®ã§ãã¹ãã©ã¯ãåŒã¶ããšããæããªãã§ãããïŒ
print(a)
ã§ã ãŒããèµ·ããã®ããšæã£ãŠããŸã£ãŠããŸããããèµ·ãããªãã° Mojo çã«ã¯ print(a^)
ãšæžãããã§ãããå¯èœãªéãæ©ã解æŸã§ããã¿ã€ãã³ã°ã§ãã¹ãã©ã¯ã¿ãåŒã°ãããšããæåã説æãããããã«ç¢ºãã«èŠããŸããã