Thursday, October 06, 2016

Cascadia Ruby Conf 2012 A Taste of Prolog by Aja Hammerly

were in talk about prologue and for some basics
I like prologue but I'm not an expert in Prolog
last time I did any significant for a lot more was honestly in college
and this is just gonna be in action I only have half an hour
/url so what is prologue for those who don't know probably the logic
programming language
and was logic programming language logic programming languages
uses the use the concept a formal logic
induction deduction implication to program
declared a programming language if the language
like sequel that expresses the program without specifying the control-flow
and Prolog as a declarative programming language but most for long as a weird
programming language
pretty much everyone I talked about problem has some version and it breaks
my brain
as their first reaction to language was prone used
I used for natural language processing
now showing at processing is processing language as humans use to communicate
with each other
of Burma languages and written languages problems also use for person grammer's
again natural language grammars but also computer grammer's fraud has been used
for fair
and often used for expert systems and other kinds of artificial intelligence
work
so why should I think I should learn program
Polak I think usually prone to expand your toolbox I'm sure you've all heard
the phrase that
only have a hammer everything looks like a nail boy does water to programming
languages are gonna try to solve all problems using
us and sometimes that's an excise exercise in futility
prong is very very good at so many very specific causal problems and if you know
it you'll be able to recognize those problems and get your answers quickly
I'll think prolonged changes your perspective on Capp you have to think
about problems differently and understand your partners a lot more
deeply
to build a proper program while in Prolog and
forwarding pro-life helps make you into a polyglot assault several talks couple
years ago about how important it was that room a special parking lot a lot a
different languages
so they had a wider appreciation for computer programming in general
and produce a great one because it's so weird so different really can expand
your horizons about how programming works so
let's talk about why it's weird biggest where nasa pro-law
in that it's all about the what and not the how in room e you're probably used
to saying okay take this array
enter it through and do these things with the content on their way
you don't do any of that prologue are you know it describe your problem space
really really well
and prolong takes care of the rest and what I mean by describing a problem
space really well
well in Prolog programs are expressed as to thinks fax
kangaroos hot the sky is beautiful in blue today those are facts
and rolls an example overall would be that even numbers are divisible by 2
evenly to visible light air and
part in the research from this talk and read this book first this book called
The Art of prologue had the fantastic work first computational on your program
as a deduction
consequences on the program a program to find a set of consequences which is its
meaning
art logic programming is constructing can fight
elegant programs that have the desired meaning and
hopefully I can we talk you'll see some other power a prologue how
really relates to misquote so are the standard hello world example in Prolog
is
always family tree I think an example is boring and relatively lame and it's been
way overdone
I'm gonna pick an entirely different but equally contrived example
about coming up with a radical pairs for Seattle
from her program night at Seattle army so we need to start with some fax
so salaries in Seattle army meeting I went around asking what it really is
as inspiring as the max doctor brain is a bit
them pete King and his band and tender love you to
and just remember counselor because I'm gonna and appears in the form of a
caterpillar even I'm
trying not to then spun ernie max
and doctor bringing them all those are what are called atoms in prolonged and
all that basically means that the string const
they start with lower case letter editor is the name on the wall
and runs by dirty max the two arguments for that role
just a vocabulary case I slept but she can fall
so in Prolog once you've got a fax you can
ask questions I copied over to our editor rules from the previous art or a
fax from the previous line
and that question mark dash as an interactive problem proper I can
interact with the system so I RBI
and some first question to ask is doesn't inspire you emaxx not what I'm
asking what the senators and Bydureon max line
prolonged looks or fax it knows it says yeah as an spider is the max
well I go doesn't inspire you to bet Pollo Loco FactSet nose and says no
is inspired is not used pretty basic
so now we're gonna ask more interesting question
asking something RT now we're gonna ask a question I don't know the answer to
or india is that we're gonna put variable
in place and what are the two atoms so that at nine with the capital that
couple
couple water makes it a variable in Prolog all very most are the capital
letter
so what open what but question is actually asking is does doctor braiker
what editor just doctoring yes
and justice I know yes questions will prolong and with the period yes that's
weird
go with it army so
what role doesn't think it was prepared to back but not about in the order
they're defined in the file
and for the first one said that inspire you to the max want to try to do is try
to match the thing I gave it what
something in our ninos and try to fill in the variables
values that make sense so check out the match doctor brain was inspired
knows don't match they're not the same such gaps that line goes to the next
just the next backed nothing is the doctor brain matches doctor brain
it's like okay well what can I put into that editor bearable to make the rest a
bit
back matches like okay I make the editor variable match with them so I put them
into that editor variable in all match
so that tells me the substitution had to make to make the world
to make my question for so the output is editor equal to them
because is trying to make the question I asked trip
so just tell me the doctor rain is that okay not actually all that exciting
I could have gone asked him in the time it took me explains
so what's going on to the next one okay I'm gonna have to get me a person and
the editor
so now I got to Merkel's doesn't substitution the same way
matching up then spider but the person and
editor with the maxim just for reference the colors are here to help
to illustrate what's going on problem is not normally anger shirt solid
so make it a little more complicated
yes there's a giant pile cat summer were third Flo
so every fact that I brought over backslide
tender love uses them as the 1i on and now I've got three sections to my
question
info on when you have multiple sections to a question or older called
polls and what brought her to tears tries to find
set up some situations that make all goals for
and that sounded like formal logic kinda gobbledygook what walk through it
and sure an example it'll start to make sense
so what we're saying is we need to find a person who uses them
that's the first line we're gonna call that person person one
where to find another person person to who also uses them
and then that last line there with the flash equally called
that's actually not equals control and we're just saying that we don't want to
have
person one be the same as per cent because someone pairing with himself sad
and in Prolog use a comma as an and
to combine cloth comma is and so
what problem does it take up first first-line the first goal
and goes there are two tries to find a match the first line that matches
that's where someone uses Dennis fly with doctor brain uses
so it's okay we'll make person one doctor bring of
now gone on to the second line we have person to freezes
start to the top first ok or doctor brain uses
a person what is doctor is doctor brain and person to his doctor
goes down about for my career said for someone to 94 percent owners
up doesn't work so what does not come back tracking
and it was back online back one goal goes back to the fact some picks up
where it left off
so back on that person to really be online and what is going on there isn't
gonna look for the fact that has and gonna pick the next one
which this case the match is tender love you sounds good sale doctor brain
using them didn't match my second line as well try tender love you not comes
back down to that not equal climate arlyne
and said okay doctor brain is not the same a standalone
we found a solution and so points out that my question is true
me two people both use them or not the same 11 a.m. a starter Brandon other one
is tender
no I just found a pair are people who use the same matter
and I can make it even more complicated
I don't actually care people F editor and am i cant variable in their tail
and so now I'm done as a substitute in them for a parable editor
same exact me on that and I'm like giving to people use the same editor
help that's why I'm not from prague brought doctor Brent under law
abt a matter if an editor Isabel okay
I'm getting really sick typing all the stuff way to make auto care
so we make a rule what our role is this just a shortcut for a bunch
from punchlines the go together margin a bunch of questions you wanna ask it has
a name
so in this case we're gonna name are all pair
supper two people are a pair and this case
embarrass the same and they're not the same person you'll see the editor is the
same on both most lines
and as long as the variable have the same name is the same value
you also notice the editor is an editor isn't in my definition
because I don't actually care what had occurred to me as long as it's the same
one
they call me and remind it wouldn't bother me so
I go back to an art a prolonged prompt and I say
well give me a pair for someone in person to and just like we saw before
the first
para comes up with his doctor Brian Turner so so far so good and I don't
have to keep typing
all that Qatar so
hear your number you can use the role
you can say and any other people compare doctor brain comes up with tenor
second choose to but constants in there too Adams
and Ishant variables when you can also in Prolog asking for a lot of solutions
because there's probably more than one solution cell
asking for parents as person Morning Post October in person to request under
Lyme and I type something calling
which is the prologue for what or and like okay so that's one pair
or him your number one okay well you could have doctor grant I love or you
could have doctor rainer
against I could give me another one and it's like are you have 10 or 11 doctor
brennan upon like
okay already saw that pair and i ended
but it was reversed person one person to understand can prolong it doesn't
understand the apparent doctor brand 10-11
many much pretty much the same as a pair tender love doctor Prince let's fix that
and the way we fix that is by taking honor
praising not equal sign in putting in to create a greater than sign answer
at greater than and at is because it's comparing Adams
it's trying to be in demonic and
her now that number what darlin forces in ordering
so the only one out of ten or eleven doctor but one pack version of 10 11
doctor brain is valid and you probably think this and other languages before
past
specially if you're done anything functional to pretty common way of
handling
best two possible solutions problem now I asked for lying to me to people
compare goes okay tender lovin doctor bring to know everything and Figgins and
doctor brand
cut off about on the flight is the word now because I can find anything else
and prolonged really likes now
so if you let me know actionable many pairs present spider in that last set
so I'm like okay what's come up with the rents and fighter can find a pair
so was thinking more laps so brief survey other group
and I with keyboard layouts and that spider doctor brain is the more ac
kinda I'm taken to court so got a role here
so in a way of more questions what keyboard layout as doctor brain years
doctor bring it into more
against doing the pattern matching so it's trying to figure out how can I make
the statement read by substituting values and for the variables
an hour and overall check it out almost the same rules last time only
we have key markets and pink because tender love is not here and someone
needed to put pink on their side
and we use the word keyboard got a better
and for now I can ask my parents question again I can say
give me a pair to people who have the same keyboard layout our payroll right
here
fines to people I'm thinking we're glad approach is inspired doctor brain
like great now we've got two rules with the same name
and this may be a little weird but this is a really standard when prologue to
say okay well
here in one way that you can come up with a solution here's another way
and you can how long these were all the same file
prologue will use them often or cell a goblet open a file
I asked my question me two people compare have switched XY because I was
running on a room on Florence
pressure alcohol and what I ended up with it
3 answers the answers in brown are from the
editor role that we saw at the beginning
and the bottom two answers are and pink are from the key moral
so we saw before that tender lovin doctor bramble he is them
pendulum taken both use them we're gonna doctor bramble his fam
those are all the other roles nuns fighter doctor brain bowl fiesta more
action television spot his party and also the keyword matches:
and growing has no problem having two different definitions on the same or on
the same file just uses them in order
me to the mall fine answers and you also notice on this that don't think I'm
sure Obama twice I saw that might break let's make a new role College super pair
the two people who have the same manner and the same keyboard are super pair
cock in the closet from the other two rules hammer person greater in person
one is greater than
or equal to percent error got this awesome girl
ask with my a cock in the back summer and we have a question
who is a super parent pickens really basic partner for a lot
and that's kinda the simplest example now we're getting into less
connect have hands raised if anyone has done any functional programming ever
scheme has gone well something like that okay yeah he doesn't follow us
also sup for are really like left so we've got
you now I'm US square brackets lester 123 separated by commas
and what about all the banana you can have her genius left like an RBI with a
number and
adamant all you know have been established so we've got a list
unless at the very bottom while I'm in while I'm to coconuts
left the blasts and what
have entails the head of the first is the first elementalist
intelligent the rest if you know a skinny language
this is car and Connor should be familiar based on the number
and I solved on functional programming probably seen something like this before
prologue syntax this is a square bracket
variable are now to a pipe
another variable Adam square bracket
and that's that for the boy down there and that's red age martini
so what i'm saying there's head bar tale and when I try to match that up with
pattern matching a problem does it says okay well
a matching it with a list 123 it says well we'll
support one in the variable H I will put the list
23 in the variable 23 so one is the head
the list 23 is the tail and
another random piece of zantac the underscore means I don't care
actually very similar to really worry is that for anonymous variables unused
variables
underscore mean I don't care so I have a blessed one
underscore 3 it can be the last one to three
it can be the last one part 3 it can be last one
the list apple pie and then i three doesn't matter
all of any possible value can go in that underscore
and also slightly important to know that if you have to wonder
they're different so the balkan refer two completely different things
each and every underscore each and every don't care is a different value
ok within that window
some basic list manipulation so this is me dumbest definition and include
ever I remember me members are not familiar with functional language
takes two arguments first argument is something to find
second argument is a collection everything you're trying to find him
collection its
otherwise it's false so
water and a really really quickly so we're gonna return false
fr can use an array here our list is empty
because every time they obviously think we're trying to find not there well
return true
air nothing we're trying to find it at the very beginning of our list
using then euro and exposition and then we're gonna rehearse
and try to find me I don't in the rest of the list
you haven't seen that syntax before to range that's just everything but the
first element analyst
already winners were basically saying hey go try to find it in the tail
I just like to talk about here's the prologue 11
and blinds are calling the same are functionally equivalent
so the first caught the first line member H in smart
underscore is saying OK something is a member
if if the first thing on the list
the federal court below is the recursion and says okay well
X is in the list and put in the tail
so the top climber nor in the head were saying just give me the tail
all we're doing is saying hey go try to find it in detail West
really basic fashion so what happened out
I say K for a lot is to remember herbalist 123
process true if I say hey prologue
sex a memorable s23 problems now okay so far so good we got a great
announced her something different failed variable in there so it's a
interlock what what a member in the last one to three
that's why there's a burglar and so first thing process as one
and then turned to prolong turmoil you can say are in Taipei which means give
me all the results are a lot of times it was a bad idea
right now it's okay and Prolog says okay well be other values that could be acts
are 2&3
so what or just did was basically implement iterator just
all elements I love be
West and when to do it we are the exact same code but we also use for include a
remake will include so we're going to methods for the price of one here
it's actually really cool but wait there's more you can verbal
anywhere plotkin variable in from the west so now only more same number six
axe
is saying progeny all est in the entire universe
the contain the number six and pro I can do this
so first answer gives me is OK will enable us to have sex in the first spot
I'm usually contains a section we don't care what lol abolished because you know
sexism for spot doesn't matter what and the Restless Leg at school
after the next one as well and it was taken in the section spot
contains a sex and you can you don't care for the rest because
you know you don't care to me anything there and so on so what
today was a generator all that contain the number six
at the same time in implementing an iterator in the same company implemented
a fight
and that's just amazingly powerful cuz the whole thing was to appoint
so neighborly of the examples of playing
the link to the list is obviously how many on internet
so first-line return 0 have an empty list
second line is we're gonna call a blank on the Restless figure out how long
everything what the first element is in at one
for the first element basic repressive definitional and
here the equipment for long hair almost as first
then be willing to bet as there are no return values you have to include the
value that
going so first line so and delist
is 120 second line says that me nor
want to have a list we don't actually care what elements are countdown
and we say okay well will call and one
bowling from the tail all senate n bowling from
list is the link for the tail plus one
online line up pretty well just simple recursive definition
in taxable aware both are using it so we try to use it we have
less than abcd we say like this form problem says yes you're right
we ask prologue what length like the last one 3s
for like says 3 K everything everything seems to be working so far
so let us get what mister link
totally figure that out right away okay well your last one
to has square bracket and don't care a call-up another don't care
square bracket within those values
I just told me well most in the almost in the world that are like tail
I can pick up another program like Avatar we had that can do that
at just amazing and I can just do that we didn't have to write that much
again 383 wants to do that
Sep actually on very what action might have some basic examples
on circus cell
this is an inverter it takes an important gives you the opposite is the
output
we're gonna have to fax an important here handed out 11
an important one output 0 of all along so far
better than or 2 I'm
and beer 0 BR 0.0 0 has one and be easier on the open as one
pretty sure we all have seen on or before
this is Hans same mind here
I know you probably realize that the fax and up looking very similar to a truth
table a fan if you had the CS back
yes experience in college I did 300 truth tables for really long
russians so much faster way to do it
here's an axe or again as for fax because there's for possible
combinations of inputs an unmanned
not and for those of you who don't know that of a top your hand
better half actor I upon this
wiring diagram of Wikipedia and
turned on making improper are really really easy away okay we've got a real
called after
it has an at and to be employed had to carry a bit
in Assam and there's an axe or nine and showcase
you look at the diagram here like k so the A&B go into the act or
some comes that's the first goal and the A&B go into
be and output is the carrion of the second call and check it out you're done
half hour are you cut it out really basic writing
writing the fact doesn't take that long so it right out
I girls for winter variables in there and kept the truth over half adder
totally done comments or
paparazzi kinda boring on their own cell unspeakable latter
so flattered slightly more complicated you've got some temporary
wiring in between the two half-hour Mets
her 1/2 hour takes an Ambien outputs a temporary some
temporary care taken another half hour and it takes a carrion
and it have some going out have
second temporary carry out and you or the two carriers together and I can hear
final carry out
just added 10 and you get you take the
take every box from a circuit diagram and you're able to make it a single
wanna pro-law
old pieces up all the variables correspond with your wires
really easy and you end up with a working half working for water
I want so many can play with her full outer an actor
all so I can say okay well
in prague have a carry and one and I want output I wanna someone
what are my possible inputs problems like okay well
after the first line translates to by the way
okay remember target I know carrying 101 someone
okay if you want to do that you don't have to zeros
or 21 no other options under ok a lot
not actually all that interesting like a wall what values to give me a carry
out one happily generate all that
so that's really simple and I get even better
lime mood open for books looking at the stuff michael has a really pretty
circuit diagram was he would
third question and all hooked up use copy online generic a live person and
has
in and be important output a1 sector 1/2
and he wanted to help us to do is copy all those pieces in line by line
you can update at the end like at all but also to government
like okay well problem for miniature temple for less
but everything is variable data printing out the truth table like
right that of course the more actually look at it line by line
that's actually an exclusive or everything comes back in and
and thing is that you think about trying to do this in any other language I can't
think of a language arenas like take what you know about the situation
you know that for an inverter an important 0 gives you an output of
one or you know for an and 21 2001
you can just bright those facts down and then you can put those pieces together
and very logical way somewhat how we would physically put circuits together
in the code and hapless but all the output out
I can't imagine actually trying to cover this up in Ruby is
just wouldn't be as pretty and it wouldn't not modeled the exact situation
I'm dealing with nearly as nice
got one more example are you guys when I was in 6th and 7th grade I love these
things these larger problems we're like so and so has a blue hat and lives in
the gray house and has three dogs and so on and his wife liked enough to figure
out who likes everything
I think they're awesome and you can actually do
really hoping to be in for a lot cell
I think they're also won our obstruction interpretation of computer programs to
use as an example
if you like the other one of the more cum find me during lightning talks I
have like six cut it up
and love to show you and her
a basic 1x5 friend to live in an apartment building
and their apartment thing happens have five floors and each person was on one
floor
all we can try but this is a basic Sep
began with a buncha claire's out another live on the top floor bill doesn't live
on the bottom floor
coroner to live on you know the top or the bottom for
dare let their lives on a higher floor then don't bill and my mom
does not live on the farm Jesinta chorus car does not live on the floor Jason
Bell
much back-channel the implied question all times where is everyone
fired up so far me to come to pick a data structure used in quotes because
there's only one data structure and prolong and that is
a name and some Adams their arguments
want so that we're gonna try to get her answer as a last people ordered by floor
and for real
become obvious shortly we're gonna say the pocket on the left
sorted out top them before Lord third floor then the second floor and on the
bottom
and I'm just gonna the names the Walmart names all her friends right there and
they have lower case because the constant
hamburgers been variables and that's not gonna work
so doesn't five people who live in this apartment building
so I'm wondering do I'm gonna but we're gonna bust up I'm gonna build this up
with
each goal at a time and I'll show you how it all fits together into a single
roll
very end so first goal Adams loll among the top floor
so whoever is named for them atop her Morecambe out
that are crazy not equal signs flashing what he calls from earlier on
bill mccollum on the bottom floor again crazy flashing on equal sign
court is not among the top or on the bottom floor to cause this time but
two goals good day love her for the bill
so I saw this I like I'll really one thing I deal with all right now so I'm
just gonna say when I'm this role
med called higher that says that Dale and Bill Amanda else for the list them
all the answers were not
gonna at our final list of places where people have
and although pointing out later well hurling right now is later
so here's the definition for that and
and leaders meeting special functions a mess if you don't understand the right
now please confine
I can explain them to you but I'm running short on time so
this question 'cause the first one fat that if
X is the personal on the highest floor is the front our list
whining me style more in detail it doesn't matter cuz access on the front
the second one is our workers call it says that
if neither of them is an app for spot and we can you don't care for that
then X&Y need to be higher in the tail
we just say okay what near them is not top is not for spot unless just
apply hire the right by the tire world rest
all that and reason this is the reason that the higher scores on
on the left so they can be the first one this recursion
so now we have an ex our next clue is it
errand small amount of flora Jessop the chorus and again we're just gonna find a
role
now a look at the code I bet so
here looking for not adjacent this is the most complicated peace accord in the
entire presentation
sorry I take it one cause at a time so
first not adjacent XO wire not adjacent
accessible from a list there someone using is acting as a buffer between them
and why is somewhere in the tail said that's what this is.
I'll accessible from the list is from the first line what we do the matching
act at the front that X martini for
me is not why is that
is that first goal Z does not equal why which means that
that buffer person he can be either a few people were saying on Jason
and that member whitey is just saying why somewhere in the rest on the list
so that's one version are not Jason does not imply ordering
so the first person may be higher in the building on the second
this is the exact same role with the Axis and Allies swapped
all it is and then this last one
is not adjacent on the tail and salsa saying is that we're just gonna
me remember the first person lol we can actually if I not adjacent so just go
do not ingest on the rest all this is the recursive call
so that whatever another definition on adjacent and one more clue
which is not adjacent koran bill
and so couple more things we need to add to make sure we get the solution we want
as we know we need to get prologue and here that
our answer is going to be some rearrangement the five friends names
mathematically rearrangement equals permutation permutations built in the
prologue
all I'm saying is that our answer L is going to be some permutation of my
friends names in this last
all I can pick up the fermentation for us am gonna park all the role puzzle
and Seraphina rose puzzle on L and
this little man at the centre carrabelle polls with all these variables is just
saying hey these are the names of all the pieces on that
lest I want columns I can use the one somebody not altogether
we get a buncha can we've actually walked the wrong ones in this cut
already
cells run it so I say a polo ABCDE
ABCDE it gives me a little people and
because we define higher on the left this is actually less for their women
buildings and Dale top four cars on the fourth floor out on the third floor
so on and much larger problems if the written well
only have one solution side just to check my answer I type this I'm going to
make sure there weren't any solutions
abroad doesn't know there aren't any solution other solutions that's the only
one side
pressure i right applied on the internet you can check to make sure that it meets
the requirements
are set out what it does actually pretty confident
so times up so little bit about learning more are
I is mostly boxer prepare for this presentation are also a lot again videos