Pages: [1]   Go Down
Print
Author Topic: esempio perche' <: e' controvariante sui tipi output  (Read 525 times)
0 Members e 1 Utente non registrato stanno visualizzando questa discussione.
Franco Barbanera
Moderator
Forumista Eroico
*****
Offline Offline

Posts: 2.622



WWW
« on: 03-05-2016, 16:27:21 »

For simplicity sake, let us assume we have also the type
Real in PICT, and the prdefined functions sqroot (square root)
on Real.
We also assume that through the channel "printr" it is possible
to print a real value, and moreover that Int<:Real

 

def consumer [chanInt:?Int chanReal:?Real] =
       (chanInt?i =  fact![i printi])
                   
       |chanReal?r = sqroot![r printr]
        )

def producer [channelInt:!Int channelReal:!Real] =
        ( channelInt!5

        | channelReal!4.3
        )

new cInt:^Int
new cReal:^Real

run consumer![cInt cReal]
run producer![cInt cReal]


the system evolves to the above def's and the following processes


       
(
       (cInt?i =  fact![i printi]
                   
       |cReal?r = sqroot![r printr]
        )
 |
        ( cInt!5
        | cReal!4.3
        )
)

I cannot replace cInt, of type !Int, where a channel of type !Real is
expected

(
       (cInt?i =  fact![i printi]
                   
       |cReal?r = sqroot![r printr]
        )
 |
        ( cInt!5
        | cInt!4.3
        )
)

otherwise it might result in computing (fact 4.3)

But I can replace cInt, oftype ?Int, where a channel of type ?Real is
expected

(
       (cInt?i =  fact![i printi]
                   
       |cInt?r = sqroot![r printr]
        )
 |
        ( cInt!5
        | cReal!4.3
        )
)

In fact there is no problem in computing (sqroot 5)

Logged
Pages: [1]   Go Up
Print
Jump to: