CS152
Chris Pollett
Nov 22, 2021
twoRandomInts :: StdGen -> ([Integer], StdGen)
-- type signature: twoRandomInts is a function that takes an
-- StdGen (the state of the RNG) and returns a tuple containing
-- a list of Integers and a new StdGen.
twoRandomInts gen = let
(rand1, gen2) = random gen
(rand2, gen3) = random gen2
in ([rand1, rand2], gen3)
main = let
gen = mkStdGen 123 -- new RNG, seeded with 123
ints = fst (twoRandomInts gen) -- extract first element
in print ints
twoMoreRandomInts :: IO [Integer] -- twoMoreRandomInts returns a tuple of two Integers. It also -- implicitly accepts, and returns, the state of the IO monad. twoMoreRandomInts = do rand1 <- randomIO rand2 <- randomIO return [rand1, rand2] main = do moreInts <- twoMoreRandomInts print moreInts
a1 ∧ a2 ∧ ... an → b
b is called the head of the clause, and the rest of the clause is called the body. Each ai in the body is called a premise.
loves(jane,X) :- flies(X). loves(X,bob) :- movieStarring(bob).
sky(blue). chase(dogs, cats).
A :- A1, ..., Amwhere Bi = A, a resolution inference on these two clauses yields:
B :- B1, ..., Bn
B :- B1, ... Bi-1, A1, ..., Am, Bi+1, ... Bn.
| ?-
| ?- halt.
| ?-[filename].
Here is a short Prolog program:
/* Jane's connections */
/* Comments in Prolog are just like in C or C++ */
bird(ostrich).
bird(penguin).
bird(seagull).
bird(eagle).
flies(W) :- bird(W),
W \= ostrich,
W \= penguin.
loves(jane,X) :- flies(X).
loves(penguin, jane).
loves(aadvark, jane).
h :- g1, ..., gn.
| ?-bird(seagull).and hit return. Prolog would respond yes. and give us a new prompt.
| ?-bird(duck).and hit return.
| ?-bird(X), loves(X,jane).
X=penguinIf we were to hit return at this point Prolog would print yes. and give a prompt.