A question about static types

How would you statically type the following JavaScript program

function c(str) {
  c = eval(str);
  return c(str);
}

I’m not trying to start a flame war but I’m genuinely curious. I’ve always assumed that dynamic languages allow more programs to be written because of examples like the above. But if a statically typed language is Turing complete then it should be able to express the equivalent of the above program. So where exactly does my reasoning break down because this seems paradoxical to me? My reasoning is that this is obviously a function that can not be typed in a statically typed language and still allow the generality that the above code allows for.

7

There are a few ways you could go about typing this.

Forget All The Types!

You can mimic dynamic types in statically typed languages by simply forgetting that you had types in the first place and switching to using runtime type information.

In haskell this would look something like

evalThingy s = let result = eval s
                   resultTy = typeOf result
               if resultTy == typeOf (id :: String -> String)
               then unsafeCoerce result s
               else fail in some manner

So we have a runtime switch which just checks whether it’s safe to continue and if it is, beat the typechecker into submission and proceed.

Personally I dislike this way since if I’m going to have to write something in a static type system, I want to get the safety benefits. That means coercions like the above are a no-no.

Explicit Failure

The simplest is to force eval to either return a specific type or fail in a controlled manner

eval : String -> Either Error a

And then we could say something like

 c = eval(str) : Either Error (str -> Int)
 case c of
   Right f -> f str
   Left err -> blowup str

In order to support this well are language needs something called return type [parametric] polymorphism. ATM I’m only aware of this being supported through type classes. That means that we need to different things depending on what the type called is.

Dependent Types

The more convoluted approach would be to actually reflect the typing judgement of the language into some function, let’s say typeOf.

typeOf : String -> TypeOfTypes

Where TypeOfTypes is the universe of all types that programs normally use. This can’t include itself if we want some sanity in our type system.

Next we can assign eval a fairly convoluted dependent type to eval

eval : (s : str) -> typeOf str

applySelf : (s : String) -> typeOf s = (String -> String) -> String
applySelf = function(s, p){
  case p of
    reflexivity -> eval(s)(s)
}

So we force the caller of applySelf to prove that the string they’re asking us to evaluate will have the type string -> string at which point we can ignore the safety checks since we have a static proof of correctness.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật