profile
viewpoint
If you are wondering where the data of this site comes from, please visit https://api.github.com/users/demotomohiro/events. GitMemory does not store any data, but only uses NGINX to cache data for a period of time. The idea behind GitMemory is simply to give users a better reading experience.

demotomohiro/glew4kb 4

Automate OpenGL extension initialization for 4k intro.

demotomohiro/Falsity-of-employment 3

正式名称は"雇用の欺瞞"です。 Tokyo Demo Fest 2014で公開した4k introのソースコードです。

demotomohiro/Google-Colaboratory-SSH-samples 3

Sample code to access Google Colaboratory using SSH.

demotomohiro/kokocandy 1

Automatic demo video downloading and converting script

demotomohiro/littlesugar 1

Nim macros that might help writing simpler and safer code.

demotomohiro/Loremo 1

Loremo is a neovim plugin to help using neovim inside neovim by adding local/remote mode to each window displaying terminal buffer.

demotomohiro/netlify-nim-test 1

Try using nim compiler on netlify

demotomohiro/adir01pcpp 0

Bit Trade Oneの製品であるUSB赤外線リモコンアドバンス(ADIR01P)をC++から使えるようにしたライブラリ

issue commentnim-lang/Nim

`create` does not work for UncheckedArray, as sizeof(UncheckedArray[T])==0

Documentation comment of create says:

Allocates a new memory block with at least T.sizeof * size bytes.

So I think when create is called with T such that sizeof(T) == 0, it should be compile time error. Because T.sizeof * size is always 0 and create cannot allocate a new memory block that you actually needs.

Making sizeof(UncheckedArray[T]) != 0 can be a breaking change. For example, payloadCheck() proc in tests/misc/tsizeof.nim become failed.

From https://nim-lang.org/docs/manual.html#types-unchecked-arrays

Additionally, an unchecked array is translated into a C array of undetermined size:

In C, C array of undetermined size in a struct is count as 0 bytes.

#include <stdio.h>
#include <stdlib.h>

typedef struct {
  int len;
  int cap;
  int data[];
} MySeq;

int main(void)
{
    printf("sizeof(int): %ld, sizeof(MySeq):%ld\n", sizeof(int), sizeof(MySeq));
    return EXIT_SUCCESS;
}

Output:

sizeof(int): 4, sizeof(MySeq): 8

I think you should create a new proc like proc createUncheckedArray(T: typedesc; size = 1.Positive): ptr UncheckedArray[T] if you really need such a proc.

shirleyquirk

comment created time in 7 days

issue commentnim-lang/Nim

Distinct generic parameter types

This bug is still not fixed.

In Nim manual, there is following example code: https://nim-lang.org/docs/manual.html#generics-implicit-generics

proc p(a: Table, b: distinct Table)

# is roughly the same as:

proc p[Key, Value, KeyB, ValueB](a: Table[Key, Value], b: Table[KeyB, ValueB])

But this code results in compile error.

import tables

proc p(a: Table, b: distinct Table) =
  discard

proc p2[Key, Value, KeyB, ValueB](a: Table[Key, Value], b: Table[KeyB, ValueB]) =
  discard

var
  x: Table[int, string]
  y: Table[string, int]

# p2 works
p2(x, y)

# Compile error:
p(x, y)
/home/jail/prog.nim(17, 2) Error: type mismatch: got <Table[system.int, system.string], Table[system.string, system.int]>
but expected one of:
proc p(a: Table; b: distinct Table)
  first type mismatch at position: 2
  required type for b: CompositeTypeClass
  but expression 'y' is of type: Table[system.string, system.int]

expression: p(x, y)

Nim devel version:

Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-10-12
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 83128f217f63045974a48e61b65386abbfc97352
active boot switches: -d:release
def-

comment created time in 10 days

issue commentnim-lang/RFCs

Procs Bound Exclusively to Objects

Just copying what other major programming language do to Nim doesn't seems good idea unless there is a good reason.

All those who have experience with other programming languages and are learning Nim could benefit from this.

For people who is not familiar with these feature, that just makes learning Nim harder. Also adding new features makes language spec complicated and maintaining Nim compiler harder.

Currently procs operating on objects will always populate the definitions of the current module, even if this is not always necessary nor wanted. As such, intellisense will start suggesting a lot of stuff, even if some of it may be completely impractical (for example, objects that have complicated "constructors"). Not importing these procs is not an option, because otherwise dot notation will not work at all.

If a module has so many procs and you want to import a few of them because importing them all cause problem, I think you should split the module to smaller multiple modules. Even if you can bind procs to a specific type, from module import Book or from module import Book.* can import many unused procs if the imported module defines many procs that are bound to Book object type and you use only a few of them, isn't it?

Having less definitions makes reasoning about code easier. Having less definitions makes certain bugs easier to detect (especially since overloading is possible)

Can your RFC reduce a number of procs need to be implemented? Isn't it just make easier to reduce a number of imported procs?

Some procs have quite general names and can thus be confused with other procs within the current module.

tables module defines multiple types (Table, OrderedTable, CountTable) and it defines multiple general name procs (add, len, del, etc). There are also add, len and del procs for seq type. Can these same name procs be confusing? I can use these procs without problems:

import tables

var t = {'a': 5, 'b': 9, 'c': 13}.toTable
t.del 'a'
echo t.len

var s = @[0, 1]
s.del 0
echo s.len

var ct: CountTable[char]
ct.inc 'a'
ct.inc 'b'
ct.del 'a'
echo ct.len

Even if there are same name and same parameter type procs in different module, you can disambiguate a call by adding module name like modulename.procname(). (But I don't think having mutiple procs with same name and same parameter types in different modules are good idea.)

After you wrote following code, when you write a., your intellisense should suggests only procs that has Table type as first paramter.

import tables

var
  a = {1: "one", 2: "two"}.toTable

Anyway, If books.nim defines proc was_released_before_or_during_1970*(this: Book): bool = and it is bound to Book ref object type and example.nim also defines same name proc and need to import Book type, your RFC still doesn't seems to solve the problems.

Acmion

comment created time in 13 days

issue commentnim-lang/Nim

`==` on NimNode doesn't work for `proc` types

In your isEqual macro, a and b are NimNode types and NimNode type is a ref type. So your code is not comparing types, but pointers.

This file contains same macro that compares given 2 types correctly: Nim/tests/macros/tsametype.nim

isEqual macro can be fixed like this:

import macros

macro isEqual(a, b: typedesc): untyped =
  # If you want to know why it needs `a.getType[1]`
  #echo typeof(a)
  #echo a.getType.treeRepr
  #echo b.getType.treeRepr
  echo sameType(a.getType[1], b.getType[1])

isEqual(int, int)
isEqual(float, int)
isEqual(proc(i:int):int, proc(i:int):int)
isEqual(proc(i:int):int, proc(i:int):float)
isEqual(proc(i:float):int, proc(i:int):int)

Output:

true
false
true
false
false
deech

comment created time in 23 days

issue commentnim-lang/RFCs

Object init shorthand

I also wrote a macro that create a object type definition from a constructor proc: https://github.com/demotomohiro/objectDef

But I'm not using it so much. Standard Nim way to define an object type and a constructor proc is fine.

dom96

comment created time in a month

issue commentdemotomohiro/remocolab

colab gets disconnected

Google Colab Pro page says it still have usage limits. If you are not reached to usage limits and colab runs simple code like this and disconnects within 10 minutes, it is problem of colab and it is better to ask to Google.

import time

begin = time.time()
for i in range(30 * 6):
  time.sleep(10)
  print(time.time() - begin)

I'm not a paid user and I always tested remocolab as free user. I think it works for paid user if it uses same VM image used for a machine for free user.

D-EVE-R

comment created time in a month

delete branch demotomohiro/remocolab

delete branch : update-cloudflared

delete time in a month

push eventdemotomohiro/remocolab

demotomohiro

commit sha 835f3084d948d079b056843a66a9589a22baced4

Update cloudflared download link

view details

Tomohiro

commit sha 3761cda2c03f0e471288a1f15f662a38af06ab8e

Merge pull request #100 from demotomohiro/update-cloudflared Update cloudflared download link

view details

push time in a month

create barnchdemotomohiro/remocolab

branch : update-cloudflared

created branch time in a month

issue commentnim-lang/Nim

Compilation outputs strange error messages (packaging issue?)

How Nim reads configuration files is explained here: https://nim-lang.org/docs/nimc.html#compiler-usage-configuration-files

I think your problem is files in https://github.com/nim-lang/Nim/tree/devel/config are missing. If these configuration files were correctly installed, Nim prints paths to the configuration file it uses.

For example:

$ nim c foo.nim
Hint: used config file '/etc/nim/nim.cfg' [Conf]
Hint: used config file '/etc/nim/config.nims' [Conf]
.................CC: stdlib_io.nim
Nuc1eoN

comment created time in a month

issue commentnim-lang/Nim

Compilation outputs strange error messages (packaging issue?)

They are warning messages comming from backend C compiler. When Nim is executed with c command (like nim c foo.nim), Nim generates C code and asks backend C compiler to compile them and produce an executable file. Nim calls backend C compiler with an option that inhibits warning messages. But it seems your C compiler is called without it.

Did you changed compiler options for backend C compiler? Please run nim c -r --listcmd -f hello_gobo to see which compile options are used when Nim calls backend C compiler.

Nuc1eoN

comment created time in a month

issue commentdemotomohiro/remocolab

It doesn't work, runtime disconnects

No, you don't need to do it. Sometimes colab disconnects after running a cell about 5~10 minutes regardless of what the code do. I don't know how to fix it. Sometimes, it fixed after a few days. Maybe, colab trys to allocate more machines for paid user by disconnecting a machine for free user.

schissmantics

comment created time in a month

issue openednim-lang/Nim

Passing Natural to bitops.BitsRange[T] parameter in generic proc is compile error

Minimum code:

import std/bitops

testBit(1, 1.Natural)

Compile error message:

f:\temp\nim-1.5.1\lib\pure\bitops.nim(63, 37) Error: 'sizeof' requires '.importc' types to be '.completeStruct'

Minimum code without importing bitops:

type BitsRange[T] = range[0..sizeof(T)*8-1]

proc bar[T](a: T; b: BitsRange[T]) =
  discard

bar(1, 2.Natural)

Compile error message:

f:\temp\testbitr.nim(1, 36) Error: 'sizeof' requires '.importc' types to be '.completeStruct'

This code also produces same compile error:

type BitsRange[T] = range[0..sizeof(T)*8-1]

proc bar[T](a: T; b: BitsRange[T]) =
  discard

bar(1.Natural, 2.Natural)

Just converting Natural variable to BitsRange[int] doesn't make compile error. Following code compiles without erorr.

type BitsRange[T] = range[0..sizeof(T)*8-1]

var x: BitsRange[int] = 0
x = 1.Natural
echo x

var y: BitsRange[Natural] = 0
y = 1.Natural
echo y
y = 2
echo y

proc foo(x: BitsRange[int]) =
  discard

foo(1)
foo(1.Natural)

proc foo2(x: BitsRange[Natural]) =
  discard

foo2(1)
foo2(1.Natural)

proc bar[T](a: T; b: BitsRange[T]) =
  discard

bar(1.Natural, 2)

Nim version:

f:\temp>nim -v
Nim Compiler Version 1.5.1 [Windows: amd64]
Compiled at 2021-09-08
Copyright (c) 2006-2021 by Andreas Rumpf

active boot switches: -d:release

created time in a month

Pull request review commentnim-lang/Nim

Fix initrand to avoid random number sequences overlapping

 block: # bug #17670     type UInt48 = range[0'u64..2'u64^48-1]     let x = rand(UInt48)     doAssert x is UInt48++block: # bug #17898+  # Checks whether `initRand()` generates unique states.+  # size should be 2^64, but we don't have time and space.++  # Disable this test for js until js gets proper skipRandomNumbers.+  when not defined(js):+    const size = 1000+    var+      rands: array[size, Rand]+      randSet: HashSet[Rand]+    for i in 0..<size:+      rands[i] = initRand()+      randSet.incl rands[i]++    doAssert randSet.len == size++    # Checks random number sequences overlapping.+    const numRepeat = 100+    for i in 0..<size:+      for j in 0..<numRepeat:+        discard rands[i].next+        doAssert rands[i] notin randSet

Could you tell me why is it weaken randomness? It just works like evenly cut a random number sequence generated by calling next(r: var Rand) with single Rand, and assign each sub sequence to each Rand. If you think randomness of random module is not good enough, I think we need to use different algorithm like xoroshiro128++, xoroshiro128** or PCG.

demotomohiro

comment created time in 2 months

PullRequestReviewEvent

Pull request review commentnim-lang/Nim

Fix initrand to avoid random number sequences overlapping

 proc shuffle*[T](x: var openArray[T]) =    shuffle(state, x) -when not defined(nimscript) and not defined(standalone):-  import times+when not defined(standalone):+  when defined(nimscript):+    import std/hashes++    var baseState = block:+      var ret = Rand(+        a0: CompileTime.hash.Ui,+        a1: CompileDate.hash.Ui)+      if not ret.isValid:+        ret = DefaultRandSeed+      ret+  elif defined(js):+    import std/times+  else:+    import std/[hashes, os, sysrand, monotimes]++    var baseState: Rand = block:+      var+        ret: Rand+        urand: array[sizeof(Rand), byte]++      for i in 0 .. 7:+        if sysrand.urandom(urand):+          copyMem(ret.addr, urand[0].addr, sizeof(Rand))+          if ret.isValid:+            break++      if not ret.isValid:+        # When 2 processes executed at same time on different machines,+        # `ret` can still have same value.+        let+          pid = getCurrentProcessId()+          t = getMonoTime().ticks+        ret.a0 = pid.hash().Ui+        ret.a1 = t.hash().Ui+        if not ret.isValid:+          ret.a0 = pid.Ui+          ret.a1 = t.Ui++      if not ret.isValid:+        ret = DefaultRandSeed+      ret++    when compileOption("threads"):

@planetis-m As @Varriount said, only initRand() proc acquires lock. If you create Rand in each threads and never share them between threads, you don't need to lock when you call next(r: var Rand). If you create only one Rand and want to share it by multiple threads, you would need to acquire lock when you call next(r: var Rand), but it is not a problem this PR cause.

@Varriount In this document, T0 is a initial state of baseState and it is the value came from sysrand.urandom. https://www.mathcha.io/editor/7JPgyt0WSLdH4L7VywudZkBrQF4qJOe8Fp3JBB Then, first Rand returned from initRand() has state T0. second one has T2^64, third one has T2^642, 4th one has T2^643, n-th one has T2^64*(n-1) and so on. Then, first Rand returned from initRand() can use states from T0 to T2^64-1 without overlapping the state with second one. Second one can use from T2^64 to T2^642-1, third one can use from T2^642 to T2^64*3-1, and so on.

@timotheecour I don't think performance of initRand() is so important, because it would not be called as frequently as next(r: var Rand) as initRand() is called only when you create a new Rand. When many Rand are created but they generate few random numbers, performance of initRand() can be important. But in that case, I think hash functions tested in paper would be better: http://jcgt.org/published/0009/03/02/

An obvious way to solve this to make state a thread-local global, and check/initialize it when it is accessed.

I think making state a thread-local variable and Nim compiler automatically inserts a call to randomize() so that it is called when a new thread starts would be better. But I don't know it is possible.

demotomohiro

comment created time in 2 months

PullRequestReviewEvent
PullRequestReviewEvent

Pull request review commentnim-lang/Nim

Fix initrand to avoid random number sequences overlapping

 when not defined(nimscript) and not defined(standalone):       let time = int64(times.epochTime() * 1000) and 0x7fff_ffff       result = initRand(time)     else:-      let now = times.getTime()-      result = initRand(convert(Seconds, Nanoseconds, now.toUnix) + now.nanosecond)+      proc getRandomState(): Rand =+        when defined(nimscript):+          result = Rand(+            a0: CompileTime.hash.Ui,+            a1: CompileDate.hash.Ui)+          if not result.isValid:+            result = DefaultRandSeed+        else:+          var urand: array[sizeof(Rand), byte]++          for i in 0 .. 7:+            if sysrand.urandom(urand):+              copyMem(result.addr, urand[0].addr, sizeof(Rand))+              if result.isValid:+                break++          if not result.isValid:+            # When 2 processes executed at same time on different machines,+            # `result` can still have same value.+            let+              pid = getCurrentProcessId()+              t = getMonoTime().ticks

It quit when urandom failed 8 times in a row now.

demotomohiro

comment created time in 2 months

Pull request review commentnim-lang/Nim

Fix initrand to avoid random number sequences overlapping

 when not defined(nimscript) and not defined(standalone):       let time = int64(times.epochTime() * 1000) and 0x7fff_ffff       result = initRand(time)     else:-      let now = times.getTime()-      result = initRand(convert(Seconds, Nanoseconds, now.toUnix) + now.nanosecond)+      proc getRandomState(): Rand =+        when defined(nimscript):+          result = Rand(+            a0: CompileTime.hash.Ui,+            a1: CompileDate.hash.Ui)+          if not result.isValid:+            result = DefaultRandSeed+        else:+          var urand: array[sizeof(Rand), byte]++          for i in 0 .. 7:+            if sysrand.urandom(urand):+              copyMem(result.addr, urand[0].addr, sizeof(Rand))+              if result.isValid:+                break++          if not result.isValid:+            # When 2 processes executed at same time on different machines,+            # `result` can still have same value.+            let+              pid = getCurrentProcessId()+              t = getMonoTime().ticks+            result.a0 = pid.hash().Ui+            result.a1 = t.hash().Ui+            if not result.isValid:+              result.a0 = pid.Ui+              result.a1 = t.Ui

Done!

demotomohiro

comment created time in 2 months

PullRequestReviewEvent

push eventdemotomohiro/Nim

demotomohiro

commit sha a254cca2572d9fdade47f5612429c85d50bc023f

baseState is initialized only with sysrand.urandom and quit if failed

view details

demotomohiro

commit sha 42c28cb7e6b28db4d985dfcd6a70b0bfbdb6ab47

Add comments

view details

push time in 2 months

issue commentdemotomohiro/remocolab

It doesn't work, runtime disconnects

Please read this: https://github.com/demotomohiro/remocolab/wiki/Frequently-Asked-Questions#google-colab-disconnected-before-or-right-after-remocolabsetupvnc-done

Do you still get disconnected within minutes even if you run a simple python3 code that do not use remocolab like this?

import time

begin = time.time()
for i in range(30 * 6):
  time.sleep(10)
  print(time.time() - begin)
schissmantics

comment created time in 2 months

Pull request review commentnim-lang/Nim

Fix initrand to avoid random number sequences overlapping

 proc shuffle*[T](x: var openArray[T]) =    shuffle(state, x) -when not defined(nimscript) and not defined(standalone):-  import times+when not defined(standalone):+  when defined(nimscript):+    import std/hashes++    var baseState = block:+      var ret = Rand(+        a0: CompileTime.hash.Ui,+        a1: CompileDate.hash.Ui)+      if not ret.isValid:+        ret = DefaultRandSeed+      ret+  elif defined(js):+    import std/times+  else:+    import std/[hashes, os, sysrand, monotimes]++    var baseState: Rand = block:+      var+        ret: Rand+        urand: array[sizeof(Rand), byte]++      for i in 0 .. 7:+        if sysrand.urandom(urand):+          copyMem(ret.addr, urand[0].addr, sizeof(Rand))+          if ret.isValid:+            break++      if not ret.isValid:+        # When 2 processes executed at same time on different machines,+        # `ret` can still have same value.+        let+          pid = getCurrentProcessId()+          t = getMonoTime().ticks+        ret.a0 = pid.hash().Ui+        ret.a1 = t.hash().Ui+        if not ret.isValid:+          ret.a0 = pid.Ui+          ret.a1 = t.Ui

isValid(r: Rand): bool func checks whether state is zero or not.

func isValid(r: Rand): bool {.inline.} =
  not (r.a0 == 0 and r.a1 == 0)

This PR retrys urandom if isValid was false.

demotomohiro

comment created time in 2 months

PullRequestReviewEvent

Pull request review commentnim-lang/Nim

Fix initrand to avoid random number sequences overlapping

 proc shuffle*[T](x: var openArray[T]) =    shuffle(state, x) -when not defined(nimscript) and not defined(standalone):-  import times+when not defined(standalone):+  when defined(nimscript):+    import std/hashes++    var baseState = block:+      var ret = Rand(+        a0: CompileTime.hash.Ui,+        a1: CompileDate.hash.Ui)+      if not ret.isValid:+        ret = DefaultRandSeed+      ret+  elif defined(js):+    import std/times+  else:+    import std/[hashes, os, sysrand, monotimes]++    var baseState: Rand = block:+      var+        ret: Rand+        urand: array[sizeof(Rand), byte]++      for i in 0 .. 7:+        if sysrand.urandom(urand):+          copyMem(ret.addr, urand[0].addr, sizeof(Rand))+          if ret.isValid:+            break++      if not ret.isValid:+        # When 2 processes executed at same time on different machines,+        # `ret` can still have same value.+        let+          pid = getCurrentProcessId()+          t = getMonoTime().ticks+        ret.a0 = pid.hash().Ui+        ret.a1 = t.hash().Ui+        if not ret.isValid:+          ret.a0 = pid.Ui+          ret.a1 = t.Ui++      if not ret.isValid:+        ret = DefaultRandSeed+      ret++    when compileOption("threads"):

Please read this: https://www.mathcha.io/editor/7JPgyt0WSLdH4L7VywudZkBrQF4qJOe8Fp3JBB

Initial state of all Rand must be T2^64n (n is natural number including 0) to avoid overlapping. If baseState were per-thread variable and they were initialized with urandom, initial state of all Rand cannot become T2^64n.

demotomohiro

comment created time in 2 months

PullRequestReviewEvent

Pull request review commentnim-lang/Nim

Fix initrand to avoid random number sequences overlapping

+import std/[random, sets]

I have removed tests/stdlib/trandom.nims and added import stdlib/trandom to tests/test_nimscript.nims.

demotomohiro

comment created time in 2 months