{ "metadata": { "language": "Julia", "name": "", "signature": "sha256:0b43dda4f4929ba951ffdb65a44e33a5e5619e0de7942dacb7e939c8a35868fa" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "## Desiderata\n", "## ================\n", "## This notebooks aims to:\n", "## * Organize itself around a minimalistic, functional core (like \u00b5Kanren)\n", "## * Explore basic \"notions\" about nice syntax; \n", "## Many fanciful notions about syntax abound, \n", "## but most will saved for later notebooks.\n", "\n", "## Methodology\n", "## =================\n", "## The beginning will be rough and sloppy; any elegance will have to be accumulated later, or maybe never.\n", "## This is a notebook, not a library --> Prior definitions will be redefined as we go along, \n", "## so keep your eyes peeled! \n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "## First, some odds and ends.\n", "\n", "## For now we would like to stick to core Julia datatypes, but, aesthetically, var? = vector? is a bitter pill.\n", "## So instead we'll use our own type just this once. \n", "\n", "## it's best to wrap Julia types in a module, otherwise it is very difficult to change your mind\n", "module LilKanren \n", " immutable type Var\n", " id::Int\n", " end\n", "end \n", "LK = LilKanren\n", "\n", "\n", "## Package dependencies: Lazy, Patchwork, Gadly, Dataframes\n", "## these should be available on juliabox by default, but you may need to add\n", "## them to your local julia install" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "LilKanren" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "## Also, in case you are new to Julia, the following snippets may be helpful\n", "x = LK.Var(0)\n", "y = LK.Var(0)\n", "\n", "@show x==y\n", "@show x===y #egal\n", "\n", "## multiple dispatch\n", "gotSomeVars(x,y) = \"nope\" ## base case\n", "gotSomeVars(x::LK.Var,y::LK.Var) = \"yes\"\n", "gotSomeVars(x::LK.Var,y) = \"kinda\"\n", "gotSomeVars(x,y::LK.Var) = gotSomeVars(y,x)\n", "\n", "@show gotSomeVars(x,y)\n", "@show gotSomeVars(1,y)\n", "@show gotSomeVars(1,2)\n", "\n", "## julia allows for convenient destructuring of tuples\n", "z = (1,(2,3))\n", "(\u03b1,(\u03b2,\u03b3)) = z\n", "## n.b. little schemer's, (a,b) means (a . b), and there is\n", "## no primacy of lists in Julia\n", "\n", "## infix\n", "\u2237 = gotSomeVars\n", "@show x \u2237 y\n", "@show 1 \u2237 y\n", "@show 1 \u2237 2\n", "\n", "nothing ## to suppress notebook output" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x == y => true" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "x === y => true\n", "gotSomeVars(x,y) => \"yes\"\n", "gotSomeVars(1,y) => \"kinda\"\n", "gotSomeVars(1,2) => \"nope\"\n", "(x \u2237 y) => \"yes\"\n", "(1 \u2237 y) => \"kinda\"\n", "(1 \u2237 2) => \"nope\"\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "## On with the show, being relatively faithful from now on... \n", "##\n", "## A translation of \u00b5Kanren\n", "## ---------------------------\n", "##\n", "## Heaven\n", "## ======\n", "unify(u, v, s) = begin\n", " (u,s) = walk(u,s) # dramatic foreshadowing \n", " (v,s) = walk(v,s)\n", " unifyTerms(u,v,s)\n", "end\n", "\n", "\n", "## Earth\n", "## =====\n", "## The Church\n", "## ----------\n", "unifyTerms(u,v,s) = begin\n", " sn = unifyLeft(u,v,s)\n", " sn == nothing ? unifyLeft(v,u,s) : sn ## symmetrize\n", "end\n", "## s -- the substitution store\n", "const empty_store = ()\n", "ext_s(x,v,s) = ((x,v),s)\n", "\n", "## st -- a (bindings,count) based state \n", "const empty_state = (empty_store,1) ## when in Rome (Julia is one's based...) \n", "const \u2205 = empty_state\n", "const \u221e = empty_state ## it's a \"glass half full\" sort of thing...\n", " ## \u2205 constraints \u2261 \u221e possibilities\n", "\n", "## The State\n", "## --------------\n", "unifyLeft(u,v,s) = u===v ? s : nothing # base case\n", "unifyLeft(u::LK.Var,v,s) = u==v ? s : ext_s(u,v,s)\n", "unifyLeft(u::(Any,Any),v::(Any,Any),s) = begin\n", " (uhead,utail) = u\n", " (vhead,vtail) = v\n", " s = unify(uhead,vhead,s)\n", " s == nothing ? s : unify(utail,vtail,s)\n", "end\n", "\n", "\n", "## The Bourgeoisie\n", "## ----------------\n", "walk(u,s) = (u,s) ## base case\n", "walk(u::LK.Var,s) = begin\n", " (found,value) = lookup(u,s)\n", " found == :\u2714\ufe0e ? walk(value,s) : (u,s) \n", "end\n", "\n", "\n", "## And finally, Joe The Plumber\n", "## ----------------------------\n", "## a pedantic man's assp <-- n.b. non-schemer's, this does not mean something dirty\n", "lookup(x,s::()) = (:\u2718,nothing) ## base case\n", "lookup(x,s) = begin\n", " (head,rest) = s\n", " (key,value) = head\n", " x == key ? (:\u2714\ufe0e, value) : lookup(x,rest)\n", "end\n", "\n", "second(x) = x[2]\n", "\n", "\n", "## Purgatory -- aka Algebra \n", "## ========================\n", "equivalent(u,v) = begin\n", " (st) -> begin\n", " (s,count) = st\n", " s = unify(u,v,s)\n", " s == nothing ? mzero : unit((s,count))\n", " end\n", "end\n", "const \u2261 = equivalent \n", "\n", "fresh(f::Function) = begin\n", " (st) -> begin\n", " (bindings,count) = st\n", " var = LK.Var(count)\n", " goal = f(var)\n", " st = (bindings,count+1)\n", " goal(st) \n", " end\n", "end\n", "\n", "disj(g1,g2) = (st) -> mplus(g1(st), g2(st))\n", "conj(g1,g2) = (st) -> bind(g1(st), g2)\n", "const \u2228 = disj\n", "const \u2af7 = disj #this is the picture in my head -- a weave \n", "const \u2227 = conj\n", "const \u2af8 = conj #this is the picture in my head -- a braid\n", "\n", "## The Bowels of Hell... \n", "## (or Heaven -- the whole thing is just a big snake eating its tail)\n", "## ==================================================================\n", "const mzero = ()\n", "unit(st) = (st,mzero)\n", "\n", "mplus(strand1::(),strand2) = strand2\n", "mplus(strand1::Function,strand2) = () -> mplus(strand2,strand1()) ## A true braid\n", "mplus(strand1::(Any,Any),strand2) = (first(strand1),mplus(second(strand1),strand2))\n", "\n", "bind(strand::(),g) = mzero\n", "bind(strand::Function,g) = () -> bind(strand(),g) \n", "bind(strand::(Any,Any),g) = mplus(g(first(strand)), bind(second(strand),g)) ## A true weave\n", "\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "## Ok -- Let's take this puppy out for a spin...\n", " \n", "using Lazy: @>>, @> ## this should work on juliabox, if not, Pkg.add(\"Lazy\") \n", "\n", "vars = [LK.Var(i) for i=1:10] ## order up some vars\n", "\n", "## create a scratch store for testing\n", "s = @>> empty_store begin\n", " ext_s(vars[1], 2)\n", " ext_s(vars[2], vars[1])\n", " ext_s(vars[1], 3) #shadows prior binding\n", " ext_s(vars[3], (:\u2746,vars[2]))\n", "end\n", "@show s\n", "\n", "println()\n", "@show lookup(\"not even close\", s)\n", "@show lookup(vars[1], s)\n", "@show lookup(vars[2], s)\n", "@show lookup(vars[3], s)\n", "@show lookup(vars[4], s)\n", "\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "s => ((Var(3),(:\u2746,Var(2))),((Var(1),3),((Var(2),Var(1)),((Var(1),2),()))))" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "lookup(\"not even close\",s) => (:\u2718,nothing)\n", "lookup(vars[1],s) => (:\u2714\ufe0e,3)\n", "lookup(vars[2],s) => (:\u2714\ufe0e,Var(1))\n", "lookup(vars[3],s) => (:\u2714\ufe0e,(:\u2746,Var(2)))\n", "lookup(vars[4],s) => (:\u2718,nothing)\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "@show first(walk(vars[1],s))\n", "@show first(walk(vars[2],s))\n", "@show first(walk(vars[3],s))\n", "@show first(walk(vars[4],s))\n", "@show first(walk(\"not even close\",s))\n", "@show first(walk((:\u272f,vars[4]),s))\n", "\n", "\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "first(walk(vars[1],s)) => 3" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "first(walk(vars[2],s)) => 3\n", "first(walk(vars[3],s)) => (:\u2746,Var(2))\n", "first(walk(vars[4],s)) => Var(4)\n", "first(walk(\"not even close\",s)) => \"not even close\"\n", "first(walk((:\u272f,vars[4]),s)) => (:\u272f,Var(4))\n" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "## Look, Ma! I can fly!\n", "@show unify((:\u2746,vars[4]), vars[3],s)\n", "# for clarity\n", "println()\n", "@show first(unify((:\u2746,vars[4]), vars[3],s))\n", "\n", "## That's nice, sweetie. \n", "\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "unify((:\u2746,vars[4]),vars[3],s) => ((Var(4),3),((Var(3),(:\u2746,Var(2))),((Var(1),3),((Var(2),Var(1)),((Var(1),2),())))))" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "first(unify((:\u2746,vars[4]),vars[3],s)) => (Var(4),3)\n" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "## Reassured by Mother's kind encouragement, we try climbing higher...\n", "\n", "fresh() do q\n", " q \u2261 :\u2663\n", "end (\u221e)\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "((((Var(1),:\u2663),()),2),())" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "## and little higher...\n", "\n", "\u2af8(\n", "fresh() do a\n", " a \u2261 :\u2663\n", "end,\n", "\n", "fresh() do b\n", " \u2af7(\n", " b \u2261 :\u2660,\n", " :\u2661 \u2261 b \n", " ) \n", "end\n", ")(\u221e)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "((((Var(2),:\u2660),((Var(1),:\u2663),())),3),((((Var(2),:\u2661),((Var(1),:\u2663),())),3),()))" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "## (I think) I like the above notation, but you may prefer:\n", "\n", "\u221e |> fresh(a-> a \u2261 :\u2663) \u2227 fresh(b-> (b \u2261 :\u2660) \u2228 (:\u2661 \u2261 b) )" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "((((Var(2),:\u2660),((Var(1),:\u2663),())),3),((((Var(2),:\u2661),((Var(1),:\u2663),())),3),()))" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "## \u266a Pump up the jam \u266a\n", "## let's make a few new toys\n", "\n", "fresh(f::Function,n) = begin\n", " (sc) -> begin\n", " (bindings,count) = sc\n", " vars = [LK.Var(i) for i=count:count+(n-1)]\n", " goal = f(vars...)\n", " sc = (bindings,count+n)\n", " goal(sc) \n", " end\n", "end\n", "\n", "fresh(3) do a,b,c\n", " \u2af8(\n", " (c,:\u2660) \u2261 (((a,b),:\u2663),:\u2660),\n", " \u2af8(\n", " (:\u2661,a) \u2261 b,\n", " a \u2261 :\u2663\n", " ))\n", "end (\u221e)\n" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "((((Var(1),:\u2663),((Var(2),(:\u2661,Var(1))),((Var(3),((Var(1),Var(2)),:\u2663)),()))),4),())" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "fresh(3) do a,b,c\n", " \u2af8(\n", " (c,:\u2660) \u2261 (((a,b), :\u2663), :\u2663), # <-- look here\n", " \u2af8(\n", " (:\u2661,a) \u2261 b,\n", " a \u2261 :\u2663\n", " ))\n", "end (\u221e)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "()" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "fresh(3) do a,b,c\n", " \u2af8(\n", " (c, a) \u2261 (((a,b),:\u2663),:\u2663), # <-- still look here\n", " \u2af8(\n", " (:\u2661,a) \u2261 b,\n", " a \u2261 :\u2663\n", " ))\n", "end (\u221e)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "((((Var(2),(:\u2661,Var(1))),((Var(1),:\u2663),((Var(3),((Var(1),Var(2)),:\u2663)),()))),4),())" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "## variadic paradise is one of our many goals\n", "## but for now this will suffice\n", "## n.b. This is not a nanny-state. BYO\u03b7\u207b\u00b9 \n", "## (but see below for some gov't approved options)\n", "conj(goals...) = begin \n", " # avoiding an infinite loop relies \n", " # on our prior definition and \n", " # Julia's dispatch being smart re: splats\n", " conj(goals[1], conj(goals[2:end]...)) \n", "end\n", "\n", "disj(goals...) = begin \n", " disj(goals[1], disj(goals[2:end]...)) \n", "end\n", "conj(goal) = goal \n", "disj(goal) = goal\n", "\n", "\n", "fresh(3) do a,b,c\n", " \u2af8(\n", " (c,:\u2663) \u2261 (((a,b),:\u2663), a),\n", " (:\u2661,a) \u2261 b,\n", " a \u2261 :\u2663\n", " )\n", "end (\u221e)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "((((Var(2),(:\u2661,Var(1))),((Var(1),:\u2663),((Var(3),((Var(1),Var(2)),:\u2663)),()))),4),())" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "## Our first relation!\n", "##\n", "## And one of our first serious transgressions: \n", "## the output will come first --> my cult is organized around prophecies of variadic paradise\n", "## But the cute \u1d52 at the end isn't going anywhere! \n", "select\u1d52(o,tuple,which) = begin\n", " fresh(2) do a, b\n", " \u2af8(\n", " (a,b) \u2261 tuple,\n", " \u2af7( \n", " \u2af8(which \u2261 :\u2116\ud835\udfd9,o \u2261 a),\n", " \u2af8(which \u2261 :\u2116\ud835\udfda,o \u2261 b), \n", " ) \n", " )\n", " end\n", "end\n", "\n", "fresh(2) do q,r\n", " \u2af8(\n", " select\u1d52(q, (:\u2661, :\u2663), r),\n", " \u2af7( r \u2261 :\u2116\ud835\udfd9, r \u2261 :\u2116\ud835\udfda)\n", " )\n", "end (\u221e)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ "((((Var(1),:\u2661),((Var(2),:\u2116\ud835\udfd9),((Var(4),:\u2663),((Var(3),:\u2661),())))),5),((((Var(1),:\u2663),((Var(2),:\u2116\ud835\udfda),((Var(4),:\u2663),((Var(3),:\u2661),())))),5),()))" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "## Onto divergence\n", "\n", "## But first, in Julia, we require the following helper\n", "list() = ()\n", "list(x...) = (x[1], list(x[2:end]...))\n", "const \u2740 = list # There should be more flowers in CS\n", "\n", "@show \u2740(1,2,3,4) \n", "\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\u2740(1,2,3,4) => (1,(2,(3,(4,()))))" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "## grant ourselves a magic power\n", "\n", "# DRAGON, SLEEP!\n", "\u03b7\u207b\u00b9(goal) = :((st) -> () -> $(esc(goal))(st))\n", "\n", "macro \u263d(goal) ## It's a half moon\n", " \u03b7\u207b\u00b9(goal)\n", "end\n", "\n", "macro zzz(op, goals...) \n", " Expr(:call, op, [\u03b7\u207b\u00b9(goal) for goal in goals]...)\n", "end\n", "\n", "\n", "\n", "nothing\n", "\n", "\n", "## What's all this poppycock about a 'nanny-state'?\n", "##\n", "## (now is probably a good time for a reminder that you are very likely \n", "## reading the blustery rantings of a lunatic....)\n", "#\n", "## First a little perspective is in order:\n", "## The first part of the \u03bcKanren paper introduces a functional kernel for search, \n", "## the second part demonstrates how to reintroduce features available\n", "## in miniKanren, a relational system designed to be syntactically similar to Scheme \n", "## (presumably for didactic and aesthetic reasons)\n", "## \n", "## conj+ and disj+ are offered to partially recover miniKanren's semantics, wrapping\n", "## zzz's around the kit and kaboodle.\n", "##\n", "## We set out on a different path. Neither shall we prophylactically 'zzz', \n", "## only to find that our fear of dragons makes us prisoners of our own design,\n", "## nor shall we become crotchety misers, hoarding our power for a journey that \n", "## we never take.\n", "##\n", "## Rather, we shall look to travel the Silk Roads, where a clever knack for the\n", "## apropos application of pixie dust may just lead us to marvels beyond belief. \n", "## \n", "## So now we have two quests: Variadic Paradise and the Pixie Dust Legerdemain " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "@show macroexpand(:( @\u263d(:\u2740 \u2261 :\u2663) ))\n", "\n", "@show macroexpand(:( @zzz \u2228 (:\u2740 \u2261 :\u2663) (:\u2740 \u2261 :\u2663) ))\n", "\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "macroexpand(:(@\u263d (:\u2740 \u2261 :\u2663))) => :(#224#st->begin # In[17], line 4:\n", " ()->begin # In[17], line 4:\n", " ((:\u2740 \u2261 :\u2663))(#224#st)\n", " end\n", " end)" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "macroexpand(:(@zzz \u2228 (:\u2740 \u2261 :\u2663) (:\u2740 \u2261 :\u2663))) => :(\u2228(#226#st->begin # In[17], line 4:\n", " ()->begin # In[17], line 4:\n", " ((:\u2740 \u2261 :\u2663))(#226#st)\n", " end\n", " end,#227#st->begin # In[17], line 4:\n", " ()->begin # In[17], line 4:\n", " ((:\u2740 \u2261 :\u2663))(#227#st)\n", " end\n", " end))\n" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "## Now that we have the power to put the dragon to sleep, we need a way to wake it up.\n", "\n", "## But not this way!\n", "allIn(x)=x ## as they say in Texas Holdem'\n", "allIn(x::Function)=allIn(x()) \n", "allIn(x::(Any,Any))= begin (h,t)=x; (h,allIn(t)) end \n", "\n", "## lilinjn is not Navajo; but if this notebook were a tapestry,\n", "## 'allIn' would be his \"ch\u2019ih\u00f3n\u00edt\u2019i\" -- use 'allIn' maybe never \n", "\n", "## For now we will content ourselves with the following hand tools:\n", "step(x,n=1)=x\n", "step(x::Function,n=1) = begin\n", " ## In my heart, Julia is a fine Scheme; however, I don't think Jeff B.\n", " ## and co. are going to be driving around with TCO vanity plates\n", " ## anytime soon...\n", " while(typeof(x) <: Function && n > 0)\n", " x = x()\n", " n -= 1\n", " end\n", " x\n", "end\n", "\n", "## Oishii onigiri for the long journey ahead\n", "stepInTime(x,nanos)=x ## \u266a Chim Chimer-ee, Chim Chim Cher-ee \u266a\n", "stepInTime(x,nanos) = begin \n", " start = Base.time_ns()\n", " current = start\n", " while(typeof(x) <: Function && current-start < nanos)\n", " x = x()\n", " current = Base.time_ns()\n", " end\n", " x\n", "end\n", "\n", "\u03b7s = 1\n", "s = 1_000_000_000\n", "\n", "\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "@show step(:\u2661)\n", "\n", "myLovely\u2661Thunks = () -> () -> () -> () -> :\u2606\n", "@show step(myLovely\u2661Thunks, 2) ## \u266a gonna make you luv drunk \u266a\n", "@show step(myLovely\u2661Thunks, 10) ## \u266a off my thunks \u266a\n", "\n", "@show stepInTime(myLovely\u2661Thunks, 1\u03b7s)\n", "@show stepInTime(myLovely\u2661Thunks, 1s)\n", "\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "step(:\u2661) => :\u2661" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "step(myLovely\u2661Thunks,2) => (anonymous function)\n", "step(myLovely\u2661Thunks,10) => :\u2606\n", "stepInTime(myLovely\u2661Thunks,1\u03b7s) => (anonymous function)\n", "stepInTime(myLovely\u2661Thunks,1s) => :\u2606\n" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "## rest your weary eyes, little dragon\n", "res = \u221e |> fresh(q-> (q \u2261 :\u2660) \u2228 @\u263d(:\u2661 \u2261 q) )\n", "\n", "# since we're so new to this, we'll break down the result manually\n", "@show res\n", "(res,cont) = res # <-- it's hard to have known you would have needed this in advance \n", "@show (res,cont)=step(cont)\n", "\n", "\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "res => ((((Var(1),:\u2660),()),2),(anonymous function))" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "(res,cont) = step(cont) => ((((Var(1),:\u2661),()),2),())\n" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "## Variadic Sleep \n", "\n", "res = \u221e |> fresh(q-> @zzz \u2af7 (q \u2261 :\u2660) (:\u2661 \u2261 q) (q \u2261 :\u272a) )\n", "\n", "## still practicing tearing down the result\n", "@show res\n", "@show res=step(res) # a pure thunk arrives this time\n", "@show (res,cont)=step(res) \n", "@show (res,cont)=step(cont)\n", "@show (res,cont)=step(cont)\n", "\n", "\n", "\n", "nothing\n", "\n", "## Something to ponder... \n", "## Why does it go \"thunk,thunk,kebang\"\n", "## rather than \"thunk, dribble, dribble\"?\n", "## Ans: Because it was broken, but hopefully now it is fixed \n", "## (p.s. orginally there were only two clauses)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "res => (anonymous function)" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "res = step(res) => (anonymous function)\n", "(res,cont) = step(res) => ((((Var(1),:\u2660),()),2),(anonymous function))\n", "(res,cont) = step(cont) => ((((Var(1),:\u2661),()),2),((((Var(1),:\u272a),()),2),()))\n", "(res,cont) = step(cont) => ((((Var(1),:\u272a),()),2),())\n" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "forever\u1d52(o,val) = begin \n", " (o \u2261 val) \u2228 @\u263d(forever\u1d52(o,val))\n", "end\n", "\n", "res = \u221e |> fresh(2) do q,r \n", " \u2af8(\n", " forever\u1d52(r, q),\n", " r \u2261 :\u272a\n", " )\n", "end\n", "\n", "## Uncomment to hear the theme to M*A*S*H\n", "## @show res = allIn(res)\n", "\n", "## one last time\n", "@show res\n", "(res,cont) = res\n", "@show (res,cont) = step(cont)\n", "@show (res,cont) = step(cont)\n", "@show (res,cont) = step(cont)\n", "@show (res,cont) = step(cont)\n", "@show (res,cont) = step(cont)\n", "\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "res => ((((Var(1),:\u272a),((Var(2),Var(1)),())),3),(anonymous function))" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "(res,cont) = step(cont) => ((((Var(1),:\u272a),((Var(2),Var(1)),())),3),(anonymous function))\n", "(res,cont) = step(cont) => ((((Var(1),:\u272a),((Var(2),Var(1)),())),3),(anonymous function))\n", "(res,cont) = step(cont) => ((((Var(1),:\u272a),((Var(2),Var(1)),())),3),(anonymous function))\n", "(res,cont) = step(cont) => ((((Var(1),:\u272a),((Var(2),Var(1)),())),3),(anonymous function))\n", "(res,cont) = step(cont) => ((((Var(1),:\u272a),((Var(2),Var(1)),())),3),(anonymous function))\n" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "## Lead us not into Temptation...\n", "##\n", "## Reification is Lucifer's work. \n", "##\n", "## But at the same time, we are lunatics, not ascetics,\n", "## and so, on this day, we grant ourselves a few guilty pleasures.\n", "##\n", "## (Don't worry -- when time comes around to get down with the underground,\n", "## we plan to get our swerve on)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "## First some infrastructure\n", "\n", "using Patchwork\n", "using Patchwork.HTML5\n", "\n", "## hackety-hack\n", "## 'using Patchwork' embeds vital js into the output cell, \n", "## but 'using' is meant to be idempotent in Julia, which causes issues\n", "## when re-evaluating the entire sheet en masse. \n", "## (the re-evaluated cell no longer has the js)\n", "## this hack forces the js embedding regardless (possibly duplicating it)\n", "Patchwork.load_js_runtime() \n", "\n", "\n", "## flex layout wrapper\n", "wrapper(e...) = begin \n", " d = div(style=[:display => :flex, \n", " \"flex-wrap\"=>:wrap]) \n", " for i=1:length(e)\n", " d = d << (div(e[i]) & [:style => [:display=>\"inline-block\", :margin=>\"10px\"]])\n", " end\n", " d \n", "end\n", "\n", "\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "display_data" }, { "html": [ "" ], "metadata": {}, "output_type": "display_data" } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "## A Table for your Store \n", "## (but no reification on which to rest)\n", "\n", "##\n", "## Can't you just feel your muscles and joints getting stiff?\n", "\n", "\n", "storeTable(s) = begin\n", " function items(s::(),res=Elem[])\n", " return res\n", " end\n", " function items(s::(Any,Any),res=Elem[])\n", " (b, rest) = s\n", " push!(res,binding(b, length(res)+1))\n", " items(rest,res)\n", " end\n", " function binding(b, index) \n", " r = tr(td(var(first(b))), td(value(second(b))))\n", " if index % 2 == 0\n", " r = r & [:className => \"alt\"]\n", " end\n", " r\n", " end\n", " function var(v)\n", " s = @sprintf \"%s\" v\n", " span(s)\n", " end\n", " function value(v)\n", " s = @sprintf \"%s\" v\n", " span(s)\n", " end\n", " \n", " div(\n", " table(\n", " thead(tr(th(\"Var\"),th(\"Binding\"))), \n", " tbody(items(s)...))) & [:className => :datagrid]\n", "end\n", "\n", "## and the cholesterol clogging you arteries?\n", "\n", "\n", "## Our friend from before:\n", "s = @>> empty_store begin\n", " ext_s(vars[1], 2)\n", " ext_s(vars[2], vars[1])\n", " ext_s(vars[1], 3) #shadows prior binding <- breaking a key invariant (Thanks, Will!)\n", " ext_s(vars[3], (:\u2746,vars[2]))\n", "end\n", "\n", "stable = storeTable(s)\n", "\n", "display(\"text/html\", wrapper(stable,stable,stable,stable)) # one is never enough\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "display_data" } ], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "## Forgetting to apply our beeswax prior to entering the neighborhood big box superstore, \n", "## we are lulled into submission by the Sirens of Hock,\n", "## and impulsively purchase a flimsy power tool on the way out...\n", "\n", "function slurpInTime(goal; # Mary Poppins does not approve\n", " maxResults = 10, maxSteps=1_000_000, maxTime=5s,\n", " suppressOutput=false, elideAfter=20) \n", " \n", " ## VIP's\n", " steps = 0\n", " results = Any[]\n", " \n", " ## thank you, julia!\n", " startBytes = Base.gc_bytes()\n", " start = Base.time_ns()\n", " startGC = Base.gc_time_ns()\n", "\n", " thisStart = start\n", " thisFirstStep = steps\n", "\n", " stream = \u221e |> goal \n", " \n", " done = () -> (steps >= maxSteps || length(results) >= maxResults || current - start >= maxTime || stream == ())\n", " \n", " steps += 1\n", " current = Base.time_ns()\n", " \n", " while(!done())\n", " if (typeof(stream) <: Function)\n", " stream = stream()\n", " steps += 1\n", " else\n", " if (typeof(stream) <: (Any,Any))\n", " push!(results, (first(stream),steps-thisFirstStep,current-thisStart))\n", " thisFirstStep = steps\n", " thisStart = current\n", " stream = second(stream)\n", " end\n", " end\n", " current = Base.time_ns()\n", " end\n", " \n", " endGC = Base.gc_time_ns()\n", " current = Base.time_ns()\n", " endBytes = Base.gc_bytes()\n", " \n", " \n", "########################################\n", " ##utils\n", " function niceTime(nanos;morePrecision=false) \n", " f = morePrecision ? format2 : format\n", " if nanos < 1_000.0\n", " \"$(f(nanos)) \u03b7s\"\n", " elseif nanos < 1_000_000.0\n", " \"$(format2(nanos/1_000.0)) \u03bcs\"\n", " elseif nanos < 1_000_000_000.0\n", " \"$(format2(nanos/1_000_000.0)) ms\"\n", " else \n", " \"$(format2(nanos/1_000_000_000.0)) s\"\n", " end \n", " end\n", "\n", " function niceBytes(bytes;morePrecision=false) \n", " f = morePrecision ? format2 : format\n", " if bytes < 2^10\n", " \"$(f(bytes)) bytes\"\n", " elseif bytes < 2^20\n", " \"$(format2(bytes/2^10)) KB\"\n", " elseif bytes < 2^30\n", " \"$(format2(bytes/2^20)) MB\"\n", " else \n", " \"$(format2(bytes/2^30)) GB\"\n", " end \n", " end\n", "\n", " function format(x)\n", " @sprintf \"%.f\" x\n", " end\n", " function format2(x)\n", " @sprintf \"%.2f\" x\n", " end\n", "\n", " function annotateTable(table, nextVar, steps, nanos)\n", " info = \"Next: $(LK.Var(nextVar)) Steps: $(steps) Time: $(niceTime(nanos))\"\n", " div(table,div(span(info) & [:style => [\"font-size\" => \"7pt\", \"font-family\" => \"Georgia, Times New Roman\"]]))\n", " end\n", " \n", " gcTime = endGC-startGC\n", " gcBytes = endBytes-startBytes\n", " runTime = current-start\n", "\n", " \n", "########################################\n", " \n", " ##display\n", " if !suppressOutput\n", " tables = [annotateTable(storeTable(s),count,steps, nanos) for ((s,count),steps,nanos) = results[1:min(elideAfter,length(results))]]\n", " display(\"text/html\", wrapper(tables...))\n", " \n", " summaryString = \n", " \"\"\"\n", " Summary:\n", " ==============================\n", " Total # of steps: $steps\n", " Total # of results: $(length(results)) ($(format2(100.0*length(results)/steps))% Hit rate)\n", " Total Time: $(niceTime(runTime)) $(niceTime(runTime/steps,morePrecision=true))/step $(niceTime(runTime/length(results),morePrecision=true))/result\n", " Total GC Time: $(niceTime(gcTime)) $(niceTime(gcTime/steps,morePrecision=true))/step $(niceTime(gcTime/length(results),morePrecision=true))/result ($(format2(100.0*gcTime/runTime))%)\n", " Total GC Bytes: $(niceBytes(gcBytes)) $(niceBytes(gcBytes/steps,morePrecision=true))/step $(niceBytes(gcBytes/length(results),morePrecision=true))/result\n", " \"\"\"\n", "\n", " if elideAfter < length(results) \n", " summaryString = \"\\n$(length(results)-elideAfter) Results Suppressed\\n$summaryString\" \n", " end\n", "\n", " display(\"text/html\",div(pre(summaryString)))\n", " end\n", " \n", " \n", " results\n", "\n", "end\n", "\n", "## re-define for good measure\n", "\u03b7s = 1\n", "s = 1_000_000_000\n", "minutes = 60*s\n", "nothing " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 27 }, { "cell_type": "code", "collapsed": false, "input": [ "goal = fresh(3) do a,b,c\n", " \u2af8(\n", " (c,:\u2663) \u2261 (((a,b),:\u2663), a),\n", " (:\u2661,a) \u2261 b,\n", " a \u2261 :\u2663\n", " )\n", "end\n", "\n", "slurpInTime(goal)\n", "\n", "nothing\n" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "display_data" }, { "html": [ "
" ], "metadata": {}, "output_type": "display_data" } ], "prompt_number": 28 }, { "cell_type": "code", "collapsed": false, "input": [ "goal = \n", "fresh(2) do q,r \n", " \u2af8(\n", " forever\u1d52(r, q),\n", " r \u2261 :\u272a\n", " )\n", "end\n", "slurpInTime(goal)\n", "\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "display_data" }, { "html": [ "
" ], "metadata": {}, "output_type": "display_data" } ], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "goal = \n", "fresh(5) do q,a,b,c,decoy\n", "\u2af8(\n", " \u2af7(forever\u1d52(:\u2658,a),forever\u1d52(:\u2657,a)),\n", " \u2af7(forever\u1d52(:\u2663,b),forever\u1d52(:\u2660,b)),\n", " \u2af7(forever\u1d52(:\u262f,c),forever\u1d52(:\u262e,c)), \n", " \u2af7(forever\u1d52(:\u2640,decoy),forever\u1d52(:\u2642,decoy)), \n", " \u2740(a,b,c) \u2261 q \n", ")\n", "end\n", "\n", "slurpInTime(goal,maxResults=20)\n", "\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "display_data" }, { "html": [ "
" ], "metadata": {}, "output_type": "display_data" } ], "prompt_number": 30 }, { "cell_type": "code", "collapsed": true, "input": [ "## Go big, or go home\n", "\n", "big = slurpInTime(goal,maxResults=1_000_000,maxSteps=10_000_000,maxTime=2minutes)\n", "\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "display_data" }, { "html": [ "
" ], "metadata": {}, "output_type": "display_data" } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "## what happens if we zzz\n", "goal = \n", "fresh(5) do q,a,b,c,decoy\n", "@zzz(\u2af8,\n", " @zzz(\u2af7,forever\u1d52(:\u2658,a),forever\u1d52(:\u2657,a)),\n", " @zzz(\u2af7,forever\u1d52(:\u2663,b),forever\u1d52(:\u2660,b)),\n", " @zzz(\u2af7,forever\u1d52(:\u262f,c),forever\u1d52(:\u262e,c)), \n", " @zzz(\u2af7,forever\u1d52(:\u2640,decoy),forever\u1d52(:\u2642,decoy)), \n", " \u2740(a,b,c) \u2261 q\n", " )\n", "end\n", "bigSnooze = slurpInTime(goal,maxResults=1_000_000,maxSteps=10_000_000,maxTime=5minutes)\n", "\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "display_data" }, { "html": [ "
" ], "metadata": {}, "output_type": "display_data" } ], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "## The artful part of this notebook has quickly come to an end.\n", "## \n", "## Unable to contain our curiousity, \n", "## we crunch a few extra numbers.\n", "\n", "\n", "## as of Julia 0.3.4, bringing these guys in can take several seconds \n", "using Gadfly\n", "using DataArrays, DataFrames\n", "\n", "## also the first plot can be slow\n", "\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ "## extract data\n", "## takes several seconds.... why?\n", "\n", "score(char) = begin\n", " if char==:\u2658 || char==:\u2663 || char==:\u262f || char==:\u2640\n", " 1\n", " else\n", " 0\n", " end\n", "end\n", "\n", "vars = [LK.Var(i) for i=1:5]\n", "\n", "steps = [steps for ((s,count),steps,nanos)=big] \n", "nanos = [int(nanos) for ((s,count),steps,nanos)=big] ##not sure why I need to cast\n", "scores = [score(first(walk(vars[i],s))) for ((s,count),steps,nanos)=big, i=2:5] \n", " \n", "nothing" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 45 }, { "cell_type": "code", "collapsed": false, "input": [ "steps = float(steps)\n", "nanos = float(nanos)\n", "scores = float(scores)\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 46 }, { "cell_type": "code", "collapsed": false, "input": [ "## How fairly does the search sample correct answers?\n", "mean(scores,1)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 47, "text": [ "1x4 Array{Float64,2}:\n", " 0.666802 0.666806 0.666846 0.500295" ] } ], "prompt_number": 47 }, { "cell_type": "code", "collapsed": false, "input": [ "## What about when we use zzz?\n", "scoresZZZ = [score(first(walk(vars[i],s))) for ((s,count),steps,nanos)=bigSnooze, i=2:5]\n", "\n", "nothing" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 48 }, { "cell_type": "code", "collapsed": false, "input": [ "mean(scoresZZZ,1)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 49, "text": [ "1x4 Array{Float64,2}:\n", " 0.667041 0.66704 0.667024 0.500228" ] } ], "prompt_number": 49 }, { "cell_type": "code", "collapsed": false, "input": [ "## Can you explain this result? -- is there a bug lurking in the bowels of hell?" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [ "## We might as well draw a few pictures\n", "\n", "## Because Chrome+Gadfly is not that big\n", "graphpoints = 1_000\n", "samplepoints = linspace(1,length(big), graphpoints)\n", "\n", "sample(data) = begin\n", " index = 1\n", " result = zeros(graphpoints,3)\n", " for i=1:graphpoints\n", " mn = Inf\n", " mx = -Inf\n", " mean = 0\n", " count = 0\n", " while (index <= samplepoints[i])\n", " d = data[index]\n", " mn = min(d,mn)\n", " mx = max(d,mx)\n", " mean += d\n", " count += 1\n", " index += 1\n", " end\n", " mean = mean/count\n", " result[i,:] = [mn,mean,mx] \n", " end\n", " result\n", "end\n", "\n", "\n", "nothing\n", "\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "nanosamp = sample(nanos)\n", "\n", "nanoDF = DataFrame()\n", "nanoDF[:points] = samplepoints \n", "nanoDF[:min] = nanosamp[:,1]\n", "nanoDF[:mean] = nanosamp[:,2]\n", "nanoDF[:max] = nanosamp[:,3]\n", "plot(nanoDF,x=:points,y=:mean,Geom.line,Guide.title(\"Avg. Time/Result (nanos)\"))\n", "\n", "## Looks like we're generating quite some garbage...\n" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", " \n", " points\n", " \n", " \n", " -1.5\u00d710\u2076\n", " -1.0\u00d710\u2076\n", " -5.0\u00d710\u2075\n", " 0\n", " 5.0\u00d710\u2075\n", " 1.0\u00d710\u2076\n", " 1.5\u00d710\u2076\n", " 2.0\u00d710\u2076\n", " 2.5\u00d710\u2076\n", " -1.00\u00d710\u2076\n", " -9.50\u00d710\u2075\n", " -9.00\u00d710\u2075\n", " -8.50\u00d710\u2075\n", " -8.00\u00d710\u2075\n", " -7.50\u00d710\u2075\n", " -7.00\u00d710\u2075\n", " -6.50\u00d710\u2075\n", " -6.00\u00d710\u2075\n", " -5.50\u00d710\u2075\n", " -5.00\u00d710\u2075\n", " -4.50\u00d710\u2075\n", " -4.00\u00d710\u2075\n", " -3.50\u00d710\u2075\n", " -3.00\u00d710\u2075\n", " -2.50\u00d710\u2075\n", " -2.00\u00d710\u2075\n", " -1.50\u00d710\u2075\n", " -1.00\u00d710\u2075\n", " -5.00\u00d710\u2074\n", " 0\n", " 5.00\u00d710\u2074\n", " 1.00\u00d710\u2075\n", " 1.50\u00d710\u2075\n", " 2.00\u00d710\u2075\n", " 2.50\u00d710\u2075\n", " 3.00\u00d710\u2075\n", " 3.50\u00d710\u2075\n", " 4.00\u00d710\u2075\n", " 4.50\u00d710\u2075\n", " 5.00\u00d710\u2075\n", " 5.50\u00d710\u2075\n", " 6.00\u00d710\u2075\n", " 6.50\u00d710\u2075\n", " 7.00\u00d710\u2075\n", " 7.50\u00d710\u2075\n", " 8.00\u00d710\u2075\n", " 8.50\u00d710\u2075\n", " 9.00\u00d710\u2075\n", " 9.50\u00d710\u2075\n", " 1.00\u00d710\u2076\n", " 1.05\u00d710\u2076\n", " 1.10\u00d710\u2076\n", " 1.15\u00d710\u2076\n", " 1.20\u00d710\u2076\n", " 1.25\u00d710\u2076\n", " 1.30\u00d710\u2076\n", " 1.35\u00d710\u2076\n", " 1.40\u00d710\u2076\n", " 1.45\u00d710\u2076\n", " 1.50\u00d710\u2076\n", " 1.55\u00d710\u2076\n", " 1.60\u00d710\u2076\n", " 1.65\u00d710\u2076\n", " 1.70\u00d710\u2076\n", " 1.75\u00d710\u2076\n", " 1.80\u00d710\u2076\n", " 1.85\u00d710\u2076\n", " 1.90\u00d710\u2076\n", " 1.95\u00d710\u2076\n", " 2.00\u00d710\u2076\n", " -1\u00d710\u2076\n", " 0\n", " 1\u00d710\u2076\n", " 2\u00d710\u2076\n", " -1.0\u00d710\u2076\n", " -9.0\u00d710\u2075\n", " -8.0\u00d710\u2075\n", " -7.0\u00d710\u2075\n", " -6.0\u00d710\u2075\n", " -5.0\u00d710\u2075\n", " -4.0\u00d710\u2075\n", " -3.0\u00d710\u2075\n", " -2.0\u00d710\u2075\n", " -1.0\u00d710\u2075\n", " 0\n", " 1.0\u00d710\u2075\n", " 2.0\u00d710\u2075\n", " 3.0\u00d710\u2075\n", " 4.0\u00d710\u2075\n", " 5.0\u00d710\u2075\n", " 6.0\u00d710\u2075\n", " 7.0\u00d710\u2075\n", " 8.0\u00d710\u2075\n", " 9.0\u00d710\u2075\n", " 1.0\u00d710\u2076\n", " 1.1\u00d710\u2076\n", " 1.2\u00d710\u2076\n", " 1.3\u00d710\u2076\n", " 1.4\u00d710\u2076\n", " 1.5\u00d710\u2076\n", " 1.6\u00d710\u2076\n", " 1.7\u00d710\u2076\n", " 1.8\u00d710\u2076\n", " 1.9\u00d710\u2076\n", " 2.0\u00d710\u2076\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " -7.0\u00d710\u2075\n", " -6.0\u00d710\u2075\n", " -5.0\u00d710\u2075\n", " -4.0\u00d710\u2075\n", " -3.0\u00d710\u2075\n", " -2.0\u00d710\u2075\n", " -1.0\u00d710\u2075\n", " 0\n", " 1.0\u00d710\u2075\n", " 2.0\u00d710\u2075\n", " 3.0\u00d710\u2075\n", " 4.0\u00d710\u2075\n", " 5.0\u00d710\u2075\n", " 6.0\u00d710\u2075\n", " 7.0\u00d710\u2075\n", " 8.0\u00d710\u2075\n", " 9.0\u00d710\u2075\n", " 1.0\u00d710\u2076\n", " 1.1\u00d710\u2076\n", " 1.2\u00d710\u2076\n", " 1.3\u00d710\u2076\n", " -6.00\u00d710\u2075\n", " -5.80\u00d710\u2075\n", " -5.60\u00d710\u2075\n", " -5.40\u00d710\u2075\n", " -5.20\u00d710\u2075\n", " -5.00\u00d710\u2075\n", " -4.80\u00d710\u2075\n", " -4.60\u00d710\u2075\n", " -4.40\u00d710\u2075\n", " -4.20\u00d710\u2075\n", " -4.00\u00d710\u2075\n", " -3.80\u00d710\u2075\n", " -3.60\u00d710\u2075\n", " -3.40\u00d710\u2075\n", " -3.20\u00d710\u2075\n", " -3.00\u00d710\u2075\n", " -2.80\u00d710\u2075\n", " -2.60\u00d710\u2075\n", " -2.40\u00d710\u2075\n", " -2.20\u00d710\u2075\n", " -2.00\u00d710\u2075\n", " -1.80\u00d710\u2075\n", " -1.60\u00d710\u2075\n", " -1.40\u00d710\u2075\n", " -1.20\u00d710\u2075\n", " -1.00\u00d710\u2075\n", " -8.00\u00d710\u2074\n", " -6.00\u00d710\u2074\n", " -4.00\u00d710\u2074\n", " -2.00\u00d710\u2074\n", " 0\n", " 2.00\u00d710\u2074\n", " 4.00\u00d710\u2074\n", " 6.00\u00d710\u2074\n", " 8.00\u00d710\u2074\n", " 1.00\u00d710\u2075\n", " 1.20\u00d710\u2075\n", " 1.40\u00d710\u2075\n", " 1.60\u00d710\u2075\n", " 1.80\u00d710\u2075\n", " 2.00\u00d710\u2075\n", " 2.20\u00d710\u2075\n", " 2.40\u00d710\u2075\n", " 2.60\u00d710\u2075\n", " 2.80\u00d710\u2075\n", " 3.00\u00d710\u2075\n", " 3.20\u00d710\u2075\n", " 3.40\u00d710\u2075\n", " 3.60\u00d710\u2075\n", " 3.80\u00d710\u2075\n", " 4.00\u00d710\u2075\n", " 4.20\u00d710\u2075\n", " 4.40\u00d710\u2075\n", " 4.60\u00d710\u2075\n", " 4.80\u00d710\u2075\n", " 5.00\u00d710\u2075\n", " 5.20\u00d710\u2075\n", " 5.40\u00d710\u2075\n", " 5.60\u00d710\u2075\n", " 5.80\u00d710\u2075\n", " 6.00\u00d710\u2075\n", " 6.20\u00d710\u2075\n", " 6.40\u00d710\u2075\n", " 6.60\u00d710\u2075\n", " 6.80\u00d710\u2075\n", " 7.00\u00d710\u2075\n", " 7.20\u00d710\u2075\n", " 7.40\u00d710\u2075\n", " 7.60\u00d710\u2075\n", " 7.80\u00d710\u2075\n", " 8.00\u00d710\u2075\n", " 8.20\u00d710\u2075\n", " 8.40\u00d710\u2075\n", " 8.60\u00d710\u2075\n", " 8.80\u00d710\u2075\n", " 9.00\u00d710\u2075\n", " 9.20\u00d710\u2075\n", " 9.40\u00d710\u2075\n", " 9.60\u00d710\u2075\n", " 9.80\u00d710\u2075\n", " 1.00\u00d710\u2076\n", " 1.02\u00d710\u2076\n", " 1.04\u00d710\u2076\n", " 1.06\u00d710\u2076\n", " 1.08\u00d710\u2076\n", " 1.10\u00d710\u2076\n", " 1.12\u00d710\u2076\n", " 1.14\u00d710\u2076\n", " 1.16\u00d710\u2076\n", " 1.18\u00d710\u2076\n", " 1.20\u00d710\u2076\n", " -1\u00d710\u2076\n", " 0\n", " 1\u00d710\u2076\n", " 2\u00d710\u2076\n", " -6.00\u00d710\u2075\n", " -5.50\u00d710\u2075\n", " -5.00\u00d710\u2075\n", " -4.50\u00d710\u2075\n", " -4.00\u00d710\u2075\n", " -3.50\u00d710\u2075\n", " -3.00\u00d710\u2075\n", " -2.50\u00d710\u2075\n", " -2.00\u00d710\u2075\n", " -1.50\u00d710\u2075\n", " -1.00\u00d710\u2075\n", " -5.00\u00d710\u2074\n", " 0\n", " 5.00\u00d710\u2074\n", " 1.00\u00d710\u2075\n", " 1.50\u00d710\u2075\n", " 2.00\u00d710\u2075\n", " 2.50\u00d710\u2075\n", " 3.00\u00d710\u2075\n", " 3.50\u00d710\u2075\n", " 4.00\u00d710\u2075\n", " 4.50\u00d710\u2075\n", " 5.00\u00d710\u2075\n", " 5.50\u00d710\u2075\n", " 6.00\u00d710\u2075\n", " 6.50\u00d710\u2075\n", " 7.00\u00d710\u2075\n", " 7.50\u00d710\u2075\n", " 8.00\u00d710\u2075\n", " 8.50\u00d710\u2075\n", " 9.00\u00d710\u2075\n", " 9.50\u00d710\u2075\n", " 1.00\u00d710\u2076\n", " 1.05\u00d710\u2076\n", " 1.10\u00d710\u2076\n", " 1.15\u00d710\u2076\n", " 1.20\u00d710\u2076\n", " \n", " \n", " mean\n", " \n", " \n", " Avg. Time/Result (nanos)\n", " \n", "\n", "\n", "\n", " \n", "\n", "\n", "\n", "\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 41, "svg": [ "\n", "\n", "\n", " \n", " points\n", " \n", " \n", " 0\n", " 5.0\u00d710\u2075\n", " 1.0\u00d710\u2076\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " 0\n", " 1.0\u00d710\u2075\n", " 2.0\u00d710\u2075\n", " 3.0\u00d710\u2075\n", " 4.0\u00d710\u2075\n", " 5.0\u00d710\u2075\n", " 6.0\u00d710\u2075\n", " \n", " \n", " mean\n", " \n", " \n", " Avg. Time/Result (nanos)\n", " \n", "\n", "\n", "\n", " \n", "\n", "\n" ], "text": [ "Plot(...)" ] } ], "prompt_number": 41 }, { "cell_type": "code", "collapsed": false, "input": [ "hitrate = 100.0*(1:length(big))./cumsum(steps)\n", "hitDF = DataFrame()\n", "hitDF[:points] = samplepoints[2:end] ## skip first point\n", "hitDF[:hitrate] = sample(hitrate)[2:end,2] ## skip first point\n", "plot(hitDF,x=:points,y=:hitrate,Geom.line,Guide.title(\"Hit Rate(%)\"),Scale.x_log10)\n" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", " \n", " points\n", " \n", " \n", " 10-1\n", " 100\n", " 101\n", " 102\n", " 103\n", " 104\n", " 105\n", " 106\n", " 107\n", " 108\n", " 109\n", " 1010\n", " 100.0\n", " 100.1\n", " 100.2\n", " 100.3\n", " 100.4\n", " 100.5\n", " 100.6\n", " 100.7\n", " 100.8\n", " 100.9\n", " 101.0\n", " 101.1\n", " 101.2\n", " 101.3\n", " 101.4\n", " 101.5\n", " 101.6\n", " 101.7\n", " 101.8\n", " 101.9\n", " 102.0\n", " 102.1\n", " 102.2\n", " 102.3\n", " 102.4\n", " 102.5\n", " 102.6\n", " 102.7\n", " 102.8\n", " 102.9\n", " 103.0\n", " 103.1\n", " 103.2\n", " 103.3\n", " 103.4\n", " 103.5\n", " 103.6\n", " 103.7\n", " 103.8\n", " 103.9\n", " 104.0\n", " 104.1\n", " 104.2\n", " 104.3\n", " 104.4\n", " 104.5\n", " 104.6\n", " 104.7\n", " 104.8\n", " 104.9\n", " 105.0\n", " 105.1\n", " 105.2\n", " 105.3\n", " 105.4\n", " 105.5\n", " 105.6\n", " 105.7\n", " 105.8\n", " 105.9\n", " 106.0\n", " 106.1\n", " 106.2\n", " 106.3\n", " 106.4\n", " 106.5\n", " 106.6\n", " 106.7\n", " 106.8\n", " 106.9\n", " 107.0\n", " 107.1\n", " 107.2\n", " 107.3\n", " 107.4\n", " 107.5\n", " 107.6\n", " 107.7\n", " 107.8\n", " 107.9\n", " 108.0\n", " 108.1\n", " 108.2\n", " 108.3\n", " 108.4\n", " 108.5\n", " 108.6\n", " 108.7\n", " 108.8\n", " 108.9\n", " 109.0\n", " 100\n", " 105\n", " 1010\n", " 100.0\n", " 100.2\n", " 100.4\n", " 100.6\n", " 100.8\n", " 101.0\n", " 101.2\n", " 101.4\n", " 101.6\n", " 101.8\n", " 102.0\n", " 102.2\n", " 102.4\n", " 102.6\n", " 102.8\n", " 103.0\n", " 103.2\n", " 103.4\n", " 103.6\n", " 103.8\n", " 104.0\n", " 104.2\n", " 104.4\n", " 104.6\n", " 104.8\n", " 105.0\n", " 105.2\n", " 105.4\n", " 105.6\n", " 105.8\n", " 106.0\n", " 106.2\n", " 106.4\n", " 106.6\n", " 106.8\n", " 107.0\n", " 107.2\n", " 107.4\n", " 107.6\n", " 107.8\n", " 108.0\n", " 108.2\n", " 108.4\n", " 108.6\n", " 108.8\n", " 109.0\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " 55\n", " 60\n", " 65\n", " 70\n", " 75\n", " 80\n", " 85\n", " 90\n", " 95\n", " 100\n", " 105\n", " 110\n", " 115\n", " 120\n", " 125\n", " 60\n", " 61\n", " 62\n", " 63\n", " 64\n", " 65\n", " 66\n", " 67\n", " 68\n", " 69\n", " 70\n", " 71\n", " 72\n", " 73\n", " 74\n", " 75\n", " 76\n", " 77\n", " 78\n", " 79\n", " 80\n", " 81\n", " 82\n", " 83\n", " 84\n", " 85\n", " 86\n", " 87\n", " 88\n", " 89\n", " 90\n", " 91\n", " 92\n", " 93\n", " 94\n", " 95\n", " 96\n", " 97\n", " 98\n", " 99\n", " 100\n", " 101\n", " 102\n", " 103\n", " 104\n", " 105\n", " 106\n", " 107\n", " 108\n", " 109\n", " 110\n", " 111\n", " 112\n", " 113\n", " 114\n", " 115\n", " 116\n", " 117\n", " 118\n", " 119\n", " 120\n", " 60\n", " 80\n", " 100\n", " 120\n", " 60\n", " 62\n", " 64\n", " 66\n", " 68\n", " 70\n", " 72\n", " 74\n", " 76\n", " 78\n", " 80\n", " 82\n", " 84\n", " 86\n", " 88\n", " 90\n", " 92\n", " 94\n", " 96\n", " 98\n", " 100\n", " 102\n", " 104\n", " 106\n", " 108\n", " 110\n", " 112\n", " 114\n", " 116\n", " 118\n", " 120\n", " \n", " \n", " hitrate\n", " \n", " \n", " Hit Rate(%)\n", " \n", "\n", "\n", "\n", " \n", "\n", "\n", "\n", "\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 42, "svg": [ "\n", "\n", "\n", " \n", " points\n", " \n", " \n", " 103\n", " 104\n", " 105\n", " 106\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " 80\n", " 85\n", " 90\n", " 95\n", " 100\n", " \n", " \n", " hitrate\n", " \n", " \n", " Hit Rate(%)\n", " \n", "\n", "\n", "\n", " \n", "\n", "\n" ], "text": [ "Plot(...)" ] } ], "prompt_number": 42 }, { "cell_type": "code", "collapsed": false, "input": [ "## Some final thoughts:\n", "##\n", "## Did you look closely at all of the output?\n", "## What about the statistics, can you explain them?\n", "## What did you find suprising?\n", "##\n", "## Perhaps you liked the syntax? \n", "## But you _do_ believe in pixie dust, don't you?\n", "##\n", "## That's a wrap -- See you next time for another twisted journey\n", "#################################################################\n", "#################################################################\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 43 }, { "cell_type": "code", "collapsed": false, "input": [ "## Hackety-Hack\n", "## Some temporary work-arounds related to unresolved IJulia/juliabox warts\n", "\n", "## Most likely, this cell nevers needs to be evaluated again\n", "## It works by injecting mutant DNA (html) into the sheet which is then saved for posterity\n", "\n", "\n", "##nuke the red box around unicode chars\n", "display(\"text/html\", \n", "\"\"\"\n", "\"\"\"\n", ")\n", "\n", "\n", "## table styling\n", "## generated with http://tablestyler.com/#\n", "css = \".datagrid table { border-collapse: collapse; text-align: left; width: 100%; margin:0px auto; } .datagrid {margin:0px auto; font: normal 12px/150% Georgia, Times New Roman, Times, serif; background: #fff; overflow: hidden; border: 1px solid #8C8C8C; }.datagrid table td, .datagrid table th { padding: 4px 10px; }.datagrid table thead th {background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #8C8C8C), color-stop(1, #7D7D7D) );background:-moz-linear-gradient( center top, #8C8C8C 5%, #7D7D7D 100% );filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#8C8C8C', endColorstr='#7D7D7D');background-color:#8C8C8C; color:#FFFFFF; font-size: 13px; font-weight: bold; border-left: 1px solid #A3A3A3; } .datagrid table thead th:first-child { border: none; }.datagrid table tbody td { color: #1A1E21; border-left: 1px solid #DBDBDB;font-size: 12px;font-weight: normal; }.datagrid table tbody .alt td { background: #EBEBEB; color: #1A1E21; }.datagrid table tbody td:first-child { border-left: none; }.datagrid table tbody tr:last-child td { border-bottom: none; }.datagrid table tfoot td div { border-top: 1px solid #8C8C8C;background: #EBEBEB;} .datagrid table tfoot td { padding: 0; font-size: 12px } .datagrid table tfoot td div{ padding: 2px; }.datagrid table tfoot td ul { margin: 0; padding:0; list-style: none; text-align: right; }.datagrid table tfoot li { display: inline; }.datagrid table tfoot li a { text-decoration: none; display: inline-block; padding: 2px 8px; margin: 1px;color: #F5F5F5;border: 1px solid #8C8C8C;-webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #8C8C8C), color-stop(1, #7D7D7D) );background:-moz-linear-gradient( center top, #8C8C8C 5%, #7D7D7D 100% );filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#8C8C8C', endColorstr='#7D7D7D');background-color:#8C8C8C; }.datagrid table tfoot ul.active, .datagrid table tfoot ul a:hover { text-decoration: none;border-color: #7D7D7D; color: #F5F5F5; background: none; background-color:#8C8C8C;}div.dhtmlx_window_active, div.dhx_modal_cover_dv { position: fixed !important; }\"\n", "display(\"text/html\", \"\")\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "display_data" }, { "html": [ "" ], "metadata": {}, "output_type": "display_data" } ], "prompt_number": 29 } ], "metadata": {} } ] }