Hey everyone, this is Dan with another episode of my weekend analysis. Since the beginning of the year, SPY has been down 6% and QQQ is down 13%. SPY was down as much as 13% in the middle of March and QQQ was down 20% at that point. The market then rebounded a little in the last couple of weeks, but it started to drop again in the last couple of days. Will the market continue to recover, or will the market go down again? In this video, I will explain what has been driving the market so that we can understand where the market might go next. I will also look at the charts for SPY, QQQ, BEYONDTECH, Moderna, ASML, AMD, NVIDIA, and Qualcomm. Let's get into the details. This is the chart showing the movement of SPY, Dow Jones, and QQQ in the last 6 months. As you can see, SPY went up just a little bit by 2.9% Dow Jones was up only by 0.6% and QQQ, primarily the tech stocks have been down actually 2.58%. If you look at year-to-day, that's a little bit more than 3 months. The picture is even more bearish, Dow Jones has been down 5%, SPY down 6% and QQQ down 13%. Will the market continue to head down or will it go up? For us to understand where the market might go, we need to understand a few big picture issues. First and foremost is what's happening with the Federal Reserve Bank. As of March 17, the Fed announced that it would increase the Fed funds rate by 0.25% and they expected to have 6 more rate hikes for the rest of 2022. If you look at the Treasury rates in the last 30 days, here I'm showing the Treasury rates for 1 month, 2 months, 3 months all the way up to 30 years. As you can see, the rates have been going up and actually the Fed started to talk about rate hikes as early as November of last year and they finally put a trigger on March 17. As you can see, the market has been anticipating the rate hike in the last month and since there will be 6 more rate hikes for the rest of the year, I believe these lines will continue to head up for the rest of the year. If you look at specifically the numbers behind these rates, here's a very alarming situation. You can see that, for example, as of April 8, the 3-year rate is 2.73, 10-year rate is 2.72. That means the shorter-term rate is actually higher than the longer-term rate. We see the same situation, even more of a discrepancy on April 5 between 2.69 and 2.54. That's called the U-curve inversion. For a lot of people, that's a leading indicator of a recession. But historically, when we have U-curve inversion like this, a recession might not start until 6 months to 2 years later. So even though it's a leading indicator, it's kind of a weak indicator at this point. Nevertheless, we need to be aware of it in case the situation gets worse. So that's a bearish signal to some extent. If you like what you've seen so far, click the like, subscribe and notification button because that will encourage me to make more videos like this in the future and it will also allow you to receive notifications when I publish my next video. Oh, by the way, the video I published on April 8, just a couple days ago, got into a lot more details about the Fed rate hikes. And also, I mentioned this ETF TMV, which I believe is a good investment because the value of TMV will go up as long as the 20-year rate will continue to go up. And actually, I bought a substantial amount of TMV already. So if you're interested, you might want to refer to this video in my YouTube channel. The other thing the Fed is doing is that they are starting to reduce their total asset purchase. This chart published by the Fed is showing that the Fed has increased their total asset to the tune of $120 billion a month, pretty much since June of 2020. As you can see, this line, the total asset, has been go up at a pretty steady rate, month to month. But most recently, it has flattened out. And pretty soon, according to the latest FOMC meeting minutes, it will start to come down. And that will certainly cause the market to go bearish. At this point, we need to look at the mission and the strategic goals of the Fed. And this is directly from the website of the Federal Reserve Board. It says the mission for the Fed is to foster stability, integrity, and efficiency of the nation's monetary, financial, and payment system. And then for strategic goals, they listed six main strategic goals. The very first one has to do with maximize employment and maintain stable prices. That means these two indicators are the primary concerns of the Fed. Then as related to the market, there's an indirect reference to the market because the second goal says the Fed is responsible for maintaining a stable financial market. But then it depends on what they define to be stable. Now the market is down pretty much S&P 7% or 6%. That's certainly considered still within the range of being stable. But if the market is down 10%, 15%, would it still be considered stable? How about 20% or 30%? I will talk about that in the next few minutes. Stay tuned. Let's look at inflation because one of the primary strategic goals of the Fed, as we saw previously in the last page, is to maintain stable prices. That's why the Fed is very much focused on having low or acceptable rate of inflation as measured by CPI. For the last 10, 20 years, CPI has been pretty low, less than 2% to 3%. But in the last 2, 3 months, it has jumped up quite a bit. Now it's around 8%. If you look back in history, the last time when it was as high as 8%, it was 1982. That's many, many years ago. And that's why the Fed wants to now raise interest rate and reduce the total asset in order to bring down the inflation rate. In the meanwhile, because one of the main strategic goals of the Fed is to promote stable employment, that's why the Fed is also very occupied with the unemployment rate. From this chart, you can see that the current unemployment rate is at 3.6%, very low. That also is an indication that the economy has been pretty robust. The 3.6% unemployment rate is the lowest rate in 50 years. Because the unemployment rate is low, that gives the Fed even more reason to raise interest rate, which will have the effect of dampening the market. It will cause the market to go down a little bit, hopefully not too much. And which will in turn slow up the economy and cause the unemployment rate to increase. But since we are starting with the base of such a low unemployment rate, that means the Fed will have a lot of room to raise interest rate without worrying about causing the economy to crash and causing high level unemployment. And that's the reason why I believe the Fed will be very aggressive in the next few months with rate hikes and also with asset reduction. When the Fed reduces asset, it will be reducing the amount of money supply in the market, which will in turn cause the stock market to go down. I published a video on January 30th talking about a potential market crash or at least a market correction caused by these actions from the Fed, particularly interest rate hikes and asset reduction, otherwise known as QE tapering. I actually went back to the period of 1978 to 1984 when we also had 8% inflation rate above and I analyzed step by step how the Fed tried to balance the CPI, the unemployment rate, and by changing the Fed funds rate. And how did all the changes affect the market as reflected by the S&P 500 index. And I found that back then the S&P 500 went down as much as 21% before the Fed was able to bring the inflation under control. So the same scenario might happen again in the next few months. As you remember, S&P is down 7% yesterday. Are we going to have for a 21% drop of worse? I think there's a possibility and we need to be prepared for it in case it happens. And we need to be aware of what indicators to look at so we don't sustain the damages if that happens again. We can sell early or even go short on a market to actually profit from such a market drop if it is going to happen. Another thing that's driving the market is the unfortunate Russian invasion of Ukraine. We started on February 24th and it's still happening now. And that's indeed quite a tragedy for a lot of the people in Ukraine. Let's hope that this war will be ending soon. But as long as the war is happening, it's driving up the oil price, natural gas price, as well as the price for grains. And that definitely has a very profound impact on the market. Actually, it's causing the market to be bearish in the last month or so. One little bit of bright news which tend to have a bullish influence on the market is that the daily COVID cases, new cases, have been going down and same with the daily death related to COVID. There's, however, an exception that's in China. China recently has an outbreak of the Omicron variant, even though they only found a few thousand new cases a day and that's pretty low by American standard or by European standard. But to China, which had almost zero case for several months, that was unacceptable. And that's why they wanted to lock down Shanghai and quite a few major cities to stop the spread of the virus. Because of the shutdown in Shanghai, which already lasted for 12 days, transportation and shipping activities in the port of Shanghai have been delayed. With the Shanghai lockdown, it will or it has already caused further delay to the supply chain. As you know, China supplied a lot of industrial products to the US and to the Europe and the rest of the world. If China is going to continue to have this type of lockdowns, definitely the supply chain issues will be aggravated, which will cause inflation to go up even more in the United States and in the rest of the world. And that's a bearish driver for the market. On November 28th, when I posted this video, the market was very bullish at the time. But I said the market is not going to be going up and up forever. The question is, when will the market become bearish and what are the indicators that we should look at? I listed a few possibilities that will cause the market to go down, including any new COVID variants that are damaging, continual high inflation rate, and so on and so forth. Out of the six points that I listed, three of them came to reality. We now do have very high inflation rate at 8%. Back in November, it was only about 6%, 7%. And then continue to go up. And that's why not the Fed is under a lot of pressure to increase interest rate in order to contain the inflation rate. And the Fed tapering indeed started to happen. Back in November, the Fed was talking about reducing asset buying from $120 billion a month to $105 billion a month. But now the rate of asset increases at about $30 to $20 billion a month. It will soon go to zero and then it will become negative. That means the Fed will actually be reducing assets. That means they will be reducing the amount of money supply in the market, which definitely is going to be a bearish driver. In November, the Fed was talking about increasing the Fed funds rate and finally they put a trigger on March 17. So these three concerns came to fruition and that's why the market has been heading down since the beginning of the year. So if you look at the market reality now, we have, first of all, these three concerns that are listed. Back in November, in addition, we have the Ukraine war, which has driven up the oil price, gas price and grain prices.  These are the main reasons why the market has been going down since the beginning of the year. How will the market recover? First of all, first quarter earnings are being announced by the various companies in the next few weeks. If we have very positive Q1 earnings, that will certainly help the market recover. But the more important thing is what they are predicting for the second quarter. Because the second quarter is during the time when we will see the impact of the Ukraine war and the various new issues. So if the second quarter projections from most companies are bullish, that will certainly help create a market rebound. The end of Ukraine war will help the market recover and the end of COVID lockdown in China will help the supply chain recover. If the inflation in the next few months can fall to 3-4% range, then the Fed will no longer be under the pressure to continue to raise interest rate. And then if the Fed finally ends QE tapering, that will stabilize the money supply in the market, which will then stabilize the stock market. I will be watching the indicators for these issues because that will allow me to decide what stocks or ETFs to buy or sell. And I will share my analysis with you in the next few months as I post more videos. In the meanwhile, let's look at the different sectors, how they perform in the last month and also in the last week. If you look at these two tables, which ranked the various sectors, by the highest return, which is on top, to the lowest return, which is on the bottom. As you can see, the healthcare sector actually ranked as number one for the last week and for the last month. The healthcare sector has been doing well. So that would be a good investment even if the market is continue going to be bearish because people still have to go to the hospital and they need medical care. And also in the meanwhile, because COVID is fading, the healthcare costs related to COVID are reducing, which will help improve the profit margins of the healthcare carriers. And then next to that, consumer stable sector seems to be doing well. Then the utility sector has been performing pretty well also. And therefore, in the next few weeks, I will be tracking stocks and ETFs related to these three sectors. And I will post more videos about these three sectors. Stay tuned. So what will be my strategies considering what's been happening in the last two, three months? First of all, I continue to hold shares of stocks with strong fundamentals because these stocks belong to a very strong company with good cash flow. They're not going to go bankrupt if you have a recession. I'm holding them for the long term. And besides, these companies include Google, ASML, AMD, Taiwan Semiconductor and Qualcomm. And I've been swing trading some of my other shares. And I just sell off some of the shares as the market was going down because I wanted to be able to buy them back at a lower price. For swing trading, I would sell a stock when it loses support at key levels or pulls back from a resistance level or when an adverse news develop about a company behind a stock. Or I will buy a stock when the price bounces up from a key support level when it breaks above a key resistance level when positive news develops about a company. And I'll be definitely watching the broad market and monitoring the news events. And I've been buying a few ETFs that have been going up due to recent events, such as SQQQ, UCO, the oil ETF, UNG, natural gas ETF, XLE, energy sector ETF and TMV, the ETF that's pegged to the 20 year treasury rate. A couple of days ago, I published this video and did a deep dive analysis of the Fed rate hikes and how TMV has been moving up as the interest rates increase. I believe TMV will be a very good investment for the next few months. I will continue to update my Twitter subscribers almost on a daily basis about some of my trades and about some important current events. At this point, I'd like to suggest for you to subscribe to my Twitter account in addition to subscribing to my YouTube channel. For example, on February 1st, I said I sold some of my ASML shares. I sold at 667 and today it's 610. So in retrospect, I avoided 10% loss of my shares and I do intend to buy them back at lower prices. On February 18, I said I bought SQQQ shares and since then I've been selling and buying SQQQ. Then on March 17, I tweeted that I bought TMV shares and those shares are up 12% already. On March 30th, I bought XLE shares and those shares have gone up as well. If you like what you're seeing, I'd like to remind you to click the like, subscribe and notification button. As usual, I will welcome your comments, questions and suggestions. Let's look at the individual charts for the different ETFs or stocks. First, let's look at SPY, which is back to the S&P 500. I will be using the charts from the Think or Swim software published by TD Ameritrade. In each of the charts, I will be plotting some special lines. The blue dash line will be the 50 period exponential moving average, the EMA and the yellow dash line will be the 100 EMA and the purple dash line will be the 150 EMA and the red dash line will be the 200 EMA. I'm showing three panels here. The left one is SPY hourly chart. The middle one is daily chart and the right hand side is a weekly chart. And then this here, the candlestick chart is the actual price of SPY. I also show the Bollinger Band as well as the 50 EMA, which is the blue line and then the 100 EMA, the yellow line and then the 150 EMA, the purple dash line and the 200 EMA, the red dash line. So if you look at the daily chart first, SPY reached the peak on January 4th and then it went down quite significantly, then it started to recover on March 11th or so. Then it reached a short-term peak around March 29th and started to come down again. If you look at the latest price points, as of last Friday, it closed at 447, which is a little bit above the 20-day simple moving average the middle of the Bollinger Band. And that's why the 20-day simple moving average will be a very important support or resistance point. And if you look at the other support point, that'll be at 446, which is the 50-day exponential moving average. And then if you go down from there, the next support point will be at 439, 440 or so, the 200-day exponential moving average. For resistance levels, the next level up will be at the previous peak of 457, which is right here. So that'll be a very important resistance point. And also the next level up will be 461 right around here. The factors that will be driving down SPY will of course be the Ukraine wall, the Fed rate hikes, and QE tapering. The positive factors that will cause SPY to go up will be if there are any positive, very positive first quarter earnings from major corporations, and also if they have any very positive second quarter projections, those will boost the market and cause SPY to go up. Let's look at QQQ. For QQQ, it's very similar to SPY. It peaked actually earlier in November, around November 22nd, then it started to come down and it bounced up, starting around March 15th, and then it hit a short-term peak around March 29th, and came down just like SPY. So if you zoom in, you can see that the closing price for Friday was below the 20-day simple-moving average in the middle of the Boling band, and that was a major support point. You can see here in the last two days before Friday, the middle of the Boling band was supporting the price, but it breached that support level and went below it, and that's a very bearish sign, and that's why on Friday, I bought SQQQ about an hour before market closing. That's because of the bearishness of the chart, and also because of the current events with the Ukraine war and consecutive rate hikes that would happen pretty soon in the next few months. For major support point, the next level of support will be 328, which is the lower Boling band, and then also 329, right around here, as well as 324, which will be this line, and then the next level support will be 318, and that's going to be a very, very important support point because if we go below that, that's going to be making a very significant lower low, and that's definitely going to be a very bearish sign. Let's hope the market doesn't go there anytime soon. For resistance level, the next level up where we will see some resistance will be certainly now the 20-day simple-moving average. It was a support level, but it will turn to a resistance level, so the price has overcome that very strong resistance level. Then the next level of resistance will be 354, and then the next level up will be the 200-day exponential-moving average, the red dashed line here, and then the next level up will be 100-day exponential-moving average, the yellow line here at 360, and then the next level of resistance will be 369, this historical point. Similar to the SPY, the bearish drivers for QQQ will continue to be the Ukraine war and fat-rate hikes as well as QV tapering. Anything that will boost QQQ will be positive earnings from the major corporations and positive second quarter projections. Let's look at BeyondTag, BNTX. The last time I published a video on BeyondTag was January 15, 2022. At the time, my target was that the price would reach $240 by the end of March, 2022. Because of what's been happening to the market recently, I will have to reset my time horizon to say it'll reach 250 one year later by March 31, 2023. BeyondTag is still a very strong company fundamentally, but then if the water level of the entire ocean is dropping, then all boats will drop, and that's why it's going to be taking longer for BeyondTag to reach my price target. And I will revise my target if the situation changes in the near future. For BeyondTag, if you look at the daily chart, it peaked in August of 2021 as being pretty much coming down very dramatically since then. And the main reason is because of the fading of the COVID pandemic. It reached a bottom here on March 8th and started to recover, and the recovery corresponding to the approval of the booster shots for the second booster shot, which will be the fourth shot for certain age groups in the U.S. and in Europe. But then it hit a short-term peak on April 4th and started to come down, but in the last couple of days, it seems to start to have a little rebound. It's getting support from the middle of the Bollinger Band. So if you look at support level, definitely the middle of the Bollinger Band, 20 days, simple moving average, and that's being a good support level. The next level support would be the lower Bollinger Band at 150. And then beyond that, the next level support would be around 135, which is this historical point around here. And then the next level support would be at 124, which would be right around here. And then the next level support would be 121. If you get below 121, that will be a very, very sign because then it will be making a lower low. And that means the price will continue to go down even more. Let's hope that doesn't happen. For resistant level, the next level of resistance will be at the 50 EMA of 171, which is the green dashed line here. And then the next level up will be the 186 level, which is this historical level around 186. The next level of resistance will be 196. And we'll have to go a little bit further back in time around here. So we're looking at a gap closure here. When we get to this point, we'll experience pretty steep resistance. And then the next level up will be 218 at this historical point. The negative influence on beyond tech is the fading of the COVID pandemic. It's good for humanity, but bad for the price of beyond tech, unfortunately, if you have been holding beyond tech shares. Any positive influence for the stock price will be the approval and the use of the second booster shot in the US, EU, UK, and the rest of the world. And also any progress they will be making with regard to the product development pipeline for new vaccines and treatments for various diseases. BioNTech does have quite a few products in the pipeline at this point. For me to be buying more beyond tech shares, I want to look for higher highs and higher lows. And looking from March 7, we do have higher highs here and higher lows if you compare this point to this point. So I'm still being very cautious. Actually, at this point, it might just have a rebound because if you look at here, the RSI indicator is definitely not overboard. It's pretty much in the middle with a value around 50. DMI on a daily basis has been positive bullish and MACD was bullish. Now it's just converging to a neutral point. But if you look at the weekly chart, it's definitely still bearish if you look at DMI and MACD, although both of them are on the verge of turning bullish. And hourly chart, certainly it looks pretty good for the last couple of days. So maybe there will be a buying opportunity for BioNTech soon. Let's look at Moderna. My previous target was that the price would be at $240, but in March 2022, just like BioNTech, I have delayed the timeline for reaching the target by one year. Moderna, the chart looks very similar to the chart for BioNTech. It's been coming down since April 4th, April 5th, and then it hit a bottom here and started to rebound on March 8th. It rebounded a little bit and then started to come down and it's been recovering a little bit, although the recovery instead of looking like BioNTech, which rebounded at a 20-day simple moving average, it went below that line and then started to rebound. So if you look at the next level support, we'll be at $150, which is here. And then if it goes below that, that's definitely going to be a very bearish sign. Then we have to go all the way back to here at around $135, which would be another support point. And then the next level support would be at $128, which is here. From the positive side, if the price does go up, then we can see the next level of resistance at the middle of the Bollinger Band, 20-day simple moving average, and then the next level support will be $180 right here, and then the next level support, $187 right here. Similar to BioNTech, the fading away of the COVID pandemic is actually hurting the stock price. It's good for humanity, but bad for the stock, as for any factors that will drive up the stock price, it includes approval of the booster shots in more countries and also the use of booster shots in more countries. Just like BioNTech, I really want to wait for a clear sign of the stock price making higher highs and higher lows before I go in and buy more shares. Let's look at ASML. The last time I posted a video on ASML was November 3rd, 2021. At the time, my target was that it would be 910 by the end of February, 2022. Again, because of the bearish market recently, I'm delaying the timeframe by one year. So I'm saying that it'll reach a target by the end of February, 2023, but ASML is basically a very strong company fundamentally. I'm still holding some ASML shares for the long term. ASML, if you look at the daily chart, it reached a peak on September 14th and it's been ping-ponging up and down but generally trending down. Generally speaking, the semiconductor stocks have been going down since around August, September of last year because of the fear of interest rate increases. It rebounded a little bit after March 8th and then reached a short-term peak on March 29th and started to come down and down. So far, if you look at the shape of the curve, it's still very bullish. I don't see any sign of recovery yet. The next level of support will be the lower bowling demand at about 590. And then if it goes below that, the next level of support will be around 578 and then 574 and then this point will be 559. If it goes below this intraday bottom of 559, that'll be a very bearish sign and definitely that would not be a time to get more shares. For the next level of resistance, if the price does go up, the next level of resistance will be around 628 right here, this historical level and then the next level up will be around 640 at this line and the next level of resistance will be 653 around here, 60 and then next level of resistance will be the middle of the bowling demand at around 660 and then the next level up will be 680. Recently, the semiconductor industry turned even more bearish because of the potential of a neon shortage due to the Ukraine war and of course because of supply chain issues. Any factors that will be driving up the price of ASML will be the continued buildup of semiconductor manufacturing capabilities for the semiconductor industry and also because of the very aggressive stock buyback program by ASML. ASML is a very strong company if you look at their fundamentals and also if you look at their very strong cash flow, they have the monopoly on the EUV lithography market and that's why I'm still holding quite a lot of ASML shares for the long term. I will have confidence that ASML will eventually recover. The big question is when will be a good time to buy more shares? And looking at the chart, I'm not going to be buying more shares now until I can see definitive pattern of the stock price making higher highs and high lows. At this point, I don't see that yet. Let's look at AMD. The last time I posted an AMD video was February 9th, 2022 and at the time my target was that it was going to reach $160 a share by the end of June, 2022. Again, I'm delaying the time horizon by one year so I'm saying that it'll be at or above $160 by the end of June, 2023. Again, AMD is a very strong company that's generating a lot of cash flow and it will eventually recover and that's why I'm still holding some AMD shares. For AMD on a daily chart, you can see that it reached a peak on November 30th and it pretty much has been coming down and then it made a quadruple bottom here right around 101. So if the price can be supported at 101, maybe it'll have a nice, at least short-term recovery. At this point, it's been going sideways pretty much. Well, you see a little bit of a triangular pattern here that tend to be going a little bit bearish but 101 is definitely a very strong support point although it went a little bit below that as of last Friday. So if you look at support levels, the next level support will be at just about 99 to 100. If that's breached, that will be a bearish sign because with the quadruple bottom, if it's breached, the stock price will most likely go down another 5, 10%. So if it goes down more from there, then we'll have to go all the way back to November of last year to see any support level and which will be at around 89 actually here. So we'll be going all the way back to July of last year to see any support point. And then if you go further down from there, the next level support will be around 85 and that will be definitely very bearish of AMD. AMD recently was downgraded by Barclays. Barclays lowered the target price from 148 to 115 and that's what's been the primary factor for driving the bearishness of AMD. The good news is that I don't see a lot of downgrades from other analysts. In my opinion, AMD is still a very strong company. I still hold AMD shares and I will be holding the shares for the long term. I think the shares will eventually recover. They have a strong product line and recently they acquired this company by the name of Pensano, which will help them increase their competitiveness in the data center segment. At this point, I'm not going to buy more AMD shares until I see a rebound from here. And actually in the next two, three days, if it starts to rebound from here and if it starts making higher highs, then that'll be a very positive sign. So at this point, I'm hoping that AMD is oversold. If you look at RSI, it's certainly oversold. It's at 37, but if you look at the daily DMI line, it's saying it's bearish. The MACD indicator is also bearish. So let's hope it'll overcome this quadruple bottom and start recovering. It's a good company. Let's look at NVIDIA. The last time I posted a video on NVIDIA was February 9th, 2022. My target at the time was $310 a share by the end of June, 2022. And my new target is going to be $310 by the end of June, 2023 because of the bearishness of the market and because of the expected rate hikes from the Fed as well as QE tapering. Again, NVIDIA is a very strong company. Fundamentally, it will eventually recover. NVIDIA, similar to the other semiconductor stocks, it peaked back in November, then it's been coming down and also it's been getting support around 209. It's critical that NVIDIA stays above that level. Then it stays a rebound starting in the middle of March. Hit a short-term peak on March 25th and it's been coming down pretty rapidly. If you look at potential support levels, the next level support will be at 219 at this level. And then the next level support will be 213. And then next level support will be 207. If 207 is breached, definitely that'll be a very bearish sign. Next level of resistance will be at 242 right here. See this cluster of daily close around 242. That'll be a strong resistance point. And then next level up will be 259, the middle of the Bollinger Band. Next level up will be 264. And the next level of resistance will be around 283 right here. Again, like any other semiconductor stocks, NVIDIA has been coming down recently because of the interest rate hikes concerns and because of the fear about potential neon shortage due to the Ukraine war. Most recently also Intel has been producing dedicated GPU graphic processors, which will be in direct competition with NVIDIA. As you might know already NVIDIA is a leader in the graphic processor segment of the market. Those are the bearish factors driving down the price of NVIDIA, but NVIDIA is still a very strong company fundamentally with very strong cash flow. So I have confidence that NVIDIA will definitely stage a recovery sooner or later. But this is definitely not a time to buy yet because of the bearishness of the price movement in the last couple of weeks. I will want to see higher highs and especially if the price start to rebound around 210. I might buy shares and swing trade with them for the short term. Let's look at Qualcomm. The last time I posted a video on Qualcomm was November 18, 2021. At the time my target was $240 by the end of January, 2022. Just like the other stocks, I'm delaying the timeline by one year. Again, Qualcomm is a very strong company fundamentally. I still hold some Qualcomm shares because I believe it'll recover eventually. For Qualcomm, it peaked in January and then it's been coming down. The most recent short-term peak was March 28th and it has gone below several support points up until now. So if it's going to go further down, the next level of support will be drawing reference way back from around October of last year. That'll be at around 130 at this point. And then the next level support will be around 126 and then the next level support will be 122, 123. Definitely Qualcomm has not been doing well lately. And if you look at the price movement in the last few days, it's definitely very bearish. Not a good time to buy into Qualcomm yet. But Qualcomm generally speaking is a very strong company with strong cash flow and that's why I believe it'll be able to survive any downturn in the next few months. I still hold some Qualcomm shares for the long term. If the price is going to rebound, the next level of resistance will be at 143 at this level and then 150, which is the middle of the Bollinger Band. And then the next level up will be 153 at this level and then the next level up will be 158 at this level. Most recently, there was a pretty significant downgrade that was issued by JP Morgan. They downgraded on April 5th and lowered their target from 205 to 240 although they maintain their overweight rating for the stock. Nevertheless, it's a bearish influence on the stock price. For the long term, because Qualcomm makes very competitive cell phone processors, I believe the company will be able to weather the storm in the near future. As I mentioned, I still hold Qualcomm shares and when I start seeing Qualcomm making higher highs, then I will most likely buy more shares. I'd like to remind you that I'm not a financial advisor. I share my stock analysis and trading strategies for entertainment and educational purposes only. If you want to buy or sell stocks, you should make your own decisions and you should definitely consult with your financial advisors before you do so. This wraps up my video for now. I will chat with you again in the next few days. In the meanwhile, I'd like to wish you the very best of luck with your financial investments.


The Global Shapers community is the first truly global youth network that's focused on improving the state of the world. It's a global community of youth between the ages of 20 and 30 who feel involved in the cities that they live in and care about the cities that they live in and want to have a positive impact. Our role is to be able to try and accelerate some of the great initiatives that are being discussed and hopefully implemented through the WEF organization. I think we're the voice of the young. It's such an illustrious crowd of dynamic people and it's great because when we sit on the same platform we have so much to share with each other. It's just an incredibly diverse, innovative, extraordinary group of people who are working really hard in their personal lives to make a change around a particular cause and it brings them together. Global Shapers primarily meet in their local hubs but then they also are connected through a global platform to interact and exchange with other global shapers around the world. I think what's been really interesting about the WEF has done it is by creating a hub-based model. We live in, I guess you could call it a borderless world at this point.  So you could have somebody in the Washington DC hub who's doing something or project or collaborate with somebody who's in the hub in the Middle East and might directly tie into what they're doing. So I think that's really powerful about it is that you just have this ability to interact and transact with somebody halfway across the world and connect right away. We really want to address some of the world's problems and we might not have all of the resources out there or all of the experience but I know that we have the energy.


Okay, welcome to the second lecture. A few announcements to begin. In order to submit the homework on Sunday, you will need a class account. I'm sure you are familiar with these. So there are a few left. Please pick them up at the end of the lecture. Number two, maybe not all of you are on Piazza yet. So if you are not signed up for Piazza, because you will find announcements such as the starter kit, a starter code for the homework was released. And so please do the sign up. Next thing I need to announce now I need to remember. Okay, that's right. So if you want to work with a partner for projects and in fact you will have to because the projects are hard enough that you will want to have a partner and will insist that you have one. So if you have a partner already or you know who that would be, send us email by end of Sunday. And for people who do not have a partner, or will not have a partner by Sunday, we will try to use the homework and a little questionnaire to match you up according to your skills and schedules. That's probably all there is to it. So the homework you need to know about that. If you are on Piazza, you do know. Today is Thursday, so no laptops. We'll talk instead to each other and to me hopefully. And that's probably all about Manistrativia, unless you guys have some questions. The lectures are screencasted. The one on Monday was recorded probably missing audio for the first six minutes or so. When they post it, I don't know. You guys know how long it takes to get this posted. This is the first time I'm trying it. I'm also recording it myself, but this mic doesn't seem to work really well. So we have probably one screencast with good audio and one with good video, and then you can maybe put it together with the slides. Yes? It gets recorded. What I write on the screen gets recorded, but things that I only scribble to underline and use as a pointer I erase, but things that I mean to stay part of the published PDF. Okay, so let's start. So two more things you probably notice on the calendar that there are two midterms, one of them in the middle of the semester, the other one at the end, where the last lecture would be, and then final exam is actually show and tell with posters and demos. The grading is roughly like this, of course, quite a bit into the projects. The final project is important too, but you only have three weeks to it, maybe this phone. So only 15% for that, considering it's about three weeks. Class participation is important because it's an upper-level class. In fact, it's sort of a design class where you should build stuff. We want to think of it to the extent possible. As a studio, you build stuff, you show it to people, they come, they give you ideas, they critique it. So the talking is important, and I know not everybody feels comfortable raising your hands in the lecture room. I never did as a student, but there are other ways. You can talk in the smaller venue of the recitation section, you can talk on Piazza, you can post your comments to the lecture notes. There are many ways how to express yourself, ask questions, and participate. So what did we do last time? Last time we looked at about ten examples, five of them in more detail of small languages that programmers are actually likely to write in their practice, and you would be among them perhaps. We talked about what is a programming abstraction. Programming abstraction is essentially, you could say, a bunch of data types with operations on them, objects with method calls. When you have streams, you would have filters and the data flowing between them, so the streams. And usually a way of, and that's important, putting abstractions together, composing bigger abstractions out of smaller abstractions. So building bigger procedures out of smaller procedures would be this example of using abstractions to get bigger and bigger and bigger software. And this is how software is built, because you cannot write one million lines of code completely flat, of course. So what is a language and what is a small language? We also covered to some extent, but let's actually practice it with something that we did not have a chance to cover. So the map reduce story. So who knows what some map reduce? You probably used it, all of you. So how would you call it? Is it a library, framework, tool, compiler? Actually, it doesn't matter what it is. I'm curious how you perceive it as, what you perceive it as. Higher order functions. Okay, so it is some sort of a library with higher order functions, right? You pass it the operation that happens in the map phase, operation that will take place in the reduce phase, and then this big runtime will get the computation crunching on thousands of machines, if necessary. So to compare map reduce to the state of the art before, how do you think people implemented these large-scale data processing computations on thousands of nodes in a cluster? You can pose the question differently. You can ask, if map reduce is so useful, it's presumably because it abstracted a lot of plumbing, nasty, dirty detail away from the state of the art before. So even if you don't know what it was, you probably can venture to guess what tools people used to write such applications before. Exactly. So they use a library like MPI, which is a library for sending messages from one process to another, receiving it, unpacking it, and probably a library for starting processes on all these nodes. And when nodes crashed, it was in the hands of the programmer to recover the nodes, recover the data, and do all of this. So you could say that, oops, it all started with a library like MPI for message passing, and this is nothing but a bunch of procedures that you could call. And then came map reduce, which encapsulated more, abstracted more of the things, and I would call it a framework. Now, there are a million definitions of frameworks on the web, but I have something clean for you. So library is sort of a simple flat function that you call. And it returns and it gives you some value. Another example of an abstraction package is a library would be a socket. It has a beautiful abstraction, right? It's a complete illusion, in fact, because if you look at the hardware at the network, there is no such thing as a socket. You send a message across the socket, everybody listening on the Ethernet can listen to the message, but you have the illusion that the message goes from socket on one machine to the socket on the other machine. It's a fantastic abstraction because it completely divides something that doesn't really exist in reality, and therefore it hides a lot of plumbing. So socket would be such a library. Just call it and it returns back the data. Framework would be a library that is high-order in the sense that you give it some functions. Right? So a framework would be a library that you could say has holes in it. Right? And these holes are plugged in by the user. So in this hole, you would put a function F and G, and these are the functions that would be involved in the map phase and the reduce phase. It's high-order. So you could say that the framework is a parameterizable library. Of course, it comes with the abstractions. Libraries come as well. Sockets are abstractions. But it allows you to hide more and parameterize it, and therefore you can write more stuff because you can sort of plug in the function that the user wants, where they belong. So now if you think about the limitations of map reduce, what do you think would come as the next step to make the programming of such clusters easier? Well, it could be sort of an optimizing compiler where you write a C program or a scheme program and it compiles it. This is in general quite hard to do well. So it could work in some cases, but sort of a sober solution that is guaranteed to work. Sort of a small delta that improves on the state of the art of map reduce would be what? Something that does not actually require the compiler to do these heroic acts of distributing it out automatically. So you could lift it up a little bit indeed by sort of making it easier for the user to write the normal program and then tag things, okay, so this needs to go to map, this needs to go to reduce. That's true, but from what I know from map reduce programmers, this sort of lifting it up a little bit to shorten the number of lines of map reduce code is not really the burning problem. The burning problem is that map reduce does what? It gives you one pipeline, right? You have one map, one reduce. Real programs require putting maybe three or four such pipelines together in various ways. And if you want to do it with map reduce, you are back to the original MPI problem of taking care of the buffers, runtime management, and again all the slow level details of buffer management are exposed to you. So map reduce has an abstraction, but it's really not a true language because these abstractions cannot be arbitrarily composed. They cannot take two map reduce pipelines, stick them together into a bigger one and map reduce or whatever other composition. And you cannot keep doing this. So that was the big limitations. So this is where language is like flume, Java, not that well known, but existing and in fact this one is developed by Google and used apparently by hundreds of programmers at Google. Give you the power to lift it to a high level and compose these pipelines again into bigger one, bigger one. So you could now have libraries of map reduce pipelines. You stick them into bigger ones, you stick them into libraries, somebody else can use them. So flume Java is called Java because technically it's a library, somebody implemented it in Java, looks like a library, you can download it, but it comes with abstractions like these pipelines that are stuck together and they are optimized by a compiler. The compiler has a reasonable easy job in this case because it's not working with C code or scheme code, but with dedicated abstractions of pipelines, filtered, joins and such. And so optimization is easy because it's a library program just those. So you could say flume Java is composable abstractions. Okay, so you take one, map reduce, you take another. Okay, so this is map, reduce, map reduce and you can stick them together into something that looks like another pipeline that can be plugged somewhere else. So this flume Java is what we call here in this class small languages. It's written for only small narrow class of programs, but in a company like Google it takes care of many different problems, makes programmers more productive. And flume Java is not the only, there is a bunch of things that lift map reduce to a higher level like source all defined soon after map reduce. So we talked about all of these what are composable abstractions, what are not, so sockets are not because you cannot take a socket and put two of them together to build composable because this here is a regex, this here is a regex and so this whole regular expression here itself is a regex and you can build them up like this forever. In fact this one here itself is a regex build out of this and the star and this one here is in fact build as a concatenation of these three strings each of which is a regex. We'll talk more about the regex later. Okay, so what do we do today? So today we look at sort of the very basic what are programs, what are values, what are types, how do you evaluate program, how do you represent them, how do you represent values, how do you represent types, but we'll do it all in a small fun example that we'll actually present design challenges and make you think about all these issues. And most importantly we'll get to the question of how you can actually make your language extensible. Right? So often these small languages are written in terms of big languages. The flume Java is written sort of as an extension of Java. Java is not quite well suited for writing these small languages, for embedding them into Java, but you sort of can get along with generings. Scheme is better for that purpose. So we want to learn today how to build a small language that actually somebody else who uses the language can grow it, can extend it without going into the interpreter or compiler and doing hacking. And what we are going to do is remember the Google calculator, right? That's a language after all. It's true language because you can take an expression and another expression and combine it into arbitrarily big expressions so the programs can be arbitrarily large. It doesn't have some procedural abstraction, so in a sense it cannot hide things, but I'd say it's a language. Now, we'll do better than this. We'll show you how to implement that, but we want to see what other features we could add to this calculator language so that it is even more useful than the Google calculator. Okay? So this is what the calculator does to refresh your memory. Imagine you want to find out how fast those ferry boats work. On the San Francisco Bay, you go to their website, figure out it's 34 knots. I have no clue what that means, but this calculator is translated and here is the same expression that we have seen, and this is really, of course, the volume, which is this with energy and another volume and power and happens to be time expressed in days. So what constructs does the language already have? Can we get a quick list of what's available in the language in this calculator language? So we would say there are numbers of what type do you think they are? The numbers. Inns, floats, both? Floats. Okay. There might be ins. We would need to poke a little bit to see, but I don't know, it's not so important. We'll need to decide for our language what we do. So do you think these units are these are what? These are themselves numbers. These units. Is that something you can liken these units to? Yeah, it's one legal view that you can add a type, sort of a type. Not like a string type or a flow type. I think it's fair to say that these are types. Definitely, that's how I plan to talk about them. What else is there? So we have numbers and units. These are the leaves of the expressions. Do you see? So then there are operators. So some operators are obvious. Other some operators are not so obvious and are specific to this example. There would be this converter operator, this in, which you could say conversion operator. That's probably it. Is there more? Per, I see. So I would say, well, okay, good. So what is per? Is it conversion or is it just can we somehow clump it with something else? What's that? Division. So I think that per is actually an alias for the division operator. It's of course cool to make it so easy to add new operators by just saying this is the same as that. You can work on types just like division you can put between meter and seconds. But if you think of numbers and units as sort of the leaf things on which you operate, they are either values or types, the operators, okay. The operators are overloaded to work on one times two, but they can also work on meter times second. So in a sense, the operators work for both. We'll get to it during this lecture. So is there more in this language than that? Okay, excellent. So parentheses. Okay. And parentheses are interesting because they are in the language kind of and kind of not. Does the interpreter you think really know about parentheses? At some stage, these parentheses evaporate. At some stage of the evaluation of the program. Why is it that we don't need them at some point? Okay, I'm looking. Right, more people. Why don't we need those parentheses? How do you think those programs or expressions are represented internally? Okay, that they are structured and pre-parts and they are structured as what? As what data structure? A tree would be the natural choice, just like the scheme expressions, right? Scheme doesn't need a parser because it's already parsed for you into these expressions, okay? Exactly. So these parentheses are there but only exist on the surface level at where you type it. After that, they are gone. And when they print you the result, they put them back in so that you can understand the precedence of these operators. All right, I think that's it, unless somebody can tell me that we are missing something. All right, okay. So, like, have a synonym for some constant, so good point, right? But even with these, these are fun to add in and you'll add them when you build your parser, but this is not enough to make it challenging you, as you will see. What I want to do now is how do we extend it? How do we turn the Google calculator into a Berkeley calculator to make it actually more useful to people? So can you now... Actually, this is right. This is where we want to be. So can you talk to your neighbor for a few minutes and try to come up with scenarios of how we could extend this calculator to make it more useful? I have a few such scenarios in the lecture and it will drive the development of the language, but I'm curious what you'll come up with. I want some extra scenarios because I would like to show you that maybe we can support fun, useful features by doing very little in the language. Okay, so I think you probably have something, so let's hear from a few people. In future lecture I may consider having a random generator to generate an x-coordinate and a y-coordinate and pick somebody, but for now it looks like we could hear from volunteers. Okay, we could have variables that have essentially a constraint solver and equation solver. That would probably complicate things quite a bit. Not that this is a bad idea, it would be great because now all of a sudden it's not just a matter of evaluation, some program you need to essentially solve equations. These things are not too hard, but all of a sudden the inside of the interpreter would change quite a bit. So I love the idea. It probably would not be a tiny extension that I can do in three lines of interpreter code. The idea is good. Okay, so if you didn't hear more complex numbers, base 2, base 16, okay, complex. But let me write these. So essentially equation solving which involves variables of course whose value solve, now we say complex base 2, okay. What was that? Matrices, okay, I'll add it here since these are essentially richer type, right? Just like complex number is two numbers, two floats, matrix would be matrix of numbers. All right, okay. So that may also require quite a rich library of algebraic rules that we can use to perhaps simplify to do integrals since there isn't a sort of deterministic algorithm. But would be useful, okay, please. Okay, so that should be easy to add, right? Because this is just one expression. So let's call it a power function. So I'm looking for something that is easy to understand and useful. So a good ratio in that way. Okay, so new unit types, right? And conversion naturally comes with it, please. Okay, okay. New operators. So this is power and arbitrary operators. So presumably you would need to give them some general language, right? Well, sometimes you may create operators out of existing ones, and probably you would need essentially the ability to define a procedure, use it as an operator. Okay, three more. One. Yes. Okay, so you could say infinite arithmetic, right? Infinite precision including for rationales. Okay, okay, okay. So time dealing with time including the current time, right? Oh, okay. Okay. Reverse polish notation. So this is essentially you are saying new syntax, right? Since that's really all that it changes. Sure, why not? Right? If you grew up with the HP calculator. Okay, one more. It looks integer division. So we do have integer division. I think it just does the usual that it takes the whole the floor of the result. Actually, no, it doesn't. You're right. It will convert it into float. So I would put it here. You're right. Int division. So this is great. I put new unit types and time left up because this is what happens to have prepared in the slides. Okay? And the interesting thing is that I view this as letting the user, not the programmer, the user. I mean your grandma extend this language with new units. So you want to do it in such a way that they don't need to learn Python and change the interpreter. They can just define a new units. And there is something cool about the time as well. So let's go there. Okay? So we could, yes, add variable functions. These are the operators. But let's see what we'll do. Okay. So how do we grow the language? So now I don't mean growing by the end user. I mean by us. We'll build the language sort of piece by piece. So we'll start with simple arithmetic numbers. And the language is defined by syntax and by semantics. Syntax will tell you what. How the program looks on the surface, right? What operations you have, how you can compose together. And semantics will tell you what they mean. What does it mean to do multiplications of integers? If I take five and two and multiply them, what is the result? That's semantics of the language. Okay? So in this language, what do we have in this sort of subset for arithmetic expressions? We say the program needs to be an expression which could be either an int. So a number. Let's just have ints. It could be an expression in parentheses. Or it is two expressions connected with some operator. And the operator can be well plus minus times division and power. Right? So this is nothing but a grammar that defines the language. We need to use a grammar because these expressions could be arbitrarily large. So we need a grammar to define an infinitely large language, right? Because the grammar recurses to itself, right? This e recursively refers to that, right? So one example from this language would be program one, program one minus two. It could be also two minus three divided by four. These are all examples from this language. Now, the meaning so the syntax defines that we have these programs. In here we're assuming we have some floating points, some floating point numbers as well, but that doesn't matter. Now how do we define what e1 plus e2 means? So given the values of e1 plus e2 e1 plus e2 is the sum of these numbers. So we're saying whenever we see a plus operator we need to take these numbers and add them. Seems trivial, but we have some subtle issues there, right? So when we want to tell somebody go and implement this language for us, he will come back to us and ask well, what should plus do? What little details do we have to spell out for the implementer? Okay, so we need to parse the numbers. So there are these little things like how is the number represented as a text? Is it binary? Is it decimal? Can it have a decimal point? Okay. Once we turn them into some binary representation then how do we specify what plus means? What other issues are there? Exactly. Add these 32-bit numbers and after that they throw an exception or overflow to back to zero or back to negative 2 to those 31 or something. Okay, so all this is important. So these are roughly these questions. Now what we are going to do is say we are not going to worry about this. We are going to implement this language in Python and we will use Python Arithmetic to implement our plus. So whenever we see our plus we will just call Python plus. Whenever we see our times we call Python times. Okay, let's see whether this works for us. Okay, and the same, right, for float. So there is nothing much to say here. We just made the decision to delegate Arithmetic to the Python interpreter. So now how do we represent how little it takes to scare two adults? So how do we represent the program? So we talked about the fact that these programs will be trees and so there is nothing much more we need to say except I'd like to make the distinction between the concrete syntax which is what you type in and abstract syntax which is what the program is represented with internally for the interpreter, right? And so the concrete syntax is 1 plus 2. The abstract syntax would be 3 plus 1, 2. This is the Python notation for what kind of data structure. It's a tuple. In this case it's a triple which has a string for plus. We represent operators as strings. It's not beautiful but doesn't really matter in this case. And then two numbers are the leaves, right? And then this is a somewhat bigger tree and so on. So the concrete turns into abstract and it's abstract because useless stuff that is needed in the text like parentheses are abstracted away. So this is so-called abstract syntax tree or ASD. This is essentially what we'll be working with for first few weeks. This should be all well known from 61A. Okay, so now we are going to build an interpreter. So here is one ASD. We built it by hand. We also have a parser that you can download. It will parse it for you from the concrete syntax to this ASD. And now we want to call an eval procedure that walks over this tree and prints the result back. So how do we write the eval? What sort of program structure will you use? We have all written such evaluators in 61A, I bet. Unless that was the week when the weather was really nice and you went to the beach instead. So what is the structure of the interpreter that we'll use? Okay, we need more people to this. Hopefully it's obvious. Well, let's try over there. Yes, please. So you do recursive evaluation. So you presumably walk the tree. Are you propagating the values in order, pre-order, post-order, bottom up, top down? How will that work? Which is bottom up, right? So we have a star with a plus and here we have three, four, which is this tree here and then five. We'll push the values bottom up, right? So here we'll push value five, here we'll push value four and three. What value will flow here? Seven and then, oops, the result would be 35. That's it. Not all evaluators work like that, but this is what this particle language needs. So what do we do here? You get the tree and so this E, okay, what do we do? What do you think we are checking for in here? Let me show you a few more cases. This is Python, which you may not have seen, but it should be self-readable, self-documented, self-evident, everything. This is what's good and bad about Python. So what does this do in the recursive scheme? Please. So this is the leaf of the recursion, or you could say the base case, right? And so this would be invoked, right? This line for three and four and five, this would be this line. So type one evaluates to int because the type is int, and if the type of E, so this is the E, is int, then we just return the number. If the number is a float number, we don't have one in this program, then it is also return. Okay? So what do you think this case means here? I see you, but I want others to participate as well. So what would be the type with this funny parenthesis inside? It's a tuple. So this is essentially says, if this is a tuple, this means we are at some internal node of the tree. Okay? And now you are breaking the tuple, you are looking at the first operator, which is here and see, oh, it's a plus. You recursively evaluate the left child or right child, then you take the result, do a plus, you return it. Same for all the other operators, including power. See, so we are turning our operator power here into Python operator double star, because that just happens to be the index for power. Okay? Is there anything funny here that you see any dangers? Okay, so if the input is an empty tuple, so essentially an empty tree, then yes, this will crash with a big fire, right? So there is no error handling here. We are assuming that these trees are nicely well formed, so an excellent point. There is absolutely no error handling and no interpreter should look like this, but I want to have it nice and small on one slide. Okay? It doesn't differentiate between unit and integer, because so far we are only assuming integers, so there are no units here so far. Okay? Please. And division by zero, okay, excellent. So this is another error handling that what will happen if I divide by zero? Is the program going to crash with an error message or is there going to be an error message? Who generates it? Yeah, the Python interpreter will be called here, right? It will perform Python this division with this value and the zero. This will evaluate to zero, right? And an error will be thrown by the underlying Python interpreter, which may not be the right thing. We may want to check for zeros ourselves and throw our own message. If you are writing an interpreter in some strange language, I don't know, Mesopotamian, then you may want to at least translate the Python-English message into that language. An illustration by your own error checking may be useful. But otherwise, this should be really straightforward unless you have some interest. Let's leave your comments for the more fancy stuff. Okay, so we are done with arithmetic expressions. We are going to do physical units. So far only the standard units like meters and kilograms, not feet and so on, all right? So we want to do now things like this, two meters squared which evaluates to four meters squared, okay? And what we did so far is absolutely trivial. We just said we now define units, okay? And we add u among the expressions. So now we can have things like one times m, okay? So did you notice a new, funny operator in our grammar? So what is, okay, what is the epsilon doing there? So the epsilon you may not know means an empty string. Do you know what construct are we enabling with this? Okay, things like thanks to this you can write one m is now legal in this language, okay? Yes. Or you're saying that what we want is a matching number of left and right parenthesis. Oh, I see. You're asking what does it mean when I write say 2 space 3, right? Is this 2 times 3 or is it 23 or is it 2 3, right? Okay, so that's a good question. I was hoping to avoid it for now since we'll talk about these issues later. But usually the programs are compiled in such way that first you have a lexer, okay, which is our lexical analyzer and what it will do it will first look at the input down into sort of chunks of text. So the chunk of text would be 42 space and 35. And the space would be omitted, actually. So 42, 35 would be actually the sequence of lexines or tokens that is passed down to the evaluator. So this lexical analyzer would be responsible for throwing out spaces and merging when spaces are irrelevant and dividing things where they should be divided. So here the space is important because it breaks 42 and 35 into two things. Now whether the parser which comes later and processes these tokens understands this to be a multiplication it's a different story, but in our language it should, right? Because if we have 42, 35 they can only be next to each other with this operator between them. And we presumably are defining this to be multiplication. The meaning of 1 times N is the need. So remember this here is concrete syntax which the parser turns into the more simple abstract syntax. So notice what happened here. Do we have a multiplication here? We don't, right? Yet the parser, so oh I see what you really meant is the epsilon operator and the parser decided to put the star at the top of the tree because it is really 3 times M squared. So notice this. There is really an epsilon here, okay? An empty string, please. Oh excellent, I like it. So 1 plus all right. Don't tell them I paid you $20 to ask the question. Okay, so this is a fantastic opportunity to ask the question. Clearly according to this grammar, 1 plus M is a legal program and so is 2 feet minus 3 kilograms. So the question is presumably we want to catch these errors and give the programmer an error message or a warning. Where will that error checking happen? In the parser as we go from the text into the tree or in the evaluation as we are walking the tree choice C, these two will have to collaborate together to make the error checking possible and D is well we can catch these errors in some cases but not in general. So who thinks it's A? Don't be shy, you'll have to raise your hand once during these four options. Okay, so A. Okay, and who thinks B? Okay, how about C? Okay and D? Okay, so there are quite a few people who thought either A or C or D. Well even B is, okay, try to now talk to your neighbor and convince him or her that you are right. Without violence, of course. So, okay, so it has quieted it down. Some people are not talking so I think I'll have to write a random number generator to poke at people. So let's take the vote again. So who thinks it's A? Okay, it looks like opinions have not changed that much. B? Okay, there are more B's now. Alright, how about C? A few are C's and D? Alright, so you have an example that cannot be called. This is good. I'd like to hear. B and D, okay. But there are some cases that you'll miss. Oh, I see, I see, I see. So you are really thinking about can we verify prior to running the program that there is no error? Okay. So that might be difficult depending on the language. In our language we could do it unless we don't know what value the programmer will input. So I see what you are saying. But I think we are working in the domain where we would be happy to just sketch those errors during the evaluation time rather than prior to seeing the data. So not compile time, but we are happy to catch them all at evaluation time. And these errors can be all caught at evaluation time easily. Now clearly you know how you would do it during the evaluation. You propagate the types, bottom up, right, together with values. And when there is some mismatch, you are subtracting kilograms from feet, you throw an error. That's exactly how it works. Now, why would it be difficult for the parser to do such a thing? Parser can clearly catch an error here because it will see, oh, this is a number, this is a unit. Clearly these two should not be added together. It's obvious, right? Right, so if we assume that the parser only has this local knowledge, it can see 1 plus m, then yes, this error can be caught. But as soon as you have something like, well, 1 plus m, all of a sudden the type is not local. It's hidden under the parentheses somewhere deeper. And now the parser would need to propagate the type. It can. But now it's all of a sudden doing the job of the evaluator, right? So it's not that parser cannot do it, but it can do it by playing the role of the evaluator and pushing the type bottom up. So I'd say that this is the right answer, okay? So the units are essentially types. We will be propagating now these types during the evaluation because we need to know at each operation what is the type of a particular value. So how do you think we will represent these types? What would you suggest? So how would we represent this AST, right? This AST when evaluated it is 1 meter squared, right? What data structure would you use to propagate the value? Okay, somebody else now. There are many choices, none of them did the right one. There may be some less optimal than others, but more or less anything we'll do. Maybe a tuple. So what would be stored in this tuple, okay? So for this case, what would be in the tuple? Okay, so that definitely will be a tuple, right? We'll have the value, the numeric value and the unit, the type. Okay, let's call it a unit from now on. How do we represent the unit? Now here is where we have more choices. Okay, somebody who didn't speak can make a guess or inform suggestion. Now you need to start predicting what operations we'll have to do on this unit. And now some operations will become better than some representations. Uh-huh. Two m's meaning m squared, okay? Uh-huh, all right. So what would work? Try to think abstractly. Are we mapping something to something or is it the set of somethings, the set of pairs? Okay, so what about things like meter per second, right? We could have values of that type. The result of some computation could be five meters per second. So we need to be able to represent this meter per second thing. So it would be a list of what? We would have a list on the nominator and a list for denominator. That could work. So I think that's right, because you cannot have a plus, so you are saying it doesn't make sense to have something that represents meter plus kilograms, right? Okay, so that's why just nominator and denominator. Okay, I think that's very good. That could work. A simplification of this exponent, exactly. So now we are getting closer. It would be a set of pairs. You need an exponent, all right? Uh-huh. And so, but now we need to understand the semantics a little bit better. If you're saying, well, if it's a set, then I can represent something like m, let's put it as a string, two m1, right? That's a set of pairs. But you would like to say that this unit is illegal. You would like to represent this differently, right? You would like to represent it as m to the third. You know what I'm saying? That what you are saying is correct, but if we allow any set of such pairs, then I could create such a set. And it's probably not what you want. Gray shirt. Okay. So it would be map from, you could say si type to exponent, right? So this would now become essentially m mapped 2, 3. So if I write a program which says m squared times m, it will be normalized into m cubed. It's sort of essentially what you said, but cannot nickelize to the sort of normal form, all right? So this is indeed a map. How do you represent a map in something like Python? It could be an array or a hash array, exactly, right? This is what we'll do. So my pen misbehaved just a second. So here this is essentially a map m map to second. So here is our tuple. This should be an arrow, okay? 3m to the 2. So with that, I think that we actually got it quite nice. And so here is the program. Very little change is needed. Now, this eval still takes an AST, so the type of this is AST. And it will return a pair of, say, a number and this unit. And I'll see how we evaluate. When we do subtraction, a unit and another number and a unit, and it will check are the units compatible, are they the same? We cannot subtract meters squared from meters. So here the normalization to the same representation is actually the right thing, okay? Oh, okay. So let's hold that salt for a minute because so far we are only doing SI units, so milliseconds are not allowed, but we'll do it in a second, right? Here the fact that they are normal allows us to do a really simple test of compatibility, and if they are normal you just keep the same unit and do the normal subtraction. Multiplication does the subtraction as a multiplication of numbers and you need to write multiply units, right? What does multiply unit do, you think? So this will take, say, m squared times kilogram squared. It will just merge these two hash sets together, right? If it is m squared times m to the third, it will result in m5, right? This should be relatively straightforward. You can actually click on this and see the code. It takes you to the version of the interpreter which has just exactly the sub-language. Okay, so now we add known SI units. So things like milliseconds, okay? So now we want to add feet, here, and in fact milliseconds here, okay? Uh-huh, okay. So let's go here. So this would be the right place to So look at this expression here. This one here is three times meter squared, right? And I'm going to evaluate it into a Python data structure that is a tuple of number three, and now this unit here, and the unit does what? It is a hash table that is sort of the key. So this is the key. Okay, the key is the string m, and the value is two. So this is, sorry, I should have explained this, the Python syntax for a associative array which maps m the key to two. And of course it could have other exponents. It could have kilograms in it and so on. Good question, please. Okay, so if you, uh, well, so what happens if you do meter divided by meter? The result needs to be one comma and the description of a unit that is actually no unit. So how would you represent that? Okay, as an empty, right, in fact it would be a good question. So m divided by m would evaluate into one and an empty set, which means you essentially have no units associated with it. Which could be the same as m with exponent zero, but at least in my implementation I believe such would be flushed from the hash set, just so that it's not printed as m to the zero at the end. Okay, so like Joe, for Joe, the unit of energy will get there too. Okay? So, non-SI units, so we want to do feet and milliseconds and clearly the foot will evaluate to whatever 0.3 meter because we want to normalize it to that and how do we do it? It's very simple. The leaves of the AST when we evaluate this feed, we evaluate it into 0.3 whatever of the SI unit. If we cannot do it automatically, we need to code it into the interpreter and it needs to come from somewhere. So you effectively take these non-SI values and we turn them into these SI representations. Well, how does it happen? Well, look at the interpreter. If it is one, you just return one with no unit. If it's float again, one with no unit. And now it becomes interesting if this E is a unit, any unit for that matter. So we have encountered a string in the AST. We go into this lookup function and what's in this lookup function? Can you see what we are doing? The lookup function takes this string, the representation of the unit, uses it as an index into what? Into this hash array. And if it finds feet it returns this value which is 0.3 meter to the first. And you can see how you would do a millisecond here, right? You would here have millisecond map to 1,000 of a second. So all these units you need to of course insert here yourself because where else would the system know it? Well, so you are essentially saying rather than normalizing everything into SI, let's keep it in feet and propagate sort of feet units up to multi only normal, okay. So that's actually that's an excellent comment. So what we are doing right at the leaves of the evaluation we are turning feet into meters and other such things and then we do the evaluation in meters and maybe somebody actually wants to print the result in feet. So we will do one conversion from feet to meters here and then on the result we do conversion from meters to feet back. We could do it. It would mean more complicated evaluator because it needs to work with non-SI units and you could do it. I just wanted to have something simple. If the arithmetic is with infinite precision then we are clearly not losing anything, okay. So if you can rely on Python using infinite arithmetic then these two equivalent and my solution is probably simpler because inside these operators we are only dealing with SI units and the conversion to SI at the leaves so all feet goes to meter is fantastic because I no longer need to check when do I do say addition or is feet actually a unit of length or not because it would be converted to meters and because it has the same unit as the other operator on the other side of the plus I know they are compatible and I can do the plus. Otherwise I would need to have another table somewhere which will tell me feet are actually meters and therefore they are together, okay. So you can see here how we do the evaluation. For example 3 has no unit the plus will have this unit the result has that unit after multiplication we have a unit that is 1 plus 1 gets into 2. Okay. Now the more fun stuff, okay. So in the interpreter that we have written so far if we write 1 meter divided by 1 year the result will be 0 meters per second. That's clearly not what you want. Anybody can venture a guess why this happened? We would like to have a more accurate answer, right? 0.00 something. So why did it happen? Right, because we put an integer on the input and the interpreter just delegates it into the integer division in Python and integer division in Python produces an integer so it correctly rounded it down to the closest integer. So how would we fix it? Well we need to have a little bit smarter division, okay. And what we do when we see something like int divided by int we'd like to keep it as int whenever possible whenever loss no loss of accuracy would happen. If producing int would mean that we are losing some accuracy we need to turn it to a float. So now we see how you may want to build your own arithmetic operators on top of those provided in the underlying implementation language. So again we keep it as an int whenever this would mean no loss of precision. Okay, so again there is the code there. Now this is becoming more fun because we are going to handle this operator here. So the first question is how do we extend the grammar with this intc where c is some unit. Okay, so this is our first attempt. I just made intc part of the expression. Is that a good idea or a bad idea? Well c would be whatever can appear I didn't specify what c is but let's imagine that c are any units that can appear after int so you can say int feet, int feet where in meter divided by seconds. Let's keep that open for now. So without knowing precisely what we allow for c is this a good idea what I have done. Try to think where e in c can appear in the resulting program. Please. Exactly, so this is exactly the problem with this is that the way I have written it now you can write this expression which has int feet inside parentheses. What does it mean? We are doing a conversion which doesn't make any sense. The int presumably only wants to convert how you print it. So the second attempt is right. It will be a top level operator and it will decide how the value is printed. So now we have the program is either an expression without int or expression with int and then the rest as before. Okay. Does it make sense for c to be this? Yeah, no, somebody doesn't like it. Okay. Please. So it is true that we cannot subtract meters and seconds but we already made a choice that the parser cannot catch this. So we could in principle allow it here. The question is whether adding, whether allowing plus and minus in c gives us anything, any power. Would you ever want to say print something in meters minus feet? Probably doesn't make much sense, right? So the question is do you want to say two feet in meters minus millimeters? So this is nonsense, right? So this c would of course allow us to do it. So we don't want c to be the same and arbitrary combination of units. So what do we want to have in c? Which operators make sense? Right. So in terms of which units we want to allow, it would be the whole dictionary, right, of si units and non-si units, so that's good. How about operators? Looks like we don't want minus or plus because they are meaningless when you are defining the format in which things should be printed. How about division and times? Makes sense, right? You want to print something in meters per feet, sorry, meters per hour rather than meters per second. So the right value seems to be c would be multiplication. Also this little epsilon here division and turns out that we want to have a power as well, right? It took me actually a while to get this right. But this seems to be what we want for c. I think you could write. So this needs to be okay, sorry. I knew there was a bug somewhere here, okay. I think this needs to be all c and I think this is okay and then a u for base case. Okay, that's right. Seems to be correct, maybe not entirely correct. So meter in parentheses squared, yeah, you could allow it. I think at that point I went to Google calculator and see whether they do it and I said no, let's keep the exponent simple.  Question? And non-SI, absolutely. Yeah. And now I don't know what would happen if you print something in meter per feet. It could be that that we want to complain. Oh, you would need a different symbol for pound as a force and pound as mass. No, I think you would need to have a different sort of text symbol for the two. So before we close, how do we actually evaluate c? Now we have an expression that has e in c, so it's a tree that has in on top. It has expression here on the left that we want to evaluate. Right, so we have now in and now we have expression here and we have a c here. We know how to evaluate e. And ideally at this point I don't want to make any changes to that part of the interpreter, because I debug it and I don't want to touch it again. I would like to evaluate c somehow and then write the case statement in my Python interpreter for in. So what should c evaluate to? So that's essentially right. It will be a pair of value and a unit, but both of them are different than before. So syntactic it is again a number and a unit except this unit here is not converted to si. It will stay in the original format. So if I say feet times meter times feet it will stay in feet squared times meter, because that's how I want to print it. So the evaluation is different. Not the normalization, but you just collect them all and collect their exponents. You collect it too. The first number again is not a value but it's sort of conversion between the si value and this funny value, feet squared times meter. So this is the value used at the end to scale up the result we got from the evaluation of e. You need to know it's still there. Yeah. I circled it so it could be there. It would be handled just the same way. So I have a few more extensions to the language, but at this point I want to stop and ask you to fill out a little questionnaire about the courses you are taking and a little bit about background. There are some simple questions that should take you just maybe three minutes to answer, I hope.