MoonBit月兔编程语言 现代编程思想 第十二课 案例:自动微分 我们以符号微分定义表达式构建的⼀种语义 1. enum Symbol { 2. Constant(Double) 3. Var(Int) // x0, x1, x2, ... 4. Add(Symbol, Symbol) 5. Mul(Symbol, Symbol) 6. } derive(Debug) 7. 8. // 定义简单构造器,并重载运算符 9. fn Symbol::constant(d : Double) -> Symbol { Constant(d) } 10. fn Symbol::var(i : Int) -> Symbol { Var(i) } 11. fn Symbol::op_add(f1 : Symbol, f2 : Symbol) -> Symbol { Add(f1, f2) } 12. fn Symbol::op_mul(f1 : Symbol, f2 : Symbol) -> -> Symbol { Mul(f1, f2) } 13. 14. // 计算函数值 15. fn Symbol::compute(f : Symbol, input : Array[Double]) -> Double { ... } 15 符号微分 利⽤函数求导法则,我们计算函数的(偏)导数 如果 为常值函数 ⽉兔实现 1. fn differentiate(self : Symbol0 码力 | 30 页 | 3.24 MB | 1 年前3
The Zig Programming Language 0.1.1 DocumentationGlobalVarDecl | UseDecl) ErrorValueDecl = "error" Symbol ";" GlobalVarDecl = VariableDeclaration ";" VariableDeclaration = option("comptime") ("var" | "const") Symbol option(":" TypeExpr) option( ContainerMember ContainerMember = (ContainerField | FnDef | GlobalVarDecl) ContainerField = Symbol option(":" Expression) "," UseDecl = "use" Expression ";" ExternDecl = "extern" option(String) (FnProto | VariableDeclaration) VariableDeclaration) ";" FnProto = option("coldcc" | "nakedcc" | "stdcallcc") "fn" option(Symbol) ParamDeclList option( VisibleMod = "pub" | "export" FnDef = option("inline" | "extern") FnProto Block ParamDeclList =0 码力 | 82 页 | 2.67 MB | 1 年前3
The Zig Programming Language 0.2.0 Documentationfunctions from being duplicated. Reasons for having multiple @cImport expressions would be: To avoid a symbol collision, for example if foo.h and bar.h both #define CONNECTION_COUNT To analyze the C code with @import @export @export(comptime name: []const u8, target: var, linkage: builtin.GlobalL Creates a symbol in the output object file. @tagName @tagName(value: var) -> []const u8 Converts an enum value VariableDeclaration = ("var" | "const") Symbol option(":" TypeExpr) optio ContainerMember = (ContainerField | FnDef | GlobalVarDecl) ContainerField = Symbol option(":" PrefixOpExpression option("="0 码力 | 117 页 | 2.84 MB | 1 年前3
Haskell 2010 Language Report→ a horizontal tab uniWhite → any Unicode character defined as whitespace comment → dashes [ any⟨symbol⟩ {any} ] newline dashes → -- {-} opencom → {- closecom → -} ncomment → opencom ANYseq {ncomment ANYseq} closecom ) {ANY }⟩ ANY → graphic | whitechar any → graphic | space | tab graphic → small | large | symbol | digit | special | " | ’ small → ascSmall | uniSmall | _ ascSmall → a | b | . . . | z uniSmall Unicode letter symbol → ascSymbol | uniSymbol⟨special | _ | " | ’⟩ ascSymbol → ! | # | $ | % | & | * | + | . | / | < | = | > | ? | @ | \ | ˆ | | | - | ˜ | : uniSymbol → any Unicode symbol or punctuation0 码力 | 329 页 | 1.43 MB | 1 年前3
The Zig Programming Language 0.3.0 Documentationfunctions from being duplicated. Reasons for having multiple @cImport expressions would be: To avoid a symbol collision, for example if foo.h and bar.h both #define CONNECTION_COUNT To analyze the C code with @intToError @export @export(comptime name: []const u8, target: var, linkage: builtin.GlobalL Creates a symbol in the output object file. @fence @fence(order: AtomicOrder) The fence function is used to introduce VariableDeclaration = ("var" | "const") Symbol option(":" TypeExpr) optio ContainerMember = (ContainerField | FnDef | GlobalVarDecl) ContainerField = Symbol option(":" PrefixOpExpression) option("="0 码力 | 175 页 | 4.08 MB | 1 年前3
Agda User Manual v2.6.2know the proof. The clause consists of the name of the property, the input variables, the equals symbol = and the question mark ?. +-assoc-proof x y z = ? When we reload the file, Agda no longer throws types as well): example₁ = \ (A : Set)(x : A) → x example₂ = \ A x → x You can also use the Unicode symbol λ (type “\lambda” in the Emacs Agda mode) instead of \\. The application of a function f : (x : : (forall x y → C) is-the-same-as (forall x → forall y → C) You can also use the Unicode symbol ∀ (type “\all” in the Emacs Agda mode) instead of forall. Functional abstraction: (\x y → e)0 码力 | 348 页 | 414.11 KB | 1 年前3
Agda User Manual v2.6.2.2know the proof. The clause consists of the name of the property, the input variables, the equals symbol = and the question mark ?. +-assoc-proof x y z = ? When we reload the file, Agda no longer throws types as well): example₁ = \ (A : Set)(x : A) → x example₂ = \ A x → x You can also use the Unicode symbol λ (type “\lambda” or “\Gl” in the Emacs Agda mode) instead of \ (type “\” in the Emacs Agda mode) : (forall x y → C) is-the-same-as (forall x → forall y → C) You can also use the Unicode symbol ∀ (type “\all” in the Emacs Agda mode) instead of forall. Functional abstraction: (\x y → e)0 码力 | 354 页 | 433.60 KB | 1 年前3
Agda User Manual v2.6.2.1know the proof. The clause consists of the name of the property, the input variables, the equals symbol = and the question mark ?. +-assoc-proof x y z = ? When we reload the file, Agda no longer throws types as well): example₁ = \ (A : Set)(x : A) → x example₂ = \ A x → x You can also use the Unicode symbol λ (type “\lambda” or “\Gl” in the Emacs Agda mode) instead of \ (type “\” in the Emacs Agda mode) : (forall x y → C) is-the-same-as (forall x → forall y → C) You can also use the Unicode symbol ∀ (type “\all” in the Emacs Agda mode) instead of forall. Functional abstraction: (\x y → e)0 码力 | 350 页 | 416.80 KB | 1 年前3
Agda User Manual v2.6.2know the proof. The clause consists of the name of the property, the input variables, the equals symbol = and the question mark ?. +-assoc-proof x y z = ? When we reload the file, Agda no longer throws types as well): example1 = \ (A : Set)(x : A) → x example2 = \ A x → x You can also use the Unicode symbol ? (type “\lambda” in the Emacs Agda mode) instead of \\. The application of a function f : (x : prop5 : (forall x y → C) is-the-same-as (forall x → forall y → C) You can also use the Unicode symbol ∀ (type “\all” in the Emacs Agda mode) instead of forall. Functional abstraction: (\x y → e) is-the-same-as0 码力 | 255 页 | 1.13 MB | 1 年前3
Agda User Manual v2.6.2.2know the proof. The clause consists of the name of the property, the input variables, the equals symbol = and the question mark ?. +-assoc-proof x y z = ? When we reload the file, Agda no longer throws types as well): example1 = \ (A : Set)(x : A) → x example2 = \ A x → x You can also use the Unicode symbol ? (type “\lambda” or “\Gl” in the Emacs Agda mode) instead of \ (type “\” in the Emacs Agda mode) prop5 : (forall x y → C) is-the-same-as (forall x → forall y → C) You can also use the Unicode symbol ∀ (type “\all” in the Emacs Agda mode) instead of forall. Functional abstraction: (\x y → e) is-the-same-as0 码力 | 257 页 | 1.16 MB | 1 年前3
共 49 条
- 1
- 2
- 3
- 4
- 5













