toType :: T.Canonical -> Expression () -> Expression ()
toType tipe x =
    case tipe of
      T.Lambda _ _ ->
          error "functions should not be allowed through input ports"

      T.Var _ ->
          error "type variables should not be allowed through input ports"

      T.Aliased _ args t ->
          toType (T.dealias args t) x

      T.Type (Var.Canonical Var.BuiltIn name)
          | name == "Float"  -> from JSNumber
          | name == "Int"    -> from JSInt
          | name == "Bool"   -> from JSBoolean
          | name == "String" -> from JSString
          where
            from checks = check x checks x

      T.Type name
          | Var.isJson name ->
              x

          | Var.isTuple name ->
              toTuple [] x

          | otherwise ->
              error "bad type got to foreign input conversion"

      T.App f args ->
          case f : args of
            T.Type name : [t]
                | Var.isMaybe name ->
                    CondExpr ()
                        (equal x (NullLit ()))
                        (_Maybe "Nothing")
                        (_Maybe "Just" <| toType t x)

                | Var.isList name ->
                    check x JSArray (_List "fromArray" <| array)

                | Var.isArray name ->
                    check x JSArray (_Array "fromJSArray" <| array)
                where
                  array = DotRef () x (var "map") <| toTypeFunction t

            T.Type name : ts
                | Var.isTuple name ->
                    toTuple ts x

            _ -> error "bad ADT got to foreign input conversion"

      T.Record _ (Just _) ->
          error "bad record got to foreign input conversion"

      T.Record fields Nothing ->
          check x (JSObject (map fst fields)) object
        where
          object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
          keys = map convert fields
          convert (f,t) = (prop f, toType t (DotRef () x (var f)))
toType :: T.Canonical -> Expression () -> Expression ()
toType tipe x =
    case tipe of
      T.Lambda _ _ ->
          error "functions should not be allowed through input ports"

      T.Var _ ->
          error "type variables should not be allowed through input ports"

      T.Aliased _ args t ->
          toType (T.dealias args t) x

      T.Type (Var.Canonical Var.BuiltIn name)
          | name == "Float"  -> from JSNumber
          | name == "Int"    -> from JSInt
          | name == "Bool"   -> from JSBoolean
          | name == "String" -> from JSString
          where
            from checks = check x checks x

      T.Type name
          | Var.isJson name ->
              x

          | Var.isTuple name ->
              toTuple [] x

          | otherwise ->
              error "bad type got to foreign input conversion"

      T.App f args ->
          case f : args of
            T.Type name : [t]
                | Var.isMaybe name ->
                    CondExpr ()
                        (equal x (NullLit ()))
                        (_Maybe "Nothing")
                        (_Maybe "Just" <| toType t x)

                | Var.isList name ->
                    check x JSArray (_List "fromArray" <| array)

                | Var.isArray name ->
                    check x JSArray (_Array "fromJSArray" <| array)
                where
                  array = DotRef () x (var "map") <| toTypeFunction t

            T.Type name : ts
                | Var.isTuple name ->
                    toTuple ts x

            _ -> error "bad ADT got to foreign input conversion"

      T.Record _ (Just _) ->
          error "bad record got to foreign input conversion"

      T.Record fields Nothing ->
          check x (JSObject (map fst fields)) object
        where
          object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
          keys = map convert fields
          convert (f,t) = (prop f, toType t (DotRef () x (var f)))
toType tipe x =
    case tipe of
      T.Lambda _ _ ->
          error "functions should not be allowed through input ports"

      T.Var _ ->
          error "type variables should not be allowed through input ports"

      T.Aliased _ args t ->
          toType (T.dealias args t) x

      T.Type (Var.Canonical Var.BuiltIn name)
          | name == "Float"  -> from JSNumber
          | name == "Int"    -> from JSInt
          | name == "Bool"   -> from JSBoolean
          | name == "String" -> from JSString
          where
            from checks = check x checks x

      T.Type name
          | Var.isJson name ->
              x

          | Var.isTuple name ->
              toTuple [] x

          | otherwise ->
              error "bad type got to foreign input conversion"

      T.App f args ->
          case f : args of
            T.Type name : [t]
                | Var.isMaybe name ->
                    CondExpr ()
                        (equal x (NullLit ()))
                        (_Maybe "Nothing")
                        (_Maybe "Just" <| toType t x)

                | Var.isList name ->
                    check x JSArray (_List "fromArray" <| array)

                | Var.isArray name ->
                    check x JSArray (_Array "fromJSArray" <| array)
                where
                  array = DotRef () x (var "map") <| toTypeFunction t

            T.Type name : ts
                | Var.isTuple name ->
                    toTuple ts x

            _ -> error "bad ADT got to foreign input conversion"

      T.Record _ (Just _) ->
          error "bad record got to foreign input conversion"

      T.Record fields Nothing ->
          check x (JSObject (map fst fields)) object
        where
          object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
          keys = map convert fields
          convert (f,t) = (prop f, toType t (DotRef () x (var f)))