fact:: Nat -> Nat
fact 1 = 1
fact (x+1) = (x+1) * fact (x)
f:: Int -> Int
f x = f(x) + 1
datatype Nat
Zero:: Nat
Succ:: Nat -> Nat
Example:
3 = Succ 2
= Succ (Succ 1)
= Succ (Succ (Succ Zero))
fact 1 = 1
fact (x + 1) = (x+1) * rec x
datatype Bus (n::Nat,a)
Nil:: Bus (0,a)
Cons:: a (Bus (n,a)) -> Bus (n+1,a)
abbreviated: [a]#n
rowr:: ((a,b)->(c,a)) a [b]#n->(a,[c]#n)
rowr f r Nil -> (r,Nil)
rowr f r (Cons a as) -> let (rx,bs) <- rec as
(b,r1)<-f (rx,a)
in (r1,Cons b bs)
zip:: [a]#n [b]#n -> [(a,b)]#n
zip Nil Nil -> Nil
zip (Cons a as) (Cons b bs)-> Cons (a,b) (rec as bs)
rc_adder::[Bit]#n [Bit]#n Bit -> (Bit,[Bit]#n)
rc_adder as bs cin -> rowr full_adder cin (zip as bs)
fact_tr N = fact1 N 1
fact1 1 y = y
fact1 x y = let y1= x * y
x1= x-1
in fact1 x1 y1
x:=N; y:=1;
while x>=1 do
{y:= x* y; x:= x - 1}
fact 1 = 1
fact (x+1) = (x+1) * fact (x)
fact_tr N = fact1 N 1
fact1 1 y = y
fact1 x y = fact1 (x * y) (x-1)Example:
fact 3 = 3 * fact 2
= 3 * (2 * fact 1)
= 3 * 2 * 1
fact_tr 3 = fact_tr 3 1
= fact_tr 2 (1*3)
= fact_tr 1 (1*2*3)
= 1 * 2 * 3
Question: which is faster in a serial machine and why
entity NAND2 iS - case insensitive
port (A,B: in BIT;
Z: out BIT);
end NAND2
architecture STRUCTURAL of NAND2 is
signal I: Bit;
component AND_2 -- Not needed in VHDL-92
port(I1,I2: in Bit; O1: out BIT);
end component;
component INVERT
port(I1: in Bit; O1: out Bit);
end component;
begin
U0: AND_2 port map (I1,I2,I); --positional
U1: INVERT port map (I1=>I,O1=>Z);
end STRUCTURAL
;
architecture DATAFLOW of NAND2 is
begin
z = A nand b;
end DATAFLOW
architecture BEHAVIORAL of NAND2 is
begin
process(A,B)
begin
if (A='1') and (B='1') then
Z<='0';
else
z<='1';
end if;
end process;
end BEHAVIORAL;