HNHacker News
TopNewBestAskShowJobs

nilknarf

256 karma · joined July 3, 2010

franklinta.com
submissionscomments
nilknarf··on The Stack Monoid
Although I didn't know it by that name, I have seen the bicyclic semigroup in competitive programming many times before (usually any problem involving intervals/brackets and segment trees).

For example in this problem https://codeforces.com/contest/1285/problem/E, one solution is to parse and count the number of top-level parentheses after small modifications. For example the original string might be "(()())()". Then "(()())" is 1, "(())()" is 2, "()()()" is 3.

This is solved with the following monoid to make incremental reparsing fast:

    neutral = Node(0, 0, 0)


    def combine(x, y):
        # Each node tracks numClose, numCount, numOpen:
        # e.g., )))) (...)(...)(...) ((((((
        if x.open == y.close == 0:
            ret = x.close, x.count + y.count, y.open
        elif x.open == y.close:
            ret = x.close, x.count + 1 + y.count, y.open
        elif x.open > y.close:
            ret = x.close, x.count, x.open - y.close + y.open
        elif x.open < y.close:
            ret = x.close + y.close - x.open, y.count, y.open
        return Node(*ret)

Reference solution: https://codeforces.com/contest/1285/submission/92169958

Monoid cached trees are extremely popular in competitive programming, but never by that name. It's always referred to as a "segment tree" without using any mathematical terminology more advanced than "associativity".

Due to its popularity, all kinds of crazy monoid states have already been explored (though you need to squint a bit to see them due to the terminology mismatch): https://codeforces.com/blog/entry/15890. I've never seen stack used though. You almost always want to compress the state as much as possible and the problems I've seen only needed stack depth (aka the bicyclic semigroup), not contents. Requiring O(N) to merge also highly limits its use cases.

Anyway, not sure how relevant this is to you since the use of monoids in data structures and for parallelism is slightly different (binary merges all the way down versus stopping at some chunk size).

nilknarf··on The Stack Monoid
> Automatically generating monoids such as the stack monoid from their corresponding simple sequential programs seems like an extremely promising area for research.

I was interested in this problem a while back and the papers I remembered being useful were:

"Automatic Inversion Generates Divide-and-Conquer Parallel Programs": https://core.ac.uk/download/pdf/192663016.pdf

which is based on "The Third Homomorphism Theorem": http://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/th...

The motivating example is whether you can generate a mergesort (where the monoid is combine two sorted lists) given an implementation of an insertion sort (where the sequential program is inserting one element by one into a sorted list).

I remember being disappointed by the answer after reading these papers but in the citation graph there are a bunch of relatively recent program synthesis papers. Maybe there's something better now.

nilknarf··on Show HN: WebRTC signalling data using QR codes
There were a few interesting ideas by others in the original comment thread. Mostly on how to compress the sdp so you can get away with just one large QR code without flashing (for example by presharing a dictionary or stripping the sdp down to the bare minimum: https://webrtchacks.com/the-minimum-viable-sdp/).

I lost interest in this approach because it was way more practical to give up on serverless in exchange for only scanning in one direction instead of both ways (e.g., scan a QR code once on mobile to both open the webpage and join a websocket channel, then use it to upgrade to a webrtc connection). It's a neat trick for use cases like AR.js where this is natural to do: https://github.com/jeromeetienne/AR.js

nilknarf··on Show HN: WebRTC signalling data using QR codes
Did you happen to see my post on this? https://franklinta.com/2014/10/19/serverless-webrtc-using-qr... (The demo unfortunately doesn't work anymore since it's from 2014)

The flow is very similar to yours, down to the flashing QR codes! https://youtu.be/rw4f9LfqqpI?t=8

nilknarf··on Curry–Howard correspondence
The answer is surprisingly yes!

It's usually called equational reasoning or Bird–Meertens formalism.

Transforming an insertion sort into merge sort is covered in Section 5 of "FUNCTIONAL PEARLS The Third Homomorphism Theorem":

http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.45....

nilknarf··on Making Visible Watermarks More Effective
It's pretty hard to fully protect in the first place because someone desperate enough can always inpaint (aka content-aware fill) over the watermark. Project Naptha has a pretty nice web demo showing how well that works for erasing text from images: https://projectnaptha.com/

Off-topic: Can you fill out your profile? I am not active on HN but I sometimes go by the initials fta elsewhere

nilknarf··on Machine Learning Crash Course: Part 2
I didn't expect to see Hopcroft/Kannan in that list. I used it in a course taught by Kannan and back then it was called "Computer Science Theory for the Information Age". Apparently the book is now called "Foundations of Data Science". I always thought the old naming was terrible but compared to the new title it actually describes the content a lot better since the book is mainly about CS theory and mathematical foundations for them. It was one of my favorite courses but I would not classify it as ML book.

[1]https://www.cs.cmu.edu/~venkatg/teaching/CStheory-infoage/

[2]https://www.cs.cornell.edu/jeh/book2016June9.pdf

nilknarf··on IP Over QR Code
Hey I'm the author of the project you linked to! The blog post is better for details: http://franklinta.com/2014/10/19/serverless-webrtc-using-qr-...

Flashing qr codes was just a quick hack and actually doesn't work super well. I think if I had more time I would try compressing and stripping down the message more so it would fit in a single qr code instead (like in https://webrtchacks.com/the-minimum-viable-sdp/).

nilknarf··on From first principles: Why I bet on Scala.js
Great post! Just curious, why didn't you include emscripten (C/C++) in your comparisons?
nilknarf··on Math.random in V8 is broken
I have previously written about how to predict the next Math.random in Java (and hence Firefox also): http://franklinta.com/2014/08/31/predicting-the-next-math-ra... The TL;DR; is that it is easy since you only have to brute force around 2^22 possibilities (which runs in < 1 second).

Someone then asked whether it is also possible in Chrome and Node: https://github.com/fta2012/ReplicatedRandom/issues/2. After examining the MWC1616 code I saw the same “two concatenated sub-generators” problem explained in this post. The implication of it is that you can brute force the top and bottom 16 bits independently. So it is also easy since that’s just doing 2^16 possibilities twice! Code: https://gist.github.com/fta2012/57f2c48702ac1e6fe99b

nilknarf··on ImageMagick, four point perspective distortion in a video
The math for working out the transform is actually the same (not an inverse!) since you are still just trying to map 4 points to 4 points.

I had a demo for the 'inverse' transform also: http://codepen.io/fta/pen/LHonf

Here is the same thing except using the video from this post: http://codepen.io/fta/pen/JoGybG

nilknarf··on Show HN: QuaggaJS, a barcode scanner written in JavaScript
Being able to scan barcodes using a web app is pretty cool. I used something similar when I was tinkering with transmitting data visually for the handshake step of webrtc: http://franklinta.com/2014/10/19/serverless-webrtc-using-qr-....

I used Lazarsoft's javascript QR code scanner which worked, but it wasn't very robust since it was a manual port of an early version of zxing. It is pretty easy to compile the c++ branch of zxing v2.3 using emscripten but it is a pain to optimize: https://github.com/fta2012/zxing-emscripten (or demo link: http://fta2012.github.io/zxing-emscripten/).