Code Monkey home page Code Monkey logo

Comments (1)

sharkdp avatar sharkdp commented on August 25, 2024

I implemented a static type checker with physical dimensions acting as types, but there is one big (and potentially fatal) problem: the exponentiation operator. If we want to generally support

expr1 ^ expr2

then the type of that compound expression relies on the value of expr2. It's not enough to statically compute the type/dimension of expr2 (which needs to be unity), we also need the value because

T[expr1 ^ expr2] = 
  if T[expr2] == 1:
    T[expr1] ^ expr2
  else:
    TypeCheckError

This does not just force us to evaluate expr2 at compile time (type-check time), it forces us to evaluate everything at compile time, because expr2 could reference anything.

What are possible solutions to this?

  1. Limited syntax in exponents
    We could strictly limit the syntax of expr2. For example: only allow integer exponents, nothing else. Or we could allow for simple computations like expr1^(2 * 3 + 4). That would be a real shame because we certainly want to support things like e^(-x^2/sigma^2) somehow.

  2. Distinguish between integer/rational exponents and decimals
    We could dispatch exponentiation with non-integer (non-rational?) exponents to a builtin powf(expr1, expr2) function that would only allow scalars as its first argument (and second). This way, we would always know the return type (also scalar):

    powf(expr1: Scalar, expr2: Scalar) -> Scalar
    

    In addition, exponentiation with integer exponents would be type checked as above, i.e. we would still allow things like v^2 or possibly even area^(1/2). But area^0.5 would not be possible anymore (unless we immediately transform that 0.5 literal to a rational).


Example:

T[0.5 * m * v^2] = T[m] * T[v^2] = T[m] * T[v]^2 = Mass * Speed^2 = Energy

from numbat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.