You are previewing Programming F# 3.0, 2nd Edition.

Programming F# 3.0, 2nd Edition

Cover of Programming F# 3.0, 2nd Edition by Chris Smith Published by O'Reilly Media, Inc.
  1. Programming F# 3.0
  2. Preface
    1. Introducing F#
    2. Who This Book Is For
    3. What You Need to Get Going
    4. How the Book Is Organized
      1. Part I
      2. Part II
      3. Part III
    5. Part IV
    6. Conventions Used in This Book
    7. Using Code Examples
    8. Safari® Books Online
    9. I’d Like to Hear from You
    10. Acknowledgments
  3. I. Multiparadigm Programming
    1. 1. Introduction to F#
      1. Getting to Know F#
      2. Visual Studio 11
      3. F# Interactive
      4. Managing F# Source Files
    2. 2. Fundamentals
      1. Primitive Types
      2. Comparison and Equality
      3. Functions
      4. Core Types
      5. Organizing F# Code
    3. 3. Functional Programming
      1. Understanding Functions
      2. Pattern Matching
      3. Discriminated Unions
      4. Records
      5. Lazy Evaluation
      6. Sequences
      7. Queries
    4. 4. Imperative Programming
      1. Understanding Memory in .NET
      2. Changing Values
      3. Units of Measure
      4. Arrays
      5. Mutable Collection Types
      6. Looping Constructs
      7. Exceptions
    5. 5. Object-Oriented Programming
      1. Programming with Objects
      2. Understanding System.Object
      3. Understanding Classes
      4. Methods and Properties
      5. Inheritance
    6. 6. .NET Programming
      1. The .NET Platform
      2. Interfaces
      3. Object Expressions
      4. Extension Methods
      5. Extending Modules
      6. Enumerations
      7. Structs
  4. II. Programming F#
    1. 7. Applied Functional Programming
      1. Active Patterns
      2. Using Modules
      3. Mastering Lists
      4. Tail Recursion
      5. Programming with Functions
      6. Functional Patterns
      7. Functional Data Structures
    2. 8. Applied Object-Oriented Programming
      1. Operators
      2. Generic Type Constraints
      3. Delegates and Events
      4. Events
    3. 9. Asynchronous and Parallel Programming
      1. Working with Threads
      2. Asynchronous Programming
      3. Asynchronous Workflows
      4. Parallel Programming
      5. Task Parallel Library
    4. 10. Scripting
      1. F# Script Files
      2. Directives
      3. F# Script Recipes
    5. 11. Data Processing
      1. Indexing
      2. Querying
  5. III. Extending the F# Language
    1. 12. Reflection
      1. Attributes
      2. Type Reflection
      3. Dynamic Instantiation
      4. Using Reflection
    2. 13. Computation Expressions
      1. Toward Computation Expressions
      2. Computation Expression Builders
      3. Custom Computation Expression Builders
    3. 14. Quotations
      1. Quotation Basics
      2. Generating Quotation Expressions
    4. 15. Type Providers
      1. Typed Data Versus Typed Languages
      2. Type Providers
  6. IV. Appendixes
    1. A. Overview of .NET Libraries
      1. Visualization
      2. Data Processing
      3. Storing Data
    2. B. F# Interop
      1. .NET Interop
      2. Unmanaged Interop
  7. Index
  8. About the Author
  9. Colophon
  10. Copyright

Chapter 14. Quotations

Earlier, we looked at .NET reflection as a way to do metaprogramming, analyzing static type information to reason about program code. Although metaprogramming using reflection can do things like load program plug-ins, if you want to reason about how that code operates, you are out of luck. The .NET reflection APIs allow you to get at the raw MSIL op codes, but you have to reverse engineer what that code does—a daunting task at best.

However, there are many applications where knowing not only the structure of program code but also how it operates can be beneficial. For example, taking a function written in F# and converting it into a form that can execute on a graphics card’s GPU (presumably to be executed much, much faster).

F# provides a mechanism called quotation expressions by which you can access not only the static type information for a block of code, but also see the F# compiler’s internal representation of the code (sometimes referred to as an abstract syntax tree, or AST).

Using F# quotations you can:

  • Perform code analysis and inspection

  • Defer computation to other platforms (SQL, GPU, etc.)

  • Generate new code

We look at these capabilities shortly, but first let’s look at how we can get started with this language feature. Be warned though, quotations are deep wizardry, and not for the faint of heart. This chapter only provides a crash course on the subject. To truly master quotations, refer to online resources at the F# developer center at ...

The best content for your career. Discover unlimited learning on demand for around $1/day.