;;;; extending Casui ;; all module prefixes can be omitted, name lookup depends on the context ;; all types of extensions have the same namespace ;;;; basic extension types ;; (module [extensions ...]) ;; define extensions in a named module ;; the enclosed extensions automatically get the name: prefix ;; the extensions can include another module, eg foo:bar:baz ;; (script:property ) ;; define a type of property ;; the property can then be used as an extension, to define a tuple ;; eg. (name a) (name a b) (name a b c) ;; (script:alias ) ;; creates an alias for the other name ;; (script:type ) ;; create a user-defined type ;; (script:context ) ;; define a custom context for expressions ;; (script:constant ) ;; (script:variable [value]) ;; (script:subroutine ([arguments*]) ) ;; these three allow to define variables, constants and subs ;; (script:op ) ;; define an operator ;; (match:canonical ) ;; equivalent expression used when pattern matching ;; (math:simplify ) ;; (ui:input-alias ) ;; (ui:input-alias ( [arguments*])) ;; input alias ;; (script:use [none | [ | ( )]* ]) ;; modules to include ;; default use is (script ui math view expression equations) ;; priority is left to right ;; (script:define [ | (property [arguments*])])* ;; shorthand for defining multiple properties ;;;; Properties ;; (ui:view ) ;; the view of an instance, an object or a type, ;; eg. (view math:add view:binop) ;; (view:operator ) -- string - the character for unop or binop ;; (ui:handler ( [arguments*]) ) ;; trigger code when event happens ;; arguments are deep pattern matching ;; (ui:priority ) ;; operator prority, currently 0 to 1200 based on prolog ;;;; ops ;; math:add ;; math:mult ;; math:div ;; math:pow ;; math:neg ;; math:equals ;; data:list ;; script:rule ;; math:inv ;;;; Views ;; view:function ;; view:binop ;; view:unop ;; view:frac ;; view:sqrt ;; view:div ;; view:pow ;; view:name ;; view:bool ;;;; Types ;; symbol, exact, inxact, string, list ;;;; Contexts ;; View resolution: instance -> object -> type -> context -> loaded modules in order ;; equations:context ;; default context for equations in the ui ;; script:context ;; default context for script files ;;;; subroutines ;; the language is a kind of lisp ;; with somevariables dynamically bound ;; builtin:handle-key-press ;;;; Handlers ;; the most specific handler pattern match is chosen ;; (expression:keypress any (handle-key-press)) ;; enter a number ;; add operator ;; move with arrows ;;;; Modules (module elementary-algebra ; http://en.wikipedia.org/wiki/Elementary_algebra ;; todo: ;; monotone (increasing/decreasing/by variable) ; operations defined in this module (define add op (inverse subtract) (zero 0) commutative associative (sub list (apply + list)) (operator "+")) (define subtract op (zero 0) (inverse add) (sub (a b) (- a b)) (operator "-")) (define negate op (operator "-") (sub (a) (- a))) (define multiply op (zero 1) commutative associative (distributes add) (sub list (apply * list)) (operator "*")) (define divide op (zero 1) (inverse mul (not-equal b 0)) (sub (a b) (/ a b)) (condition (not-equal b 0)) (operator "/")) (define inverse op (sub (a) (divide 1 a)) (condition (not-equal a 0))) (define log op (sub (a b) (log a b)) (sub (a) (log a default-log-base))) (define pow op) (define nth-root op) (define not-equal op inequality) (define equal op equality inequality) (define less-than op inequality) (define greater-than op inequality) (define less-than-or-equal op inequality) (define greater-than-or-equal op inequality) (canonical (subtract a b) (add a (negate b))) (move source dest ('op source dest) ; match enginge knows about commutativity and associativity (prop (commutative op)) (rebuild empty (add source (mark dest)))) ;and rebuilds expression like it was (move source dest ('op1 source dest) ; match enginge knows about commutativity and associativity (prop (distributive (op1 (op dest)))) (rebuild empty ; match:empty, a special value (mark ; change selection to here, match:mark (apply (op dest) (map (lambda (child) (op1 source child)) ; todo: correct order (right/left distributivity) (children dest)))))) (move source dest (divide dest (either source (* source))) true (rebuild empty (multiply dest (inverse (mark source))))) (move source dest (multiply source (dest (root a b))) true (rebuild empty (root (multiply (pow (mark source) b) a) b))) (move source dest (op (either (add source)) dest) (prop (inequality op)) (rebuild empty (add dest (negate source)))) (move source dest (pow (dest (pow a b)) source) true (pow a (multiply b (mark source)))) ) ; end module elementary-algebra