/* Qualitative reasoning in Picat. This is a port of the Prolog code of qualitative reasong from Bratko "Prolog programming for Artificial Intelligence" (4th edition), page 528f. This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ main => go. go ?=> circuit1(SwitchPos,BulbStat,Lightness), println([switchPos=SwitchPos,bulbStat=BulbStat,lightness=Lightness]), fail, nl. go => true. go2 ?=> circuit2(Sw1, Sw2, Sw3, B1, B2, B3, L1, L2, L3), println([sw1=Sw1, sw2=Sw2, sw3=Sw3, b1=B1, b2=B2, b3=B3, l1=L1, l2=L2, l3=L3]), fail, nl. go2 => true. go3 => % page 529 circuit2(on, on, on, ok, ok, ok, L1, L2, L3), println([l1=L1, l2=L2, l3=L3]), fail, nl. go3 => true. go4 => % page 529 circuit2(_, _, off, B1, B2, B3, light, _, dark), println([b1=B1, b2=B2, b3=B3]), fail, nl. go4 => true. go5 => % page 530 circuit2(SwPos1, SwPos2, SwPos3, ok, ok, ok, _, _, light), println([swPos1=SwPos1,swPos2=SwPos2,swPos3=SwPos3]), fail, nl. go5 => true. % Modelling simple electric circuits % Qualitative values of voltages and currents are: neg, zero, pos % Definition of switch % switch( SwitchPosition, Voltage, Current) % Note: we cannot use index/3 here since all variables % are not bound. % Switch on: zero voltage switch( Pos, Voltage, _Current) ?=> Pos=on, Voltage=zero. % Switch off: zero current switch( Pos, _Voltage, Current) => Pos=off, Current=zero. % Definition of bulb % bulb( BulbState, Lightness, Voltage, Current) bulb( BulbState, Lightness, _Voltage, Current) ?=> BulbState=blown, Lightness=dark, Current=zero. bulb( BulbState, Lightness, Voltage, Current) ?=> BulbState=ok, Lightness=light, Voltage=pos,Current=pos. bulb( BulbState, Lightness, Voltage, Current) ?=> BulbState=ok, Lightness=light, Voltage=neg,Current=neg. bulb( BulbState, Lightness, Voltage, Current) ?=> BulbState=ok, Lightness=dark, Voltage=zero,Current=zero. % A simple circuit consisting of a bulb, switch and battery circuit1( SwitchPos, BulbState, Lightness) => println($circuit1( SwitchPos, BulbState, Lightness)), switch( SwitchPos, SwVolt, Curr), writeln(after1=SwitchPos), bulb( BulbState, Lightness, BulbVolt, Curr), qsum( SwVolt, BulbVolt, pos). % Battery voltage = pos % A more interesting circuit made of a battery, three bulbs and % three switches circuit2( Sw1, Sw2, Sw3, B1, B2, B3, L1, L2, L3) => switch( Sw1, VSw1, C1), bulb( B1, L1, VB1, C1), switch( Sw2, VSw2, C2), bulb( B2, L2, VB2, C2), qsum( VSw2, VB2, V3), switch( Sw3, V3, CSw3), bulb( B3, L3, V3, CB3), qsum( VSw1, VB1, V1), qsum( V1, V3, pos), qsum( CSw3, CB3, C3), qsum( C2, C3, C1). % qsum( Q1, Q2, Q3): % Q3 = Q1 + Q2, qualitative sum over domain [pos,zero,neg] index(-,-,-) qsum( pos, pos, pos). qsum( pos, zero, pos). qsum( pos, neg, pos). qsum( pos, neg, zero). qsum( pos, neg, neg). qsum( zero, pos, pos). qsum( zero, zero, zero). qsum( zero, neg, neg). qsum( neg, pos, pos). qsum( neg, pos, zero). qsum( neg, pos, neg). qsum( neg, zero, neg). qsum( neg, neg, neg).