# logic programming examples

The List Monad transparently constructs all 2^4, possibilities. parent1_statement_is_valid(parent1_sex, child_described_sex). If Parent2 is Female then (Female, *, Female) are both true. There happens The below code is a full solution to a relatively simple, but classic, logic puzzle. Obviously (Male, Male) and (Female, *) are valid statements. a row. Parent 1 said "The child said 'I am a boy'". lesbian couples. Find. This would mean the child is in fact a girl and the We (Male, Female), because that'd imply a Male (the parent) lied. does not lie. (Female, *) childs_statement_is_valid(child_sex, child_described_sex). Similarly if the Child is Female, then no matter if they lied or told the truth the statement is valid in the, face of the axioms, this is an example of the truth of statement differing. (I apologize for the obvious misogyny). from its logical validity. This lets us print out the data structure in a friendly way. An explanation of logic programming in general and the difference between rules and constraints, followed by examples of logic programming using Python. Rules are written as logical clauses with a head and a body; for instance, "H is true if B1, B2, and B3 are true." Here we use the List Monad to declare the four variables, each ranging over I And so on. understand the response and turns to the parents for a translation. does not lie. The only invalid combination is -- that the solution could have more than one answer. were Female since that would violate axiom 1. Each of these has two. is dubious because you can't trust a Female. fits the constraints of the puzzle. I If yes, ﬁnish. possibilities, which means we've got 2^4 == 16 possible outcomes. possibilities. understand the response and turns to the parents for a translation. The result is a list of tuples listing all possible solutions. We use any kind of map because it's possible (due to poor coding) ALF (algebraic logic functional programming language). I Idea: generate-and-test. 1. A Female will never tell two lies or two truths in a row. https://wiki.haskell.org/index.php?title=Logic_programming_example&oldid=58625. that the solution could have more than one answer. When creating an answer we stuff it into this data structure. ", Each of those three statements is realized as a function. asserts the parents are not the same sex. Logic programming is a computer programming paradigm where program statements express facts and rules about problems within a system of formal logic. Obviously These functions do Run the code at the bottom of the page :). said they were Male. Run the program. Each of these has two have four guards, the three described above and an additional guard that Jump to: navigation, search. possibilities, which means we've got 2^4 == 16 possible outcomes. Using List Monads we can realize all 2^4 outcomes in a straightforward, fashion. We need to import guard from the Monad module, it culls out unwanted solutions from the solution space. Obviously (Male, Male) and (Female, *) are valid statements. Solving Logic Puzzles I Logic grid puzzles can be easily solved by logic programming. to be only one, if there was more than one than the other legal ones would be Parent 2 said "The child is a girl. returned too. If Parent 2 is Male, then the only way this can be a legal statement is if the child is Female and, said they were Male. And we have three statements (i.e. Using the List Monad it's possible to rig up a basic logic program, where the bulk of the code simply asserts the shape of the answer rather than worrying about generating the solution space. single statement. the second one is true (the child lied -- it said Female when it was Male). There are four variables in the puzzle: Sex of parent 1, Sex of parent 2, Sex the set [Male, Female]. Rules are written as logical clauses with a head and a body; for instance, "H is true if B1, B2, and B3 are true." The mother and The child lied." Parent 2 said "The child is a girl. There happens, to be only one, if there was more than one than the other legal ones would be, -- Run the program. say they were? logical expressions) in the puzzle: Each of those three statements is realized as a function. Find It uses properties of the List Monad, in a straightforward way, to declaratively assert the answer. Others, such as Prolog, are a combination of declarative and imperative. Using List Monads we can realize all 2^4 outcomes in a straightforward asserts the sexuality of the parents. We use any kind of map because it's possible (due to poor coding). This would mean the child is in fact a girl and the, child did in fact lie, two statements which are both true. -- People are either Male or Female, this represents the constraints of the puzzle. The child lied.". The child said a single statement, in which they declared their sex. ", Language, Logic, Machine learning, Paradigm, Programming terms. Using the List Monad it's possible to rig up a basic logic program, where the bulk of the code simply asserts the shape of the answer rather than worrying about generating the solution space. The only combination that violates the axioms is (Male, Female) since a Male There are four variables in the puzzle: Sex of parent 1, Sex of parent 2, Sex, of the child, and the Sex the child said they were. Facts are expressed similar to rules, but without a body; for instance, "H is true." as a function, this lets us reuse the same code for homosexual, gay, and A researcher comes across a mother, a father, and their child. Female) is true because the first statement is false (the child is a girl) but -- When creating an answer we stuff it into this data structure, -- This lets us print out the data structure in a friendly way, The only combination that violates the axioms is (Male, Female) since a Male. fashion. I If not, generate another possible solution and test again. Facts are expressed similar to rules, but without a body; for instance, "H is true. Parent 2: "The child is a girl. Parent 1 said "The child said 'I am a boy'". A researcher comes across a mother, a father, and their child. The guard statements discard statements that are invalid. Parent 1 said a single statement: "The child said 'I am a a boy'", Parent 2 said two statements: "The child is a girl.

Příspěvek byl publikován v rubrice Novinky pitbike Moravia. Můžete si uložit jeho odkaz mezi své oblíbené záložky.