Download presentation
Presentation is loading. Please wait.
Published byEsther Curtis Modified over 9 years ago
1
Language Oriented Programming in F# Robert Pickering, Chance Coble, Roger Castillo
2
Languages are Logic Programming languages provide a way to express logic in a symbolic notation Combinators give us a way to express logic Using this approach we can create a few atoms, many rewrite rules and presto…
3
Combinators true-fn(x, y) = x false-fn(x, y) = y or-fn(x, y) = x(true-fn, y) and-fn(x, y) = x(y, false-fn) or x yx=true-fnx=false-fn y=true-fn true-fn y=false-fn true-fnfalse-fn
4
Combinators true-fn(x, y) = x false-fn(x, y) = y or-fn(x, y) = x(true-fn, y) and-fn(x, y) = x(y, false-fn) not-fn (a(x,y)) = a(y,x) equals-fn x y = x(y, (not-fn y)) Implication-fn x y = or((not-fn x), y)
5
Combinators true-fn(x, y) = x false-fn(x, y) = y or-fn(x, y) = x(true-fn, y) and-fn(x, y) = x(y, false-fn) not-fn( a (x, y)) = a(y, x) equals-fn(x, y) = x(y, (not-fn y)) implication-fn(x, y) = or((not-fn x), y) and-fn(x, y) = not-fn (or-fn ((not-fn x), (not-fn y)) = not-fn ((not-fn x)(true-fn, (not-fn y))) = not-fn (x (false-fn, y)) = x(y, false-fn) and-fn(x, y) = not-fn (or-fn ((not-fn x), (not-fn y)) = not-fn ((not-fn x)(true-fn, (not-fn y))) = not-fn (x (false-fn, y)) = x(y, false-fn)
6
While Statement while (true) { if (1!=1) Console.WriteLine(“Cosmic Death Ray Detected”); }
7
Statement Grammer if statement listBoolean expression while (true) { if (1!=1) Console.WriteLine(“Cosmic Death Ray Detected”); }
8
Statements and Expressions if statement list Boolean expression while statement list Boolean expression while (true) { if (1!=1) Console.WriteLine(“Cosmic Death Ray Detected”); }
9
Syntax Tree while (true) { if (1!=1) Console.WriteLine(“Cosmic Death Ray Detected”); } if statement list Boolean expression while Boolean expression (1!=1) (true)
10
Abstract Syntax Tree This language is a list of statements The statements must follow a grammar that can be represented by a tree if statement list Boolean expression while Boolean expression
11
Abstract Syntax Trees and Grammars This language is a list of statements The statements must follow a grammar that can be represented by BNF statement ::= ‘while’ ‘(‘expression’)’ statement-list | ‘if’ ‘(‘expression’)’ statement-list |...
12
Direct Translation From Grammar statement ::= ‘while’ ‘(‘expression’)’ statement-list | ‘if’ ‘(‘expression’)’ statement-list |... // F# Discriminated Union type Statement = | While of bool expression * Statement list | If of bool expression * Statement list
13
Abstract Syntax Tree type Statement = | While of bool expression * Statement list | If of bool expression * Statement list Tuple Syntax (X,Y)
14
Union Types – The Option Type // The pre-defined option type type Option = | Some of 'a | None // constructing options let someValue = Some 1 let noValue = None // pattern matching over options let convert value = match value with | Some x -> Printf.sprintf "Value: %i" x | None -> "No value"
15
Union Types - Trees // a binary tree definition type BinaryTree = | Node of BinaryTree * BinaryTree | Leaf of 'a // walk the tree collection values let rec collectValues acc tree = match tree with | Node(ltree, rtree) -> // recursively walk the left tree let acc = collectValues acc ltree // recursively walk the right tree collectValues acc rtree | Leaf value -> value :: acc // add value to accumulator
16
Using the Tree // define a tree let tree = Node( Node(Leaf 1, Leaf 2), Node(Leaf 3, Leaf 4)) // recover all values from the leaves let values = collectValues [] tree
17
Union Types – Multiple Patterns // balance the left side of the tree let balanceLeft tree = match tree with | RedTree (y, yv, RedTree(x, xv, a, b), c) -> RedTree(y, yv, BlackTree(x, xv, a, b), c) | RedTree (x, xv, a, RedTree(y, yv, b, c)) -> // none matched, just make a tree RedTree(y, yv, BlackTree(x, xv, a, b), BlackTree(z, zv, c, d)) | _ -> mkTree(isBlack, z, zv, l, d) // none matched, just make a tree
18
Can we do this using other constructs? Enum Inheritance O Functional Types Operations
19
Performance in Ticks / Invocation
20
type Prop = | Line of Point list * (TimedIndex -> Point) * MaterialColor | Surface of Origin * SurfaceProperties * (TimedPoint -> float) | Triangle of (int -> TriangleFace) | TextAnnotation of (int -> RectangleFace * string) | Sphere of SphereProperties | Cylinder of CylinderProperties type Scene = | AnimatedScene of Prop list * Scene * SceneSpeed | StillScene of Prop list * Scene | Empty 3D Visualization DSL
21
Demo
22
let rec eval model = function | AnimatedScene(ps,scene,speed) -> // … skipping some timer code let model' = fold evalProp model ps // … eval model' scene | Empty -> model Evaluating the Language
23
let evalProp env prop = match prop with | Line(points,updateFunction,thickness,color) -> let l = evalLine points updateFunction thickness color env.View.Children.Add(l) env | Triangle(f) -> evalTriangle f env | TextAnnotation(rectUpdate,opacity) -> evalAnnotation rectUpdate opacity env | Sphere props -> env.View.Children.Add (evalSphere props) env | Cylinder props -> env.View.Children.Add (evalCylinder props) env Production Rules
24
Wrap up Combinator libraries offer compositional thinking Increased modularity in operations (as opposed to objects) Reasoning by substitution
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.