Cairo Programming Language I
Web3Bridge·Wed, Oct 9, 2024, 12:00 AM
Transcript
all right so um yeah my name isim popularly known as white Mr I'm going to be the instructor for today's class yeah so in today's class we are going to be talking about um we are not talking actually we are we are going to be learning about the Cairo um programming language so for today's session we going to be going through the the first part of it so yeah we um in order for us to so there are things that we actually need to understand as regards skyrie so that it's going to like be a fundamental for um what we are doing so you know this particular class now is called the KY Master Class um and before you can have a master class in any session it me that you have to be first good at you know the basis so we all know that the reason we are learning K is because we want to be able to build on stack net and uh we all know that the reason we have stack net is because there's a fundamental problem with distributed systems so um when you have uh a blockchain so we have what call the blockchain trima which is that a blockchain cannot be decentralized um have security and also scaling so which means that any blockchain that you look at they're going to sacrifice to um to get the third one they would have to sacrifice two in order for them to get uh to get the third one so uh for ethereum as a lay so that's reason we have what we call the layer one blockchains and then the layer two blockchains so the layer two blockchains are basically um solving the scalability problem so I don't want to like dive too much into you know all of those talks I believe the previous class has explained it better so just a little bit of reminder uh to serve as an introduction to uh what we are going to be doing today like why why do we need to learn K since we already know solidity why do we why do we still have to learn um Kyu sorry I want to add so why do we have to learn CYO so that's the basic question now so if if you look at ethereum as a layer one blockchain ethereum is decentralized right yes or yes please this this is a chart like you can turn on your mic so that we can be listening to ourselves yeah so it's not something you know very very very serious like that so ethereum is a layer on blockchain so it is decentralized yes or yes [Music] yes so what do you mean by the word decentralization so decentralization simply means that two things either calized or centralized somethinge um if you know that you're in a noisy environment you can just be you know muting your mic when you want to speak right so you can I'm trying to um join [Music] the okay [Music] all right so so we have what we call you know you can have something that is Central or it is decentralized so Central it means that it is controlled by one party so when something is controlled by one party it means that it is a central so for example hear the word the Central Bank of Nigeria right so Central Bank of Nigeria is a one party that controls all the financial systems that is happening in Nigeria so all of us we have no information about it but you say that something is decentralized it means that you have a lot of things a lot of authority you know like a all the entities there are authorities in the in themselves so and when you have a decentralized Authority like that then there has to be a consensus mechanism there has to be a way for them to agree to a valid state so and in order for them to you know do all of those things there are some traders that comes into play so we all agree that ethereum is what is decentralized the second is that SEC the the second is security so ethereum is also secure in the sense that you know and what ethereum uses as its security mechanism is basically uh you know we have the we have the crypto economics so which is that when you want to when you are uh when if you want to like AR ethereum then you have uh some you want to act as a dishonest party so you have proof of which of your like last money so because of that you don't want to you don't want to try that so but then scalability so is ethereum is it scale so the short answer is no because the um for ethereum the number of transactions that you can have per seconds is between 15 to 30 transactions per per second which is uh which is very very low so in order for you to to solve this problem and for Layer Two Solutions there are B two types of layer two solutions so we have the optimistic optimistic roll up and what what we call the validity R up so when you say that something is optimistic you know in English optimistic is that it just trust me trust that let's just be optimistic that these transactions are what are valid and the way it also works is that if the transactions there is there context if a particular transaction is what is not valid so which means that so far as nobody contest that a particular trans there is invalid for the waiting room then the transaction is what is valid so that's the optimistic you are optimistic that oh the transactions there are what valid so the validity roll up that one is also called what the ZK up which is the zero knowledge which means that when you are bundling a transaction you are also sending its proof the proof that this transaction is what is valid you are sending it along so and you know there are many ways in which you can have the ZK so we have the ZK prunes that talks about you know all of the different um ZK systems that we have so talking about the uh talking about the circuit the R1 CS the rank one uh the you know we have the rank one constraint system the quadratic arithmetic program and then we now have uh another one that um you can now also use to what to prove that a particular transaction is what is value so the ZK we now have what we call the what the snacks and the stack so when you have stack and snack you know what snacks means snacks means something non interactive argument of knowledge why stack mean scalable transparent uh argument of argument of knowledge So when you say when you have this now for stack you want to if want to prove you know something here so you have to first convert those statements you have to convert what want to prove into arithmetics so you have to convert it into arithmetics that you can easily um you can easily prove so the process of converting whatever whatever you want to prove to arithmetic is what we call what the arithmetization so we have the arithmetization process uh you know and there are different ization processes so one of the aration process is that um the the the r constraint system is an arithmetization process so you are converting everything into way um into you know ran is that you can have X Plus y = z or x * y = z you can prove the two and you also have the quadratic arithmetic program so we also now have the third one which is what we call what so uh in this for Cairo for stet what is now used we now have something we call what the uh so we we have what we call what the Kyu so what is the meaning of cyu so Kyo simply means Kyo simply means CPU uhra intermediate representation so CPU algebraic intermediate representation that's the uh simple meaning of CYO sorry so that's the simple meaning of of CPU algebraic intermediate representation so what does what does this mean now you know that when you are uh when you are performing a program when you have a computer right now a computer is simply we have a general a computer is just you know something that makes our work easier it takes in input and gives out gives us output now the CPU of your computer you know normally before you used to have uh you used to have CPU or chips let us call them chips that are designed for a specific purpose so if you want you can design a chip that is going to you know just take two numbers and produce another one so but then we had aling that uh gave the a model of a general purpose computation uh which we call the what DET touring completeness so with that you can have a single no no no no that's the V the V architecture so the fundament architecture is the one that proposed a system where you can have a general purpose Computing so general computing isans that you can you be able to like compute uh you know program anything on the on the CPU so we have CPU model so this particular one also is going to be one of uh one of CPU models that you can make use of so what does this CPU model do so it's simply a way of converting if you have uh you know uh if you have [Music] sorry yeah so if you have any computation so if you have any computation it's going to uh you are going to be able to use this particular architecture to be able to process it you'll be able to use uh this particular uh what is it called to be able to process it so now this Cyro the Kyro that we are now talking about we need to understand that skyro can mean two things skyro can mean two things so Cyro itself so there is the CYO that deals from the low level to the you know execution of ucation of Po systems so and that one it's simply that when you have a when you write you know algebraic uh representation you're going to write whatever you want to prove it allows you to be able to like gener proof a computation you are going to generate proof of computation and you're going to verify it in is computer that has a smaller uh that has a smaller power so when you have uh CYO in that case so when you write let's say you can write uh you can write your algebric representations you can write your algebric representations and that skyu is going to help you to prove it so that's like one aspect of K that we need to firstly understand so that's the reason so that's like the first part of CYO then we now have another part of Cairo so the other part of CYO is the chiro programming language itself so the chiro programming language now allows you to write uh you know it now allows it compiles an high level programming language into uh the CYO assembly so if you look at the way the the evolution of Stack net and the CYO there was a time that we had the CYO zero so in Kyo Zer it was a python that you can simply use to write uh to write uh to write all those intermediate your program and it's going to be converted to what to k then from KIRO Z it moved toward to Kyo one so in KIRO one we now have this one it was Python and this one it is rust like uh programming language so which means that there is this is like a layer of abstraction which gives us we we don't want to think about X+ yals to 3 X plus we don't want to think about all of this low level stuff so we have the car prog language that is going to allow us to write a smart contract in a syntax of a friendly programming language like Ross and he going to compile it down so when it was in Kyo Z when it was in Kyo Z when you write your kyoo program is going to convert to what to km but for CYO one when you write a Cairo program write a Kiran program it's going to convert it to what we call what Sierra it's going to convert to Sierra what does sierra mean sierra mean save intermediate presentation save intermediate representation now this Sierra that you have will now be converted to what to K now if we are going to ask why do we have Sierra so the purpose of Sierra is simply that Sierra it wants to make sure that safe there means that it is not possible for for you to have a Ciro program that is going to panic you know if you're writing a Ros program or even JavaScript program there there are possibilities of you know for you to have different kinds of error that it can result into Panic that your program can just return error but with this it means that when you write a CYO program in a in a program in a programming language that is you know human friendly like the K when you get a k program now it is not you have to convert it first to Sierra so when you convert it to Sierra if you successfully convert it to Sierra it means that it is not possible for that program to what to fail it's is not possible for that program to fail so that's which means that for any input there is going to be an output for any input there is going to be an output then the SRA is now converted to what to kazm which is the Cairo Assembly Language so since the Sierra is safe then it means that the Assembly Language that is generated from the Sierra is also safe so that is just about what the compilation so that that particular PA of it is just about the compilation now you know at the other path of um what's it called at the other path of the K also so for that part of it it is not possible for uh for your Sierra Cod it cannot Panic or anything so when you now convert from Sierra to kazim then at at the kazim kazm is simply an assembly language which which then performs whatever it is that you want to do then we now have uh then we now have the other one you know when you already like compared it to you already have the km there so if you if you are going to be running it then it whatever compation that you are performing on it is going to generate a proof for it so it is also it is now possible you know if you have deployed a contract then it is possible for you know people to run uh they might want to like run a program and is not going to it is possible for it to fail them but when you compile your program from the high level language to cing it is not possible for it to watch it is not possible for it to F so that's the main purpose of of kyou so you know it is uh for normal CPU architecture the main the major problem that people usually look into is the Tim problem and the Tim problem you know that's also one one of the things that resed into what gas Ming of um of solidity and layer one so was for uh but for kyou it is not only ult problem that you looking at you're also looking what Panic so a Ciro program it cannot panic when you've converted it into what into Sierra so which means that when you are writing your code if there is any possibility of it to panic it is not going to compile so you need to First understand that it is not what it is not going to compile into into Sierra and yes uh and the programming language also that we are also making use of it it is also optimized in such a way that the proof system the proofs that are generated from it it has already been um optimized so using in so those ones are the low level aspect of of the KY that we are not so interested about at this point so what we are going to be doing now is we are going to be looking at okay how do we write a chiro program how do you write a chiro program so the number one thing that we need to understand is that chiro programs their syntax is rust inspired I mean by kyuan Kyan programs their syntax is rust inspired so it doesn't mean that you can do everything that you do in Ros that you can do it in in Kyo that doesn't that that's not the case because for every all the things that you are going to be making use of in Cairo they are already associated with some ins they they are compiled to you know Sierra so you need to understand that you know when you write rust it is possible for you to convert your rust compare your rust down to maybe bite code or lvm or different kind of different kinds of targets so but for Cairo your target is what is Sierra and you can only make use of what has been predefined so we are going to be going through the CYO book to uh to learn how to you know how uh how to do how to write a CYO program so of course you want to I believe everyone of us here we've already installed the we already have KY on our system right is there anyone that hasn't installed uh CYO okay so since all of us we have installed K on our system and we also have the you know it's just like rust in Rust you have Ros as the programming language then you have the package manager which is the crates so in Cairo you have Kyo as the programming language and you also have uh the package manager which is what which is the scab so we are going to be looking at how to write uh so let let's first just check out how how do we even um how do we write How does writing CYO programming language how does it how does it look like what does how does CYO look like how does km look like and all of those all of those thoughts okay okay so I actually need to set the what's it called up [Music] all right so before we go like while I'm trying to set set it up does anyone have question maybe what I'm doing it's not clear yet does anyone have any question as Kyu we all understand KY now why we are writing Kyu right that's I have a question can you hear me yes I can hear you okay so you were talking about um how it's like when we writing Kyo code it comes from the KY um high level language then down to Sierra then cin so like from that sah to cousin so you explain something that I didn't actually catch so if you can just you know rephrase like why do we need to go from why not just go down directly to cousin all right so in in the Kyo zero in the Kyo zero version you know when you're writing in uh in Python you can write your CYO program and it goes directly into into Kim right but for Kyo one so I said that the meaning of is save intermediate representation right so the word save there it means that it's uh it makes sure that whatever program what you have written in that in that high level C program language it cannot Panic do you understand the meaning of panic so Panic is anything that can make your program to stop at the m in the midle do you get anything that can make your program to stop in the middle so if you write systems programming language there are some things that you just be running your program and it's going to reach a particular whatever kind of error it reach some kind of error that you just panic and it you stop it's just like maybe you writing JavaScript and there is a runtime error there's a onetime error and let me say compile time error and it stops uh so something like that so converting to Sierra it means that it is safe so from Sierra to kazim now means that whatever that particular KIRO code it is it simply me that pic that's that's just that's just um the simple meaning cannot Panic okay so I'm going to uh refer to solid because that's what I know right so why didn't we need um SRA for solidity for solidity you uh what you so there is difference between so there's difference between the way solidity and CYO right in solidity you are running solidity on evm which is ethereum virtual man and evm it's just it's um it's just the same normal um CPU model so the normal CPU model is you know um you read write and all of those stuff like it's RIS from memory right to memory and all of those stuff but for Cairo it is it is different so for CYO CYO is proof of computation carer is proof of competition in the sense that what you do hello yeah he's having issues e I hope everyone can hear me H we not hearing [Music] you okay so [Music] okay how about now yes sir we can hear you all right thank you so I'm saying that there's difference between solidity and Cairo the difference is that we use solidity solidity is used on um on evm right so which means that solidity is just like the normal programming language is it's it's similar to the normal programming languages and it runs on the normal CPU so a normal CPU it has it can read from memory it can write to memory right so that's how the noral work so which means that solidity you just convert it into what into B bite code when you write your program in solidity it is compiled into bite code and that bite code is run on the evm right now there is now uh CYO Cairo is for stet or CYO is used for ZK proof now when you are making use of ZK proof so you make use of ZK proof or zero knowledge proof for E to achieve two things either to achieve privacy or to achieve what scalability so when you make use of uh for scalability it just allows you to perform computation on a big system let's say that you have two system you have a computer and you have a phone now you want to perform a computation on your on your system you cannot perform that computation on the on your phone because you know maybe your phone has two 2 MB 2 MB RAM but your system has one tbte ram so which means that you can perform that computation on your system however you cannot perform it on your phone so you want to be able to perform this computation on your system then prove that you correctly performed it on your system on your phone so proving is different from what so for you to be able to generate that proof you will need you know all those ZK protocols that's where that Cyro comes in and the way CYO CPU model works this is different from the way the normal CPU model works so for the normal CPU model it is read right you can read in the memory and you can also write into memory however for Cairo it is a readon model which means that you don't write is a read only model which means that you don't write and also it is proof of computation so which means that you know when you run a uh when you run a Ciro program the way it process it is different from the way normal CPU process process it so that's the reason uh for Cairo you have to convert to Sierra first then convert it to what into CM but for solidity from you just convert it to bite code and it's going to run normally on the you know because whatever it is that you are doing on with the Cairo the Cairo after it has run the program is going to generate a proof that the program run correctly and the output so that output and the proof is now going to be sent into into ethereum as the settlement layer so on ethereum there is a contract there's a smart contract there that is going to Tak in the output and the and the proof and it's going to verify the proof of computation and it's going to register uh the validity of that of that proof so there is a uh there is fundamental difference in the architecture of the way the two of them the way they work so for Cairo you have to convert to Sarah first then to uh then to kazim for solidity you go straight from uh the programming language to uh from the programming language straight to straight to bite good yeah that makes sense that makes a whole lot of sense thank you you're welcome any other [Music] question yes I have a question can I hear me yes you you mentioned that for solidity the the evm uses redri but for Cario it uses read only so how are um let's say you send a transaction now or you are performing a computational task how do you how are the information saved are they not saved or they after processing they discarded all right thank you very much so the way it works in Cairo is that there are some there is something we call in there's something you call what in so the way CYO works is that because it uses read only it doesn't write so he going to um in is that in some memory it's going to assign before the chiro program before it starts running it's going to write some values into those into those memory so what we mean by read only it means that when the program starts running it doesn't change it doesn't mutate at memory address so it's going to provide in in different places so it will just uh read and assert that okay uh what is in this memory uh is what is is what it uh maybe what is in this memory if you add it what is in this memory you are going to get what is in this memory so there are like two things that actually occurs in a in a casm so it's either you are asserting so that asserting is like the write so instead of writing into a particular memory he going to use in to have written into the memory already before the program starts already it would have and the in would have you know put the uh it should have put some values in some of in some of those memory so it just Asser that okay what is in this memory is this so that one is now equivalent to the right now and you know the next one is going to be it can transition from one memory address to another memory address so I I hope that answers the question yeah it does but but it's still little little bit confusing maybe to do more research on the okay of course uh you might need to do more research uh for that so if you want to so to learn the Cario programming language of course the best place is to use the car book so the best place is to use the CYO book and I hope everyone can see my screen I have a question yes yes what's the question so if my program needs to be compiled and then comes back to to read so does it mean the bigger the project the more time it can take to compute or that is more edible or like I cannot can you hear me now yes I can hear you now okay so I'm saying if the program needs to be compiled down to um I'm like the more the I can hear you now oh okay so I'm like the more the bigger the program the more time it will take to to be computed or that is not an issue is just the reading the reading so this is it you know when when we talked about the uh the zero knowledge or and and how it solve the skillability issue so it's just like when you have your you have your computer you have your computer that is very very big you have a computer that is very very big and you have a phone that is very small now you can perform some computation on on your computer because it has very big memory and very big processing power so you don't really care about you know about uh the size of the competition or anything so for the but is not going to generate a proof now that proof is going to be suent which means it should be small and you can easily verify it on your phone even though your phone has a very small um comp computational power now how do we relate this with um with blockchain so in layer one and Layer Two on ethereum H on ethereum if you want to perform a transaction you know how high the gas fee is the gas fee on ethereum is very high so which means that you know there is there are lots of optimizations and you know different things that you have to do in order for you you want to so that swap $50 you don't have to pay maybe $70 in order to swap $50 so however on stocket Layer Two uh blockchain the layer two blockchain now number one the gas fee is very what St is that you know it's going to take a lot of trans it will bundle them and for all of them it will now generate you know the computational power of that one is going to be very very big however we don't care about like we don't really really care about the computational power of that one because that's like our system our system has enough computational power so the one that we care more about is our phone that the computational power is small so on on stocket it is basically like it is more like you don't really pay for for for gas or anything on stack net you can perform whatever computation on uh on stack net so it is not going to take a whole lot of computations bondle them together and generate a s proof of computation for them now that sun proof of computation will now be verified on on ethereum so it is so the cash Fe that you are just paying there is just like you know uh after it has bundled everything together and this so the gas Fe is now relatively very small uh for the user it's now relatively relatively very small if you look at now currently the uh swap C or the for performing transactions now is now below a cent on uh on stack net so which means that gas fee is no longer a what a problem or it is solving the scalability isue you can perform lots of loads of transactions is going to handle all of them bundle them and what have the proof of computation on the uh I send the proof of computation to to etherum which is the base layer I hope that answered the question if it doesn't we I'm going to [Music] explain all right thank you very much so getting started installation so if you want to run uh if you want to run this you should have R installed scab and you should have installed scab so if you should run this on your terminal to know if so if you should run this then when it shows you know the SC version 2.6.4 the Kyo version is 2.6.3 and the SI version is 1.
5.0 so that's going to show you that yes you are on the you're on the right track so let's write aello word so to write L Word in uh in KY is actually very simple so just like you know if you want to write the rust in Rust if you want to write uh a program in Rust you can simply make use of the package manager you can use cargo new but in this case we are using what scab so scab is our package manager so we going to say scab new hello world right so with this now I can now let us look at let's try to understand what is happening here I hope everyone is everyone is with me is there anyone that has written rest before here how many people have written rest before okay nobody it's not a problem so if if you should look at this place now so the starting point is we have this scar. TL so which is like the the uh the configuration the package configuration so you can see in this configuration the name of the project is L world the version is 0.1.0 and the Edition is this so if we are going to have some dependencies in our in our project we are going to have them here and G IGN simply uh ignoring so this are what we don't want to push into our GitHub so in this place now we have the SRC which is like the sour the starting point of everything and inside it we have the Leb sko so the li.
skyro is our starting point the lip. skyro is our starting point so if you look at this one now in this case we have the function the main function is here and in this main function it is calling another function which is this Fib so it's generating the Fibonacci of six and so this is just so but that's not what we want to do now what we want to do now is want to write a low word so how do we write hello word in stet so in this place so it's tell us that we have the TL meaning th minimal language so that okay we have the last line is the dependency the other file created by scrap is the SLC leave. sky so let's delete all the content and putting the following so we have the mode hello hello word and so if you should put this mode hello word in this place then we would have to we have to create another file here and call it world inside this samec so it's going to be a l sky so then in this place we can have function of course the entry point of any programming language is what uh the entry point of this particular programming language don't let me say any is the main so we have function Main and we have print line hello world now I'm going to I'm going to explain something here so we someone's trying to say something who that TR to zoom in in your ID it's very tiny oh okay okay okay how is it now very very thank you okay so here we have function main print line so this print line there you know in in Rust we call them what macros so macros are like shortcuts or short hands for for some stores so the fold structure that we have now we have li. Kyu l.yu now there's something that I want us to understand here yes the normal thing is that we can write our the starting point the starting point our program is this le.
skyu is this le. Sky which means that inside this leave. Sky we can you know write any program that we like in this place this place but if you want to use another thing if you want to use another part P You' have to like make a reference to them so we call it what here now this is a mod now if you have this uh Li if you have this li. this lell word here now you can see that's what we are it uses the name of this directory as the modu name so this one that is a directory you can also make it a um a folder so when you make it a folder and it is hello hello word then inside the folder also you need to have the. Kyro inside so if you want to build now we have two things building and what and running in Ros also in in the Ros programming language you can decide to build so when you are building is like you are compiling from one particular language to another you are compiling from one particular language to another so in this case which is for Cairo if you building it means that you are converting your CYO programming language into what into Sierra so Sierra into it's going to it's going to build it into Sierra what so in that case if you should say scab build and we put the the name of the uh so if you say scab build is going to compile what we have so if you should look at now we now have this target now in the Target we have death and we have uh we have the the Sierra code there so this is the Sierra this is not formatting it so this is the Sierra of what uh the Sierra version of what we've written here this hello word that we have here so that's the Sierra version of it and we can also run it so if you are going to run the program so we say what scab CYO run so when you run it it prints what we have here which is the lell word so we have it in this place so we can say this is my first Kyu program yeah so you can see this is my first C program all right so the anatomy of a Kyo program so let's review this hello word now we have this FN main so this FN simply it stands for what function and main main is also the entry point of any function so it say that this lines Define a function named main the main function is special it is always the first code that runs in every executable chyo program here the first line declares a function named main there has no param and returns nothing so if there were parameters be they going to go into the parenthesis so which means that let's say that this function we have a function that is going to take in some parameters that is going to take in that has parameters or that is going to take in some arguments so inside you can have uh let's say another function and let's say this is going to take uh name and the name is fil so as they writing the name they are putting in the what's it called the uh the type so we also don't want it to return anything so we can decide we want maybe we want it to return something or we don't want it to return anything if you want it to return something then you need to indicate the arguments of what you want to return if you don't want it to return anything it's the same thing as it is returning void so which means you can have something like this and in this case now we say print line and so which means that when you have a particular name when you passing a name as an argument then it's going to return that name here so we can call that function in this place so let's say I can put in my name here and say right so if I should run it what's that it says what oh oh oh okay uh the the data type yes okay yes so the the reason actually is used when you making use of felt you don't use double quotations use the S is it supposed to be 25 252 there's no 256 252 252 sorry yeah so this is my first programming now I'm going to return this let me use debuger oh so this actually what it's return returned there is you know felt uh no I think is because here we [Music] have so F has to [Music] be due to previous error must be string l [Music] let just you me b [Music] oh okay so this what we have here is the same as this um this wise Mr so the the reason it is showing this way is because it has if you want to get this it has to be in the BG mode now you remember that I said that there there is the arithmetization process so arithmetization process it means that everything that you are seeing is converted into everything that you are seeing is converted into numbers so the meaning of the uh the basic data type in Cairo is what is fed 252 and what does f 252 mean it means this F it means field element so the F the field element because you know the Cairo it has it has the prime number that it is making use of under the so the strings and everything everything has converted into number so this one is still in the uh in the in the normal mode that's the reason we have we have that so is that second PR call a macro I think I have talked about this so if it called a function instead it would be entered as Sprint line without this so that's telling us difference between a function and a macro so we have a qu should we should we do this so we have what is the name of the initial function that Kyo program runs so what is the name of the function like is it not master yes me mean I think it's Master how many people choose me and how many people are with me master if you if you with Master raise your hand okay I have AEL with me ah no nobody is going for Master Okay so if you are for me let me see okay it is all you submit number two which command will run the code in your project so which command run the code in your project is it it is SC bu right scab Kyo run ah it's a scar bu that we use first now what's the difference between scar build and Scar [Music] kyun SC only converted to syri cousin SC will build and also run it [Music] comp okay so let's submit ah so this one is not master wow okay so let's move to the next common programming Concepts so in this in this case we are going to is going to cover common programming Concepts specific you're going to learn about variables basic types functions comments and control flows so these foundations be be in every C program and learning them early will give you a strong call so variables and and mutability what what's the mean of a variable and what's the mean of mutability so I said that something varies it means it changes right and of mutability mutability is just the ability of something to change right so say that K us an immutable memory model I think I have mentioned that so when you hear the word immutable memory model immutable memory model simply means that what the memory does not change when it starts running a memory it does not change do we understand it's like we are sleeping yes we do all right so meaning that once a memory cell is written to it can't be over written but only read from to reflect this IM immutable memory model variables in Cairo are immutable by default however the language abstracts this model and gives you the option to make you to make your variables mutable let's explore how and why car reinforces immutability and how you can make your variables mutable so when a variable is mutable on a value is bound to a name we can't change that yes to illustrate this generate a new project called variables in your C project a pro directory using SC new variable so in our project we are going to CD out of this and create another one we say what scab new variables then so now then your new variable so we have this in the SLC li.
skyu So when you say that let xals 5 you can print line that the value of x is this xal 6 the value of x is what is this let's let's check something so in the SLC here we going to delete everything here so uh we say let x equals 5 so here we are assigning that X our we are binding our X to five then to prove that we can just print [Music] line so the value of x is five now let us now say that let us now make X we are changing our X to six and let's Che let's check our value and say that the value of x is is this again now we are going to we are going to run that we are going to build that so let's let's look at something if you try to to build it scab build so you can see that we have an error now what's the error it says cannot assign to an immutable value cannot assign to what an immutable value what does immutable mean it means it cannot be Chang so since this one is is immutable so we said that variables are immutable by default the first thing is they immutable so which me that if you want to make it to be mutable then you have to be explicit and how do you make it IM how do you make it mutable we add the keyword mute in front of it so adding this mute keyword in front of it is going to make it what mutable so let's try that again okay so in this case now on next okay sorry there's a mistake here we are returning void not so we should build it now so let's run it what's what's happening okay so SC SC uh kyun I'm not supposed to add this scarp carun so we can see that in this case the first time the value of x is five and then X is now six which means that it has changed it do we understand that yes yes I have a question all right please go on importance of the importance of what print line that print Line Print line yes yeah it's the one that is giving us this output here okay okay it's like STD STD out um standard output you know this is the macro inside of it you have the standard output so it's because of this that we are able to see this in our time out okay all right yes when you're when you're printing it you need to pass parameters you can just pass one in my when you're printing when you're printing you need to like first start the value and like the statement and the and the value or you can just pass one okay okay so do you mean how is he able to get this x into this place right I'm like is itary yes to pass it here yes two variables or I can just pass in one like I can just pass X or do I need to have something that describes the value of x uh I not get your [Music] question um like when you're printing the line can I just pass in print line X oh okay do you mean something like this no I mean can I do the X yeah maybe or X alone instead of like doing a whole string oh it's just to make it purpose right it's just to make it purose just make it all right so we now know how to you know uh make a variable mutable now the next one we have what call constants right so like IM mutable variables constants are values that are bound to a name and are not allowed to change but there are a few differences between constants and variables first you're not allowed to use mute with constants constant s just immutable by default they are always immutable do you get oh some people are [Music] sleeping we get we are here we are here okay so con keyword instead of let keyword you make it of the con keyword instead of the let keyword and the type of the value must be annotated so when you want to pass a constant you make it of the con keyword you have to specify the so for example now if I want to let's say I want to make use of I want to use constants instead of this I'm going to say number one is I'm going to use const number two is I cannot use mute number three is I have to specific I have to specify that okay this is unit eight or something I have to specify that this unit8 and yeah so let's try this it is not it's not going to run or will it is this program going to run it's not going to run no no why is it not going to run because you already made it constant it is Conant thank you so let's let's try first print line on what it say xal 6 like why is xal 6 again it's a constant so let's delete this could not compare variables due to error what's the [Music] error skip [Music] expected I that cannot be inside function BL out side of oh so the value of x is five so yeah that constant variables can be CED with any usual data type including structs including structs inms and fixed size are so here we have con one hour in second yes so one of those things also one of those convention is that you write con constants in in capitals so you write constants in capitals so one R in second this one struct instance inom instance so this one you have you can see any stru to this a struct these en these an En any you have the any Str you have the any so here this is an array so you see that for each of them the data type you have the const the name of the variable in cap the data type then that constant so nonetheless it's possible to use use con evaluate in in mro to create a constant so this one you can use this to create a constant well the base is just to just to um use it that way so yeah want talk about shadowing how many of us understand what shadowing is in what is shadowing in solidity before we move here what's shadowing in solidity he said what is sh yes okay so um so shading this with um the variable name right so it takes um prec uh precedence when it is in the function block compared to when it is in the state level so now so when you have this same the same variable the same variable name in um in um in a contract right so the one in the function takes precedence over the one in the state so Shadows it Shadows the one in okay okay yeah so shadow shadow simply means when you have functions that have the same name right so you have variables that have the same name so it's that's the mean of Shad so yeah that variable Shad refers to the Declaration of new variable with the same name as the previous variable so car say that the first variable is shadowed by the second which means that the second variable is what the compiler will see when use the name of the variable in effect the second variable overshadows the first taking any use of the variable name for itself until either it it itself is shadowed or the scope ends now let's look at what we have here let xals 5 let xal x = x + one you know if let me delete this we say let x equals 5 I will say if you say X = to x + 1 now this will not go because we are reassigning X and if you want to be if you want to make this to work we can do two things number one is either we can change this to a mutable variable or we can Shadow it so Shadow it shadowing means that we are going to what redeclare it now red declaring it in this case means that this x that we have here which is five we are now using it again here but then assign it it is now assigned to another thing here so let's print line and say that the value of x here is this and the value of x here is this right so what does that give us so you can see that the value of x is five and the value of x is six the reason it works is because of this let e word we are declaring this same name and it Shadows the first one now in this in this case also when you not have when you have this qu bracket you declaring another Scope when you have this uh what it called You're declaring other scope so in this scope now we are we are shadowing that value again so let's have let's have that so we we declare a scope and we let x equals to x * 2 in this case so the value of x in the inner scope is this the value of x in the outer scope now what do we think the value of x is going to be in this inner scope and what do we think the value of x is going to be in the other schope can someone can someone tell us what that before we run it you are sure of yourself the the inner scope will be 12 while the outer scope will be six I will be 12 x is starting from five yes but you still we still red declared it as xal to 5 + 1 so it's going to go into the coily bracket as 5 plus one that's six and it becomes the coily bracket is now red declared as 6 * 2 but what outside the coily bracket is still going to be in the parent scope because the co braer is more like a child scope compared to the one outside so I think it's 12 and six 12 and six okay let's [Music] see so the value of x in the inner scope is 12 and the value of x in the outer scope is six thank you very much for that now it says that the program first binds X to a value of five then it creates a new variable X bying let xals taking the original value and adding one to it so the value of x is then six then within an inner scope created with the qu brackets the third Le statement also Shadows X and creat a new varable multiplying the prev value by two to give x a new value of 12 when the scope is over the inner shadowing ends and X returns to being six when you run this program we have this so answer that sh is different from marking a variable as mute because we get a compile time error if you accidentally try to reassign to this variable without using the let Key by using let so I think I've explained that okay so we have another quiz who is going to take this quiz I think maybe we should have some to sacrifice for this next one we are so sure of yourself don't go and try it before coming here okay are you in this class yes sir yeah answer this question Who Wants to Be You syntax create a variable that can be reassigned option A let mute xals 99 option b minute per hour equals 60 option C let yal 10 the time starts now let yal 10 let yals 10 that creates reassigned if you got it you can be a 1000 I one millionaire I how is it called Who Wants To Be A Millionaire so that's your answer right that's that's sir are you sure before we move ahead yes sir is is the first option is thank you very who is that the first one so we have people person all right we take note of that that you said it's the first option so number two who is taking the maybe you should take the first one then is going to answer his what would be the output of this program we have let initial equals 9 let initial equals this and let initial equals this and final balance is initial balance what's the answer the program comp say six what vict saying six six six all right so the first one the answer is incorrect let yals 10 is not correct let yals 10 is not correct number two final answer is six so let's answer so that a variable that can be reassigned the variable that can be assigned let's mute xals so this mute here it means that it is mutable you get it can be changed okay so let's M xal 99 then the next one is data types right so data types you know in in normal programming language there's something call data structures and algorithm so you start with the fundamental data structures then build up on it now in on CYO that every every value in Kyo is of a certain data type which tells KY what kind of data is being specified so it knows how to work with that data this SE covers two subet of data type scalers and compound so keep in mind that sky is a statically typed language which means that it must know the types of all variables at compile time the compiler can usually infer the desired Tye based on the value and it Us in cases where many types are possible use a cast method where we specify the desired output typ so let's look at this so we have let X X here is what F 252 three and Y is what inste two x try um unrap let me copy this and try to explain what I can doesn't go so we say let's X so is f 252 equals to 3 and what let y so y isit 32 equals [Music] to x dot do all right now is this going to work yes why here we have three we are assigning three into what into X which is f 252 you know I said something before that the basic data type on Cairo is f 252 F 252 is the real data type on which means that all those unit8 Unit 32 unit this unit that all of them they are derived from felt 252 and F 252 is also used to denote short string and when you using it to denote short string what you are simply doing is you are converting that short string huh you are converting that short string into number it will convert the sh string into number that's what it is what is basically doing so let's let's try this does it work yes it works the value of y is three now this in this case now we we have fi 252 we want to cast you say cast it's not you say cast cast here you know you casting into another thing so the data type of this X was previously felt 252 which is Big you're not casting it into something small so we have to use try in two we have to use trying two trying two is like if you if you want to convert from let's say unit unit Unit 16 to Unit 8 right you know there are some possibilities where it's going to work and there are some possibilities where it's going to fail so when there are some possibilities where it's going to fail that's when you use try to but if there is no possibility for it to fail if let's say you are converted from Unit 8 to Unit 16 that one you just need to use pass and it you go or you can just use I think in also works now scalar types A scal type represent a single Val has three primary scal type FS integers and broing so you may recognize this from other program language let's jump into how they work in so F Type in Kyo if you don't specify the type of a variable or argument it's type thece to fi element when you hear the word f the F stands for what fi elements the F stands for what they slept fi elements element so it is represented by fi 252 in in the context of K when we say a fi element we mean an integer in the r of zero lesser than or equal X lesser equal to P you see that there is no negative value here where p is a very large prime number currently equal to this so this is the very large prime number that is used in um in Cairo when adding subtracting or multiplying if the result Falls outside of the specified range of prime number and overflow underflow OCC course and an appropriate multiple of p is added or subtracted So when you say that something is a field element there there is basically no um no overflow underflow actually okay there is underflow or overflow actually so but what we mean by underflow overflow is that when it gets beyond the largest value the mode value it wraps around so let's say that the p is five where you get six instead of writing six you you write one so you have to be you have to understand how it works so the most important difference between integers and fi elements is division so in division uh that's where the problem is so in integers right we have Unit 8 Unit 16 Unit 32 unit 64 unit 28 and unit what 256 U size is also U um Unit 32 so each variance has has an explicit size so we have um this subtract two if you want to subtract two unit8 right if you subtract two unit8 you get at most unit so all integers all integer Ty previously mentioned fit into F 252 ex set 256 which needs four more bits to be sorted under the in is basically a short with Fe um with two FS so you have two uh with low and high so KY also provides support for signed integers so which is the I these integers can represent both positive and negative values with sizes ranging from I8 to I um one to it each sign variant can store between this and this where n is the number of bits I want to use so you can write integer Litas in any of the forms shown know that numbers L that can be multiple numeric types allow a types oface so if you have 57 you can have types oface so this will specify so let's say let's look at something in our so if I say let x equals to five in this case now my the data type of my of my ex is fa because I did not specify I not specify so if I want to make it to be a unit in it now you see I just have to do this now let let me do something here is this is this possible so let's see if this is going to run can see it does not run because that that value is more than unit8 uh variables we have passed this so data types so we've talked about that we've talked about so numerical operations numerical operations we have yes ah okay the difference between division in um in a field element and an integer is that for a field elements for a fud elements when you when you define when you divide two numbers you are going to get another fi element when you divide two numbers you're going to get that one is not is a it's a bit of mathematics like if I start explaining it might gets more confusing so for example let's say and firstly you want to you need to firstly understand what is even the meaning of a fe element so for you to understand what a fe element is listen to the things you need you need to understand what we there are some that we call um what it called so we start from uh a group is it even a group that start from yes we start from a we have what call mors we have group we have magma semi group then we have Feud so we now get to a fe so a f element listen number one a fi element is an element that when you have uh sorry you start from structure a fi structure it has uh it has some binary operations it is closed it is associative it has inverse element it has identity element so when you have all of that then you not have for a f elements you need two this thing you have it over two operations so for the other one also it has uh identity inverse and uh feed elements uh inverse and it has identity element inverse element and it's also closed so when you have that then you can have a few elements so for a f element for example now if I'm inan if I'm doing uh for a f element is going to mostly be in a a prime number like have to be it has to be to to the modulus of a prime number so let's say that we are taking a prime number of seven so if I ask you that what is two two which is a f element in Prime Seven and Seven and and five so if I ask that what is 2 ID 5 in that case what is 2id 5 so it might require some calculation for you to get it so in that case now 2 ID 5 is going to most likely be ah it is going to it require some culations for me to get it but 2id five in for integers in fact not even for not even integers now for a real number two five for integers it does not have a value so five for integers does have a value for um for real numbers 2id 5 is going to be 0.4 for real numbers so I don't want to explain it too much so it will not get too confusing but if you need the explanation maybe maybe later or you can read more on it actually so sum we have you can sum two number 5 + 10 get difference subtract multiplication times division so the division here is called this one is quotient and this one is called what right so each expression in these statements uses a mathematical operators and single value so the brilant type we have F of n uh function mean let tals to true we have the true so it can either be true or false so string types so string types we have short string so this so they are telling us that a short string is asky string each character is encoded on one B so this one represent this this one represent this so K uses 252 for short strings as the F 252 is on 2 uh 251 bits so a sh string is limited to 31 characters so and you can also see that you use just one quote you don't use double quotes to represent string literal so b b string so this one is a b string long string with the by string Str type casting type casting is simply moving from one uh type to another like when you're moving from one type you want to move a variable from one particular type to another type that's typ casting so let's see what what's happening here so if you have a f 252 I want to convert it to unit8 so you know that all of them they are still numbers so you can actually like convert so but in this case now we use try into you use try into when you are converting from an higher to a lower do you get from an higher to a lower and there is possibility of what of error so that's why you put this on wrap but you can handle the error better later we talk about it how to handle the error better later so uh the next one we have from y 8 to 16 you can see that this is from lower to higher so you just use in two and that goes from higher to lower and from lower to higher into lower to higher into lower to higher into this one f f 252 is actually lower than in 256 so you use in2 so this one is bigger than this so you use try into and use unrap or you can handle error better so we also have the top the T so the t is just like an order list of elements so and you can specify you can see here we have you can have it as a compound data type so you have two for and a like that and one good thing is also you can destructure a t you can destructure a element so here x will be equal to 500 y equal to 6 and Z will equal to true as you can see here so then the unit data type so the unit data type is also like is also like the nothing the unit data type is a type which has only one value this so this unit data type that is here you might not understand understand why it is called Unit very well right now but just understand that yes this is called Unit data type and you can also call it you can also call it word void so you you use it to represent when something is rning nothing so we have the quiz wants to be a millionaire so he's going to be taking this Qui session all right is Daniel here Daniel okay he's not here he slept how about AGA AGA Joshua okay can someone volunteer someone that hasn't done before okay Mary Wants To Be A Millionaire let's start question number one the largest number represented by the type I 128 is [Music] what is what what the last option 12us one okay what we happen when Computing xus [Music] one what's the [Music] answer and say it it always this one be yeah okay determine whether the program will pass the compiler or not so look at this really pass does it comp yeah comp what does it what will it what will it print in thisd are you sure yeah okay so the first one you are correct the second one you are correct but the last one H the program does compile is wrong why it should return X it should return X why the program is not going to compile that's just the answer not comp so does not ex it's just a felt 32 it is felt 252 not 32 okay okay then we have function so functions are prevalent in K code the con like that like that like that so we Define a function by entering FN Follow by a function name and set of pares the bracket tell the compiler where the function body begins and ends so you can call any function we' defined by enter it name Follow by because another function is defined in the program it can be called from the from inside the main function note that we def other function before the main function the source code you could have defined it after as well doesn't care where you define your functions only that they are defined somewhere in the scope that can be seen by the color so let's start a new project with scab named functions so we don't really need to start another project but let's just call another function like this so we can actually just write how do we start a function FN right another yes another what function so but in this function we want to take it taking an argument so we are going to take in an argument of 252 right we get to let's just um show you [Music] here wow so we can call this function in this place so another function like this right [Music] run it so from this main it CS this function outside here so I know you know how to call functions I do then now in a function we have two types of functions we have what call a statement and an expression right right so functional bodies are made up of a series of function bodies are made up of a series of statements optionally ending in an expression a series of statements optionally ending in an expression that so far the functions we've covered haven't included any an ending expression but you have seen an expression as part of a statement so a statement is you know when you have two you can have two expressions that are connected together so that's a statement so for example an equ an equation is a statement that shows that two or more expressions are equal for you to know that's like a statement so when you have more than one expression connected together that a statement so because is an expression based language this is an important dision to understand so statements are instru that perform some actions and do not return a value evalu to resultant value let's some example we have this function let yal 6 so he saying that function definition are also statement the entire proing example is the statement itself so this let xals to let yal 6 so this let yal 6 here it makes yal 6 so let xal to that y so when you run this program you say that it is missing a token so it's missing a terminal token so unused variable unused the Y is not used let yal C statement does not return the value so there isn't anything for X to buy two so this is different from what happens in other language as C and so let x = y 6 have both X and Y have the value six that is not the case in what in Kyo so in this case now we say let yals to this this is an expression this is a statement this is an expression so this expression x + one is returned so y will not be equal to x + which is four so the value of yal to four so functions with return values whenever you have a function that returns anything you have the data type of what is returned so we have a quiz again who wants to be millionaire who is volunteering anybody nobody okay we have Bim is he here sleeping Matthew a Matthew Matthew a can someone who is online actually volunteer nobody okay I get spelling get right the key word for declaring a new function in car is what what's the keyword FN is that yes all right determine whether the following the program will pass the compiler if it passes where the expected output of the program if it were executed is it going to pass and if it passes what's going to the output zero the first question we it compile yes it passes it compiles and it returns Z I hope you not test it no I'm looking a block like this is what you have a brace like this you know what the CRA is yes is a statement is a statement a statement the statement that two right okay wait okay two one and two there's no one and two one and two are you sure there's no one and two so uh let's go for two only two only the statement okay number four determine whether the program you pass the compiler if it's passes write the expected output is this going to [Music] pass I don't think I don't know I'm not sure this supposed to pass so it does not compile the first one FN correct the next one the program does compile so the program is not going to compile why the program is not going to compile I think it's because of line one like the way you were doing it before when you adding like X then colum the Type U something that's theas type of this x so what's the type of this x it's not going to compile you're supposed to put the the type the next one this it is a statement it is not be uh this isn't incorrect it's not statement this is a scope no but it is not three there's no three only there so it's um it can be be a return expression also the time the program pass the compiler this is going to is going to um it's going to pass so it's an expression and it's synthetic uh scope yeah so the program you compile and need to so it does not comp so the reason is because of parameters it [Music] need okay so comment comment is very simple this is how you comment it's similar to you know multi-line comment you can also comment this way you can also comment this way right so what is the Syntax for introducing comments it's like this right what's the is Ash [Music] it all right so control flow so the ab to some depending on whether the condition happens so that's the control FL so the if expression look at so the if expression here you just have if you not put putting you have if then the condition and number two number one is that the condition must return true like it must return true you cannot you know in JavaScript you can just do if number no it must aoan so if number is equivalent to five not if number do this else this you also have the else if so let's so look at this one now is this going to compile let's check if you should run this [Music] so is not confing so this one you have to say if this equals to three the elron successful all right so handling multiple conditions else if so if else if else if then else then you also have if in let so you can say let number equals to if condition so which means that the return value of this thing you're assigning it into this into this number and we have the repetion with Loop so you can have Loop here you Loop when you have this Loop it to continue unless you have a break value so we have the loop uh you can also return values from from Loop so let's result equals to then we have the conditional Loop which is the while while this is this so pleas let's just take it so determine whether the program will pass the compiler if it passes right the expected so you look at this let xal y let y x now the problem here is what x x what you have to you need you need when you when you are making of the if you need it to return Tye here is not so it's not going to compile what happens in in CYO if if and if condition evaluates to true the progr SK if block so the program executes the block of code with the if so the two [Music] this what is the result of running a program with an E statement where the condition evat to First and no else block is provided whens to force and no so the progr panics no no the skip it so what Prov repeat until a specif condition or gas out so use Loop or while so uhuh you should clap for me now I cannot seear thank you so the next one we have the common collections so when you hear common collections in uh in programming language what you referring to are AR right collections are is you know all those link list AR all those ones so K provides a set of common collections type it can be used to store and manipulate data so these collections are designed to be efficient so we have two arrays and dictionaries so an array is a collection of elements of the same type you can create you can create and use aray methods by using the array trait from the call library an important thing to not is that AR is have limited modification options are in fact cues do you know the meaning of q what do what's the meaning of q can anyone explain the meaning of q here oh most of you you don't yes what's q q is just like um is see um first and first out yes so first and first out well maybe that doesn't work in Nigeria so when in a queue you are the first person on the Q you be the first to attend first person to be attended to so we have q we have stack stack means what first thing last out so whose values can't be modified this has to do with the fact that once the memory slot is written to it cannot be over but only Red Front it can only items to the end of an ARR and remove items from the front so creating an array creating an array is done with the array create new for here's an example so you have let mute this mute it that you can you know modify it a do zending one so when required you can pass the type you can pass theed type updating an ARR a doen you can useen to update it when you want to remove from it use pop pop front so use po when you use this pop front there are two possibility it returns an option so option means it can either give you a some like when you use option the option is just an with some value so some means that it has something in it and N means it has nothing so when you use this on WP it means that you should Panic if there's nothing inside it so the above code will print the first value of uh this is 10 So reading elements from an array so you can get to access array elements you can use get or at so using array do at index is equival to using the subscrition this so these two are the [Music] same the get method for the get method the get function returns options with the Box pointing to the reference which means it returns an opt to a box type a snapshot to the element specifi element EX in the the ele doesn't exist gets none this method is useful when you expect to access indices that may not be within the highest B I want to suche gracefully without pan so we have let's mutable thisal this let index to access we have one Mash do get we have it is some when it returns some now this MCH we have not talked about it use march to handle different uh return values so since we know that this I do get is going to return some or none so this smch help us when it returns some value it should give us this but if it returns none then it should do this so talk about the it's going to return at about we have this thing then we have the array marro this array marro is going to instead of writing array creat new append one append two append three you can make use of this Mar to um to get value so St multiple types with enom so you can make it of uh enom to stop uh different types we have data we have integer F tole like that now these thing that we have here they are also what they also macro this thing that we have here they also macro so it's going to implement copy and drop we we still talk about um traits and generics so we also have span span is a struct that represent a snapshot of an array it is designed to provide save and controlled access to the elements an without modifying the original so span is particularly useful for ensuring data integrity and avoiding boring issues when passing between functions when performing operations as introduced in and snapshots so all methods provided by AR can also be used with SNAP and span except for the appended method so to create a span you use you call the span so yeah which call to defin util function cause a rtime panic so we have we have thisable V span this so we know that from span here to save and consol access to this thing without modify the orig span is foring integ avoiding boring issues when passing arrays between functions so which means that anything that requires M uh what's it called [Music] mutability so find it and let's mute IAL Z let mute i z then you look from if I to T break if I equals to T else this so in this case now it is starting from uh it's going to start from it's n is three till is three what is n so since we have three things here this one we most likely um give a CH tell me the program pass the compiler so this is an array accessing at zero FS send so this one now is a snapshot it shouldn't pass whether the wa yeah that would have meant the the progam pass the comp this mute this I clal to one [Music] Compass okay oh it's the last one ifal to four then for of for okay this does not [Music] compile then we have the dictionary also so dictionary is just like the map in in JavaScript so you can insert so you have you insert this into this you can also get the value so Stu like that discussionary final let's okay let's talk about the ownership model so uh understanding Kos ownership system now Kyo is a language built around a LARP system that allows us to statically ensure that in every K program a value is used exactly once so this linear typ system helps prevent longtime Errors By en that operations that would cause such errors so as writing tce to a memory are detected at compared time this achieved by implementing an ownership system and forbiding copying and dropping values by default so that K uses a linear type system in such a type system any value must be used and must only be used on use there means that value is either destroyed or moved so destruction can happen in sever ways a varable goes out of scope instruct is D structured explicit D structur using destruct so moving a value simply means passing the passing that value to another function so when you pass a value from one place to another function it means that you have moved the value so this result in somewhat similar constraint the real stship model but there are some differences in particular the ownership model exist in parts to avoid dates and concurrent notable access to the memory this obviously impossible in since the memory is immutable instead K levels it its linear type system for two main purposes ensuring that all code is provable and thus verifiable Abra away the immutable memory of the Kym so ownership in Cairo ownership applies to variables and not to values ownership applies to variables and not values a value can safely be referred to by many different variables even if they are mutable variables as the value itself is always immutable variables however can be immutable so the compiler must ensure that constant variables aren't accidentally Modified by the programmer this means it possible to talk about ownership of the owner is the code that can read and write so this that means that variables not values follow similar rules to R number one each variable in KY as an owner each variable in KY as an owner so the owner is usually the what the scope there can only be one owner at a time there can only be one owner at a time so which means that when you move out of a scope when you move to a different scope you move the owner so when the owner goes out of scope the variable is destroyed so when the owner is destroyed the variable is destroyed so now that we have pass basic that we don't include all the this okay we understand so in this case now said that let x equals to what a right now if you have let me let's write this for our own so that we can follow it along so here we have FN main so we say let s equal to hello right so in this case now this variable s the owner of this variable s is this SC this main scope now that the S refers to is from the point at which it is declared until the end of the SC so where does this it is valid from this place up to the end here up to the end here which means that anything if you should from this place now you should say print line and you put X here it is not going to work because number one the S is not then is already out of scho at this point right so then we now let's say that we now create another scope you know this is a scope here before this scope ends you now create another scope here and this scope you now say that let x equals to L and we do Sol with it and then so if you should say no let's another variable here so let's say that let's m equal to World let M go to word so in this place we can print M here but by the time we get here uh let's try to print M again but at this point m is already out of scoope so at this point here m is already out of scope that's the reason we have this we have this error m is already out of scope so a variable and scope in which it is valid in other words there are two important points here when X comes into scope it is valid it is only valid until it goes out of scope so at this point the relationship between scope and when variables are valid is similar to that in other program language now we build on top of this understanding by using the AR type the previous one so let's say that moving values we have let mutable this equals to this we have this then AR do one weend one and two so here we now have a function here let me we are going to write that so we have let mutable array equals to you know we are creating a of elements okay so this is going to like give us a so let's push some values now in this case we now have how does the type system sure that the never to write the same memory so we already have this two array so we have a function here so if I should try to so okay before that I'm going to print I'm going to print this now before and after right so in this place now we know our array this is our ARR here right so I'm going to print I'm going to print it here after printing it then we are passing that array into this function which is another scope then we are going to try again now we have an error why do we have this error wait wait wait okay oh we are supposed to use the oh we are using is it [Music] okay so in this place now we have a sp front so in this case now in the first instance it's actually possible that it has even moved it in this print line it moves in that case also oh okay let you should implement the B now yeah yes so this is what I'm looking at this this the I'm looking for so here we have on so you see that varable was previously moved here in fact it not even get this place so from here it has already moved this Ara from here this moved it from here to here so which means by the time you are calling it here there's nothing left so assuming not even move it here maybe you know move it here so when it is getting out of this print line it is get it is out of scope already so which means that this array here is nothing so that's the ownership model so let me I'm going to delete this so which means that yes it enter this first case but now let's have let's try to like pass it in two times so say that vable was previously moved so which means that in this case now it moved it there was movement how is there a movement we have an array so it was declared there which means it's supposed to be valed from here to here however when it got to this place it was moved to another scope this is another scope here so when it was moved to another scope the scope ended here so coming back to this place there's no array again coming back to that place there's no ARR so yeah so one of the ways in which we can you know get away with that is when we are moving something when we are moving from one function to another we are moving from a if that data type if it implements the copy trait if it implements the copy tra then it's not going to move um it's going to Simply copy it it's going to Simply copy it so an example is if you should have this stru so I'm going to use um let me copy all this I'm going to this so in order to I'm going to comment this first so that we can see what is happening so normally this is a this is a point so we created a point here we moved it you know we moved it here into this scope and you know it has already it has left the scope so there is nothing remaining in this place again so if you should try to run this code it's going to tell us that could not compare this what [Music] the, implementation in the context P point where is P FN P for point so let's just say Face line let's see are going to print something different here so that so that it doesn't we don't want move it beyond the fact that yeah not compare VAR was not dropped oh okay now use the so you say that no implementation for default forat Y what of theg [Music] oh oh oh okay so the mistake actually is that the drop trads yeah it's only this copy that I don't want it to have so now normally is supposed to have the drop shet so let me just so so yeah it now has so you can see that see that it's saying that it was previously moved so that's the error that we have now so however when it is moving when it is moving if it imp the copy traits so we see talk about traits and their meaning so if it implements the copy trait then it's going to it's going to run out successfully so which means that when it is also moving other thing that it also checks for is it checks if it implements the copy so if it implements the copy trait then instead of moving it it's going to it's going to copy it instead of moving it it's going to copy it so we need to um take note of that so do we have questions while we proceed do you have questions no question so I have questions all right yeah what's the question what's the question someone wanted to ask a question what sorry I said like what you did I didn't understand you can sorry Snapchat is not snap I think there was the last not to Snapchat [Music] yet I talking about the ownership model do you understand the ownership model now first you so the number one is that variables have which is the scope which they declared so that all variables they are valid from their point of Declaration till the owner is out of scope right so when it moves from one uh scope to another so it's going to move so when it moves the drop traits so drop trait is just uh it's like uh in drop trait is just like a way of clearing the memory when you are you know when when the when is when a variable when it goes out of scope it implements the drop tra so the drop tra is going to clear the memory it's like the referencing is it called you are you know when you creating a memory when you're creating a v variable you are allocating a memory so when you're dropping it you are clearing you are freeing that memory that you allocated for it so that's what the drop trait is doing so when you move when you move something it moves the ownership of varable to another and when the scope when it ends then it means that it's going to drop that particular variable right so however however when uh that variable if it implements copy it Implement copy then uh instead of moving it it's going to copy it so which means that when it goes out of scope you still have that variable you still have that variable so so that's what not talking about the copy array data so if you do want to deeply copy the data of an you can use a common trait called Lo so return values and scope let's look at this we have let A1 equals to G ownership so this gives ownership here is going to give it gives it ownership to you know own ownership of V is returned it gives it to A1 now A2 now comes into scope A3 takes the ownership of [Music] A2 no no this one it gives the it takes the ownership of A2 this function takes and gives back takes the ownership of a and then give it back to [Music] A3 so that so here A3 goes out of scope in this place now A3 goes out of scope and is dropped A2 was moved so A2 was moved to this one so nothing happens A1 goes out of scope and is dropped please do you have question on this memory uh this ownership model because I know that's where a lot of questions usually asked so that I can um I can try to explain it better [Music] now is there any question on it again yeah question all right what was that okay so when you say uh variable is owned right is it the same thing as block scoped in Block scoped like then let me say the varable is block scoped since the main the main structure is um the function Main in Cairo so it's kind of like I feel like it's kind of the same logic when you say a variable is block scoped like be assessed by that block or the parent block um so I would have said yes it is yes however you know we have different programming languages and there are some of them that are garbage colle that have garbage collectors so by garbage collector it means that it's where are scoping scoping of variables it allocate and allocate memory for you so you're not the one managing it like in programming that Paradigm the way it is is that if you're programming in programing languages like C and C++ now you have to allocate your memory and de allocate it all by yourself so which means that when you're bringing a variable into scope you allocating a memory for it and uh when it is going out of scope you deallocating it you have to hand it by yourself for JavaScript or Java they are garbage collectors so which means that handling you know when you bring a memory into space or this all those things it is Javascript it is the one that is doing everything like you don't think about it so in you can put something in a blog scope and you are still going to be able to access it outside of the scope in JavaScript so that's the reason I cannot say yes to to your um to what you said so that that's for garbage collectors but for this one now you have you are the one that you want to define the behavior so that's the reason it now has those rules so and the rules are very simple when you create a variable the variable is going to be valid till the end of that particular scope till the end of that particular scope however before the end of the scope the can be moved the variable can be what can be moved so when it is moved from that scope to another scope now in that particular scope where it is when it finishes the scope there it is going to be dropped in that scope is going to be Dro so which means that outside of that scope if you try to access it again then you uh that's going to be a non reference you're going to be making reference to a uh do you get so it's like that that that's just simply it this this one now this is a scope this is a full scope this is the main scope the main scope started from here and it's ending here this P1 is starting from here normally it should start here and end here however when it go to this place it was moved from P1 to this so when it was moved from here to here itans that it has it scope is now from here to here when the scope ended here it was dropped so which means that P1 no longer exists here P1 no longer exists here do you get so it cannot it canot it doesn't happen however I also that if this P1 if it implements copy instead of moving it from here to here it was copied from year to year so which me this one copied it so even when it was dropped it doesn't matter this one again copied it do we get do you do you understand Victoria yes yes that makes sense Abraham is asking an important question all right uh when we implement and variable is used in another scope and makes changes to that variable so which value do we get back when we ref It Again original or the change so when you say that you copy something what does it mean you've copied it right so which means that it is the copied version that you are using in that other place do you get so which means that the original is still it's actually something that we can we can actually try out so in this case now so since we have since this is [Music] uh you know we now actually there are some there are some this that implements copy okay this one that okay this implements copy so let's say that in this case now we have uh oh I I I don't want to use something that is going [Music] to I don't I don't want to use something that is going to like move okay that is going to move it [Music] again so so actually everything is just going to like it is T be five in this place so let's let's print this first let's run this first so 5 five right so inside this place let's now say that when we get we change this P to so if we change this to p.x we change it to 10 here is it mutable it is not mutable so let's say that it is mutable but so if you not change it to mutable it's going to be another so let's not change it to M yet so let's not change it to M yet but the simple this is that since you copied it this P1 here is still existing here so which means that in the next case at this particular place this one it was already dropped you know this P1 the P1 that was inside this one at the end of the scope here it was already dropped so it doesn't exist again it is still this P1 that you are still calling this place again so that's that's the that's the case I hope that answers your questions abah all right so the next one is now references and what and snapshots so reference and snapshot you all know what snapshot means right snapshot is just like when you when you take a snapshot when you camera something so when you camera something when you take the picture of something can you modify it everyone has s how many [Music] minutes yes you I'm I'm still here yes yes sir no sir so what what's the case question again well in a normal when you snap when you take the snapshot of something can you can you modify that that thing like the one SN or the original original original thank you so when you take the snapshot of something you cannot modify the original you just have a copy of things that you can just so when you make reference to something when you make reference to something you can you can still change it that's the way it works here so for for the ownership this thing the way it now works is for snapshot since it is uh since it is a snapshot that it is not mutable it's immutable you cannot change it you get so which mean that you can take one million snapshots of one variables it doesn't matter but for reference you can only take one mutable reference and unlimited immutable reference one mutable reference and unlimited imut so in the previous chapter we talked about how K ownership system prevents us from using a varable after we've moved it protecting us from potentially writing twice to the same memory self however it's not very convenient let's see how we can varable in the coiling function using snapshots using what snapshot so in Kyo snap is an immutable view it is what immutable view of a value that certain at at a certain point in time got that memory is immutable soy a value can actually create a new memory cell the old memory cell still exist and snap are varable that refer to that old value in this sense snapshots are a view into the past here is how you you define and use a calculat length so function so here we have this array now you take the snapshot you can see that snapshot is using at at so here now you've appended it you mutated this let first length equals to this so the second L equals to this calculate using so the length of the current when the snapshot was taken is first l length of of the AR is what so you see that in this case now the data type we have included this art whenever you see this art it means what snapshot has you are taking the snapshot so two things you can either have this at or ref you can either have that or ref so when you are using that art you can use it for in a lot of times but when you are using this ref you can only use ref on that is the m table so you say that we can achieve the behavior we want in M reference are actually mutable values so they actually M ref are actually M values pass to a function that implicitly return at the end of the function turn ownership to the calling context by doing so they allow you to mutate the value P while keeping ownership of it by turning it automatically at the end of the execution in Kyo a parameter can be passed as mutable reference using the r modifier so in Kyrie a parameter can only be passed as mutable referenes in ref modifier if the is declared as mutable with mute so so if you look at this one now you can see that we have ref we have a ref here so and we pass this because this varable is what is mutable here so we pass this now so small recap at any given time a can only have one owner you can pass a varable value a varable by value by snapshot or by reference to a function you can pass it by value snapshot or reference if you pass by value ownership of the varable is transferred to the function you you want to keep ownership of the varable and know that your function it can pass it as a snapshot using art you want to keep ownership of the variable and know that your function will mutate it can pass it as what a mutable reference ref do we have any [Music] question do you have any question as that hello any question no question actually the way I want us to have this class is this particular one we are going to go through so this is like the first part of it so the second part of it we are going to be writing a smart contract using Kyu you're going to be writing a proper smart contract using Kyu so we need to learn how the things work here so that so I believe everyone here can write smart contract in solidity already so we are just going to be looking at how do we convert the mental models we developed for solidity so write smart contract here so struct now this struct you can think of it as a way of you can call it a structure it's like a structure so a structure structure yes is a custom data type that let us to package together and name multiple related values that make up a meaningful B if you're familiar with an object oriented language it's always like an object data attribute in this we compare and toles with Str to build on what you already know and demonstrate where just a better way to group data so we we already know T right if you want to declare a t yes you know name of then you have all those um the data types inside it now but if you have a a what's it called a stru look at this stru now you have struct user active bu username this and that and that so one of those things that you can do is that look let's look at this this thing that we have here let this here so we have this structure user we have active username emails um signing and the like so in this place when we want to create a user we can say what user create the name of the structure and then we start assigning all of these things now another thing that we not have in structure is that in structure we can have what we call what implementations we can have implementations of what of structures so we can have using the we can uh we can have traits two things actually traits oh no no no we can have implementations so we can Implement some trait on on structures traits are different traits are just like collection of behavior so structure you can think of structure as a body so you can Implement some behaviors on the body so if um the way we have this structure now we can Implement let me say I can Implement um user traits for for user and for this one what I'm just going to do is in fact I can Implement let me I want to implement default so implementing default simply means yes so with this default now it means that I will be able to uh let's say if I want to I can say let let's check something let user three equal to so in this place now I can do user uh default or let's now say that if I had declared that the data type of this one is user so I don't even need to to even put user here I can just say what default default so let's let's check [Music] something you want to print line want to print let's see what it's going to give us so variable was the question cannot be created from a numeric lit username name oh it's B let me change let me change them to F sorry um f 252 so what's happening [Music] true is to tr has no no implementation in oh I'm supposed to for if implement default for let me just use user or this the oh okay I'm going to use the B yes so all this macro that we have here they also we not compare due to what unused variable un variable self type not found type self [Music] Sor and just and done what again okay Implement default I think this one is this one is of that they make of let check is oh there's no Implement [Music] sorry [Music] um yes okay it's off maybe okay let's see this so in this play who I still using default [Music] here even at that [Music] still a rug [Music] is oh this is not ty [Music] not invited Parts user three user default okay maybe we come back to this implementation when when we see it in in the book so method syntax an example program using struct so in this case now we have function area no this not is yet okay stru we have rectangle so let rectangle to this areals to area of rectangle so this it takes in yes it takes in this rectangle and produces the area okay that's a good example another one so method syntax yes so to Define method you make use of traits so have trait rectangle traits so FN area and function area it takes in the rectangle stru and it produces this type okay so yeah generate traits so actually whenever you do this you're supposed to add this mro generate traits so let's use the generate tra in the following so when we use that Mar it's going to that's why it use so the one that was doing that is um so you can see yes Implement Implement rectangle increment of this and it's going to give you it's going to give you all of that so methods with several parameters and can have it to methods without self s parameter so yeah methods methods can have two two so the ones that have self and the one that don't have self so for the ones that don't have self so it means that they they are what we call what con they the ones that that call Constructors but the ones that don't have the ones that have self those ones are methods or is it Behavior those ones are the constructors so in JavaScript there are methods and Constructors so uh the last very big uh what's it called here is the enom so enom to they like they like numeration so in all the all the POS directions you have not e Southwest so enumerating message creat Echo move like that implementation of En so then we can now when you you know when you use enom you're going to list everything that is associated with with it when you list everything that is associated with it so you can now you can use mash to what to exhaust to exhaust uh the thing so if you look at this message now are we yes this message so you know that it has K kit Echo move so you are using this m to say that if the message is quit you should do this if it is Echo you should do this if it is move you should do this like that then we also have option option is like an En with some or none so you can also use on this also if it is some you do this if it is none you do that so uh that's that that one so the M we we are talking about the March control flow now so you can see MASH the name of what you want to match you want to match this if it is this do this if it is this do that if it is this do this so that's so and in MCH also you can also have multiple this is the all operator is this or this do this this one is for every other thing it is either this or this do this for any other thing then do this so that's much concise control flow with if let if let so the if let sytax let's combine if and let into a less ver way to handle values that match one pattern while ignoring the rest so it's just going to match the one that handles a particular pattern so let this equals to this match this [Music] one so instead of doing this so you can just use if let this then do this so while also so then this one is just about managing your Kyo projects that's just what this one is about so these are the major things these are the major things then uh package manag your C projects we have the packages and crates [Music] so you know the packages are in the sc. thumbil and the code so when you do scab new my package you're going to have something like this and if you want to define a new mod you just use mode whatever it is and you starts so and so this one is for inline if it is inside the le. sky but it is outside it you have to uh is out everything if it is outside it this one you havec gyo this one you see have to declare it again in the uh in the LI do K even though you still have to declare it so you use the Ed keyword so we have thec Garden vegetables.
sky garden. skyro so you have this so use the use garden vegetable then inside this vegetables that's where you have this one is a struct this is the struct there so you can now start using it in this place so something like that in this place also so front of house mode hosting how to Wast weight list sit at table so this to tell you about like how you can have a modu inside another mod you can see that this modu now front of us inside it you have two modules you have hosting and you have serving inside this hosting that's where you have this two functions inside service that's where you have all these functions so if you want to this is the structure hosting serving inside this you have this and this you have this and that so and when you want to reference also you make use of use then you can start from the beginning or you start from a particular place where where it is known so look at this now this absolute part starts from restaurant then front of house front of house restaurant is the package name then front of house hosting how to waight list this one is relative if you're ready in you already in this place so you can call this one you start front of house posting how to this so do you have any question we are going to uh do you have any question so far so good yeah you can photosh it yes that's on the Snapchat but the thing is it is when what you are changing is not the picture actually it is actually the person that you snapped so is it possible that you know using the picture you're not like modifying the person's traits so that that's going to be [Music] the that's going to be the anal all right so there is no [Music] question so we would have to uh we have to stop here so the exercise the exercise is basically just to just to read up on this number eight generic types so when you use generic type how how do you use it then error and testing car program and advanc car fure so like just this remaining parts so the next class that we are going to have so please if you have any question you can ask you can ask any question that you have so by God's grace in the next class that we are going to have we are going to be writing smart contracts in Kyu so and one of those things that I would actually also want you to do is yeah those contract that you've written in solidity before just put them just have them beside you so you going to be rewriting them in Cairo and you be exploring the mental models that are used for writing smart contract in Cairo and Sol [Music] D so why why is there no question is it that if you not understand everything I've been saying or what or you fully understand understood everything what which which is the case okay for for me I don't are some Concepts that were pretty new and it was kind of like feeling overwhelming at some point but I'm sure if I go back and read what you've said and probably go back and do those squeezies again I will actually like get it more but for now I'm just overwhelmed yeah of course you you have to you have to do your your assignment too so that's I think there still there is enough time between um now and the next class so please and please to everyone go through it again and in fact the best is um the Kyo book you can see that we are almost even done with with it these other ones the main the major part of them if you explained them so generic type is simple error handling to is simple testings Advanced KY features andics then this uh this smart contract in Kyo is just like uh all those MSD do sender MSD value address this how you are going to get all those all those St that's what is there so let's all um go through it read up on it and everything so that's the end of today's class in the ass of questions an answer then we are going to call it today thank you for attending thank you so much sir
Automatic transcript — names and jargon may be misspelled.